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

if(!(isset($_POST['st_date'])&&isset($_POST['en_date']))){
exit("Error, Please try again.");
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Report - <?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
	
<style>
	.table tr td{
		font-size: 14px;
		padding: 5px;
	}
	.table tr th{
		font-size: 14px;
		padding: 5px;
	}
	.print-table{
		width: 50%;
		margin: auto;
	}
	@media print {
		.print-table{
			width: 100%;
		}
	}
</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 class="p-2 text-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>Main summary report</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>	
<hr>
	
<?php 
$total_sale_qury=mysqli_query($connation,"SELECT SUM(sl.sale_saleprice*sl.sale_qty) AS amount,SUM(sl.sale_buy_price*sl.sale_qty) AS cost
FROM sale_details sd INNER JOIN sale_list sl ON sd.sale_details_billid=sl.sale_billid
WHERE sd.sale_details_date BETWEEN '$_POST[st_date]' AND '$_POST[en_date]'");
$total_sale_resalt=mysqli_fetch_assoc($total_sale_qury);
	
$sale_qury=mysqli_query($connation,"SELECT SUM(sd.sale_details_cash_payment) AS cash_amount,SUM(sd.sale_details_card_payment) AS card_amount,SUM(sd.sale_details_discount) AS dis_amount
FROM sale_details sd
WHERE sd.sale_details_date BETWEEN '$_POST[st_date]' AND '$_POST[en_date]'");
$sale_resalt=mysqli_fetch_assoc($sale_qury);
	
$supp_pay_qury=mysqli_query($connation,"SELECT SUM(sp_amount) AS amount FROM supplier_payment WHERE sp_add_date BETWEEN '$_POST[st_date]' AND '$_POST[en_date]'");
$supp_pay_resalt=mysqli_fetch_assoc($supp_pay_qury);
?>
	
<table class="table-sm table-bordered mb-0 print-table">
  <tbody>
    <tr>
      <th colspan="2">Sale's Summery</th>
      </tr>
    <tr>
      <td>Cash Amount</td>
      <td><?php echo number_format((float)$sale_resalt['cash_amount'],2); ?></td>
    </tr>
    <tr>
      <td>Card Amount</td>
      <td><?php echo number_format((float)$sale_resalt['card_amount'],2); ?></td>
    </tr>
    <tr>
      <td>Discount Issue</td>
      <td><?php echo number_format((float)$sale_resalt['dis_amount'],2); ?></td>
    </tr>
    <tr>
      <td>Total Sale's</td>
      <td><?php echo number_format((float)$total_sale_resalt['amount']-$sale_resalt['dis_amount'],2); ?></td>
    </tr>
    <tr>
      <td>Total Cost</td>
      <td><?php echo number_format((float)$total_sale_resalt['cost'],2); ?></td>
    </tr>
    <tr style="background-color: rgba(0,128,0,0.2);">
      <td>Total Income</td>
      <td><?php echo number_format((float)$sale_resalt['cash_amount']+$sale_resalt['card_amount'],2); ?></td>
    </tr>
    <tr style="background-color: rgba(255,0,0,0.2);">
      <td>Due Amount</td>
      <td><?php echo number_format((float)($total_sale_resalt['amount']-$sale_resalt['dis_amount']-($sale_resalt['cash_amount']+$sale_resalt['card_amount'])),2); ?></td>
    </tr>
    <tr style="background-color: rgba(255,165,0,0.2);">
      <td>Total Profit</td>
      <td><?php echo number_format((float)($sale_resalt['cash_amount']+$sale_resalt['card_amount'])-($total_sale_resalt['cost']),2); ?></td>
    </tr>
    <tr>
      <th colspan="2">&nbsp;</th>
    </tr>
    <tr>
      <th colspan="2">Expenses</th>
    </tr>
<?php
$t_exp=0;
$ex_qury=mysqli_query($connation,"SELECT edc.expenses_text,SUM(ed.expenses_details_amount) AS amount
FROM expenses_details ed LEFT JOIN expenses_category edc ON ed.expenses_details_category=edc.expenses_id
WHERE ed.expenses_details_date BETWEEN '$_POST[st_date]' AND '$_POST[en_date]'
GROUP BY edc.expenses_text
ORDER BY edc.expenses_text ASC");
while($ex_resalt=mysqli_fetch_assoc($ex_qury)){
$t_exp+=$ex_resalt['amount'];
?>
    <tr style="text-transform: capitalize;">
      <td><?php echo $ex_resalt['expenses_text']; ?></td>
      <td><?php echo number_format((float)$ex_resalt['amount'],2); ?></td>
    </tr>
<?php } ?>
    <tr>
      <td>Amount</td>
      <td><?php echo number_format((float)$t_exp,2); ?></td>
    </tr>
    <tr>
      <th colspan="2">&nbsp;</th>
    </tr>
    <tr>
      <th colspan="2">Purchasing</th>
    </tr>
    <tr>
      <td>Invoice Payment</td>
      <td><?php echo number_format((float)$supp_pay_resalt['amount'],2); ?></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <th colspan="2">Summery</th>
    </tr>
    <tr>
      <td>Income</td>
      <td><?php echo number_format((float)$sale_resalt['cash_amount']+$sale_resalt['card_amount'],2); ?></td>
    </tr>
    <tr>
      <td>Cost</td>
      <td>(<?php echo number_format((float)$total_sale_resalt['cost'],2); ?>)</td>
    </tr>
    <tr>
      <td>Expenses</td>
      <td>(<?php echo number_format((float)$t_exp,2); ?>)</td>
    </tr>
    <tr>
      <td>Purchasing Payment</td>
      <td>(<?php echo number_format((float)$supp_pay_resalt['amount'],2); ?>)</td>
    </tr>
    <tr style="font-weight: bold;">
      <td>Net Profit</td>
	<?php $n_profit=($sale_resalt['cash_amount']+$sale_resalt['card_amount']-($total_sale_resalt['cost']+$t_exp+$supp_pay_resalt['amount'])); ?>
      <td><?php echo number_format((float)$n_profit,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); ?>