<?php
session_start();
require("php_con.php");
require("check_user.php");

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="favicon.ico">
<title>Report - <?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<style>
	table{
		border-collapse: collapse;
	}
	table tr td, th{
		font-size: 14px;
		padding: 5px;
		border: 1px solid #000000;
	}
</style>
</head>

<body>
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>

<div align="right"><button type="button" class="btn btn-success btn-sm" onClick="JavaScript:printDiv('print_aria');">Print Report</button></div>
	
<div class="m-4" id="print_aria">

<div align="center" class="mb-2">
<h4>Sale Report (Invoice Wise)</h4>
<p>Run time: <?php echo $current_date." - ".$current_time; ?> | User: <?php echo $current_user_resalt['user_fname']; ?><br>
Duration: <?php echo $_POST['st_date']." to ".$_POST['en_date']; ?></p>
</div>	<br>
	
<table style="margin: auto;">
  <tbody>
    <tr class="table-active font-weight-bold">
      <td>Invoice</td>
		<td>User</td>
      <td>Item's</td>
      <td align="right">Income</td>
      <td align="right">Cost</td>
      <td align="right">Profit</td>
    </tr>
	  
<?php
$total_amount=0;
$total_cost=0;
$d_qury=mysqli_query($connation,"SELECT DISTINCT(sd.sale_details_date) AS sale_date
FROM sale_details sd
WHERE sd.sale_details_date BETWEEN '$_POST[st_date]' AND '$_POST[en_date]'
ORDER BY sd.sale_details_date ASC");
while($d_resalt=mysqli_fetch_assoc($d_qury)){
?>
    <tr class="font-weight-bold">
      <td colspan="6"><strong>Date: <?php echo $d_resalt['sale_date']; ?></strong></td>
    </tr>
	  
<?php 
$bill_amount=0;
$bill_cost=0;
$sl_qury=mysqli_query($connation,"SELECT sd.sale_details_discount,sd.sale_details_billid,COUNT(sl.sale_barcode) AS count,SUM(sl.sale_saleprice*sl.sale_qty)-sd.sale_details_discount AS bill_amount,SUM(sl.sale_buy_price*sl.sale_qty) AS cost,CONCAT(ud.user_fname,' ',ud.user_lname) AS user_name
FROM sale_details sd LEFT JOIN sale_list sl ON sd.sale_details_billid=sl.sale_billid
INNER JOIN user_details ud ON sd.sale_details_user=ud.user_id
WHERE sd.sale_details_date='$d_resalt[sale_date]'
GROUP BY sd.sale_details_billid
ORDER BY sd.sale_details_id");
while($sl_resalt=mysqli_fetch_assoc($sl_qury)){
$bill_amount+=$sl_resalt['bill_amount'];
$bill_cost+=$sl_resalt['cost'];
?>
	  
    <tr>
      <td><?php echo $sl_resalt['sale_details_billid']; ?></td>
		<td><?php echo $sl_resalt['user_name']; ?></td>
      <td><?php echo number_format((float)$sl_resalt['count']); ?></td>
      <td align="right"><?php echo number_format((float)$sl_resalt['bill_amount'],2); ?></td>
      <td align="right"><?php echo number_format((float)$sl_resalt['cost'],2); ?></td>
      <td align="right"><?php echo number_format((float)$sl_resalt['bill_amount']-$sl_resalt['cost'],2); ?></td>
    </tr>
	  
<?php } ?>
	  
    <tr class="font-weight-bold">
      <td colspan="3">Sub Amount (<?php echo $d_resalt['sale_date']; ?>)</td>
      <td align="right"><?php echo number_format((float)$bill_amount,2); ?></td>
      <td align="right"><?php echo number_format((float)$bill_cost,2); ?></td>
      <td align="right"><?php echo number_format((float)$bill_amount-$bill_cost,2); ?></td>
    </tr>
	  
    <tr>
      <td colspan="6">&nbsp;</td>
    </tr>
	  
<?php
$total_amount+=$bill_amount;
$total_cost+=$bill_cost;
} ?>
	  
    <tr style="font-weight: bold;">
      <td colspan="3">Net Amount</td>
      <td align="right"><?php echo number_format((float)$total_amount,2); ?></td>
      <td align="right"><?php echo number_format((float)$total_cost,2); ?></td>
      <td align="right"><?php echo number_format((float)$total_amount-$total_cost,2); ?></td>
    </tr>
	  
  </tbody>
</table>

	
</div>
	
<script>
function printDiv(print_aria){
	var printcontents=document.getElementById('print_aria').innerHTML;
	var orginalcontain=document.body.innerHTML;
	document.body.innerHTML=printcontents;
	window.print();
	document.body.innerHTML=orginalcontain;

}
</script>

	
</body>
</html>
<?php @mysqli_close($connation); ?>