<?php 
 $PageName ='Comprative' ;	
$Prefix_SQL = "Select prefix_id,prefix_code,prefix_name from prefix_tbl Where prefix_name ='".$PageName."'";
$Prefix_Result = DB_query($Prefix_SQL,$db);
$Prefix_Row = DB_fetch_array($Prefix_Result);
$Prefix_code = $Prefix_Row['prefix_code'];
$Prefix_date = date("y");
$Prefix_date++;
$CurrentFinancialYear = date("y")."-".$Prefix_date; 

if($_REQUEST['FinalQuot'])
{
$dataquotation_id=array();
$dataassignqty=array();
$dataRequisitionNo=array();
$dataVendor_Quot_No=array();
$dataitemid=array();
$datavenername=array();
$datavendorid=array();
	
	//**************************  Code Generate ******************************//
	   $sql="select * from comprative_detail where 1=1";	
	   $result = DB_query($sql,$db);
	   $numrows = DB_num_rows($result);
	 	 if($numrows>0)
	                 {
						
						 $strmax = "";
						 $myrow=DB_Fetch_array($result);						
						 if($_REQUEST['sitedata']!='')
						 {
								$val= substr($myrow['comprative_no'],0,19);
								$strChar1 = substr($myrow['comprative_no'],0,7);
								$strChar = $strChar1."/".$_REQUEST['sitedata']."/".$CurrentFinancialYear."/";  
							 
									if($numrows>0)
									{ 
										$maxval = $numrows;
									}
									else
									{
										$maxval=0;
									}
		  

							}
							else
							{
							 	$strChar = $Prefix_code."/".$_REQUEST['sitedata']."/".$CurrentFinancialYear."/";
							}
				  }
				  	else
					{
				  	    $strChar = $Prefix_code."/".$_REQUEST['sitedata']."/".$CurrentFinancialYear."/";
					}
			$strFinal = $maxval+1;
			
		 	$strBothFinal = $strChar.str_pad($strFinal,4,"0",STR_PAD_LEFT);
	
	
	
	
	//*************************  End of Generations ****************************//
	
$data_cmp=array();

for($i=0;$i<count($_REQUEST['ACGProductOrder']);$i++)
{
	
  	$value1=$_REQUEST['ACGProductOrder'][$i];
	$RequisitionNo = $_REQUEST['RequisitionNo'][$_REQUEST['ACGProductOrder'][$i]];
  	$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'];
	
	
	
	
		$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'];
		$ReqFarmCode = $REQ_Result_Row['Reqno'];
		$prdate = $REQ_Result_Row['prdate'];	
		 $value=$_REQUEST['ACGProductOrder'][$i];		
		 $item[]=$_REQUEST['item_id'][$value];
		 $itemdata5[]=$_REQUEST['ACGProductOrder'][$i];	
		  $item1=$_REQUEST['item_id'][$value];
		 
	// echo $_REQUEST['vender1-id'][$value];
	    // $venid= $s->GetFieldValue('venderquotations',"id='".$_REQUEST['vnederiddata'][$value]."'" ,'mst_supplierno');
  	 	$supplier=$s->GetFieldValue('suppliers',"id='".$_REQUEST['vnederiddata'][$value]."'" ,'vendor_name');
		//$data_cmp1['comprative_no']= $strBothFinal;
 		$datavendorid[]=$s->GetFieldValue('venderquotations',"id='".$_REQUEST['vnederiddata'][$value]."'" ,'mst_supplierno');
 		$data_cmp1['vendor_id'] =$s->GetFieldValue('venderquotations',"id='".$_REQUEST['vnederiddata'][$value]."'" ,'mst_supplierno');
		$datavenername[]=$supplier;
		$data_cmp1['vendor_name']=$supplier;
		//$data_cmp1['sitecode']=$lvendorid['sitecode'];		
	    $data1=$_REQUEST['venderqty'][$value];	
		$dataitemid[]=$item1;
	    $data_cmp1['mst_product_list']=$item1;	
		$dataVendor_Quot_No[]=$_REQUEST['vnederiddata'][$value];	
		//$data_cmp1['Vendor_Quot_No']= $_REQUEST['vnederiddata'][$value];
		$dataRequisitionNo[]=$RequisitionNo;
		//$data_cmp1['RequisitionNo']    = $RequisitionNo;
	    //$data_cmp1['comprative_date']   = date('Y-m-d');	
		
		$dataassignqty[]=$_REQUEST['assign_qty'][$value];			
		$data_cmp1['assign_qty']=$_REQUEST['assign_qty'][$value];
		$dataquotation_id[]=$_REQUEST['vender_detail_id'][$value];		
		$data_cmp1['quotation_id']=$_REQUEST['vender_detail_id'][$value];
		
		
		
		
		
		
		//$insert=$s->RecordInsert('comprative_detail', $data_cmp);
 		//$last=  DB_Last_Insert_ID($db,'comprative_detail', 'id');	 	
		
		
	 	echo $quantity=$data1-$data_cmp1['assign_qty'];
		echo "<br>";	
	//$data1=DB_query("update  requisitionorderdetails set  App_quantity1='".$quantity."' where mst_itemid='".$_REQUEST['item_id'][$value]."'
	//and requsitionno='".$_REQUEST['RequisitionNo'][$value]."'",$db);
		

		
}
//************************************************ Data insert ***************************************//
      $data_cmp['comprative_no']= $strBothFinal;
 		$data_cmp['vendor_id'] =implode(',',$datavendorid);
 		//$s->GetFieldValue('venderquotations',"id='".$_REQUEST['vnederiddata'][$value]."'" ,'mst_supplierno');
		$data_cmp['vendor_name']=implode(',',$datavenername);
		//$supplier;
		$data_cmp['sitecode']=$_REQUEST['sitedata'];		
	    //$data1=$_REQUEST['venderqty'][$value];	
	    $data_cmp['mst_product_list']=implode(',',$dataitemid);		
		$data_cmp['Vendor_Quot_No']= implode(',',$dataVendor_Quot_No);
		//$_REQUEST['vnederiddata'][$value];
		$data_cmp['RequisitionNo']=implode(',',$dataRequisitionNo);
		// $RequisitionNo;
	    $data_cmp['comprative_date']   = date('Y-m-d');				
		$data_cmp['assign_qty']=implode(',',$dataassignqty);
		//$_REQUEST['assign_qty'][$value];		
		$data_cmp['quotation_id']=implode(',',$dataquotation_id);
		//$_REQUEST['vender_detail_id'][$value];
		
		print_r($data_cmp);
		exit;
		
		
		
		
		$insert=$s->RecordInsert('comprative_detail', $data_cmp);
 		$last=  DB_Last_Insert_ID($db,'comprative_detail', 'id');


//************************************************ end of data insertions **********************************//
 
$s->pageLocation("index.php?route=POModule/Comp-Groupwiselist&CPID=".$last.'&RequisitionNo='.$_REQUEST['RequisitionNo'].$rurl);
 }
 ?>
 <h2>Generate Comparison</h2>

