<?php 
error_reporting(0);
require_once("config/configDB.php"); 
require "userPermission.php";
require "financial_year_calculation.php";

$sql_supplier = mysqli_query($conn,"SELECT sh.supplier_id,cm.sname FROM purchaseheader as sh  join supplier_master as cm on cm.id=sh.supplier_id WHERE cm.isdelete='1' and sh.status='' GROUP BY sh.supplier_id order by cm.sname");

$row_supplier = '';

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

$sql_product = '';

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

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

$cID 		= $_POST['supplierName'];
$startDate 	= $_POST['startDate'];
$endDate 	= $_POST['endDate'];

?>
<!DOCTYPE html>
<html>
<head>
    <title>Stock Report</title>
    <?php require 'header.php' ?>
    <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
         rel = "stylesheet">
 <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="purchaseStock">
  
  <form class="form-inline" action="stockreportpayment.php" method="post">
  
  <div class="col-sm-12 row">
      <div class=" col-sm-1"> <label class="control-label">supplier</label></div>
        <div class=" col-sm-2 form-group">
         
          <select class="form-control  selectpicker select-trip-vehicle" data-show-subtext="true" data-live-search="true" name="supplierName">
        <option value="">Please Select the supplier </option>
        <?php
        while($row = mysqli_fetch_array($sql_supplier)){
        ?>
        <option value="<?php echo $row['supplier_id'] ?>" > <?php echo $row['sname']; ?> </option>
        <?php
        }
        ?>
        </select>
        </div>
        
         <div class=" col-sm-1"> <label class="control-label">From&nbsp;Date:</label></div>
        <div class=" col-sm-2 form-group">
         <input type="date" class="form-control" id="fromDate" name="startDate">
        </div>
        
         <div class=" col-sm-1"> <label class="control-label">To&nbsp;Date:</label></div>
        <div class=" col-sm-2 form-group">
         <input type="date" class="form-control" id="toDate" name="endDate">
        </div>
        
        <div class=" col-sm-2 form-group">
        <button type="submit" class="btn btn-primary btn-md datewise-submit-collection">Submit</button>

    <button type="button" class="btn btn-info btn-md datewise-submit-sale" onClick="tableToExcel('a', 'W3C Example Table')">Excel</button>
    </div>
    </div>
    
    

    

  </form>
</div>



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


<div class="container col-sm-12">
<div id="a">
<table id="stockCollectionDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
        <thead>
            <tr>
            <th>S.No</th>
                <th>Invoice No</th>
                <th>Date</th>
                <th>supplier</th>
             
               
                <th>Paid Amount</th>
                 <th>Amount From</th>
                 <th>Payment Mode</th>
                
                 <th>Remark</th>
              
            </tr>
        </thead>
        <tbody>
        
        <?php
		if($cID!='' && $startDate !='' && $endDate !='' ) {
		$sql = "SELECT cc.bank_name,cc.handover,cc.transactionno,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.supplierID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount, cm.sname FROM supplier_payment_header as cc LEFT JOIN supplier_master as cm on cm.id = cc.supplierID  WHERE cc.supplierID ='".$cID."' AND cc.collectiondate BETWEEN '".$startDate."' AND '".$endDate."' and cc.status='' ";
		}
		else if($startDate !='' && $endDate !='' && $cID=='') {
		$sql = "SELECT cc.bank_name,cc.handover,cc.transactionno,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.supplierID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount, cm.sname FROM supplier_payment_header as cc LEFT JOIN supplier_master as cm on cm.id = cc.supplierID  WHERE cc.collectiondate BETWEEN '".$startDate."' AND '".$endDate."' and cc.status=''";

}

else if($startDate =='' && $endDate =='' && $cID!='') {
		$sql = "SELECT cc.bank_name,cc.handover,cc.transactionno,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.supplierID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount, cm.sname FROM supplier_payment_header as cc LEFT JOIN supplier_master as cm on cm.id = cc.supplierID  WHERE cc.supplierID ='".$cID."'  and cc.status='' ";

}
//echo $sql;

$query=mysqli_query($conn, $sql) ;
	
//(invoice_no, purchase_date, supplier_id, dc_num, po_num,purchaseGSTNO, itemcount, tamount, create_date, flag)
$ramount=0;
$sno=0;
while( $row=mysqli_fetch_array($query) ) { 

$cashno = $row["cashno"];
    
   
	$date = new DateTime($row["collectiondate"]); 
   $coll_date = $date->format('d-m-Y'); //only fetching date
    $sname = $row["sname"];
  
	$received_amt = number_format(round($row["received_amount"]),2,'.','');;
	
	$payment_through = $row["payment_through"];
    $paymenttype = $row["paymenttype"];
	
	 $remark = $row["remark"];
		?>
            
               <tr>
               <td><?php echo ++$sno;?></td>
               <td><?php echo $cashno;?></td>
                <td><?php echo $coll_date;?></td>
                 <td><?php echo $sname;?></td>
                  <td align="right"><?php echo $received_amt;?></td>
                   <td><?php echo $payment_through;?></td>
                    <td><?php echo $paymenttype;?></td>
                     <td><?php echo $remark;?></td>
               
               </tr>
               
               
            <?php
			$ramount=$ramount+$received_amt;
			}
			
			?>   
        <tr>
        <td colspan="4" align="right">Total&nbsp;Amount</td>
        <td align="right"><strong><?php echo number_format($ramount,2,'.','');?></strong></td>
        
        
        </tr>            
           
        </tbody>
        
       
    </table>
    </div>
</div>



    <?php require 'footer.php' ?>

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

    <!--<script>


$(document).ready(function() {
    $('.purchase-stockreport-pdf').hide();
    
    var dataTable =  $('#stockCollectionDashboard').DataTable({
                responsive: {
                    details: {
                        renderer: function ( api, rowIdx ) {
                            var data = api.cells( rowIdx, ':hidden' ).eq(0).map( function ( cell ) {
                                var header = $( api.column( cell.column ).header() );
                                return  '<p style="color:#00A">'+header.text()+' : '+api.cell( cell ).data()+'</p>';
                            } ).toArray().join('');

                            return data ?    $('<table/>').append( data ) :    false;
                        }
                    }
                },
                processing: true,
                serverSide: true,
                destroy: true,
                ajax: {
                    url : "./stockpaymentgriddata.php",
                    type: 'POST',
                    data:  {
                        userpermissionPrint: 1,
                        supplierID  : '',
                        startDate   : '',
                        endDate     : ''
                    }
                }
            });  
});

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