<?php
$PageSecurity = 5;
include('includes/session.inc');
?>
<style>
.bod {
	border: 1px double #666666;
	}
</style>
<?php
$title = _('Comparative Vendor Quotation');
include('includes/header.inc');
include('includes/SQL_CommonFunctions.inc');

    $id = $_REQUEST['id'];
    $RequisitionNo = $_REQUEST['RequisitionNo'];
	$lsqlvend="SELECT * FROM venderquotations WHERE pr_no='".$RequisitionNo."'";
	$lresultvand=DB_query($lsqlvend,$db);
	$lvendorid=DB_fetch_array($lresultvand);
	$vander_code = $lvendorid['vendercode'];
	$vander_id = $lvendorid['id'];
			
	 $sql='select comprative_id, comprative_no from comprative_detail order by comprative_id desc limit 0,1';
	 $result = DB_query($sql,$db);
	  $numrows = DB_num_rows($result);
	 if($numrows>0)
	 {
			 $i=0;
			 $strmax = "";
			 $myrow=DB_Fetch_array($result);
			 
			 if($myrow['comprative_no'] !='')
			 {
			         $val= substr($myrow['comprative_no'],19);
					   $strChar = substr($myrow['comprative_no'],0,19);
					
				
					 if($val!="")
					 {
						 $arr[$i] = $val;
						 $i++;
					}
			
			if(is_array($arr))
			{ 
				$maxval =  max($arr);
			}
			else
			{
				$maxval=0;
			}
		}
		else
		{
			$strChar = $Prefix_code."/".$_SESSION[UserStockLocation]."/".$CurrentFinancialYear."/";
		}
	  }
	
	$strFinal = $maxval+1;
	$strBothFinal = $strChar.str_pad($strFinal,4,"0",STR_PAD_LEFT);
	
	$REQ_SQL="Select * from materialrequisitions where requisitionno='".$RequisitionNo."'";
	$REQ_Result=DB_query($REQ_SQL,$db);
	$REQ_Result_Row=DB_fetch_array($REQ_Result);
	$month_year = $REQ_Result_Row['month_year'];
	
	if(isset($_REQUEST['id'])!="" && isset($_REQUEST['mod'])!="" && isset($_REQUEST['mod']) =="Edit")
	{
	   $SQL_Query = "select * from comprative_detail 
	   Inner join venderquotations ON (venderquotations.vendercode=comprative_detail.vendor_id) 
	   inner join suppliers ON (suppliers.supplierid=comprative_detail.vendor_id) 
	   where comprative_id ='".$id."'";
	   $Result = DB_query($SQL_Query,$db);
	   $com_row = DB_fetch_array($Result);
	   $comprative_no = $com_row['comprative_no'];
	}
 ?>
 <form action="PO_Header.php?" method="post" name="finalqt">