<form name="form1" method="post" action="">
<!--table 1 start--><div style="width:420px; margin-left:auto; margin-right:auto;">

<table width="50%" border="1" style="border-collapse:collapse; border:#CCCCCC solid 1px;" align="left">
  <tr bgcolor="#e7e7e7">
    <th style="width:100px; padding-left:10px;">Site</th>
    <td>
		<?php  
		 $ss="Select loccode,locationname from locations where company='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  
	?>
	<select name="site" id="site" style="width:320px" >
      <option value="000">---Select---</option>
      <?php
	while($site_row=DB_fetch_array($site_sql)){
	?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo ($_REQUEST['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
      <?php } ?>
    </select>
	</td>
  </tr>
  <tr bgcolor="#e7e7e7">
 <th style="padding-left:10px; white-space:nowrap; padding-right:10px;">Enter Part Name</th>
 <td><input type="text" name="" id="" value="" onkeyup="getItems(this.value)" /></td>
  </tr>
  <tr bgcolor="#e7e7e7">
    <th style="padding-left:10px; white-space:nowrap; padding-right:10px;">Item Name</th>
    <td>
			<div style="overflow:scroll; width:auto; height:120px;" >
		<div id="div_id">
	<?php  
		 $item="Select id,item_name from item_master where item_name!='' order by item_name ASC";
	$item_sql=DB_query($item,$db);
 	while($rowitem=DB_fetch_array($item_sql))
	{
		if(isset($_REQUEST['itemlist'])){
		foreach($_REQUEST['itemlist'] as $k=>$v)
		{
			if($v==$rowitem['id']){$checked="checked"; break;}else{$checked="";}
		}}
	?><input name="itemlist[]" type="checkbox" value="<?php echo $rowitem['id'];?>"  <?php echo $checked;?>><?php echo $rowitem['item_name'];?>  <br>
      <?php } ?>
	  </div>
	  
	  </div>
		
	</td>
  </tr>
  <tr>
    <th colspan="2"> <input type="submit" name="Search"  value="Proceed">  </th>
  </tr>
</table>

<div style="clear:both;"></div>

</div><!--table 1 Close-->



</form>

<?php 
if($_REQUEST['Search'])
{
 	$item_data=implode(',',$_REQUEST['itemlist']);
	$site=$_REQUEST['site'];
	
?> 

<form action="" method="post" name="finalqt">
	<input type="text" name="sitedata" value="<?php echo $_REQUEST['site']; ?>" />
<div style="width:830px; overflow:auto; float:left">
<table width="100%" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse; border:#CCCCCC solid 1px;">
<tr bgcolor="#e7e7e7">
<td><center>
  <strong>Vendor<br /> 
  code</strong>
</center></td>
<td  style="width:180px;"><center>
  <strong>Vendor Name</strong>
</center></td>
<td><center>
  <strong>Quote Date</strong>
</center></td>
<td><center>
  <strong>PR No.</strong>
</center></td>
<td  style="width:100px;"><center>
  <strong>Item Name</strong>
</center></td>
<td><center>
  <strong>Qty.</strong>
</center></td>
<td><center>
  <strong>Assign<br /> 
  Qty.</strong>
</center></td>
<td><center>
  <strong>Rate</strong>
</center></td>
<td><center>
  <strong>Discount<br />
  Amt</strong>
</center></td>
<td><center>
  <strong>Sub Total</strong>
</center></td>
<td><center>
  <strong>Special <br />
  Discount<br />
  Amt.</strong>
</center></td>
<td><center>
  <strong>Basic Amt. </strong>
</center></td>
<td><center>
  <strong>Taxable<br /> 
  Amt.</strong>
</center></td>
<td><center>
  <strong>Total </strong>
</center></td>
<?php 
//echo $item_data;
$sql="SELECT venderquotations.*, vendorquotationdetails.* 
FROM  venderquotations INNER JOIN vendorquotationdetails ON vendorquotationdetails.quotation_no=venderquotations.id 
WHERE venderquotations. sitecode='".$site ."' and vendorquotationdetails.mst_productid in($item_data)";
// venderquotations.pr_no='.$RequisitionNo. ' and venderquotations.id='.$suppvendorid['id'] ; 
$result=DB_query($sql,$db);
$j = 1;
$k=0; //row colour counter
$vid=0;
while($myrow=DB_fetch_array($result))
{	

  $getdata="select * from comprative_detail where  Vendor_Quot_No='".$myrow['id']."' and   RequisitionNo='".$myrow['mst_requisition_no']."' and status_active!='1'
  and mst_product_list in($item_data)";
 
 $rdata=DB_query($getdata,$db);
   DB_num_rows($rdata);
 if(DB_num_rows($rdata)==0)
 {

if ($k==1){ /*alternate bgcolour of row for highlighting */
echo '<tr class="OddTableRows">';
$k=0;
} else {
echo '<tr class="EvenTableRows">';
$k++;
}
?>
<tr>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><input name="ACGProductOrder[]" id="ACGProductOrder[]" type="checkbox" value="<?php echo $myrow['quotation_id'];  ?>" onclick="FinalQuotation(this.value)"/>
  <?php echo $myrow['id'].$myrow['vernder_quotation']; ?></td>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php echo  $myrow['vendername']  ; ?>
<input type="hidden" name="item_id[<?php echo $myrow['quotation_id'];  ?>]"  value="<?php echo $myrow['mst_productid']; ?>" h />
<input type="hidden" name="prod_id[<?php echo $myrow['quotation_id'];  ?>]"  value="<?php echo $myrow['prod_id']; ?>" />
<input type="hidden" name="vender_detail_id[<?php echo $myrow['quotation_id'];  ?>]" value="<?php echo $myrow['quotation_id'];  ?>">
<input type="hidden" name="vnederiddata[<?php echo $myrow['quotation_id']; ?>]" value="<?php echo $myrow['quotation_no'];  ?>" /></td>

<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php echo $myrow['date2']; ?></td>

<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><input type="hidden" name="RequisitionNo[<?php echo $myrow['quotation_id'];  ?>]" value="<?php echo $myrow['mst_requisition_no'] ?>" />
	<?php echo $myrow['mst_requisition_no'].$s->GetFieldValue('materialrequisitions',"requisitionno='".$myrow['mst_requisition_no']."'",'Reqno');

; //$myrow['mst_requisition_no']; ?></td>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php echo $myrow['prod_dec']; ?></td>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php echo $myrow['quantity']; ?>
<input type="hidden" name="venderqty[<?php echo $myrow['quotation_id'];  ?>]" value="<?php echo $myrow['quantity']; ?>"  id="venderqty_<?php  echo $vid; ?>"/></td>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><input type="text" name="assign_qty[<?php echo $myrow['quotation_id'];  ?>]" size="10px" onchange="check(<?php echo $vid; ?>)" id="assign_qty_<?php  echo $vid; ?>" value="<?php echo $myrow['quantity']; ?>"></td>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php echo $myrow['rate']; ?></td>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php echo $myrow['discount_amount']; ?></td>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php echo $myrow['sub_total']; ?>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php echo $myrow['specialdiscount_amount']; ?>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php echo $myrow['basic_amt']; ?></td>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php if($myrow['tax_type']=='NonExcisable')
			{
				 echo $myrow['taxvalue'];  
			}
			else
			{
				 if($myrow['tax_type']=='Excisable')
				 {
				 	echo $myrow['taxable_amt'] + $myrow['taxvalue'];
				 }
				 else{
				 	echo "IncludeTax";
				 }					
			}
			?></td>
<td style="white-space:nowrap; padding-right:5px; padding-left:5px;"><?php echo $myrow['total_amount']; ?></td>
  </tr>
<?php  $vid++; }  }?>
</table>
<br />
</div>
<div style="clear:both;"></div>
<div style="float:center">

 <table width="20%" border="1" cellspacing="0" cellpadding="0">
  <tr>
   <!-- <td width="49%"><div align="right"><input type="submit" name="Save"  value="Save" /></div></td>-->
    <td width="18%"><div align="center"><input type="submit" name="FinalQuot" value="Order For Purchase" /></div></td>
    <td width="33%"><div align="center"><input type="reset" name="Submit2" value="Cancel" onclick="javascript:history.go(-1)"></div></td>
  </tr>
</table>

</div>
<div style="clear:both;"></div>
</form>
<?php }?>
<script language="javascript" type="text/javascript">
function getItems(str)
{
	if(str.length==0)
	{
		document.getElementById("ContactName").innerHTML="";
		return;
	}
	if(window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
	xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	  	
		document.getElementById("ContactName").innerHTML=xmlhttp.responseText;
 	}
 }
xmlhttp.open("GET","getContact.php?Client="+str,true);
xmlhttp.send();	
	
	

}
</script>