<?php 

require_once("config/configDB.php"); 
require "userPermission.php";
require_once("config/constant.php");
error_reporting(0);

$sql_expense = mysqli_query($conn,"SELECT id, expense FROM expense_master where flag=1 ");
$row_expense = '';

$sql_employee = mysqli_query($conn,"SELECT id,ename FROM employee_master WHERE isdelete='1' order by ename ASC ");
$row_employee = '';






$fromdate=$_POST['fromDate'];
$todate=$_POST['toDate'];
$expenser=$_POST['expenser'];
$expensetype=$_POST['expensetype'];
?>

<!DOCTYPE html>
<html>
<head>
    <?php 
      require 'meta.php'; 
      require 'header.php'; 
    ?>
<style>
	
	@font-face
	{
	font-family:Bamini;
	src:url(fonts/Bamini.TTF);
	
	}
	.tamil
	{
	font-family:Bamini;
	
	}
	
	
	</style>
    <script>
 
 function printDiv() {
    var divToPrint = document.getElementById('a');
    var htmlToPrint = '' +
        '<style type="text/css">' +
        'table th, table td {' +
        'border:1px solid #000;' +
        'padding;0.5em;' +
        '}' +
        '</style>';
    htmlToPrint += divToPrint.outerHTML;
    newWin = window.open("");
    newWin.document.write(htmlToPrint);
    newWin.print();
    newWin.close();
}
 
 
 </script>
</head>
<body>
    <?php require 'menu.php' ?>
<div class="container-fluid">
<form action="report_cash_expense.php" method="post">
<div class="container-fluid text-center col-sm-12 dashboard-add">  

<div class=" col-sm-2">
    <label>From Date</label>
    <input type="date" class="form-control input-lg" name="fromDate" autocomplete="off">   
</div>
<div class=" col-sm-2">
    <label>To Date</label>
    <input type="date" class="form-control input-lg" name="toDate" autocomplete="off">    
</div>
<div class=" col-sm-2">
    <label>Expenser</label>
    <select class="form-control input-lg selectpicker" data-live-search="true" name="expenser" >
        <option value="">Please Select the Employee </option>
        <?php
        while($row4 = mysqli_fetch_array($sql_employee)){
        ?>
        <option value="<?php echo $row4['id'] ?>" > <?php echo $row4['ename']; ?> </option>
        <?php
        //echo "<option value='".$row['id']."'>".$row['cname']."</option>";
        }
        ?>
        </select>
</div>



<div class=" col-sm-2">
    <label>Expense</label>
    <select class="form-control input-lg selectpicker" data-live-search="true"  name="expensetype" >
        <option value="">Select Expense Type</option>
        <?php
        while($row = mysqli_fetch_array($sql_expense)){
        ?>
        <option value="<?php echo $row['id'] ?>" > <?php echo $row['expense']; ?> </option>
        <?php
        }
        ?>
        
      </select>
</div>

<div class=" col-sm-1" style="padding-top: 24px;">
    <button type="submit" class="btn btn-primary btn-lg datewise-submit-sale">Apply</button>
</div>
<div class=" col-sm-1" style="padding-top: 24px;">
    <button type="button" class="btn-success btn-lg btn-md datewise-submit-sale" onClick="printDiv();">Print</button>
</div>
</div>

</form>

<div class="container col-sm-12">
<div id="a">

<center><h3 > Cash  Expense Report</h3></center>

<table id="salestockDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Cash No</th>
                <th>Date</th>
				<th>Expenser</th>
                <th>Expense</th>
                <th>Expense Details</th>
                
                
				<th>Amount</th>
               
				<th>Remark1</th>
				
               
            </tr>
        </thead>
        
        <?php
				
				
				$sql = "SELECT sh.id,sh.cashno, sh.expensedate, sh.expensetype,sh.expenser,  sh.empname, sh.expense_details,  sh.amount,sh.remark,em.ename,exm.expense FROM cash_expense_entry as sh LEFT JOIN employee_master as em on em.id = sh.expenser inner join expense_master as exm on sh.expensetype=exm.id  where sh.flag='1' and sh.status='' ";
				
				if($fromdate !=''  && $todate !='' && $expenser ==''  && $expensetype ==''  ) {
	 	$sql.=" and sh.expensedate BETWEEN '".$fromdate."' AND '".$todate."' ORDER BY sh.expensedate ASC ";

	 }
	 
	 
	 else if($fromdate !=''  && $todate !='' && $expenser !=''   && $expensetype=='') {
	  	$sql.=" and sh.expensedate BETWEEN '".$fromdate."' AND '".$todate."' and expenser='".$expenser."' ORDER BY sh.expensedate ASC ";

	 }
	 
	  else if($fromdate !=''  && $todate !='' && $expenser ==''   && $expensetype!='') {
	  	$sql.=" and sh.expensedate BETWEEN '".$fromdate."' AND '".$todate."' and expensetype='".$expensetype."' ORDER BY sh.expensedate ASC ";

	 }
	 
	 else if($fromdate ==''  && $todate =='' && $expenser !=''   && $expensetype!='') {
	  	$sql.=" and expenser='".$expenser."' and expensetype='".$expensetype."' ORDER BY sh.expensedate ASC ";

	 }
	 
	  else if($fromdate ==''  && $todate =='' && $expenser !=''   && $expensetype=='') {
	  	$sql.=" and expenser='".$expenser."'  ORDER BY sh.expensedate ASC ";

	 }
	 
	  else if($fromdate ==''  && $todate =='' && $expenser ==''   && $expensetype!='') {
	  	$sql.=" and expensetype='".$expensetype."' ORDER BY sh.expensedate ASC ";

	 }
	 
	  else if($fromdate !=''  && $todate !='' && $expenser !=''   && $expensetype!='') {
	  	$sql.=" and sh.expensedate BETWEEN '".$fromdate."' AND '".$todate."' and expenser='".$expenser."' and expensetype='".$expensetype."' ORDER BY sh.expensedate ASC ";

	 }
	 
	// echo $sql;
	
				?>
        
        <tbody>
            <?php
			$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1".$sql);
			$total_price=0;
			$total_qty=0;
			while($row=mysqli_fetch_array($query))
{
	$deliver_date=$row["expensedate"];
$date=date_create($deliver_date);
$purchase_date2=date_format($date,"d-m-Y");
				
				?>
			<tr>
            <td><?php echo $row["cashno"];?></td>
            <td><?php echo $purchase_date2;?></td>
			<td><?php echo $row["ename"];?></td>
            <td><?php echo $row["expense"];?></td>
          
           
          
			<td align="right"><?php echo $row["expense_details"];?></td>
             <td align="right"><?php echo number_format(round($row["amount"]), 2, '.', '');?></td>
              
			<td><?php echo $row["remark"];?></td>
			
           
			</tr>
            <?php
			
			$total_price  += $row["amount"];
			
}


?>
       <tr>
           
           <td colspan="5" align="right"><strong>Total </strong></td>
           
           
			
            <td align="right"><strong><?php echo number_format(round($total_price), 2, '.', '');?></strong></td>
			</tr>              
                    
           
        </tbody>
       
    </table>
    </div>
</div>

</div>


    <?php require 'footer.php' ?>
   
   
</body>
</html>