<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0">
  <tr>
             <td height="20px"><a href="index.php">Purchases  &gt;&gt;</a> <a href="Comperitive_listing.php">Comparative Listing</a> </td>
           </tr>
   <tr>
     <td valign="top" align="center"><table width="95%" border="0" cellspacing="0" cellpadding="0">
       <tr>
         <td><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="bod">
          
           <tr>
             <td colspan="2">&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
           </tr>
           <tr bgcolor="#666666">
             <td colspan="2">&nbsp;</td>
             <td height="30">&nbsp;</td>
             <td><font color="#FFFFFF">Comparative No.</font></td>
             <td height="22"><input type="text" name="comprativeno" size="30" readonly=""  value="<?php
	   if($comprative_no =="")
	   {   
	    	echo $strBothFinal;
		}
		else
		{
		 	echo $comprative_no;
		}
		
		 ?>"/></td>
             <td height="22"><font color="#FFFFFF">Date</font></td>
             <td><font color="#FFFFFF"><?php echo date("Y/m/d");?></font></td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
           </tr>
           <tr>
             <td colspan="2">&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
           </tr>
           <tr bgcolor="#666666">
             <td colspan="2"><font color="#FFFFFF">Site Name</font></td>
             <td><font color="#FFFFFF"><?php echo $lvendorid['site']; ?>
                   <input type="hidden" name="sitecode" id="sitecode" value="<?php echo $lvendorid['sitecode']; ?>" />
                   <input type="hidden" name="sitename" id="sitename" value="<?php echo $lvendorid['site']; ?>" />
             </font></td>
             <td height="30">&nbsp;</td>
             <td colspan="2"><font color="#FFFFFF">Requisition No.</font></td>
             <td><font color="#FFFFFF"><?php echo $RequisitionNo; ?>
                   <input type="hidden" name="reqno" id="reqno"  value="<?php echo $RequisitionNo; ?>" />
             </font> </td>
             <td colspan="2"><font color="#FFFFFF">Requisition Date </font></td>
             <td><font color="#FFFFFF"><?php echo ConvertSQLDate($month_year); ?></font></td>
             <td>&nbsp;</td>
           </tr>
           <tr>
             <td colspan="2">&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
           </tr>
           <?php 
	 $suppsqlvend="SELECT venderquotations.id, 
	  venderquotations.sitecode,
	  venderquotations.date1, 
	  suppliers.suppname, 
	  suppliers.supplierid, 
	  venderquotations.payment_term,
	  venderquotations.payment_mode, 
	  venderquotations.event, 
	  venderquotations.vendercode, 
	  venderquotations.vendername,
	  venderquotations.quotationcategory,
	   venderquotations.total_amt, 
	  venderquotations.amtwithtax, 
	  venderquotations.totalcharge, 
	  venderquotations.grand_total, 
	  venderquotations.basis 
	   FROM  venderquotations
	  INNER JOIN suppliers ON suppliers.supplierid=venderquotations.vendercode 
	  INNER JOIN vendorquotationdetails ON vendorquotationdetails.quotation_no=venderquotations.id 
	  WHERE venderquotations.pr_no=".$RequisitionNo." group by id";	
	  $suppresultvand=DB_query($suppsqlvend,$db);
	while($suppvendorid=DB_fetch_array($suppresultvand))
	{
	$payment_Sql = "SELECT termsindicator, terms  FROM paymentterms Where termsindicator='".$suppvendorid['payment_term']."'";
	$payment_result = DB_query($payment_Sql,$db);
	$payment_myrow=DB_fetch_array($payment_result);
	$paymentother_Sql = "Select paymentid, paymentname, paymenttype, receipttype from paymentmethods Where paymentid='".$suppvendorid['payment_term']."'";
	$paymentother_result = DB_query($paymentother_Sql,$db);
	$paymentother_myrow=DB_fetch_array($paymentother_result);
	$event_Sql = "select * from events Where event_id='".$suppvendorid['event']."'";
	$event_result = DB_query($event_Sql,$db);
	$event_myrow=DB_fetch_array($event_result);
   ?>
           <tr bgcolor="#666666">
             <td width="1%">&nbsp;</td>
             <td width="10%"><font color="#FFFFFF">Vendor Quot. No</font></td>
             <td width="11%" height="30"><font color="#FFFFFF">Quot. Entry No</font></td>
             <td width="10%"><font color="#FFFFFF">Quot. Date</font></td>
             <td width="15%" height="30"><font color="#FFFFFF">Vendor ID</font></td>
             <td width="8%"><font color="#FFFFFF">Vendor Name </font></td>
             <td width="11%"><font color="#FFFFFF">Quot. Catagory </font></td>
             <td width="8%"><font color="#FFFFFF">Basis</font></td>
             <td width="9%"><font color="#FFFFFF">Payment Tearm</font></td>
             <td width="9%"><font color="#FFFFFF">Payment Mode</font></td>
             <td width="8%"><font color="#FFFFFF">Event</font></td>
           </tr>
           <tr>
             <td><div align="center">
                 <input name="final_Quot" id="final_Quot[]" type="radio" value="<?php echo $suppvendorid['id']; ?>" onclick="FinalQuotation(this.value)"/>
             </div></td>
             <td><?php echo $suppvendorid['id']; ?>
                 <input type="hidden" name="Vendorqtnid" id="Vendorqtnid" value="<?php echo $suppvendorid['id']; ?>" /></td>
             <td><div align="center">
               <?php  ?>
             </div></td>
             <td><div align="center"><?php echo ConvertSQLDate($suppvendorid['date1']); ?></div></td>
             <td><div align="center"><?php echo $suppvendorid['supplierid']; ?>
                     <input type="hidden" name="vendername" id="vendername" value="<?php echo $suppvendorid['suppname']; ?>" />
                     <input type="hidden" name="vendercode" id="vendercode" value="<?php echo $suppvendorid['supplierid']; ?>">
             </div></td>
             <td><div align="center"><?php echo $suppvendorid['vendername']; ?></div></td>
             <td><div align="center"><?php echo $suppvendorid['quotationcategory']; ?></div></td>
             <td><div align="center"><?php echo $suppvendorid['basis']; ?></div></td>
             <td><div align="center"><?php echo $payment_myrow['terms']; ?></div></td>
             <td><div align="center"><?php echo $paymentother_myrow['paymentname']; ?></div></td>
             <td><div align="center"><?php echo $event_myrow['event_desc'];?></div></td>
           </tr>
           <tr>
             <td colspan="2">&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
             <td>&nbsp;</td>
           </tr>
           <tr>
             <td colspan="11" valign="top" align="center"><table width="100%" border="1" cellspacing="0" cellpadding="0">
                 <tr>
                   <td width="98%"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                       <tr bgcolor="#666666">
                         <td width="15%" height="30"><font color="#FFFFFF">Product Code</font></td>
                         <td width="14%"><font color="#FFFFFF">Product Name</font></td>
                         <td width="14%"><font color="#FFFFFF">Quantity</font></td>
                         <td width="12%"><font color="#FFFFFF">Rate</font></td>
                         <td width="15%"><font color="#FFFFFF">Discount</font></td>
                         <td width="10%"><font color="#FFFFFF">Sub Total </font></td>
                         <td colspan="2"><font color="#FFFFFF">Lead Time</font></td>
                       </tr>
                       <?php 
		 			 $sql='SELECT venderquotations.id, 
		                  venderquotations.sitecode,
						  venderquotations.site, 
						  venderquotations.quotationentryno, 
						  venderquotations.date1, 
						  venderquotations.payment_term,
						  venderquotations.payment_mode, 
						  venderquotations.event, 
						  venderquotations.total_amt, 
						  venderquotations.amtwithtax, 
						  venderquotations.totalcharge, 
						  venderquotations.grand_total, 
						  suppliers.suppname, 
						  suppliers.supplierid, 
						  venderquotations.vendercode, 
						  venderquotations.quotationcategory, 
						  venderquotations.basis, 
						  venderquotations.remarks,
						  vendorquotationdetails.quotation_id, 
						  vendorquotationdetails. prod_id, 
						  vendorquotationdetails. sub_total, 
						  vendorquotationdetails.prod_dec, 
						  vendorquotationdetails.quantity, 
						  vendorquotationdetails.rate, 
						  vendorquotationdetails.discount,
						  venderquotations.delivery_by, 
						  venderquotations.pr_no FROM  venderquotations
						  INNER JOIN suppliers ON suppliers.supplierid=venderquotations.vendercode 
						  INNER JOIN vendorquotationdetails ON vendorquotationdetails.quotation_no=venderquotations.id 
						  WHERE venderquotations.pr_no='.$RequisitionNo. ' and id='.$suppvendorid['id']; 
			  $result=DB_query($sql,$db);
			  $j = 1;
			  $k=0; //row colour counter
			 while($myrow=DB_fetch_array($result))
			 {	
			 
				if ($k==1){ /*alternate bgcolour of row for highlighting */
					echo '<tr class="OddTableRows">';
					$k=0;
				} else {
					echo '<tr class="EvenTableRows">';
					$k++;
				}
			
       ?>
                       <tr>
                         <td><?php echo $myrow['prod_id']; ?></td>
                         <td><?php echo $myrow['prod_dec']; ?>
                             <input type="hidden" name="prod_id"  value="<?php echo $myrow['prod_id']; ?>" /></td>
                         <td><?php echo $myrow['quantity']; ?></td>
                         <td><?php echo $myrow['rate']; ?></td>
                         <td><?php echo $myrow['discount']; ?></td>
                         <td><?php echo $myrow['sub_total']; ?></td>
                         <td colspan="2"><?php echo $myrow['lead_time']; ?></td>
                       </tr>
                       <?php }  ?>
                       <tr>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td colspan="2">&nbsp;</td>
                       </tr>
                       <tr>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td><font color="#666666"><b>Total Amount</b></font></td>
                         <td width="9%"><div align="right"><?php echo $suppvendorid['total_amt']; ?></div></td>
                         <td width="11%">&nbsp;</td>
                       </tr>
                       <tr>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td><font color="#666666"><b>Total Tax</b></font></td>
                         <td><div align="right">
                           <?php
		   $Tax_SQL = "select Sum(Tax_percentage) from  quotationtaxwithcost Where requisition_no='".$RequisitionNo."' AND quatition_id
='".$suppvendorid['id']."'";
			  $Tax_result = DB_query($Tax_SQL,$db);
			  $Tax_myrow=DB_fetch_array($Tax_result);
		     echo $Tax_myrow['Sum(Tax_percentage)']; ?>
                         </div></td>
                         <td>&nbsp;</td>
                       </tr>
                       <tr>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td><font color="#666666"><b>Total Charge</b></font></td>
                         <td><div align="right"><?php echo $suppvendorid['totalcharge']; ?></div></td>
                         <td>&nbsp;</td>
                       </tr>
                       <tr>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td><font color="#666666"><b>Grand Total</b></font></td>
                         <td><div align="right"><?php echo $suppvendorid['grand_total']; ?></div></td>
                         <td>&nbsp;</td>
                       </tr>
                       <tr>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td>&nbsp;</td>
                         <td colspan="2">&nbsp;</td>
                       </tr>
                   </table></td>
                 </tr>
             </table></td>
           </tr>
           <tr>
             <td colspan="2">&nbsp;</td>
             <td colspan="2">&nbsp;</td>
             <td colspan="2">&nbsp;</td>
             <td>&nbsp;</td>
             <td colspan="4">&nbsp;</td>
           </tr>
           <?php } ?>
           <tr>
             <td colspan="11"><div id="FinalQuot"></div></td>
           </tr>
           <tr>
             <td colspan="11">&nbsp;</td>
           </tr>
         </table></td>
       </tr>
     </table></td>
   </tr>
</table>
</form>
<script type="text/javascript">
function FinalQuotation(str)
	{ 
	  xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null)
			  {
				alert ("Your browser does not support AJAX!");
				return;
			  } 
		var url="FinalQuot.php";
		url=url+"?FinalQuot="+str;
		//alert(url);
		xmlHttp.onreadystatechange=NumberChanged1;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	
  function NumberChanged1() 
	{ 
		if (xmlHttp.readyState==4)
		{ 
		   document.getElementById("FinalQuot").innerHTML=xmlHttp.responseText;
		}
	}    
  function GetXmlHttpObject()
  {
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
  }  
</script>   