<?php 
require("php_con.php");
require("function.php");

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bill Print</title>
<link href="cus_css.css" rel="stylesheet" type="text/css">
	
<style>
	*{
		font-size: 14px;
	}
</style>
<link href="data_table/css/bootstrap.css" rel="stylesheet" type="text/css">
</head>

<body style="margin: 0px; padding: 0px;">
	
<form method="get" class="mb-4">
	<input name="print" type="text" required="required" class="form-control form-control-sm" id="print" placeholder="Bill Number" autocomplete="off">
</form>
	
<?php 
if(isset($_GET['print'])){}else{exit();}

$print=mysqli_real_escape_string($connation,$_GET['print']);

$payment_qury=mysqli_query($connation,"SELECT sd.sale_details_discount,sd.sale_details_cus_payment,CONCAT(ud.user_fname,' ',ud.user_lname) AS uname,sd.sale_details_cash_payment,sd.sale_details_card_payment,sd.sale_details_date,sd.sale_details_time,sd.sale_details_customer,cd.cs_name
FROM sale_details sd INNER JOIN user_details ud ON sd.sale_details_user=ud.user_id
LEFT JOIN customer_details cd ON sd.sale_details_customer=cd.cs_mobile
WHERE sale_details_billid='$print'");
$payment_resalt=mysqli_fetch_array($payment_qury);	
?>
	
<table style="margin-bottom: 5px; width: 100%;">
<tbody>
<tr><td align="left" valign="middle" style="padding-right: 5px;"><?php if(setting_view("bill_lan")==1){echo 'දිනය';}else{echo 'Date';} ?></td><td align="left" valign="middle"><?php echo date("M d, Y",strtotime($payment_resalt['sale_details_date'])); ?></td>
<td align="left" valign="middle" style="padding-right: 5px;"><?php if(setting_view("bill_lan")==1){echo 'වේලාව';}else{echo 'Time';} ?></td><td align="left" valign="middle"><?php echo date("h:i:s A",strtotime($payment_resalt['sale_details_time'])); ?></td></tr>
<tr><td align="left" valign="middle" style="padding-right: 5px;"><?php if(setting_view("bill_lan")==1){echo 'බිල්පත';}else{echo 'Invoice';} ?></td><td align="left" valign="middle"><?php echo $print; ?></td>
<td align="left" valign="middle" style="padding-right: 5px;"><?php if(setting_view("bill_lan")==1){echo 'අයකැමි';}else{echo 'Cashier';} ?></td><td align="left" valign="middle"><?php echo $payment_resalt['uname']; ?></td></tr>
	
<?php if(!$payment_resalt['sale_details_customer']=="0"){ ?><tr><td colspan="4" align="left" valign="middle" style="padding-right: 5px;"><?php if(setting_view("bill_lan")==1){echo 'පාරිභෝගික';}else{echo 'Customer';} ?>&nbsp;&nbsp;&nbsp;<?php echo $payment_resalt['cs_name']; ?></td></tr><?php } ?>
	
</tbody>
</table>
	
<table style="width: 100%;">
  <tbody>
	  
    <tr>
      <td colspan="4"><div style="border-bottom: 1px solid #000000;"></div></td>
    </tr>
    <tr style="font-weight: bold;" align="center">
      <td style="width: 25%;"><?php if(setting_view("bill_lan")==1){echo 'අයිතමය';}else{echo 'Item';} ?></td>
      <td style="width: 25%;"><?php if(setting_view("bill_lan")==1){echo 'අපේ මිල';}else{echo 'Our Price';} ?></td>
      <td style="width: 25%;"><?php if(setting_view("bill_lan")==1){echo 'ප්‍රමාණය';}else{echo 'QTY';} ?></td>
      <td style="width: 25%;"><?php if(setting_view("bill_lan")==1){echo 'වටිනාකම';}else{echo 'Amount';} ?></td>
    </tr>
	  
    <tr>
      <td colspan="4"><div style="border-bottom: 1px solid #000000;"></div></td>
    </tr>
	  
<?php
$discount=0;
$total_amount=0;
	  
$sold_item=0;
$sale_qury=mysqli_query($connation,"SELECT *
FROM sale_list sl LEFT JOIN product_details pd ON sl.sale_barcode=pd.product_barcode
LEFT JOIN unit_list ul ON pd.product_unit=ul.unit_id
WHERE sl.sale_billid='$print'");
while($sale_resalt=mysqli_fetch_assoc($sale_qury)){
$sold_item++;
	
$price_qury=mysqli_query($connation,"SELECT prd.*,pd.product_add_discount
FROM price_details prd INNER JOIN product_details pd ON prd.price_systemid=pd.product_systemid
WHERE prd.price_systemid='$sale_resalt[product_systemid]'
ORDER BY prd.price_id DESC
LIMIT 1");
$price_resalt=mysqli_fetch_assoc($price_qury);
	
$sale_price=$sale_resalt['sale_real_sale_price'];

$price=$sale_resalt['sale_saleprice'];
	
$discount+=($sale_price-$price)*$sale_resalt['sale_qty'];
$total_amount+=$sale_price*$sale_resalt['sale_qty'];
?>
    <tr>
      <td colspan="4">
		<?php if($sale_resalt['product_name']==""){ echo $sale_resalt['sale_other_info']; }else{ echo $sale_resalt['product_name']." • ".$sale_resalt['product_name_sinhala']; } ?>
		<?php if((float)discount_cal($sale_resalt['sale_real_sale_price'],$sale_resalt['sale_saleprice'])==0){}else{ echo "(".number_format((float)discount_cal($sale_resalt['sale_real_sale_price'],$sale_resalt['sale_saleprice']),2,".",",")."%)"; } ?>
		</td>
    </tr>
	  
    <tr align="right">
      <td style="width: 25%;"><?php if($sale_price>$price){ echo number_format((float)$sale_price,2); } ?></td>
      <td style="width: 25%;"><?php echo number_format((float)$price,2); ?></td>
      <td style="width: 25%;"><?php echo (float)$sale_resalt['sale_qty']; ?><?php if(setting_view("unit_display")==1){ echo $sale_resalt['unit_text']; } ?></td>
      <td style="width: 25%;"><?php echo number_format((float)$price*$sale_resalt['sale_qty'],2); ?></td>
    </tr>
	  
    <tr>
      <td colspan="4"><div style="border-bottom: 1px dashed #000000;"></div></td>
    </tr>
<?php } ?>
	  
<?php 
$discount_qury=mysqli_query($connation,"SELECT sale_details_discount,sale_details_cus_payment FROM sale_details WHERE sale_details_billid='$print'");
$discount_resalt=mysqli_fetch_assoc($discount_qury);
	  
$bill_discount=$payment_resalt['sale_details_discount'];
?>
	  

    <tr>
      <td colspan="2" align="right"><?php if(setting_view("bill_lan")==1){echo 'දළ එකතුව';}else{echo 'Gross Total';} ?></td>
      <td colspan="2" align="right"><?php echo number_format((float)$total_amount,2); ?></td>
    </tr>
	<?php if($discount>0){ ?>
	<tr>
      <td colspan="2" align="right"><?php if(setting_view("bill_lan")==1){echo 'අයිතම වට්ටම්';}else{echo 'Item Discount';} ?></td>
      <td colspan="2" align="right">(<?php echo number_format((float)$discount,2); ?>)</td>
    </tr>
	  <?php } ?>
	<?php if($bill_discount>0){ ?>
    <tr>
      <td colspan="2" align="right"><?php if(setting_view("bill_lan")==1){echo 'බිල්පත් වට්ටම්';}else{echo 'Bill Discount';} ?></td>
      <td colspan="2" align="right">(<?php echo number_format((float)$bill_discount,2); ?>)</td>
    </tr>
	<?php } ?>
    <tr>
      <td colspan="2" align="right"><?php if(setting_view("bill_lan")==1){echo 'ගෙවූ මුදල';}else{echo 'Paid Amount';} ?></td>
      <td colspan="2" align="right"><?php echo number_format((float)$discount_resalt['sale_details_cus_payment'],2); ?></td>
    </tr>
    <tr style="font-weight: bold;">
      <td colspan="2" align="right"><?php if(setting_view("bill_lan")==1){echo 'මුළු වටිනාකම';}else{echo 'Invoice Amount';} ?></td>
      <td colspan="2" align="right" style="font-weight: bold;"><?php echo number_format((float)$total_amount-($bill_discount+$discount),2); ?></td>
    </tr>
    <tr>
      <td colspan="2" align="right"><?php if(setting_view("bill_lan")==1){echo 'ඉතිරිය';}else{echo 'Balance';} ?></td>
      <td colspan="2" align="right"><?php echo number_format((float)$discount_resalt['sale_details_cus_payment']-($total_amount-($bill_discount+$discount)),2); ?></td>
    </tr>
	  
  </tbody>
</table>
	
<div style="border-bottom: 1px solid #000000; margin-top: 5px; margin-bottom: 5px;"></div>
	
<div><?php if(setting_view("bill_lan")==1){echo 'විකුණූ අයිතම';}else{echo 'Sold Items';} ?>: <?php echo number_format((float)$sold_item); ?></div>
	
<?php if((float)$discount>0||(float)$bill_discount>0){ ?>
<div style="border: 1px dashed #000000; border-bottom: 5px solid #000000; margin: 5px 0px; text-align: center; font-weight: bold;">
<?php if(setting_view("bill_lan")==1){echo 'ඔබට හිමි ලාභය';}else{echo 'Your Saving';} ?><br>
<?php echo number_format((float)$discount+$bill_discount,2); ?>
</div>
<?php } ?>
	
<?php 
if(!$payment_resalt['sale_details_customer']=="0"){
?>
<div style="border: 1px dashed #000000; margin: 5px 0px; text-align: center;>
<?php if(setting_view("bill_lan")==1){ if(cus_balance($payment_resalt['sale_details_customer'])<0){echo 'ගෙවීමට ඇති හිඟ මුදල';}else{echo 'පවතින ශේෂය';} } else{ if(cus_balance($payment_resalt['sale_details_customer'])<0){echo 'Amount Due';}else{echo 'Balance';} } ?><br>
<?php echo number_format((float)cus_balance($payment_resalt['sale_details_customer']),2);
echo "</div>";
}
?>

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