<?php 
error_reporting(0);
require_once("config/configDB.php");
require_once("config/constant.php");

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

$row_customer = '';




$tripfromDate=$_POST['fromDate'];
				$triptoDate=$_POST['fromDate'];
				//$triptoDate=$_POST['toDate'];
				
				$area=$_POST['area'];
				


?>
<!DOCTYPE html>
<html>
<head>
    <title>Area 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>

<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="saleStock">
  
  <form class="form-inline" method="post" action="areaReport.php">
    
    <div class="form-group">
      <label for=""> Date:</label><br>
      <input type="date" class="form-control input-lg " id="fromDate" name="fromDate" autocomplete="off">
    </div>
   <!-- <div class="form-group">
      <label for="">To Date</label><br>
      <input type="date" class="form-control input-lg" id="toDate" name="toDate" autocomplete="off">
    </div>-->

<!--<div class="form-group">
    <label for="">Customer </label>

        <select class="form-control  customerStock selectpicker" data-show-subtext="true" data-live-search="true" name="customer">
        <option value="">Select the Customer </option>
        <?php
        while($row = mysqli_fetch_array($sql_customer)){
        ?>
        <option value="<?php echo $row['customer_id'] ?>" > <?php echo $row['cname']; ?> </option>
        <?php
        }
        ?>
        </select>
    </div>-->
    
    
    <div class="form-group">
    <label for="">Area </label>

        <select class="form-control  customerStock selectpicker" data-show-subtext="true" data-live-search="true" name="area" required>
        <option value="">Select the Area </option>
       <?php
	   $sqlproduct = mysqli_query($conn,"SELECT area, area_tamil FROM saleheader WHERE status='' group by area order by area ASC ");



