<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quotation</title>
<style type="text/css">
<!--
.style1 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 16px;
	font-weight: bold;
	text-decoration:underline;
}
.style2 {
	font-family: Arial;
	font-size: 15px;
}
.style4 {
	font-size: 22px;
	font-weight: bold;
}
-->
</style>
</head>

<body><center>
<?php 
require_once("config.inc.php");
	require_once("DBConnector.php");
	$db = new DBConnector();
	?>
	<?php
    	$sql_s_quomas="SELECT * from s_quomas where REF_NO= '" . trim($_GET["invno"]) . "'  order by id";
			$result_s_quomas =$db->RunQuery($sql_s_quomas);
			$row_s_quomas = mysql_fetch_array($result_s_quomas);
   ?>
    
    <?php
    
			$sql_invpara="SELECT * from invpara";
			$result_invpara =$db->RunQuery($sql_invpara);
			$row_invpara = mysql_fetch_array($result_invpara);
			
				$sql_ven="SELECT * from vendor where CODE='".$row_s_quomas['C_CODE']."'";
			$result_ven =$db->RunQuery($sql_ven);
			$row_ven = mysql_fetch_array($result_ven);
			
			echo $_GET["CURRENT_USER"];

		   $txtrepono = $_GET["CURRENT_USER"] . " " . date("Y-m-d") . "  " . date("H:i:s");

$adress=$_GET["invdate"];
$cusname=$_GET["cus_name"];
	
	
	
	$vatRate = (float)$row_s_quomas["GST"];
	$txtvat = "VAT " . $vatRate . "%: ";
	$txtvata = 0;
	if (isset($_GET["tax"])) {
		$txtvata = (float)str_replace(',', '', $_GET["tax"]);
	}
                                
	?>
