<?PHP
ob_start();
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

$gtp=mysql_query("select * from admin where username='$user'");
$gtp1=mysql_fetch_array($gtp);

include 'top.php';
?>



			<div class="main-content">
				<div class="main-content-inner">
					<div class="breadcrumbs ace-save-state" id="breadcrumbs">
						<ul class="breadcrumb">
							<li>
								<i class="ace-icon fa fa-home home-icon"></i>
								<a href="#">Home</a>
							</li>
							<li class="active">Expense</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

						<div class="page-header">
                        
<?php

$aca=mysql_query("select * from financial where user='$user' order by ID desc");
$aca1=mysql_fetch_array($aca);

$financial=$aca1['financial'];

?>                        
                        
							<table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Expense
							</h1>
                            
                            </td>
                            
                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">
                            
                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>
                            
                            </td></tr></table>
                            
						</div><!-- /.page-header -->

						<div class="row">
							<div class="col-xs-12">
                            
								<!-- PAGE CONTENT BEGINS -->


<script src="assets/js/jquery.js"></script>

<script>

$(".bank").live('change',function() {

		  var bank=$(this).val();

		  var dataString = 'bank='+ bank;

		  //alert(dataString);

		  $.ajax

		  ({

		  type: "GET",

		  url: "getacc.php",

		  data: dataString,

		  cache: false,

		  success: function(resp)

		  {
		  
		  $('.account').html($('#account',resp).html());

		  }

		  });

});
		  
</script>

<script>
	
jQuery(function($){
		   
	// simple jQuery validation script
	$('#course').submit(function(){
		
		var valid = true;
		var errormsg = 'This field is required!';
		var errorcn = 'error';
		
		$('.' + errorcn, this).remove();			
		
		$('#required', this).each(function(){
			var parent = $(this).parent();
			if( $(this).val() == '' ){
				var msg = $(this).attr('title');
				msg = (msg != '') ? msg : errormsg;
				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')
					.appendTo(parent)
					.fadeIn('fast')
					.click(function(){ $(this).remove(); });
				valid = false;
			};
		});
		
		return valid;
	});
	
});	

</script>   
								
										
                                        
<?php