while($prow = mysqli_fetch_array($sqlproduct)){
	   
	   ?>
        <option value="<?php echo $prow['area'];?>" > <?php echo $prow['area'];?>/ <?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-sale">Submit</button>
<button type="button" class="btn-success btn-lg btn-md datewise-submit-sale" onClick="printDiv();">Print</button>
    <!--<button type="button" class="btn btn-info btn-md datewise-submit-sale" onClick="tableToExcel('a', 'W3C Example Table')">Excel</button>-->
    </div>
  </form>
  
</div>


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



<div class="container col-sm-12">
<div id="a">

        <?php
        
        if($tripfromDate !='' && $triptoDate !='' && $area !='') {
$supplierqry=mysqli_query($conn,"select id,area,area_tamil,customer_id from saleheader where status='' and sale_date between'".$tripfromDate."' AND  '".$triptoDate."' and area='".$area."'  group by area");
}

//echo "select id,area,area_tamil,customer_id from saleheader where status='' and sale_date between'".$tripfromDate."' AND  '".$triptoDate."' and area='".$area."' ";

$slamt=0;
$clamt=0;
while($serrow=mysqli_fetch_array($supplierqry))
{

$area=$serrow['area'];
$area_tamil=$serrow['area_tamil'];
$cusid=$serrow['customer_id'];
$hid=$serrow['id'];
      ?>  
        
        
      <h3 align="center">Area Report</h3>  
       
<h3>Date:<?php  $date=date_create($tripfromDate); echo date_format($date,"d-m-Y");?></h3>
<h3><?php echo $area;?>&nbsp;-&nbsp;<?php echo $area_tamil;?></h3>


<?php

$sql_cus=mysqli_query($conn,"select shd.customer_id,cmh.cname,cmh.cname_tamil from saleheader as shd inner join customer_master as cmh on shd.customer_id=cmh.id where shd.status='' and shd.area='$area' and shd.sale_date between'".$tripfromDate."' AND  '".$triptoDate."' and tamount!='0'");

//echo "select shd.customer_id,cmh.cname,cmh.cname_tamil from saleheader as shd inner join customer_master as cmh on shd.customer_id=cmh.id where shd.status='' and shd.area='$area' and shd.sale_date between'".$tripfromDate."' AND  '".$triptoDate."'";

while($row_cus=mysqli_fetch_array($sql_cus))
{
$cusname=$row_cus['cname'];
$cname_tamil=$row_cus['cname_tamil'];
$cusname_id=$row_cus['customer_id'];

?>
      <h3><?php echo $cusname;?>(<?php echo $cname_tamil;?>)</h3>     
                <table id="salestockDashboard" 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>Area</th>-->
               
                
                <th>Product</th>
                <th>Rate</th>
				<th>Qty</th>
				
				
                <th>Net Amount</th>
               
                <!-- <th>Action</th> -->
            </tr>
        </thead>
			<?php
				
				
				$sql = "SELECT sh.id,sh.invoice_no,sh.area,sh.area_tamil, sh.sale_date,sh.customer_id, sh.vehicleno as vehicle,sh.ename as ename, sh.itemcount, sh.tamount, sh.create_date,sh.id,cm.cname,cm.cname_tamil,sd.productID,sd.sale_prate prate,sd.sale_eqty qt,pm.productname pname,pm.product_tamil prname,pm.qty_type,sd.salenetamt FROM saleheader as sh inner JOIN customer_master as cm on cm.id = sh.customer_id inner join saledetails as sd on sh.id=sd.saleheader_id inner join product_master as pm on pm.id=sd.productID where  sh.status='' and sh.customer_id='$cusname_id' and sd.status='' and sh.area='$area' and sh.sale_date between'".$tripfromDate."' AND  '".$triptoDate."' and tamount!='0' ORDER BY sh.sale_date ASC";
				
				
				
				
				
			$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1".$sql);
			$total_price = 0;
			$total_qty = 0;
			while($row=mysqli_fetch_array($query))
{
	$deliver_date=$row["sale_date"];
$date=date_create($deliver_date);
$sale_date2=date_format($date,"d-m-Y");



				
				?>
			<tr>
            <td><?php echo $row["invoice_no"];?></td>
           <!-- <td><?php echo $sale_date2;?></td>
			
            <td><?php echo $row["cname_tamil"];?></td>
            <td><?php echo $row["area_tamil"];?></td>-->
            
            
            <td><?php echo $row["prname"];?></td>
            <td><?php echo $row["prate"];?></td>
			<td align="right"><?php echo number_format((float)$row["qt"], 2, '.', '');?>&nbsp;<?php echo $row['qty_type'];?></td>
			
			
            <td align="right"><?php echo number_format(round($row["salenetamt"]), 2, '.', '');?></td>
			</tr>
            <?php
			
			$total_price  += $row["salenetamt"];
			$total_qty  += $row["qt"];
			
}


?>

<?php
            $sql_collection=mysqli_query($conn,"select sum(received_amount) as collection from customer_payment_header where status='' and customerID='$cusname_id' and collectiondate='$tripfromDate'");

while($row_collection=mysqli_fetch_array($sql_collection))
{        
$colamt=$row_collection['collection'];

}  
          ?> 
       <tr>
           
           
           
			<td  align="right" colspan="3" ><strong>Total</strong></td>
            <td align="right"><strong><?php echo number_format(round($total_qty), 2, '.', '');?></strong></td>
            <td align="right"><strong><?php echo number_format(round($total_price), 2, '.', '');?></strong></td>
			</tr> 
            <tr>
           
           
           
			<td  align="right" colspan="4" ><strong>Total Collection Amount</strong></td>
            <td align="right"><strong><?php echo number_format(round($colamt), 2, '.', '');?></strong></td>
			</tr>   
            
        
    </table>
    
    <?php
	$slamt=$slamt+$total_price;
$clamt=$clamt+$colamt;
	
	}
	
	
	}
?>

<h3 align="right">Total Sales:<?php echo number_format($slamt,2,'.','');?></h3>
<h3 align="right">Total Collection:<?php echo number_format($clamt,2,'.','');?></h3>
    
    <div>&nbsp;</div>
    <h4 align="center">Total Products</h4>
    <table id="salestockDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
    <tr>
    <th>Product</th>
    <th>Product</th>
    <th>Sales Qty</th>
    </tr>
    <?php
	
/*	 if($tripfromDate !='' && $triptoDate !='' && $area !='') {
$sumqry=mysqli_query($conn,"select id,area,area_tamil,customer_id from saleheader where status='' and sale_date between'".$tripfromDate."' AND  '".$triptoDate."' and area='".$area."' ");
}


while($sumprrow=mysqli_fetch_array($sumqry))
{
$hdrid=$sumprrow['id'];
*/
//echo $hdrid;
	
	$sql_sumpr=mysqli_query($conn,"select sh.id,sum(sd.sale_eqty) as sale_eqty,pm.productname,pm.product_tamil,pm.qty_type from saleheader as sh inner join saledetails as sd on sd.saleheader_id=sh.id inner join product_master as pm on pm.id=sd.productID where sd.status='' and sh.status='' and  sh.sale_date between'".$tripfromDate."' AND  '".$triptoDate."' and sh.area='$area' group by sd.productID");
	
	while($prrow=mysqli_fetch_array($sql_sumpr))
	{
	?>
    <tr>
    <td><?php echo $prrow['productname'];?></td>
   
    <td><?php echo $prrow['product_tamil'];?></td>
    <td><?php echo $prrow['sale_eqty'];?>&nbsp;<?php echo $prrow['qty_type'];?></td>
    </tr>
    
    
    <?php
	
	
	}
	//}
	
	?>
    </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>

    <!--<script>



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

    //toDate

    $("#toDate").datepicker({
        minDate: '1',
        maxDate: '+1Y+6M',
        dateFormat: 'yy-mm-dd',
        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>