<?php
error_reporting(0);
include('cocoon_config.php');

$id = $_REQUEST['id'];
$comp_id = $s->GetFieldValue('purchorders', "id='" . $_REQUEST['id'] . "'", 'comperativeId');
$lsqlvend = "SELECT * FROM comprative WHERE id='" . $comp_id . "'";
$result_comp = DB_query($lsqlvend, $db);
$rs_comp = DB_fetch_array($result_comp);
extract($rs_comp);
?>

<!-- JS PART AMITSINGH  02-05-2013 -->

<!-- <script type="text/javascript">
function checkItems(id)
{
	var count_items=document.getElementById("count1_"+id).value;
	
	if(document.getElementById("final_Quot_"+id).checked==true){
	for(var k=1;k<count_items;k++)
	{
		document.getElementById("ACGProductOrder_"+id+""+k).disabled=false;
		document.getElementById("assign_qty_"+id+""+k).disabled=false;
	}}
	else
	{
		for(var k=1;k<count_items;k++)
		{
			document.getElementById("ACGProductOrder_"+id+""+k).disabled=true;
			document.getElementById("ACGProductOrder_"+id+""+k).checked=false;
			document.getElementById("assign_qty_"+id+""+k).value='';
			document.getElementById("assign_qty_"+id+""+k).disabled=true;
		}
	}
}

function FinalQuotation(str,id)
{ 
	if(document.getElementById("ACGProductOrder_"+id).checked==true)
	{
		document.getElementById("assign_qty_"+id).value=document.getElementById("venderqty_"+id).value;
		var data=check1(id);
		document.getElementById("Savebutton").style.display='block';
	}
	else if(document.getElementById("ACGProductOrder_"+id).checked==false)
	{
		document.getElementById("assign_qty_"+id).value='';
		//document.getElementById("Savebutton").style.display='none';
	}

}

  
function check(i,t,pending)
{


	var x = checkQty();
//	alert(x+"  = "+pending);
	if(parseFloat(x)>parseFloat(pending))
	{
		alert("Assign Quantity cannot be greater than Quantity");
	}

	var vid=document.getElementById("viddata").value;
	var k=0;
	var j;
	for(var m=0;m<vid;m++)
	{
		if(document.getElementById("ACGProductOrder_"+m+""+t).checked==true && (document.getElementById("ACGProductOrder_"+i+""+t).value==document.getElementById("ACGProductOrder_"+m+""+t).value))
		{
			if(i==m)
			{	
				var k=k+parseFloat(document.getElementById("assign_qty_"+i+""+t).value);
				var j=parseFloat(document.getElementById("venderqty_"+i+""+t).value);
				break;	
			}
			else
			{		
				var k=k+parseFloat(document.getElementById("assign_qty_"+m+""+t).value);
				var j=parseFloat(document.getElementById("venderqty_"+m+""+t).value);	
			}
		}
	}
	if(k>j)
	{
		alert("Assign Qty Cannot Greater Then Actual Qty");
		document.getElementById("assign_qty_"+i+""+t).value='';
		document.getElementById("assign_qty_"+i+""+t).focus;
	}
}


function check1(id)
{

	
	var t=id.substr(1,1);
	var i=id.substr(0,1);
	var vid=document.getElementById("viddata").value;
	var k=0;
	var j;
	for(var m=0;m<vid;m++)
	{
		if(document.getElementById("ACGProductOrder_"+m+""+t).checked==true && (document.getElementById("ACGProductOrder_"+i+""+t).value==document.getElementById("ACGProductOrder_"+m+""+t).value))
		{
			if(i==m)
			{	
				var k=k+parseFloat(document.getElementById("assign_qty_"+i+""+t).value);
				var j=parseFloat(document.getElementById("venderqty_"+i+""+t).value);
				break;	
			}
			else
			{		
				var k=k+parseFloat(document.getElementById("assign_qty_"+m+""+t).value);
				var j=parseFloat(document.getElementById("venderqty_"+m+""+t).value);	
			}
		}
	}
	if(k>j)
	{
		alert("Assign Qty Cannot Greater Then Actual Qty");
		document.getElementById("assign_qty_"+i+""+t).value='';
		document.getElementById("assign_qty_"+i+""+t).focus;
	}
}