<table width="922"  border="0" >
<tr>
    <?php 
    $x=substr($_GET["invno"],0,1); 
    if($x=="B"){
        ?>
        
    <img src='images/AMT-01.jpg' style='width:922px;height:120px;'>
    <?php  }else{?>
    <img src='images/aas_Auto_Spare.png' style='width:922px;height:120px;'>
    <?php  }?>
    
 
  </tr>

   
  <tr>
       <td colspan="6"></td>
     <td colspan="2" align="center"></td>
   
  </tr>
  <tr>
       <td colspan="6"></td>
     <td colspan="2" align="center"></td>
   
  </tr>
  <tr>
       <td colspan="6"></td>
     <td colspan="2" align="center"></td>
   
  </tr>
  <tr>
       <td colspan="6"></td>
     <td colspan="2" align="center"><b>Date : &nbsp
      <?php echo $_GET["invdate"]; ?></b></td>
   
  </tr>
    <tr style="margin-top: 200px;">
        <td colspan="3"><b>To :&nbsp  <?php echo $row_ven["NAME"]; ?></b></td>
     <td colspan="3"></td>
      <td colspan="3"  align="center"><b>Qu No:&nbsp<?php echo $_GET["invno"]; ?></b></td>
   
  </tr>
  <tr style="margin-top: 200px;">
        <td colspan="3"><b>Remark :&nbsp  <?php echo $row_s_quomas["REMARK"]; ?></b></td>
     <td colspan="3"></td> 
   
  </tr>

  
  
  <tr>
     
    <td height="64" colspan="7"><table border="0" cellpadding="0" cellspacing="0"><tr><td>
        
        
      <tr  bgcolor="#999999"> 
          <td width="260"><span class="style1">Part No</span></td>
        <td width="140" align="left"><span class="style1">Brand</span></td>
        <td width="400"><span class="style1">Description</span></td>
        <td width="100" align="right"><span class="style1">Qty</span></td>
        <td width="170" align="right"><span class="style1">PRICE</span></td>
        <td width="90" align="right"><span class="style1">Dis (%)</span></td>
        <td width="130" align="right"><span class="style1">Sub Total</span></td>
        </tr>
        </table>
       <table width="900" height="81" border="0" cellspacing="0"  height=200px;>
      <?php 
      
    
	  	$sql_rsPrInv="SELECT * from s_quotrn where REF_NO= '" . trim($_GET["invno"]) . "'  order by id";
		
    // Totals
    $grossTotal = 0;
    $discountTotal = 0;
    $netTotal = 0;
    	$result_rsPrInv =$db->RunQuery($sql_rsPrInv);	
		while ($row_rsPrInv = mysql_fetch_array($result_rsPrInv)){
		    
		    $sql_1="SELECT * from s_mas where STK_NO='".$row_rsPrInv["STK_NO"]."' ";
		     
			$result_1 =$db->RunQuery($sql_1);
			$row_1 = mysql_fetch_array($result_1);
                        
                        $part ="";
                        $description = "";
                        $description1 = "";
			if($row_1["PART_NO"]=="PISTON RING SET"){
                           $description = $row_rsPrInv["DESCRIPT"];
			} else{
			    $part =$row_1["PART_NO"];
                            $description = $row_1["DESCRIPT"];
			}
                        $description1 =  str_pad($description, 40, "?");
                        $description1 = str_replace("?", "&nbsp;", $description1);
                        
                        $partNo1 =  str_pad($part, 15, "?");
                        $partNo1 = str_replace("?", "&nbsp;", $partNo1);

                        $brandName = $row_1["BRAND_NAME"];
                        $brandName1 =  str_pad($brandName, 22, "?");
                        $brandName1 = str_replace("?", "&nbsp;", $brandName1);
			
      echo "<tr height=\"30\"> 
			<td><span class=\"style2\">".$partNo1.''."</span></td>
			 <td align=\"left\"><span class=\"style2\">".$brandName1."</span></td>
			 <td><span class=\" style2\">".$description1."</span></td> 
			<td  align=\"right\"><span class=\"style2\">".number_format($row_rsPrInv["QTY"], 2, ".", ",")."</span></td>
			<td align=\"right\"><span class=\"style2\">".number_format($row_rsPrInv["PRICE"], 2, ".", ",")."</span></td>
			<td align=\"right\"><span class=\"style2\">".number_format($row_rsPrInv["DIS_per"], 2, ".", ",")."</span></td>";
			
      $lineGross = (float)$row_rsPrInv["PRICE"] * (float)$row_rsPrInv["QTY"];
      $lineDiscount = $lineGross * (float)$row_rsPrInv["DIS_per"] / 100;
      $lineNet = $lineGross - $lineDiscount;
      echo "<td align=\"right\"><span class=\"style2\">".number_format($lineNet, 2, ".", ",")."</span></td></tr>";
			
      $grossTotal += $lineGross;
      $discountTotal += $lineDiscount;
      $netTotal += $lineNet;
		
		}
		
    $tax = 0;
		
		
// 		$final_total=0;
// 		if ((trim($_GET["tax"])!="") and ($_GET["tax"]>0) and ($_GET["tax"]!="0.00")){
// 			$tax=$totdis*$row_invpara["vatrate"]/100;
// 			echo "<tr><td colspan=5></td><td align=right><b>".number_format($totdis, 2, ".", ",")."</b></td></tr>";
// 			echo "<tr><td colspan=4></td><td>VAT ".$row_invpara["vatrate"]."%</td><td align=right><b>".number_format($tax, 2, ".", ",")."</b></td></tr>";
// 			$tot=$totdis+$tax;
// 			$final_total=$tot;
// 			echo "<tr><td colspan=5></td><td align=right><b><u>".number_format($tot, 2, ".", ",")."</u></b></td></tr>";
// 		}	else {
// 			echo "<tr><td colspan=5></td><td align=right><b><u>".number_format($totdis, 2, ".", ",")."</u></b></td></tr>";
// 			$final_total=$totdis;
// 		}
		
		
      $vatRate = (float)$row_s_quomas["GST"];
      $txtvat = "VAT " . $vatRate . "%: ";
      $txtvata = 0;
      $reqTax = 0;
      if (isset($_GET["tax"])) {
        $reqTax = (float)str_replace(',', '', $_GET["tax"]);
      }
      if ($reqTax > 0) {
        // If caller already calculated VAT/tax, use it.
        $txtvata = $reqTax;
      } else if ($vatRate > 0) {
        // Otherwise, assume line totals are VAT-inclusive (existing behavior).
        $txtvata = $netTotal / ($vatRate + 100) * $vatRate;
      }
		  
	  // User requirement: footer Sub Total must equal the sum of the "Sub Total" column.
      $subTotal = $netTotal;
      $grandTotal = $netTotal;
		
		
	   ?>
	    
	   </tr>