if(isset($_GET['id']))
{

$id=$_GET['id'];

if(isset($_POST['submit']))
{

$id=$_POST['id'];

$name=$_POST['name'];

$date=$_POST['date'];

$amount=$_POST['amount'];

$description=$_POST['description'];

$category = $_POST['category'];

$factory=$_POST['factory'];

$paymentmode=$_POST['paymentmode'];

$chequeno=$_POST['chequeno'];

$chequedate=$_POST['chequedate'];

$account=$_POST['account'];


if($paymentmode=='Bank')
{
	$chequeno=$chequeno;
	$chequedate=date('Y-m-d',strtotime($chequedate));
	$account=$account;
}
else
{
	$chequeno='';
	$chequedate='';
	$account='';
}


if($name)
{

$date=date('Y-m-d',strtotime($date));

$up=mysql_query("update expense set name='$name',date='$date',category='$category',factory='$factory',description='$description',amount='$amount',paymentmode='$paymentmode',account='$account',chequeno='$chequeno',chequedate='$chequedate' where ID='$id'") or die(mysql_error());

if($up)
{

header("location:viewexpense.php?s=1");

}

}

}

else
{

$ex=mysql_query("select * from expense where ID='".$id."'");

$ex1=mysql_fetch_array($ex);

echo'<form action="viewexpense.php?id='.$id.'" method="post" id="work">

<center><table width="70%" id="page" style="line-height:35px;">

<input type="hidden" name="id" value="'.$id.'" />

<tr><td id="page" style="width:32%;">Date <span class="red">*</span></td><td width="2%">:</td> <td id="page"><input type="text" name="date" autocomplete="off" class="datepicker" value="'.date('d-m-Y',strtotime($ex1['date'])).'" id="required" title="Enter Date" /></td></tr>

<tr><td>Factory <span class="red">*</span></td><td>:</td><td><select name="factory" id="required" title="Select Factory" style="width:220px;">

<option value="">Select Factory</option>';

$sel=mysql_query("select * from factory order by ID ASC");
while($sel1=mysql_fetch_array($sel))
{

$fct="<option value='".$sel1['ID']."'>".$sel1['factory']."</option>";
$fct=str_replace("<option value='".$ex1['factory']."'","<option value='".$ex1['factory']."' selected",$fct);
echo $fct;

}

echo'</select></td></tr>

<tr><td>Category <span class="red">*</span></td><td>:</td><td><select name="category" id="required" title="Select Ledger" style="width:220px;"><option value="">Select Category</option>';

$sel=mysql_query("select * from exp_category order by ID ASC");
while($sel1=mysql_fetch_array($sel))
{

$led="<option value='".$sel1['category']."'>".$sel1['category']."</option>";
$led=str_replace("<option value='".$ex1['category']."'","<option value='".$ex1['category']."' selected",$led);
echo $led;

}

echo'</select></td></tr>

<tr><td id="page" style="width:32%;">Name <span class="red">*</span></td><td width="2%">:</td> <td id="page"><input type="text" name="name" id="required" title="Enter Name" value="'.$ex1['name'].'" autocomplete="off" /></td></tr>

<tr><td id="page" style="width:32%;">Amount <span class="red">*</span></td><td width="2%">:</td> <td id="page"><input type="text" name="amount" id="required" title="Enter Amount" value="'.$ex1['amount'].'" autocomplete="off" /></td></tr>

<tr><td id="page">Reason <span class="red">*</span></td><td width="2%">:</td> <td id="page"><textarea name="description" rows="2" cols="22" id="required" title="Enter Description" style="width:220px; height:70px;">'.$ex1['description'].'</textarea></td></tr>';

?>


<script type="text/javascript">

function showstuff(element){

    document.getElementById("Cash").style.display = element=="Cash"?"block":"none";

    document.getElementById("Bank").style.display = element=="Bank"?"block":"none";

}

</script>

<tr><td style="width:32%;">Payment Mode <span class="red">*</span></td><td>:</td><td><select name="paymentmode" onchange="showstuff(this.value)" style="width:220px;" id="required" title="*" class="paymentmode">

<option value="Cash" <?php if($ex1['paymentmode']=='Cash'){ echo 'selected="selected"'; } ?>>Cash</option>  

<option value="Bank" <?php if($ex1['paymentmode']=='Bank'){ echo 'selected="selected"'; } ?>>Bank</option>

</select></td></tr>

</table>



<DIV ID="Cash" style=" <?php if($ex1['paymentmode']=='Cash'){ echo'display:block;'; } else { echo'display:none;'; } ?> margin:0px; float:left;"></DIV>


<center><DIV ID="Bank" style=" <?php if($ex1['paymentmode']=='Bank'){ echo'display:block;'; } else { echo'display:none;'; } ?> margin:auto; width:70%;">

<table style="width:100%; line-height:35px;">

<tr><td id="page" style="width:32%;">Bank </td><td width="2%">:</td><td id="page"><select name="bank" class="bank" style="width:220px;"><option value="">-- Select --</option>

<?php

$bbn=mysql_query("select * from bank where ID='$ex1[account]'");
$bbn1=mysql_fetch_array($bbn);

$bnk=mysql_query("select * from bank group by bank order by bank asc");
while($bnk1=mysql_fetch_array($bnk))
{
	$bns="<option value='".$bnk1['bank']."'>".$bnk1['bank']."</option>";
	$bns=str_replace("<option value='".$bbn1['bank']."'","<option value='".$bbn1['bank']."' selected",$bns);
	echo $bns;
}

?>

</select></td></tr>

<tr><td id="page" style="width:32%;">Account </td><td width="2%">:</td><td id="page"><select name="account" style="width:220px;" class="account"><option value="" selected="selected">Select Account</option>

<?php

$acn=mysql_query("select * from bank where bank='$bbn1[bank]'");
while($acn1=mysql_fetch_array($acn))
{
	$accn="<option value='".$acn1['ID']."'>".$acn1['accountname']."</option>";
	$accn=str_replace("<option value='".$ex1['account']."'","<option value='".$ex1['account']."' selected",$accn);
	echo $accn;
}

?>

</select></td></tr>

<tr><td id="page" style="width:32%;">Cheque No </td><td width="2%">:</td><td id="page"><input type="text" name="chequeno" value="<?php echo $ex1['chequeno']; ?>" autocomplete="off" /></td></tr>

<tr><td id="page">Cheque Date </td><td width="2%">:</td><td id="page"><input type="text" name="chequedate" class="popupdatepicker" value="<?php echo date('d-m-Y',strtotime($ex1['chequedate'])) ?>" autocomplete="off" /></td></tr>

</table>

</DIV>


<?php

echo'<button class="btn btn-sm btn-primary" type="submit" name="submit" value="Add">Save</button>

</center>

</form>';

}

}

