<?php 

require_once("config/configDB.php");


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

$row_customer = '';

$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>";
}

?>
<!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 col-sm-12">
<button type="button" class="btn btn-info btn-md datewise-submit-sale" onClick="tableToExcel('a', 'W3C Example Table')">Excel</button>
<div id="a">


<table id="stockCollectionDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">

<caption><center><h3>Payment Pending Purchase Invoice (GST)</h3></center></caption>
        <thead>
            <tr>
                <th>S.no</th>
              
                <th>Supplier</th>
                <th>Contact Number</th>
                <th>Purchase Amount</th>
               
                <th>Paid Amount</th>
                 <th>Pending Amount</th>
                
                <!-- <th>Action</th> -->
            </tr>
        </thead>
        <tbody>
        
        <?php
		
		$sqlj=mysqli_query($conn,"SELECT sh.supplier_id,sum(sh.tamount) as tamount,cm.mobile,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 ");
		
		$count=0;
		$saleamount=0;
		$collection=0;
		$remaining=0;
		
		while($rowj=mysqli_fetch_array($sqlj))
		{
			$customer_id=$rowj['supplier_id'];
			
			
			$sqlj1=mysqli_query($conn,"SELECT sum(received_amount) as camount FROM `supplier_payment_header` WHERE supplierID='$customer_id' and status=''");
			while($rowj1=mysqli_fetch_array($sqlj1))
		{
			
			$pending=$rowj['tamount'] - $rowj1['camount'];
		$samount=number_format(round($rowj['tamount']),2,'.','');
			$camount=number_format(round($rowj1['camount']),2,'.','');
			
			if($samount==$camount)
			{
				$stl= "style='display:none'";
			}
			else
			{
			$stl=" ";	
			}
		?>
        
            <tr <?php echo $stl;?>>
            <td><?php echo ++$count;?></td>
            <td><?php echo $rowj['sname'];?></td>
             <td><?php echo $rowj['mobile'];?></td>
            <td align="right"><?php echo number_format(round($rowj['tamount']),2,'.','');?></td>
            <td align="right"><?php echo number_format(round($rowj1['camount']),2,'.','');?></td>
             <td align="right"><?php echo number_format(round($pending),2,'.','');?></td>
            
             </tr>
             
             <?php
			 
			 $saleamount +=$rowj['tamount'];
		$collection +=$rowj1['camount'];
		$remaining =$saleamount - $collection ;
		}
		}
		?>
           <tr>
            
            
            <td colspan="3" align="right"><strong>Total Amount</strong></td>
            <td align="right"><strong><?php echo number_format(round($saleamount),2,'.','');?></strong></td>
            <td align="right"><strong><?php echo number_format(round($collection),2,'.','');?></strong></td>
             <td align="right"><strong><?php echo number_format(round($remaining),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" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js"></script>

   
</body>
</html>