</td></tr></table></td>
    </table>
    <footer>
    <table style="table-layout: fixed;"  width="922"  valign="bottom">
    
  </tr>
  <td >
   <tr>
                    <td colspan="5" align="center"></td>
                    <td colspan="2" align="right" ><b>Sub Total</b></td>
                    <td colspan="1" style="padding-left:35px;"><u><?php echo number_format($subTotal, 2, ".", ",") ?></u></td>
    </tr>
    <tr>
                    <td colspan="5" align="center"></td>
                    <td colspan="2" align="right" ><b>Discount</b></td>
                    <td colspan="1" style="padding-left:73px;"><?php echo number_format($discountTotal, 2, ".", ",") ?></td>
    </tr>
    
     
                                <?php  if ($txtvata >0) { ?>
     <tr>
                    <td colspan="5" align="center"></td>
                    <td colspan="2" align="right" ><b><?php echo $txtvat?></b></td>
                    <td colspan="1" style="padding-left:35px;"><u><?php echo number_format($txtvata, 2, ".", ",") ?></u></td>
    </tr>      
     <?php } ?>
                                
    <tr>
                    <td colspan="5" align="center"></td>
                    <td colspan="2" align="right" ><b>Grand Total</b></td>
                    <td colspan="1" style="padding-left:35px;"><u><?php echo number_format(($grandTotal+$txtvata)-$discountTotal, 2, ".", ",") ?></u></td>
    </tr>
    
    <tr>
                    <td colspan="1" align="left" ><b><u>Instructions </u></b></td>
                    <td colspan="5" align="left" ></td>
                    <td colspan="1" style="padding-left:100px;"></td>
    </tr>
    <tr>
                    <td colspan="5" align="left"><b>1) Credit Period &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp:&nbsp&nbsp<? echo $row_s_quomas["pay_type"]?></b></td>
                    <td colspan="1" align="left"  ></td>
                    <td colspan="1" ></td>
    </tr>
    <tr>
                    <td colspan="5" align="left"><b>2) Deliver Time &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp: &nbsp<? echo $row_s_quomas["delivery"]?></b></td>
                    <td colspan="1" align="right" ></td>
                    <td colspan="1" style="padding-left:100px;"></td>
    </tr><tr>
                    <td colspan="5" align="left"><b>3) Price Validity &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp:&nbsp&nbsp<? echo $row_s_quomas["validity"]?></b></td>
                    <td colspan="1" align="right" ></td>
                    <td colspan="1" style="padding-left:100px;"></td>
    </tr>
    <tr>
                    <td colspan="5" align="left"><b>4) Country of Origin :&nbsp&nbsp<? echo $row_s_quomas["Country"]?></b></td>
                    <td colspan="1" align="right" ></td>
                    <td colspan="1" style="padding-left:100px;"></td>
    </tr>
     <tr>
                    <td colspan="5" align="left"><b>5) Warrenty &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;<? echo $row_s_quomas["warranty"]?></b></td>
                    <td colspan="1" align="right" ></td>
                    <td colspan="1" style="padding-left:100px;"></td>
    </tr>
    
  <tr>
    <td height="94" colspan="2" valign="bottom" align="left">_______________________</td>
    <td colspan="2" valign="bottom" align="left">________________________</td>
    <td colspan="4" valign="bottom" align="center">Please fax or mail the signed price quote to</td>
  </tr>
  <tr>
    <td colspan="2"  align="left">Sales Person </td>
    <td colspan="2"  align="center">Marketing Manager</td>
    <td colspan="4"align="center">the address above customer Acceptance</td>
    
  </tr>
  <tr>
    <td height="50" colspan="2">&nbsp;</td>
    <td>&nbsp;</td>
    <td colspan="2">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  
  <tr>
    <td  colspan="8" align="center">If you have any questions about this price quote,please contact</td>
  </tr>
  <tr>
    <td  colspan="8" align="center"><i><b>Thank You For Your Business</b></i></td>
  </tr>
  </td>
    </table>
    </footer>
  
  






</body>
</html>
