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

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

$q_qury=mysqli_query($connation,"SELECT * FROM quotation_details WHERE qd_bill_id='$qd_bill_id'");
$q_resalt=mysqli_fetch_assoc($q_qury);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bill Print - <?php echo $qd_bill_id; ?></title>
<link href="cus_css.css" rel="stylesheet" type="text/css">
	
<style>
	*{
		font-size: <?php echo setting_view("bill_font_size"); ?>px;
	}
</style>
</head>

<body style="margin: 0px; padding: 0px;">
	
<?php if(setting_view("bill_logo_display")==1){ ?><div align="center"><img src="image/logo.png" style="width: <?php echo setting_view("logo_width"); ?>%; margin-bottom: 5px;"></div><?php } ?>	
<?php if(setting_view("bill_title_display")==1){ ?><div align="center" style="font-weight: bold;"><?php echo setting_view("bill_title"); ?></div><?php } ?>
<?php if(setting_view("bill_address_display")==1){ ?><div align="center" style="white-space: pre-line;"><?php echo setting_view("bill_address"); ?></div><?php } ?>
<?php if(setting_view("bill_mobile_display")==1){ ?><div align="center" style="margin-bottom: 5px;"><?php echo setting_view("bill_mobile"); ?></div><?php } ?>
	
<div align="center" style="font-weight: bold; font-size: <?php echo setting_view("bill_font_size")+2; ?>px;"><?php if(setting_view("bill_lan")==1){echo 'ඇස්තමේන්තුව';}else{echo 'Quotation';} ?></div>
	
<div style="border-bottom: 1px solid #000000;"></div>
	
<table style="margin-bottom: 5px;">
<tbody>
<tr><td align="left" valign="middle" style="padding-right: 5px; font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'දිනය';}else{echo 'Date';} ?></td><td align="left" valign="middle" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php echo date("M d, Y",strtotime($q_resalt['qd_c_date'])); ?></td></tr>
<tr><td align="left" valign="middle" style="padding-right: 5px; font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'වේලාව';}else{echo 'Time';} ?></td><td align="left" valign="middle" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php echo date("h:i:s A",strtotime($q_resalt['qd_c_time'])); ?></td></tr>	
<tr><td align="left" valign="middle" style="padding-right: 5px; font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'පාරිභෝගික';}else{echo 'Customer';} ?></td><td align="left" valign="middle" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php echo $q_resalt['quotation_cus_name']; ?></td></tr>	
<tr><td align="left" valign="middle" style="padding-right: 5px; font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'දුරකථන';}else{echo 'Mobile';} ?></td><td align="left" valign="middle" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php echo $q_resalt['quotation_mobile']; ?></td></tr>
<?php if(!$q_resalt['quotation_remark']==""){ ?><tr><td align="left" valign="middle" style="padding-right: 5px; font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'සටහන්';}else{echo 'Remark';} ?></td><td align="left" valign="middle" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php echo $q_resalt['quotation_remark']; ?></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="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'සාමාන්‍ය මිල';}else{echo 'Market Price';} ?></td>
      <td style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'අපේ මිල';}else{echo 'Our Price';} ?></td>
      <td style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'ප්‍රමාණය';}else{echo 'QTY';} ?></td>
      <td style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?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;
	  
$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='$qd_bill_id'");
while($sale_resalt=mysqli_fetch_assoc($sale_qury)){
	
$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" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;">
		<?php if(setting_view("bill_lan")==1){ if($sale_resalt['product_name_sinhala']==""){ echo $sale_resalt['sale_other_info']; }else{ echo $sale_resalt['product_name_sinhala']; } }else{ if($sale_resalt['product_name']==""){ echo $sale_resalt['sale_other_info']; }else{ echo $sale_resalt['product_name'];} } ?>
		<?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>
      <td style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if($sale_price>$price){ echo number_format((float)$sale_price,2); } ?></td>
      <td style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php echo number_format((float)$price,2); ?></td>
      <td style="font-size: <?php echo setting_view("bill_font_size"); ?>px;" align="center"><?php echo (float)$sale_resalt['sale_qty']; ?><?php if(setting_view("unit_display")==1){ echo $sale_resalt['unit_text']; } ?></td>
      <td style="font-size: <?php echo setting_view("bill_font_size"); ?>px;" align="right"><?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 

$bill_discount=0;
?>
	  

    <tr>
      <td colspan="3" align="right" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'වටිනාකම';}else{echo 'Sub Amount';} ?></td>
      <td align="right" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php echo number_format((float)$total_amount,2); ?></td>
    </tr>
	<?php if($discount>0){ ?>
	<tr>
      <td colspan="3" align="right" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'ඔබට හිමි ලාභය';}else{echo 'Profit';} ?></td>
      <td align="right" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;">(<?php echo number_format((float)$discount,2); ?>)</td>
    </tr>
	  <?php } ?>
	<?php if($bill_discount>0){ ?>
    <tr>
      <td colspan="3" align="right" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;"><?php if(setting_view("bill_lan")==1){echo 'මුළු බිල් වට්ටම්';}else{echo 'Total Bill Discount';} ?></td>
      <td align="right" style="font-size: <?php echo setting_view("bill_font_size"); ?>px;">(<?php echo number_format((float)$bill_discount,2); ?>)</td>
    </tr>
	<?php } ?>
    <tr style="font-weight: bold; font-size: <?php echo setting_view("bill_font_size"); ?>px;">
      <td colspan="3" align="right" style="font-size: <?php echo setting_view("bill_font_size")+4; ?>px;"><?php if(setting_view("bill_lan")==1){echo 'මුළු වටිනාකම';}else{echo 'Total Amount';} ?></td>
      <td align="right" style="font-size: 20px; font-weight: bold; font-size: <?php echo setting_view("bill_font_size")+4; ?>px;"><?php echo number_format((float)$total_amount-($bill_discount+$discount),2); ?></td>
    </tr>
	  
  </tbody>
</table>

	
<div style="border-bottom: 1px solid #000000; margin-top: 5px;"></div>
	
<div class="bill-footer" style="font-size: <?php echo setting_view("bill_font_size")-1; ?>px;">
<?php /*?><p align="center"><img src="barcode.php?text=<?php echo $print; ?>" style="width: 100%;"></p>	<?php */?>
<?php echo setting_view("bill_footer_text"); ?><br>System powerd by NOVA POS<br>0769146421 / 0777404233
</div>

	
<script>
this.print();
</script>
</body>
</html>
<?php @mysqli_close($connation); ?>