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


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


?>
<!DOCTYPE html>
<html>
<head>
    <title>Daily 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="saleStock">
  
  <form class="form-inline" method="post" action="daily_report.php">
    
    <div class="form-group">
      <label for="">From 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"><br>

    <button type="submit" class="btn btn-primary btn-md datewise-submit-sale">Submit</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">
<div id="sales">
<h3 align="center">Sales  Report</h3>
<table id="salestockDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
        <thead>
       
            <tr>
                <th>S.No</th>
                <th>Date</th>
				
                <th>Customer</th>
                <th>Area</th>
               
               
				
				
                <th>Net Amount</th>
               
                <!-- <th>Action</th> -->
            </tr>
        </thead>
        
        <?php
				
				
				if($tripfromDate !=''  && $triptoDate !=''  ) {
	 	$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, sum(sh.tamount) as tamount, sh.create_date,sh.id,cm.cname,cm.cname_tamil FROM saleheader as sh LEFT JOIN customer_master as cm on cm.id = sh.customer_id  where sh.flag='1' and sh.status='' and sale_date BETWEEN '".$tripfromDate."' AND '".$triptoDate."' group by sh.customer_id ORDER BY sh.area,cm.cname  ASC ";

	 }
	 
	 //echo $sql;
				?>
        
        <tbody>
            <?php
			$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1".$sql);
			$total_price = 0;
			$total_qty = 0;
			$sno1=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 ++$sno1;?></td>
            <td><?php echo $sale_date2;?></td>
			
            <td><?php echo $row["cname"];?>(<?php echo $row["cname_tamil"];?>)</td>
            <td><?php echo $row["area"];?>(<?php echo $row["area_tamil"];?>)</td>
            
         
			
			
            <td align="right"><?php echo number_format(round($row["tamount"]), 2, '.', '');?></td>
			</tr>
            <?php
			
			$total_price  += $row["tamount"];
			
}


?>
       <tr>
           
           <td colspan="4" align="right"><strong>Total </strong></td>
           
           
			
            <td align="right"><strong><?php echo number_format(round($total_price), 2, '.', '');?></strong></td>
			</tr>             
           
        </tbody>
       
    </table>
    </div>
    
    <div id="sales_return">
<h3 align="center">Sales Return  Report</h3>
<table id="salestockDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
        <thead>
       
            <tr>
                <th>S.No</th>
                <th>Date</th>
				
                <th>Customer</th>
                <th>Area</th>
               
               
				
				
                <th>Net Amount</th>
               
                <!-- <th>Action</th> -->
            </tr>
        </thead>
        
        <?php
				
				
				if($tripfromDate !=''  && $triptoDate !=''  ) {
	 	$sql_return="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, sum(sh.tamount) as tamount, sh.create_date,sh.id,cm.cname,cm.cname_tamil FROM sale_return_header as sh LEFT JOIN customer_master as cm on cm.id = sh.customer_id  where sh.flag='1' and sh.status='' and sale_date BETWEEN '".$tripfromDate."' AND '".$triptoDate."' group by sh.customer_id ORDER BY sh.area,cm.cname  ASC ";

	 }
	 
	 //echo $sql;
				?>
        
        <tbody>
            <?php
			$query_return=mysqli_query($conn, $sql_return) or die("brandgriddata.php: get brand Data step 1".$sql);
			$total_price_return = 0;
			
			$sno3=0;
			while($row_return=mysqli_fetch_array($query_return))
{
	$deliver_date_return=$row_return["sale_date"];
$date2=date_create($deliver_date_return);
$sale_date2_return=date_format($date2,"d-m-Y");
				
				?>
			<tr>
            <td><?php echo ++$sno1;?></td>
            <td><?php echo $sale_date2_return;?></td>
			
            <td><?php echo $row_return["cname"];?>(<?php echo $row_return["cname_tamil"];?>)</td>
            <td><?php echo $row_return["area"];?>(<?php echo $row_return["area_tamil"];?>)</td>
            
         
            <td align="right"><?php echo number_format(round($row_return["tamount"]), 2, '.', '');?></td>
			</tr>
            <?php
			
			$total_price_return  += $row_return["tamount"];
			
}


?>
       <tr>
           
           <td colspan="4" align="right"><strong>Total </strong></td>
           
           
			
            <td align="right"><strong><?php echo number_format(round($total_price_return), 2, '.', '');?></strong></td>
			</tr>             
           
        </tbody>
       
    </table>
    </div>
    
    <div id="Collection">
    <h3 align="center">Sales Collection Report</h3>