else
	{
		
	
		
		
	if (isset($_GET['s'])) 	
	{
	 $status=$_GET['s'];
		
		 if($status=='1')
		 {
			 echo'<div class="alert alert-block alert-success">
									<button type="button" class="close" data-dismiss="alert">
										<i class="ace-icon fa fa-times"></i>
									</button>
						Values Edited Successfully.
				  </div>';				  
		 }
		 if($status=='7')
		 {
			 echo'<div class="alert alert-block alert-success">
									<button type="button" class="close" data-dismiss="alert">
										<i class="ace-icon fa fa-times"></i>
									</button>
						Values Deleted Successfully.
				  </div>';				  
		 }
	}		

if(isset($_GET['submit']))
{
	
	$fromdate=$_GET['fromdate'];
	$todate=$_GET['todate'];
	
	
	echo'<table style="width:99%; margin:11px auto 10px auto;">
                                        
                                        <tr><td style="width:20%;">
                                        <a href="expense.php"><button class="btn btn-minier btn-info">+ Add Expense</button></a>
                                        </td>
                                        
                                        <td style="width:50%;"></td>
                                        
                                        <td style="width:30%; text-align:right;">
                                        
										Period : '.$fromdate.' - '.$todate.'
										
                                        </td>
                                        
                                        </tr>
                                        
                                        </table>';

$frmdate=date('Y-m-d',strtotime($fromdate));
$tdate=date('Y-m-d',strtotime($todate));

echo"<table id='dynamic-table' class='table table-striped table-bordered table-hover'>

	<thead><th width='7%'>S.No</th><th width='10%'>Date</th><th width='15%'>Category</th><th>Description</th><th width='15%'>Amount</th><th style='width:10%;'>Edit</th></thead>";

	$getsite3="select * from expense where date between '$frmdate' and '$tdate' order by ID asc";

	$getsite4=mysql_query($getsite3) or die("Could not get");

	$sno=0;

	while($getsite5=mysql_fetch_array($getsite4))

		{

		$sno=$sno+1;

		echo "<tr><td>".$sno."</td><td>".date('d-m-Y',strtotime($getsite5['date']))."</td><td>$getsite5[category]</td>
		<td>$getsite5[description]</td><td style='text-align:right;'>$getsite5[amount]</td><td>
		
														<div class='hidden-sm hidden-xs btn-group'>
													   
															<a href='viewexpense1.php?id=".$getsite5['ID']."' title='View'>
																<i class='ace-icon fa fa-search-plus bigger-100 blue'></i></a> &nbsp;";
																
															if($gtp1['type']!='staff')
															{
															echo"<a href='viewexpense.php?id=".$getsite5['ID']."' title='Edit'>
																<i class='ace-icon fa fa-pencil bigger-100 green'></i></a> &nbsp;
																
															<a href='del.php?id=".$getsite5['ID']."&desc=expense' title='Delete'>
																<i class='ace-icon fa fa-trash-o bigger-100 grey'></i></a>";
															}
															
														 echo"</div>
					  
		</td></tr>";

		}

		echo "</table><br />
		
		<center><a href='viewexpense.php'>Back</a></center>";

}

else
{
	
	echo"<center>
	
	<form action='viewexpense.php' method='get'>
	
	<table style='line-height:23px;'>
	
	<tr><td>From Date</td><td style='padding:0 7px;'>:</td><td><input type='text' name='fromdate' class='datepicker' /></td></tr>
	
	<tr><td style='padding:3px 0;'>To Date</td><td style='padding:3px 7px;'>:</td><td style='padding:3px 0;'><input type='text' name='todate' class='popupdatepicker' /></td></tr>
	
	<tr><td colspan='3'><button type='submit' name='submit' value='submit'>Submit</button></td></tr>
	
	</table>
	
	</form>
	
	</center>";

echo'<table style="width:99%; margin:11px auto 10px auto;">
                                        
                                        <tr><td style="width:20%;">
                                        <a href="expense.php"><button class="btn btn-minier btn-info">+ Add Expense</button></a>
                                        </td>
                                        
                                        <td style="width:50%;"></td>
                                        
                                        <td style="width:30%;">
                                        
                                        </td>
                                        
                                        </tr>
                                        
                                        </table>';


echo"<table id='dynamic-table' class='table table-striped table-bordered table-hover'>

	<thead><th width='7%'>S.No</th><th width='10%'>Date</th><th width='15%'>Category</th><th>Description</th><th width='15%'>Amount</th><th style='width:10%;'>Edit</th></thead>";

	$getsite3="select * from expense where date between '$aca1[fromdate]' and '$aca1[todate]' order by ID asc";

	$getsite4=mysql_query($getsite3) or die("Could not get");

	$sno=0;

	while($getsite5=mysql_fetch_array($getsite4))

		{

		$sno=$sno+1;

		echo "<tr><td>".$sno."</td><td>".date('d-m-Y',strtotime($getsite5['date']))."</td><td>$getsite5[category]</td>
		<td>$getsite5[description]</td><td style='text-align:right;'>$getsite5[amount]</td><td>
		
														<div class='hidden-sm hidden-xs btn-group'>
													   
															<a href='viewexpense1.php?id=".$getsite5['ID']."' title='View'>
																<i class='ace-icon fa fa-search-plus bigger-100 blue'></i></a> &nbsp;";
																
															if($gtp1['type']!='staff')
															{
															echo"<a href='viewexpense.php?id=".$getsite5['ID']."' title='Edit'>
																<i class='ace-icon fa fa-pencil bigger-100 green'></i></a> &nbsp;
																
															<a href='del.php?id=".$getsite5['ID']."&desc=expense' title='Delete'>
																<i class='ace-icon fa fa-trash-o bigger-100 grey'></i></a>";
															}
															
														 echo"</div>
					  
		</td></tr>";

		}

		echo "</table>";

}

}

?>
                                        
                                            
                                            							

										<!-- PAGE CONTENT ENDS -->
									</div><!-- /.col -->
								</div><!-- /.row -->
							</div><!-- /.page-content -->
						</div>
					</div><!-- /.main-content -->


<?php
include 'bottom.php';
}
else
{

 header("location:login.php");
}
ob_flush();
?>