function checkQty()
	{
		var total=0;
		jQuery('input[id^=assign_qty_]').each(function(){
			if(jQuery(this).val()!='')
				total+=parseFloat(jQuery(this).val());
         });
		return total;
	}
	
function checkdata1()
{
	var t=0;
	var k=0;
	var vid=document.getElementById("viddata").value;
	for(var m=0;m<vid;m++)
	{	
		if(document.getElementById("final_Quot_"+m).checked==true)
		{		
			var counter=document.getElementById("count1_"+m).value;
			for(var j=1;j<counter;j++)
			{			
				if(document.getElementById("ACGProductOrder_"+m+""+j).checked==true)
				{			
					var value=document.getElementById("ACGProductOrder_"+m+""+j).value;
					var items=(document.getElementById("items_"+m+"-"+value).value).split('-');
					var t=parseFloat(document.getElementById("venderqty_"+m+""+j).value);	
					if(items[0]==document.getElementById("ACGProductOrder_"+(m+1)+""+j).value)
					{
						var k=k+parseFloat(document.getElementById("assign_qty_"+m+""+j).value);
					}
				} 
			}
		}	
	}
	alert(k);
	alert(t);
	if(k>t)
	{
		alert("Assign Qty Cannot Greater Then Actual Qty");
		return false;
	}
	return false;
}