<table  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>Collected Amount</th>
               
            </tr>
        </thead>
        <tbody>
    <?php
	$sql_collection = "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, sum(cc.received_amount) as received_amount ,cc.discount, cm.cname,cm.area,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 '".$tripfromDate."' AND '".$triptoDate."' group by cc.customerID order by cm.area,cm.cname ";
	$query_collection=mysqli_query($conn, $sql_collection) or die("php: get purchase Data3");
		$collection=0;
		$sno2=0;
		
		while( $row_collection=mysqli_fetch_array($query_collection) ) {
		$date = new DateTime($row_collection["row_collection"]); 
	?>
    
    <tr>
          <td><?php echo  ++$sno2;?></td>

    
   
	
    <td><?php echo  $date->format('d-m-Y');?></td> 
    <td><?php echo  $row_collection["cname"];?></td>
    <td><?php echo  $row_collection["area_tamil"];?></td>
	<td align="right"><?php echo  number_format(round($row_collection["received_amount"]),2,'.','');?></td>
	  
          </tr>          
           <?php
		   
		   $collection+=$row_collection["received_amount"];
		
		   }
		   ?>
           <tr>
           <td colspan="4" align="right"><strong>Total</strong></td>
           <td align="right"><strong><?php echo number_format($collection,2,'.','');?></strong></td>
            
            </tr>
        </tbody>
       
    </table>
    
    </div>
    
    <div id="expense">
    <center><h3 > Cash  Expense Report</h3></center>

<table id="salestockDashboard" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Cash No</th>
                <th>Date</th>
				<th>Expenser</th>
				<th>Amount</th>
            </tr>
        </thead>
    <?php
				
				
				$sql_expense = "SELECT sh.id,sh.cashno, sh.expensedate, sh.expensetype,sh.expenser,  sh.empname, sh.expense_details,  sum(sh.amount) as amount,sh.remark,em.ename,exm.expense FROM cash_expense_entry as sh LEFT JOIN employee_master as em on em.id = sh.expenser inner join expense_master as exm on sh.expensetype=exm.id  where sh.flag='1' and sh.status='' and sh.expensedate  BETWEEN '".$tripfromDate."' AND '".$triptoDate."' group by sh.expenser ORDER BY sh.expensedate ASC";
				//echo $sql_expense;
				?>
    <tbody>
            <?php
			$query_expense=mysqli_query($conn, $sql_expense) or die("brandgriddata.php: get brand Data step 1".$sql);
			$total_expense=0;
			$sno4=0;
			while($row_expense=mysqli_fetch_array($query_expense))
{
	$expense_date=$row_expense["expensedate"];
$date_expense=date_create($expense_date);
$expense_date2=date_format($date_expense,"d-m-Y");
				
				?>
			<tr>
            <td><?php echo ++$sno4;?></td>
            <td><?php echo $expense_date2;?></td>
			<td><?php echo $row_expense["ename"];?></td>
            
             <td align="right"><?php echo number_format(round($row_expense["amount"]), 2, '.', '');?></td>
              
			</tr>
            <?php
			
			$total_expense  += $row_expense["amount"];
			
}


?>
       <tr>
           
           <td colspan="3" align="right"><strong>Total </strong></td>
           
           
			
            <td align="right"><strong><?php echo number_format(round($total_expense), 2, '.', '');?></strong></td>
			</tr>              
                    
           
        </tbody>
       
    </table>
    </div>
    
    <table  class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" align="right">
        <thead>
            <tr>
                <td align="right"><strong>Toal&nbsp;Sales</strong></td>
                <td align="right"><strong><?php echo number_format(round($total_price), 2, '.', '');?></strong></td>
                </tr>
                
                <tr>
                <td align="right"><strong>Toal&nbsp;Sales&nbsp;Return</strong></td>
                <td align="right"><strong><?php echo number_format(round($total_price_return), 2, '.', '');?></strong></td>
                </tr>
                
                <tr >
                <td align="right"><strong>Toal&nbsp;Collection</strong></td>
                <td align="right"><strong><?php echo number_format(round($collection), 2, '.', '');?></strong></td>
                </tr>
                
                <tr>
                <td align="right"><strong>Toal&nbsp;Expense</strong></td>
                <td align="right"><strong><?php echo number_format(round($total_expense), 2, '.', '');?></strong></td>
                </tr>
                
                 <tr>
                <td align="right"><strong>Toal&nbsp;Pending</strong></td>
                <td align="right"><strong>
				<?php
				$pending=$total_price-($total_price_return+$collection);
				
				 echo number_format(round($pending), 2, '.', '');
				 
				 ?>
                 </strong></td>
                </tr>
                
                <tr>
                <td align="right"><strong>Amount In Hand</strong></td>
                <td align="right"><strong>
				<?php
				$remaining=$collection-$total_expense;
				
				 echo number_format(round($remaining), 2, '.', '');
				 
				 ?>
                 </strong></td>
                </tr>
                
        </thead>
    
    </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>