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

$sql_supplier = mysqli_query($conn,"SELECT sh.customerID,cm.cname FROM collection_customer as sh  join customer_master as cm on cm.id=sh.customerID WHERE cm.isdelete='1' and sh.status='' GROUP BY sh.customerID order by cm.cname");

$row_supplier = '';

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


?>
<!DOCTYPE html>
<html>
<head>
    <title>Collection 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="purchaseStock">
  
  <form class="form-inline" action="stockreportcollection.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="">Customer </label>

        <select class="form-control  supplierStock" name="supplierName">
        <option value="">Please Select the Customer </option>
        <?php
        while($row = mysqli_fetch_array($sql_supplier)){
        ?>
        <option value="<?php echo $row['customerID'] ?>" > <?php echo $row['cname']; ?> </option>
        <?php
        }
        ?>
        </select>
    </div>
    
    
   
<div class="form-group">
<br>
    <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>
    <!--<a href="stockreportcollectionpdf.php" target="_blank" class="btn btn-info purchase-stockreport-pdf" role="button">PDF</a>-->

  </form>
</div>


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



<div class="container col-sm-12">
<div id="a">
<h3 align="center">Sales Collection 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>Customer</th>
               <th>Bill.No</th>
                
                <th>Collected Amount</th>
               
               
                 <th>Payment Mode</th>
                
                 <th>Remark</th>
                
            </tr>
        </thead>
        <tbody>
        
        <?php
		
		if($cID=='' && $startDate =='' && $endDate ==''  ) {
		$sql = "SELECT cc.cashno,cc.collectiondate,cc.paymentreference,cc.id,cc.saleID,  cc.customerID, cc.pending, cc.paymenttype, cc.amount, cm.cname FROM collection_customer as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where  cc.status=''";
		}
		
		else if($cID=='' && $startDate !='' && $endDate !='' ) {
		$sql = "SELECT cc.cashno,cc.collectiondate,cc.paymentreference,cc.id,cc.saleID,  cc.customerID, cc.pending, cc.paymenttype, cc.amount, cm.cname FROM collection_customer as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where  cc.status='' and collectiondate BETWEEN '".$startDate."' AND '".$endDate."'";
		}
		
		else if($cID!='' && $startDate !='' && $endDate !='' ) {
		$sql = "SELECT cc.cashno,cc.collectiondate,cc.paymentreference,cc.id,cc.saleID,  cc.customerID, cc.pending, cc.paymenttype, cc.amount, cm.cname FROM collection_customer as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where  cc.status='' and collectiondate BETWEEN '".$startDate."' AND '".$endDate."' and cc.customerID ='".$cID."'";
		}
		
		else if($cID!='' && $startDate =='' && $endDate =='' ) {
		$sql = "SELECT cc.cashno,cc.collectiondate,cc.paymentreference,cc.id,cc.saleID,  cc.customerID, cc.pending, cc.paymenttype, cc.amount, cm.cname FROM collection_customer as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where  cc.status='' and  cc.customerID ='".$cID."'";
		}
		
		
		//echo $sql;
		
		$query=mysqli_query($conn, $sql) or die("php: get purchase Data3");
		$pamt=0;
		$damt=0;
		
		$cash=0;
		$bank=0;
		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["cname"];?></td>
   <td><?php echo  $row["saleID"];?></td>
	<td align="right"><?php echo  number_format(round($row["amount"]),2,'.','');?></td>
	
    
   
	
	
    <td><?php echo  $row["paymenttype"];?></td>
	
	 <td><?php echo  $row["remark"];?></td>
          
          </tr>          
           <?php
		   
		   $pamt+=$row["amount"];
		
		   }
		   ?>
           <tr>
           <td colspan="4" align="right"><strong>Total</strong></td>
           <td align="right"><strong><?php echo number_format($pamt,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/stockreportcollection.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 : "./stockcollectiongriddata.php",
                    type: 'POST',
                    data:  {
                        userpermissionPrint: 1,
                        customerID  : '',
                        startDate   : '',
                        endDate     : ''
                    }
                }
            });  
});


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