<?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 (Product 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; margin-bottom: 10px;">
  <tbody>
    <tr class="table-active">
      <th>Barcode</th>
      <th>Product</th>
      <th>QTY</th>
      <th>Income</th>
      <th>Cost</th>
      <th>Profit</th>
    </tr>
<?php 
$cost=0;
$income=0;
	  
if($_POST['product_catagory']=="all"){
$product_catagory="";
}
else{
$product_catagory="pd.product_catagory='$_POST[product_catagory]' AND ";
}

$o_qury=mysqli_query($connation,"SELECT SUM(sl.sale_buy_price*sl.sale_qty) AS cost,SUM(sl.sale_saleprice*sl.sale_qty) AS income
FROM sale_list sl INNER JOIN sale_details sd ON sl.sale_billid=sd.sale_details_billid
LEFT JOIN product_details pd ON sl.sale_barcode=pd.product_barcode
WHERE pd.product_barcode IS NULL AND sd.sale_details_date BETWEEN '$_POST[st_date]' AND '$_POST[en_date]'");

$o_resalt=mysqli_fetch_assoc($o_qury);
$cost+=$o_resalt['cost'];
$income+=$o_resalt['income'];
?>
    <tr style="border-bottom: 5px solid #000000;">
      <td colspan="2">Other Product</td>
		<td>-</td>
      <td align="right"><?php echo number_format((float)$o_resalt['income'],2); ?></td>
      <td align="right"><?php echo number_format((float)$o_resalt['cost'],2); ?></td>
      <td align="right"><?php echo number_format((float)$o_resalt['income']-$o_resalt['cost'],2); ?></td>
    </tr>
<?php ?>
	  
<?php 
$qury=mysqli_query($connation,"SELECT pd.product_barcode,pd.product_name,SUM(sl.sale_qty) AS qty,SUM(sl.sale_buy_price*sl.sale_qty) AS cost,SUM(sl.sale_saleprice*sl.sale_qty) AS income
FROM sale_list sl LEFT JOIN sale_details sd ON sl.sale_billid=sd.sale_details_billid
INNER JOIN product_details pd ON sl.sale_barcode=pd.product_barcode
WHERE $product_catagory sd.sale_details_date BETWEEN '$_POST[st_date]' AND '$_POST[en_date]'
GROUP BY pd.product_barcode
ORDER BY pd.product_name");

while($resalt=mysqli_fetch_assoc($qury)){
$cost+=$resalt['cost'];
$income+=$resalt['income'];
$resalt['income']-$resalt['cost']
?>
    <tr>
      <td><?php echo $resalt['product_barcode']; ?></td>
      <td><?php echo $resalt['product_name']; ?></td>
      <td><?php echo number_format((float)$resalt['qty']); ?></td>
      <td align="right"><?php echo number_format((float)$resalt['income'],2); ?></td>
      <td align="right"><?php echo number_format((float)$resalt['cost'],2); ?></td>
      <td align="right"><?php echo number_format((float)$resalt['income']-$resalt['cost'],2); ?></td>
    </tr>
<?php } ?>
	  
    <tr style="border-top: 5px solid #000000;">
      <td colspan="3">Other Sale Amount</td>
      <td align="right"><?php echo number_format((float)$o_resalt['income'],2); ?></td>
      <td align="right"><?php echo number_format((float)$o_resalt['cost'],2); ?></td>
      <td align="right"><?php echo number_format((float)$o_resalt['income']-$o_resalt['cost'],2); ?></td>
    </tr>
	  
    <tr>
      <td colspan="3">Product Sale Amount</td>
      <td align="right"><?php echo number_format((float)$income-$o_resalt['income'],2); ?></td>
      <td align="right"><?php echo number_format((float)$cost-$o_resalt['cost'],2); ?></td>
      <td align="right"><?php echo number_format((float)($income-$cost)-($o_resalt['income']-$o_resalt['cost']),2); ?></td>
    </tr>
	  
    <tr style="font-weight: bold;">
      <td colspan="3">Net Amount</td>
      <td align="right"><?php echo number_format((float)$income,2); ?></td>
      <td align="right"><?php echo number_format((float)$cost,2); ?></td>
      <td align="right"><?php echo number_format((float)$income-$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); ?>