<?php 
error_reporting(0);

 

require_once("config/configDB.php");
require_once("config/constant.php");

$sql_customer = mysqli_query($conn,"SELECT sh.id as SLID,sh.customer_id,cm.cname FROM saleheader as sh  join customer_master as cm on cm.id=sh.customer_id WHERE cm.flag='1' GROUP BY sh.customer_id ");

$row_customer = '';

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


$sqlproduct = mysqli_query($conn,"SELECT id, productname FROM product_master WHERE flag='1' ");

$sql_product = '';

while($prow = mysqli_fetch_array($sqlproduct)){

    $sql_product =$sql_product."<option value='".$prow['id']."'>".$prow['productname']."</option>";
}

$fdate=date_create($_POST['fromDate']);
$fromDate= date_format($fdate,"Y-m-d");

$tdate=date_create($_POST['toDate']);
$toDate= date_format($tdate,"Y-m-d");


//$fromDate=$_POST['fromDate'];
				//$toDate=$_POST['toDate'];
$expensetype=$_POST['expensetype'];
$expenser=$_POST['expenser'];

?>
<!DOCTYPE html>
<html>
<head>
    <title>Expense Report</title>
    <?php require 'header.php' ?>
    <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
<script >
var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()

</script>
</head>
<body>
    <?php require 'menu.php' ?>


    <div class="container text-center" id="saleStock">
  
  <form class="form-inline" action="stockreportexpense.php" method="post" autocomplete="off">
    
    <div class="form-group">
      <label for="email">From Date:</label>
      <input type="date" class="form-control" id="fromDate" name="fromDate">
    </div>
    <div class="form-group">
      <label for="pwd">To Date</label>
      <input type="date" class="form-control" id="toDate" name="toDate">
    </div>
    
    <div class="form-group">
      <label for="pwd">Expense</label>
      <select class="form-control customerStock" 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="form-group">
    
    <label for="">Expenser </label>
    <select class="form-control" name="expenser" >
        <option value="">Select the Expenser </option>
       <option>Admin</option>
       <option>QC</option>
       <option>Plant and Machinery</option>
       <option>Other Expense</option>
       <option>Marketing Expense</option>
        </select>
    </div>-->
    

    <button type="submit" class="btn btn-primary btn-md datewise-submit-expense">Submit</button>

    <button type="button" class="btn btn-info btn-md datewise-submit-sale" onClick="tableToExcel('a', 'W3C Example Table')">Excel</button>
    <a href="print_exp.php?fromdate=<?php echo $fromDate;?>&& todate=<?php echo $toDate;?>&& expensetype=<?php echo $expensetype;?>" class="btn btn-primary" target="_blank">Print</a>

  </form>
</div>



    

<div class="container col-sm-12" >
&nbsp;
</div>

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

<table id="stockExpenseDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
        <thead>
            <tr>
            <th>Cash No</th>
            <th>Date</th>
            <th>Expense Type </th>
             <th>Expense Qty</th>
            <th>Expenser</th>
            <th>Mode Of Pay</th>
            <th>Amount</th>
            <th>Rermark</th>
                <!-- <th>Action</th> -->
            </tr>
        </thead>
        
        
         <?php
				
				
				
				
				$sql = "SELECT ee.cashno,ee.mode,ee.remark, ee.expensedate, ee.expense,ee.expensetype,ee.expensetype,ee.expenser,ee.amount,ee.id,em.expense as expensename FROM expense_entry as ee LEFT JOIN expense_master as em ON em.id=ee.expensetype WHERE ee.flag=1 ";
				
				if($fromDate !=''  && $toDate !='' && $expensetype ==''  && $expenser ==''  ) {
	 	$sql.=" and expensedate BETWEEN '".$fromDate."' AND '".$toDate."' ORDER BY ee.expensedate ASC ";

	 }
	 
	else if($fromDate !=''  && $toDate !='' && $expensetype !=''  && $expenser ==''  ) {
		
		$sql.=" and expensetype='".$expensetype."' and expensedate BETWEEN '".$fromDate."' AND '".$toDate."' ORDER BY ee.expensedate ASC ";
	}
	
	else if($fromDate !=''  && $toDate !='' && $expensetype !=''  && $expenser !=''  ) {
		
		$sql.=" and expenser='".$expenser."' and expensetype='".$expensetype."' and expensedate BETWEEN '".$fromDate."' AND '".$toDate."' ORDER BY ee.expensedate ASC ";
	}
	
	
	else if($fromDate !=''  && $toDate !='' && $expensetype ==''  && $expenser !=''  ) {
		
		$sql.=" and expenser='".$expenser."' and expensedate BETWEEN '".$fromDate."' AND '".$toDate."' ORDER BY ee.expensedate ASC ";
	}
	
	else if($fromDate ==''  && $toDate =='' && $expensetype !=''  && $expenser !=''  ) {
		
		$sql.=" and expensetype='".$expensetype."' and expenser='".$expenser."'  ORDER BY ee.expensedate ASC ";
	}
	
	else if($fromDate ==''  && $toDate =='' && $expensetype !=''  && $expenser ==''  ) {
		
		$sql.=" and expensetype='".$expensetype."'   ORDER BY ee.expensedate ASC ";
	}
	
	else if($fromDate ==''  && $toDate =='' && $expensetype ==''  && $expenser !=''  ) {
		
		$sql.=" and expenser='".$expenser."'  ORDER BY ee.expensedate ASC ";
	}
	
				?>
        
        <tbody>
             <?php
			$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1".$sql);
			$amount=0;
			while($row=mysqli_fetch_array($query))
			{
			?>
            <tr>
            <td><?php echo $row['cashno'];?></td>
            <td><?php echo $row['expensedate'];?></td>
            <td><?php echo $row['expensename'];?></td>
            <td><?php echo $row['expense'];?></td>
            <td><?php echo $row['expenser'];?></td>
            <td><?php echo $row['mode'];?></td>
            <td align="right"><?php echo number_format($row['amount'],2,'.','');?></td>
            <td><?php echo $row['remark'];?></td>
            </tr>
            <?php
			$amount=$amount+$row['amount'];
			}
			?>
            <tr>
            <td align="right" colspan="6"><b>Total</b></td>
            <td align="right"><strong><?php echo number_format($amount,2,'.','');?></strong></td>
            </tr>
        </tbody>
       
    </table>
    
</div>



    <?php require 'footer.php' ?>

   <script type="text/javascript">var base_url = "<?= $base_url ?>";</script>
  
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js"></script>

   <!-- <script>



            $("#fromDate").datepicker({
        minDate: '-1Y-6M',
        maxDate: '+1Y+6M',
        dateFormat: 'yy-mm-dd',
        onSelect: function (dateStr) {
            var min = $(this).datepicker('getDate'); // Get selected date
            $("#toDate").datepicker('option', 'minDate', min || '0'); // Set other min, default to today
        }
    });

    //toDate

    $("#toDate").datepicker({
        minDate: '1',
        maxDate: '+1Y+6M',
        dateFormat: 'yy-mm-dd',
        onSelect: function (dateStr) {
            var max = $(this).datepicker('getDate'); // Get selected date

            $('#datepicker').datepicker('option', 'maxDate', max || '+1Y+6M'); // Set other max, default to +18 months

            
        }
    });

            


</script>-->
    
</body>
</html>