<?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['fromDate'];
$endDate 	= $_POST['toDate'];

?>
<!DOCTYPE html>
<html>
<head>
    <title>Payment 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>
 
 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 text-center" id="purchaseStock">
  
  <form class="form-inline" action="stockreportpayment.php" method="post">
    
    <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="">supplier </label>

        <select class="form-control  supplierStock" 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>
    <button type="submit" class="btn btn-primary btn-md datewise-submit-collection">Submit</button>
<button type="button" class="btn-success btn-lg btn-md datewise-submit-sale" onClick="printDiv();">Print</button>

  </form>
</div>


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


<div class="container col-sm-12">
<div id="a">
<h3 align="center">Purchase Payment Report</h3>
<table id="stockCollectionDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Invoice No</th>
                <th>Date</th>
                <th>supplier</th>
               <!-- <th>purchases Amount</th>-->
               
                <th>Paid Amount</th>
                
                 <th>Payment Mode</th>
                 <!-- <th>Handover TO</th>
                 <th>Ref.No</th>
                 <th>Txn.No</th>
                 <th>Bank Name</th>-->
                 <th>Remark</th>
                <!-- <th>Action</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.status=''";
		}
		
		else 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.collectiondate BETWEEN '".$startDate."' AND '".$endDate."' and cc.status=''";
		}
		
		else 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($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.status=''";
		}
		
		$query=mysqli_query($conn, $sql) or die("php: get purchase Data3");
		while( $row=mysqli_fetch_array($query) ) {
		$date = new DateTime($row["collectiondate"]); 
		?>       
           <tr>
          <td><?php echo  $row["cashno"];?></td>

    
   
	
    <td><?php echo  $date->format('d-m-Y');?></td> 
    <td><?php echo  $row["sname"];?></td>
    
	<td><?php echo  number_format(round($row["received_amount"]),2,'.','');?></td>
	
	
	
    <td><?php echo  $row["paymenttype"];?></td>
	
	 <td><?php echo  $row["remark"];?></td>
          
          </tr>          
           <?php
		   }
		   ?>
        </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>