function isNumberKey(sender, evt) {
    var txt = sender.value;
    //alert(evt.which);
    var dotcontainer = txt.split('.');
    var charCode = (evt.which) ? evt.which : event.keyCode;
    //alert(event.keyCode);
    if (!(dotcontainer.length == 1 && charCode == 46) && charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}
</script> -->


<!-- JS PART AMITSINGH 02-05-2013  -->


<link href="css/new_mxerp.css" rel="stylesheet" type="text/css" />
<div class="body_contain">
	<div class="tab_div">
		<h1>Comparative</h1>
		<form action="" method="post" name="finalqt">
			<table width="100%" border="0" cellpadding="3" cellspacing="0">
				<tr class="row1">
					<td class="tabledata1"><strong>Comparative No.</strong></td>
					<td><strong><?php echo $comparative_no; ?></strong>
						<input type="hidden" name="comprativeno" size="30" readonly="" style="width:100%;" value="Auto Generated" />
					</td>
					<td class="tabledata1"><strong>Date</strong></td>
					<td><strong><?php echo date("d/m/Y", strtotime($comparative_date)); ?></strong>
						<input type="hidden" name="comprative_date" size="30" value="<?php echo date("d/m/Y"); ?>" />
					</td>
				</tr>
				<tr class="row1">
					<td class="tabledata1"><strong>Site Name:</strong></td>
					<td><strong><?php echo $s->GetFieldValue('locations', "loccode='" . $site . "'", 'locationname'); ?></strong>
						<input type="hidden" name="sitecode" id="sitecode" value="<?php echo $lvendorid['Site']; ?>" />
					</td>
					<td class="tabledata1"><strong>Requisition No.:</strong></td>
					<td><strong><?php echo $s->GetFieldValue('materialrequisitions', "requisitionno='" . $indentprno . "'", 'Reqno'); ?></strong>
						<input type="hidden" name="reqno" id="reqno" value="<?php echo $RequisitionNo; ?>" />
					</td>
				</tr>
				<tr class="row1">
					<td class="tabledata1"><strong>Requisition Date:</strong></td>
					<td colspan="3"><strong><?php echo ConvertSQLDate($s->GetFieldValue('materialrequisitions', "requisitionno='" . $indentprno . "'", 'prdate')); ?></strong>
						<input type="hidden" name="reqno_date" id="reqno_date" value="<?php echo ConvertSQLDate($prdate); ?>" />
					</td>
				</tr>

				<tr class="row1">
					<td class="tabledata1" colspan="4">
						<h3>Comparison Details</h3>
					</td>
				</tr>


				<tr>
					<td colspan="4">
						<table width="100%" cellpadding="3" cellspacing="0" border="0" style="border:#333333 1px solid;">
							<tr>
								<td>
									<?php
									$data = DB_query("select distinct sppliersId from comprative_detail where parant_id='" . $_REQUEST['id'] . "' ", $db);
									while ($data_row = DB_fetch_array($data)) {
										$supp[] = $data_row['sppliersId'];
									}
									array_unique($supp);
									$man_ven1 = implode(",", $supp);


									$suppsqlvend = "SELECT venderquotations.id as id,venderquotations.quotation_no,venderquotations.Quotation_Date,party_Quo_no,party_Quo_date,suppliers.vendor_name,suppliers.id  as venderid 
		FROM  venderquotations INNER JOIN suppliers ON suppliers.id=venderquotations.mst_supplierno 
		INNER JOIN vendorquotationdetails ON vendorquotationdetails.parant_id=venderquotations.id 
		WHERE venderquotations.indent_prno=" . $indentprno . " group by id";
									$suppresultvand = DB_query($suppsqlvend, $db);
									$i = 1;
									$vid = 0;
									$k = 1;
									if (DB_num_rows($suppresultvand) > 0) {
										//echo DB_num_rows($suppresultvand);
										while ($suppvendorid = DB_fetch_array($suppresultvand)) {
											$net = 0;
											$tot = 0;
											$basic = 0;
											$tax = 0; ?>

											<table cellpadding="3" cellspacing="0" border="1" width="100%" style="border:#666666 1px solid;">
												<tr bgcolor="#d4d4d4">
													<td width="3%" height="35" class="tabledata">&nbsp;</td>
													<td width="16%" class="tabledata"><strong>Quot. No</strong></td>
													<td width="8%" class="tabledata"><strong>Quot. Date</strong></td>
													<td width="10%" class="tabledata"><strong>Vendor Quot. No</strong></td>
													<td width="15%" class="tabledata"><strong>Vendor Quot. Date</strong></td>
													<td width="27%" class="tabledata"><strong>Vendor Name </strong></td>

												</tr>
												<tr class="rowdata">
													<td height="35"></td>
													<td><strong><?php echo $suppvendorid['quotation_no']; ?></strong>
														<input type="hidden" name="Vendorqtnid" id="Vendorqtnid" value="<?php echo $suppvendorid['id']; ?>" />
													</td>
													<td><strong><?php echo ConvertSQLDate($suppvendorid['Quotation_Date']); ?></strong></td>
													<td><strong><?php echo $suppvendorid['party_Quo_no']; ?></strong>
													<td><strong><?php echo ConvertSQLDate($suppvendorid['party_Quo_date']); ?></strong></td>
													<td><strong><?php echo $suppvendorid['vendor_name']; ?></strong></td>

												</tr>
											</table>

											<table width="100%">
												<tr>
													<td colspan="12">&nbsp;</td>
												</tr>
											</table>
											<table width="100%" border="1" cellpadding="3" cellspacing="1" style="border:#666666 1px solid;">
												<tr bgcolor="#d4d4d4">
													<td class="tabledata"><strong>&nbsp;</strong></td>
													<td class="tabledata"><strong>Item Code</strong></td>
													<td class="tabledata"><strong>Item Name</strong></td>
													<td class="tabledata"><strong>Brand</strong></td>
													<td class="tabledata"><strong>Quantity</strong></td>
													<td class="tabledata"><strong>Assign Qty</strong></td>
													<td class="tabledata"><strong>Rate</strong></td>
													<td class="tabledata"><strong>Discount Amount</strong></td>
													<td class="tabledata"><strong>Amount</strong></td>
													<!--<td class="tabledata"><strong>Special Discount Amount</strong></td>-->
													<!--<td class="tabledata"><strong>Basic Amt</strong></td>
			<td class="tabledata"><strong>Taxable Amt</strong></td>
			<td class="tabledata"><strong>Total</strong></td>-->
												</tr>
												<?php
												$dis = 0;
												//echo "select * from vendor_quotes_ammendment where requisition_id='".$_REQUEST['RequisitionNo']."' and mst_supplierno='".$suppvendorid['mst_supplierno']."'  order by vendor_quotes_ammendment.revision_no DESC limit 1";
												$sqlcheck = DB_query("select * from vendor_quotes_ammendment where requisition_id='" . $indentprno . "' and mst_supplierno='" . $suppvendorid['mst_supplierno'] . "'  order by vendor_quotes_ammendment.revision_no DESC limit 1", $db);
												$numrowcheck = DB_num_rows($sqlcheck);
												$row5 = DB_fetch_array($sqlcheck);


												$sql = 'SELECT venderquotations.*, vendorquotationdetails.*, vendorquotationdetails.id as detailsId 
	FROM  venderquotations INNER JOIN vendorquotationdetails ON vendorquotationdetails.parant_id=venderquotations.id 
	WHERE include_gst="1" and venderquotations.indent_prno=' . $indentprno . '  and venderquotations.id=' . $suppvendorid['id'];
												$result = DB_query($sql, $db);
												$j = 1;
												while ($myrow = DB_fetch_array($result)) {

													//echo "select sum(assign_qty) SumAsignQty from comprative_detail
													//          inner join comprative on comprative.id=comprative_detail.parant_id
													//where  comprative_detail.indentprno='".$_REQUEST['RequisitionNo']."' and mst_itemid='".$myrow['mst_itemid']."' and status_active=0";

													$sqla = DB_query("select sum(assign_qty) as SumAsignQty from comprative_detail
                inner join comprative on comprative.id=comprative_detail.parant_id
 where  comprative_detail.indentprno='" . $indentprno . "' and mst_itemid='" . $myrow['mst_itemid'] . "' and sppliersId='" . $myrow['mst_supplierno'] . "'", $db);

													$abc = DB_fetch_array($sqla);


													$brndSql = "select id,brand_desc from branditems where id='" . $myrow['brand_id'] . "'";
													$brand_sql = mysqli_query($link1, $brndSql);
													$brand_row = mysqli_fetch_array($brand_sql);
													$brnadName = $brand_row['brand_desc'];
													$rowid = 'row_' . $j . '_' . $suppvendorid['venderid'] . '_' . $myrow['mst_itemid'] . $myrow['brand_id'];

													$sql_minRate = 'SELECT vendorquotationdetails.TotalAmount as minRate, vendorquotationdetails.id as minId
	FROM  venderquotations INNER JOIN vendorquotationdetails ON vendorquotationdetails.parant_id=venderquotations.id 
	WHERE venderquotations.indent_prno=' . $indentprno . '  and venderquotations.id=' . $suppvendorid['id'] . ' and vendorquotationdetails.mst_itemid=' . $myrow['mst_itemid'] . ' ORDER BY vendorquotationdetails.TotalAmount ASC LIMIT 1';
													$result_minRate = DB_query($sql_minRate, $db);
													$data_minRate = DB_fetch_array($result_minRate);
													//echo "<pre>"; print_r($myrow); echo "</pre>";
												?>
													<tr class="rowdata" id="<?php echo $rowid; ?>">
														<td>
															<div align="center"><?php //echo $data_minRate['minId'].' '.$myrow['detailsId']; 
																				?>
																<?php //echo $rowid; 
																?>
															</div>

															<input type="hidden" name="sppliersId[<?php echo $k; ?>]" value="<?php echo $suppvendorid['venderid']; ?>" />
														</td>
														<td><?php echo $s->GetFieldValue('item_master', "id='" . $myrow['mst_itemid'] . "'", 'StockId'); ?>
															<input type="hidden" name="currency[<?php echo $k; ?>]" value="<?php echo $myrow['currency'] ?>" />
														</td>
														<td><?php echo $s->GetFieldValue('item_master', "id='" . $myrow['mst_itemid'] . "'", 'item_name'); ?>
															<input type="hidden" name="quotation_details_id[<?php echo $k; ?>]" value="<?php echo $myrow['id'];  ?>">
															<input type="hidden" name="quotationid[<?php echo $k; ?>]" value="<?php echo $suppvendorid['id'];  ?>">
														</td>
														<td><?php echo $brnadName; ?>
															<input type="hidden" name="brand_id[<?php echo $k; ?>]" value="<?php echo $myrow['brand_id'];  ?>">
														</td>
														<td><?php echo $myrow['Quantity']; ?></td>
														<td><?php echo $abc['SumAsignQty']; ?>
														</td>
														<td><?php echo $myrow['Rate']; ?></td>
														<td><?php echo $myrow['Discount'];
															$dis += $myrow['Discount']; ?></td>
														<td class="rate<?php //if($data_minRate['minId']==$myrow['detailsId']){ echo 'rate'; } 
																		?>" data-colorid="<?php echo $rowid; ?>" data-item="<?php echo $myrow['mst_itemid']; ?>" data-brand="<?php echo $myrow['brand_id']; ?>" data-rate="<?php echo $myrow['totalPrice']; ?>"><?php echo $myrow['totalPrice'];
																																																																$net += $myrow['totalPrice']; ?></td>
														<!--<td><?php //echo $myrow['SpecialDiscounted_Amt']; 
																?></td>-->
														<!--<td><?php echo $myrow['basic_Amt'];
																$basic += $myrow['basic_Amt']; ?></td>
	<td><?php echo $myrow['taxable_amt'];
													$tax += $myrow['taxable_amt']; ?></td>
	<td><?php echo $myrow['TotalAmount'];
													$tot += $myrow['TotalAmount'];; ?></td>-->
													</tr>

												<?php $j++;
													$k++;
												} ?>
												<tr>
													<td colspan="7" style="text-align: right;padding-right: 5px"><strong>Total</strong></td>


													<td><?php echo $dis; ?></td>
													<td><?php echo $net; ?></td>
													<!--<td><?php echo $basic; ?></td>
            <td><?php echo $tax; ?></td>
            <td><?php echo $tot; ?></td>-->
												</tr>
												<input type="hidden" name="count1" id="count1_<?php echo $vid; ?>" value="<?php echo $j; ?>" />
												<input type="hidden" name="quotation_type[]" value="WithoutAmd" />

												<?php  //} 
												?>
											<?php $i++;
											$vid++;
										} ?>
											<input type="hidden" name="viddata" value="<?php echo $vid; ?>" id="viddata" />
										<?php } ?>
											</table>
								</td>
							</tr>
						</table>
					</td>
				</tr>

				<tr class="row1">
					<td style="text-align:center;" class="tabledata1" colspan="4">
						<p id="Savebutton" style="display:none;">
							<input type="submit" class="search" name="FinalQuot" value="Order For Purchase" />
							<input type="reset" class="search" name="Submit2" value="Cancel" onclick="javascript:history.go(-1)">
						</p>
					</td>
				</tr>
			</table>
		</form>
	</div>
</div>

<!-- <script type="text/javascript">
jQuery('document').ready(function(){
    var coloIds = [];
    var items = [];
    jQuery('.rate').each(function(){
      var t=jQuery(this);
      var colorId=t.attr('data-colorid');
      var itm=t.attr('data-item');
      var brand='';
      //var brand=t.attr('data-brand');
      var rate=t.attr('data-rate');
      var itmCode=itm+brand;
      var item=[];
      item['colorId']=colorId;
      item['itm']=itm;
      item['brand']=brand;
      item['rate']=rate;
      item['itmCode']=itmCode;
      if(coloIds.length>0){
        var check=1;
        for (var i = coloIds.length - 1; i >= 0; i--) {
          if(coloIds[i]['itmCode']==itmCode){
            var check=0;
            if(parseFloat(rate)<parseFloat(coloIds[i]['rate'])){
              item['color']='green';
              coloIds[i]=item;
              item='';
            }
            if(parseFloat(coloIds[i]['rate'])==parseFloat(rate) && coloIds[i]['colorId']!=colorId){
              coloIds[i]['color']='yellow';
              item['color']='yellow';
              coloIds.push(item);
              item='';
            }
          }
        }
        if(check==1){
          item['color']='green';
          coloIds.push(item);
          item='';
        }      
      } else {
        item['color']='green';
        coloIds.push(item);
        item='';
      }
    });
    for (var i = coloIds.length - 1; i >= 0; i--) {
      jQuery('#'+coloIds[i]['colorId']).css("background-color", coloIds[i]['color']);
    }
});


</script> -->