<?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,cm.area_tamil FROM customer_payment_header 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'];

$area 	= $_POST['area'];

?>
<!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>
 
 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="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']; ?>&nbsp;(<?php echo $row['area_tamil']; ?>) </option>
        <?php
        }
        ?>
        </select>
    </div>
    
    <div class="form-group">
    <label for="">Area </label>

        <select class="form-control  " name="area">
        <option value="">Select the Area </option>
       <?php
	   $sqlproduct = mysqli_query($conn,"SELECT id, areaname,area_tamil FROM area_master WHERE isdelete='1' ");



while($prow = mysqli_fetch_array($sqlproduct)){
	   
	   ?>
        <option value="<?php echo $prow['areaname'];?>" > <?php echo $prow['areaname'];?>/<?php echo $prow['area_tamil'];?> </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-success btn-md datewise-submit-sale" onClick="printDiv();">Print</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>purchases Amount</th>-->
                <th>Area</th>
                <th>Collected Amount</th>
                <th>Discount Amount</th>
                 <th>Cash Amount</th>
                  <th>Bank 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 =='' && $area =='' ) {
		$sql = "SELECT cc.handover,cc.transactionno,cc.cash_amount,cc.bank_amount,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.customerID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount,cc.discount, cm.cname,cm.area_tamil FROM customer_payment_header as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where  cc.status='' order by cc.id DESC LIMIT 100";
		}
		
		else if($cID=='' && $startDate !='' && $endDate !='' && $area =='') {
		$sql = "SELECT cc.handover,cc.transactionno,cc.cash_amount,cc.bank_amount,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.customerID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount,cc.discount, cm.cname,cm.area_tamil FROM customer_payment_header 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 !='' && $area =='') {
		$sql = "SELECT cc.handover,cc.transactionno,cc.cash_amount,cc.bank_amount,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.customerID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount,cc.discount, cm.cname,cm.area_tamil FROM customer_payment_header 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 =='' && $area =='') {
		$sql = "SELECT cc.handover,cc.transactionno,cc.cash_amount,cc.bank_amount,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.customerID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount,cc.discount, cm.cname ,cm.area_tamil FROM customer_payment_header as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where  cc.status='' and  cc.customerID ='".$cID."'";
		}
		
		else if($cID=='' && $startDate !='' && $endDate !='' && $area !='') {
		$sql = "SELECT cc.handover,cc.transactionno,cc.cash_amount,cc.bank_amount,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.customerID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount,cc.discount, cm.cname ,cm.area_tamil FROM customer_payment_header as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where  cc.status='' and collectiondate BETWEEN '".$startDate."' AND '".$endDate."' and   cm.area='".$area."'";
		}
		
		else if($cID!='' && $startDate !='' && $endDate !='' && $area !='') {
		$sql = "SELECT cc.handover,cc.transactionno,cc.cash_amount,cc.bank_amount,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.customerID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount,cc.discount, cm.cname ,cm.area_tamil FROM customer_payment_header as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where  cc.status='' and collectiondate BETWEEN '".$startDate."' AND '".$endDate."' and   cm.area='".$area."' and  cc.customerID ='".$cID."'";
		}
		
		else if($cID!='' && $startDate =='' && $endDate =='' && $area !='') {
		$sql = "SELECT cc.handover,cc.transactionno,cc.cash_amount,cc.bank_amount,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.customerID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount,cc.discount, cm.cname ,cm.area_tamil FROM customer_payment_header as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where  cc.status='' and    cm.area='".$area."' and  cc.customerID ='".$cID."'";
		}
		
		else if($cID=='' && $startDate =='' && $endDate =='' && $area !='') {
		$sql = "SELECT cc.handover,cc.transactionno,cc.cash_amount,cc.bank_amount,cc.paymentreference,cc.id,cc.cashno,cc.payment_through, cc.collectiondate, cc.customerID, cc.pending,cc.remark, cc.paymenttype, cc.received_amount,cc.discount, cm.cname ,cm.area_tamil FROM customer_payment_header as cc LEFT JOIN customer_master as cm on cm.id = cc.customerID where  cc.status=''  and   cm.area='".$area."' ";
		}
		//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["area_tamil"];?></td>
	<td align="right"><?php echo  number_format(round($row["received_amount"]),2,'.','');?></td>
	<td align="right"><?php echo  number_format(round($row["discount"]),2,'.','');?></td>
    
    <td align="right"><?php echo  number_format(round($row["cash_amount"]),2,'.','');?></td>
	<td align="right"><?php echo  number_format(round($row["bank_amount"]),2,'.','');?></td>
	
	
    <td><?php echo  $row["paymenttype"];?></td>
	
	 <td><?php echo  $row["remark"];?></td>
          
          </tr>          
           <?php
		   
		   $pamt+=$row["received_amount"];
		$damt+=$row["discount"];
		
		$cash+=$row["cash_amount"];
		$bank+=$row["bank_amount"];
		   }
		   ?>
           <tr>
           <td colspan="4" align="right"><strong>Total</strong></td>
           <td align="right"><strong><?php echo number_format($pamt,2,'.','');?></strong></td>
            <td align="right"><strong><?php echo number_format($damt,2,'.','');?></strong></td>
             <td align="right"><strong><?php echo number_format($cash,2,'.','');?></strong></td>
              <td align="right"><strong><?php echo number_format($bank,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>