<?php 
session_name('CAKEPHP'); session_start(); 
$PageSecurity = 4;
include('includes/session.inc');
include('includes/function_lib.php');
global $db;  

$SQLparant=DB_query("select * from cstest_estimation_bom where id='".$_REQUEST['estimationid']."'",$db);

$typeAS=DB_fetch_array($SQLparant); 


$data1="";
$data1.="<select name='type' id='type' style='width:150px;'>";   

$data1.="<option value=''>Select</option>";  

$TypetSQL=DB_query("SELECT * FROM type_master",$db);  
while($Type=DB_fetch_array($TypetSQL)){
$selty='';
if($Type['id']==$typeAS['type'])
$selty='selected';
$data1.="<option value='".$Type['id']."' ".$selty.">".$Type['name']."</option>";
 }
 $data1.="</select>"; 
$SQLEst=DB_query("SELECT * FROM cstest_estimation_bom_details where master_id='".$_REQUEST['estimationid']."'",$db);
$data2="";    
$data2.="<table id='deleterrw' width='100%' border='1' cellpadding='3' cellspacing='0'>";
$data2.="<tr class='row1'>";
$data2.="<td class='tabledata'>Sr. No.</td>";
$data2.="<td class='tabledata'>&nbsp;</td>";
$data2.="<td class='tabledata'>Item Code</td>";
$data2.="<td class='tabledata'>Item Name</td>";
$data2.="<td class='tabledata'>Size</td>";
$data2.="<td colspan='7' style='text-align:center' class='tabledata'>Qty.</td>";
$data2.="<td class='tabledata'>Price</td>";
$data2.="<td class='tabledata'>Per Unit</td>";
$data2.="<td class='tabledata'>Value</td>";
$data2.="<td class='tabledata'>Discount Type</td>";
$data2.="<td class='tabledata'>Discount Per</td>";
$data2.="<td class='tabledata'>Discount Price</td>";
$data2.="<td class='tabledata'>Value After Discount</td>";
$data2.="</tr>";
$i=0; 

while($GetEsti=DB_fetch_array($SQLEst)){
$SqlHRdQty=DB_query("SELECT * FROM cstest_estimation_bom_details_asmbly where mst_itemid='".$GetEsti['mst_itemid']."' and details_mst_id='".$GetEsti['id']."'",$db);
$arr='';


$amt=0;
$itemid1='';
while($hrdQty=DB_fetch_array($SqlHRdQty))   {	$arr[]=$hrdQty['asmbly_item_qty'];	$amt=$amt+$hrdQty['asmbly_item_amt'];	}
$sql_item=DB_query("select * from item_master where id='".$GetEsti['mst_itemid']."'",$db);
while($sql_item1=DB_fetch_array($sql_item)){ $itemid1[]=$sql_item1['mst_unitid'];$itemid1[]=$sql_item1['conv_unit'];}
$unitid=implode("','",$itemid1);
$qtyhrd=implode(",",$arr);
//echo $qtyhrd;
$item_Codeas=$s->GetFieldValue('item_master',"id='".$GetEsti['mst_itemid']."'",'StockID');
$item_names=$s->GetFieldValue('item_master',"id='".$GetEsti['mst_itemid']."'",'item_name');
$unit=$s->GetFieldValue('item_master',"id='".$GetEsti['mst_itemid']."'",'mst_unitid');
$conv_unit=$s->GetFieldValue('item_master',"id='".$GetEsti['mst_itemid']."'",'conv_unit');
$confac=$s->GetFieldValue('item_master',"id='".$GetEsti['mst_itemid']."'",'conv_factor');
$data2.="<tr class='row1' id='hide_".$i."'>";
$data2.="<td style='white-space:nowrap;' class='tabledata'><div id='deactive_".$i."' style='display:block;'>
		<a href='#'   onclick='deactivate(".$i.")'><img src='images/delete1.gif' /></a>
		</div></td>";
$data2.="<td class='tabledata'><input type='hidden' name='item_code[]' id='itemid1_".$i."' value='".$GetEsti['mst_itemid']."' /><input type='hidden' id='hrdqty1A_".$i."' value='".$qtyhrd."'></td>";
$data2.="<td class='tabledata'>".$item_Codeas."</td>";
$data2.="<td class='tabledata' style='white-space:nowrap'>".$item_names."</td>";
$data2.="<td class='tabledata' style'white_space:nowrap'>";
$data2.="<select style='width:100px;' name='size[]' id='size'>";
$data2.="<option>--Select--</option>"; 
$SQLSize=DB_query("SELECT * FROM size_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
	while($sizeid=DB_fetch_array($SQLSize)){
	$selsize='';
	if($GetEsti['item_size']==$sizeid['id'])
	$selsize='selected';
$data2.="<option value='".$sizeid['id']."' ".$selsize.">".$sizeid['size']."</option>";
}
$data2.="</select>";
$data2.="</td>";





$data2.="<td class='tabledata'><input name='qtytype[".$i."]' type='radio' id='qtyprim_".$i."' value='".$GetEsti['item_prim_qty']."'  onclick='showqty2(this.id)' checked='checked' /></td>";
$data2.="<td class='tabledata'>".$s->getField('unitsofmeasure','unitid',$unit,'unitname')."</td>";

$data2.="<td class='tabledata'><input type='text' name='stock_qty[]'  id='stockqty_".$i."' style='width:50px;' onkeyup='calcValue1(this.value,this.id)' value='".$GetEsti['item_prim_qty']."'  /></td>";
$data2.="<td class='tabledata'><input name='qtytype[".$i."]' type='radio' value='".$GetEsti['item_sec_qty']."' id='qtysec_".$i."' onclick='showqty4(this.id)' /></td>";
$data2.="<td class='tabledata'>".$s->getField('unitsofmeasure','unitid',$conv_unit,'unitname')."</td>";
$data2.="<td class='tabledata'><input type='text' name='secondary_qty[]'  id='secondary_".$i."' style='width:50px;' onkeyup='calcValue1(this.value,this.id)' value='".$GetEsti['item_sec_qty']."'  /></td>";
$data2.="<td class='tabledata'><input type='hidden' name='convfac[]'  id='confac_".$i."' style='width:50px;' value='".$confac."'  /></td>";
$data2.="<td class='tabledata'><input type='text' name='stock_price[]' value='".$amt."'  id='stock_price_".$i."' style='width:50px;'/></td>";
$data2.="<td class='tabledata'><input type='text' name='stock_perunit[]' onfocus='filldefaultAmt(this.value,this.id)' id='stock_perunit_".$i."' style='width:30px;' value='1' /></td>";
$total_amt=round(($GetEsti['item_prim_qty']*$amt)/1,3);

$data2.="<td class='tabledata'><input type='text' name='stock_value[]'  id='stock_value_".$i."' style='width:60px;' onfocus='CalculateTotalLeadAmt(this.value,this.id)' value='".$total_amt."'/></td>";
$data2.="<td class='tabledata'>";
$data2.="<select style='width:80px;' name='PerAmo[]' id='PerAmo_".$i."'>"; 
$data2.="<option value='Amount' >Amount</option>";  
$data2.="<option value='PercentAmt'>Percent Amt</option>";  
$data2.="</select>";   
$data2.="</td>"; 
$totalvalueamt=$total_amt;
$data2.="<td class='tabledata'><input type='text' style='width:50px;' name='Discount[]' id='Discount_".$i."' onkeyup='DiscountAmount1(this.value,this.id)' value='0'
 /></td>";
$data2.="<td class='tabledata'><input type='text' style='width:50px;' name='perc[]' id='perc_".$i."'  readonly='true' value='0'  /></td>";
$data2.="<td class='tabledata'><input type='text' style='width:70px;' name='value_dis[]' id='value_dis_".$i."'  readonly='true' value='".$totalvalueamt."'  /></td>";
$data2.="</tr>";
$i++;} 






$data2.="</table>";
$data2.="<input type='hidden' name='count1' id='count1' value='".$i."'>";


$data3="";
$data3.="<table  width='100%' border='1' cellpadding='3' cellspacing='0'>";
$data3.="<tr class='row1'>";
$data3.="<td class='tabledata1' colspan='15'><strong>Assembly & Sub Assembly Details</strong></td>";
$data3.="</tr>";
$data3.="<tr class='row1'>";
$data3.="<td class='tabledata'>Sr. No.</td>";
$data3.="<td class='tabledata'>&nbsp;</td>";
$data3.="<td class='tabledata'>Item Code</td>";
$data3.="<td class='tabledata'>Item Name</td>";
$data3.="<td class='tabledata'>Qty.</td>";
$data3.="<td class='tabledata'>Total Qty.</td>";
$data3.="<td class='tabledata'>Unit.</td>";
$data3.="<td class='tabledata'>Price</td>";
$data3.="<td class='tabledata'>Per Unit</td>";
$data3.="<td class='tabledata'>Value</td>";
$data3.="<td class='tabledata'>Discount Type</td>";
$data3.="<td class='tabledata'>Discount Per</td>";
$data3.="<td class='tabledata'>Discount Price</td>";
$data3.="<td class='tabledata'>Value After Discount</td>";
$data3.="<td class='tabledata'>Add</td>";
$data3.="</tr>";

$k=$i;
$z=0;
$qty=0;
$totalproductqty=0;

$items=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['estimationid']."' ",$db);
 while($item1=DB_fetch_array($items)) 	  {	  $item2[]=$item1['id']; 	  }   $itemsdata=implode("','",$item2);

$totalexciableamt=0;
$totalnonexciableamt=0;
$SQLhrd=DB_query("SELECT * FROM cstest_estimation_bom_details_asmbly 
where details_mst_id in('".$itemsdata."') and hardware_flag=0  GROUP BY mst_asmbly_cat_id ",$db);
while($GetHard=DB_fetch_array($SQLhrd))
{


$itemid=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$GetHard['mst_asmbly_cat_id']."'
	   and  details_mst_id in('".$itemsdata."')",'mst_asmbly_item_id'); 
  
 $totalqty=DB_query("select sum(asmbly_item_qty) as totalqty from cstest_estimation_bom_details_asmbly 
                   where details_mst_id in('".$itemsdata."') and mst_asmbly_item_id='".$GetHard['mst_asmbly_item_id']."' 
				   and   hardware_flag=0 GROUP BY mst_asmbly_cat_id ",$db) ;			   
	  
$totalqty1=DB_fetch_array($totalqty);   

$totaproductqty=0;
$totalcount1=count($item2);

for($u=0;$u<$totalcount1;$u++)
{
 $assemblyqty=$s->getField('cstest_estimation_bom_details','id',$item2[$u],'item_prim_qty');

$totalqty121=DB_query("select asmbly_item_qty as totalqty from cstest_estimation_bom_details_asmbly 
                   where details_mst_id ='".$item2[$u]."' and mst_asmbly_item_id='".$GetHard['mst_asmbly_item_id']."' 
				   and   hardware_flag=0 GROUP BY mst_asmbly_cat_id ",$db) ;			   
	  
while($totalqty1211=DB_fetch_array($totalqty121))
{
$totaproductqty=$totaproductqty+($totalqty1211['totalqty']*$assemblyqty);
}
}
                        
		$unit1=$s->getField('item_master','id',$itemid,'mst_unitid');				   
				

$data3.="<tr class='row1' >";
$data3.="<td style='white-space:nowrap;' class='tabledata'><input name='delitem[]' type='checkbox' value='' /></td>";
$data3.="<td class='tabledata'>";
$data3.="<input type='hidden' name='hardwarepart[]' id='hardwarepart' value='".$GetHard['mst_asmbly_cat_id']."' />";
$data3.="<input type='hidden' name='hrditemid[]' id='itemid' value='".$GetHard['mst_asmbly_item_id']."' /></td>";
$data3.="<td class='tabledata'>".$s->getField('item_master','id',$itemid,'StockID')."</td>";
$data3.="<td class='tabledata' style='white-space:nowrap'>".$s->getField('item_master','id',$itemid,'item_name')."</td>";
$data3.="<td class='tabledata'><input type='text' name='hrd_qty1[]'  id='hrdstock_qty21_".$z."' style='width:50px;' onkeyup='calcValue(this.value,".$z.")' value='".$totalqty1['totalqty']."'  /></td>";

$data3.="<td class='tabledata'><input type='text' name='hrd_qty[]'  id='hrdstock_qty2_".$z."' style='width:50px;' onkeyup='calcValue(this.value,".$z.")' value='".$totaproductqty."'  /></td>";
$data3.="<td class='tabledata'>".$s->getField('unitsofmeasure','unitid',$unit1,'unitname')."</td>";


$data3.="<td class='tabledata'><input type='text' name='hrd_price[]'  id='hrdstock_price1_".$z."' value='".$GetHard['asmbly_basicrate']."' style='width:50px;'/></td>";
$data3.="<td class='tabledata'><input type='text' name='hrd_perunit[]' onfocus='filldefaultAmt(this.value,this.id)' id='hrdstock_perunit1_".$z."' style='width:30px;' value='1' /></td>";
$total_amt=round(($totaproductqty*$GetHard['asmbly_basicrate'])/1,3);

$data3.="<td class='tabledata'><input type='text' name='hrd_value[]'  id='hrdstock_value1_".$z."' style='width:60px;' onfocus='CalculateTotalLeadAmt2(this.value,".$z.")'  value='".$total_amt."'/></td>";
$data3.="<td class='tabledata'>";
$data3.="<select style='width:80px;' name='hrdPerAmo[]' id='hrdPerAmo1_".$z."' >"; 
$data3.="<option value='Amount' >Amount</option>"; 
$data3.="<option value='PercentAmt'>Percent Amt</option>"; 
$data3.="</select>";
$data3.="</td>";
$discountTotal_amt=$total_amt;

$exciable_amt=$s->getField('item_master','id',$itemid,'item_exciable');
if($exciable_amt=='Y')
{
$totalexciableamt=$totalexciableamt+$discountTotal_amt;
}
else
{
$totalnonexciableamt=$totalnonexciableamt+$discountTotal_amt;
}

$data3.="<td class='tabledata'><input type='text' style='width:50px;' name='hrdDiscount[$z]' id='hrdDiscount1_".$z."' onkeyup='DiscountAmount2(this.value,".$z.")' value='0' />
<input type='hidden' name='itemtye[]' id='itemtype_".$z."' value='".$exciable_amt."'></td>";
$data3.="<td class='tabledata'><input type='text' style='width:50px;' name='hrdperc[$z]' id='hrdperc1_".$z."'  value='0' readonly='true'  /></td>";
$data3.="<td class='tabledata'><input type='text' style='width:70px;' name='hrdvalue_dis[]' id='hrdvalue_dis1_".$z."'  readonly='true' value='".$discountTotal_amt."'  /></td>";
$data3.="<td class='tabledata'><input type='checkbox' name='addhrdAmt[]' value='0' id='addhrdAmt_".$z."' onclick='addtotadhrdamt(".$z.")' disabled='disabled' /></td>";
$data3.="</tr>";
 $k++;$z++;} 
 
 
               
									
									
									
									
$SQLhrd=DB_query("SELECT * FROM cstest_estimation_bom_details_asmbly 
where details_mst_id in('".$itemsdata."') and hardware_flag=1  GROUP BY mst_asmbly_cat_id ",$db);
while($GetHard=DB_fetch_array($SQLhrd))
{
$itemidcode='';

$itemid=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$GetHard['mst_asmbly_cat_id']."'
	   and  details_mst_id in('".$itemsdata."')",'mst_asmbly_item_id'); 
  
 $totalqty=DB_query("select sum(asmbly_item_qty) as totalqty from cstest_estimation_bom_details_asmbly 
                   where details_mst_id in('".$itemsdata."') and mst_asmbly_item_id='".$GetHard['mst_asmbly_item_id']."' 
				   and   hardware_flag=1 GROUP BY mst_asmbly_cat_id ",$db) ;
		
		   
$itemiddata=DB_query("select id  from cstest_estimation_bom_details_asmbly 
                   where details_mst_id in('".$itemsdata."') and mst_asmbly_item_id='".$GetHard['mst_asmbly_item_id']."' 
				   and   hardware_flag=1",$db);				   
while($row1=DB_fetch_array($itemiddata))
{
$itemidcode[]=$row1['id'];
}	
  $assemblyid=implode(",",$itemidcode);		   
$unit1=$s->getField('item_master','id',$itemid,'mst_unitid');				   
	  
$totalqty1=DB_fetch_array($totalqty);   


$totaproductqty=0;
$totalcount=count($item2);
for($u=0;$u<$totalcount;$u++)
{

 $assemblyqty=$s->getField('cstest_estimation_bom_details','id',$item2[$u],'item_prim_qty');

$totalqty121=DB_query("select asmbly_item_qty as totalqty from cstest_estimation_bom_details_asmbly 
                   where details_mst_id ='".$item2[$u]."' and mst_asmbly_item_id='".$GetHard['mst_asmbly_item_id']."' 
				   and   hardware_flag=1 GROUP BY mst_asmbly_cat_id ",$db) ;			   
	  
while($totalqty1211=DB_fetch_array($totalqty121))
{
$totaproductqty=$totaproductqty+($totalqty1211['totalqty']*$assemblyqty);
}
}



$data3.="<tr class='row1' >";
$data3.="<td style='white-space:nowrap;' class='tabledata'><input name='delitem[]' type='checkbox' value='' /></td>";
$data3.="<td class='tabledata'>";
$data3.="<input type='hidden' value='".$assemblyid."' name='hrdid[]' id='hrdid_".$z."'  />";
$data3.="<input type='hidden' name='hardwarepart[]' id='hardwarepart' value='".$GetHard['mst_asmbly_cat_id']."' />";
$data3.="<input type='hidden' name='hrditemid[]' id='itemid' value='".$GetHard['mst_asmbly_item_id']."' /></td>";
$data3.="<td class='tabledata'>".$s->getField('item_master','id',$itemid,'StockID')."</td>";
$data3.="<td class='tabledata' style='white-space:nowrap'>".$s->getField('item_master','id',$itemid,'item_name')."</td>";
$data3.="<td class='tabledata'><input type='text' name='hrd_qty1[]'  id='hrdstock_qty21_".$z."' style='width:50px;' onkeyup='calcValue(this.value,".$z.")' value='".$totalqty1['totalqty']."'  /></td>";
$data3.="<td class='tabledata'><input type='text' name='hrd_qty[]'  id='hrdstock_qty2_".$z."' style='width:50px;' onkeyup='calcValue(this.value,".$z.")' value='".$totaproductqty."'  /></td>";
$data3.="<td class='tabledata'>".$s->getField('unitsofmeasure','unitid',$unit1,'unitname')."</td>";


$data3.="<td class='tabledata'><input type='text' name='hrd_price[]'  id='hrdstock_price1_".$z."' value='".$GetHard['asmbly_basicrate']."' style='width:50px;'/></td>";
$data3.="<td class='tabledata'><input type='text' name='hrd_perunit[]' onfocus='filldefaultAmt(this.value,this.id)' id='hrdstock_perunit1_".$z."' style='width:30px;' value='1' /></td>";
$total_amt=round(($totaproductqty*$GetHard['asmbly_basicrate'])/1,3);

$data3.="<td class='tabledata'><input type='text' name='hrd_value[]'  id='hrdstock_value1_".$z."' style='width:60px;' onfocus='CalculateTotalLeadAmt2(this.value,".$z.")'  value='".$total_amt."'/></td>";
$data3.="<td class='tabledata'>";
$data3.="<select style='width:80px;' name='hrdPerAmo[]' id='hrdPerAmo1_".$z."' >";
$data3.="<option value='Amount' >Amount</option>";
$data3.="<option value='PercentAmt'>Percent Amt</option>";
$data3.="</select>";
$data3.="</td>";
$discountTotal_amt=$total_amt;

$exciable_amt=$s->getField('item_master','id',$itemid,'item_exciable');
if($exciable_amt=='Y')
{

$totalexciableamt=$totalexciableamt+$discountTotal_amt;
}
else
{
$totalnonexciableamt=$totalnonexciableamt+$discountTotal_amt;
}
$data3.="<td class='tabledata'><input type='text' style='width:50px;' name='hrdDiscount[]' id='hrdDiscount1_".$z."' onkeyup='DiscountAmount2(this.value,".$z.")' value='0' /><input type='hidden' name='itemtye[]' id='itemtype_".$z."' value='".$exciable_amt."'></td>";
$data3.="<td class='tabledata'><input type='text' style='width:50px;' name='hrdperc[]' id='hrdperc1_".$z."'  value='0' readonly='true'  /></td>";
$data3.="<td class='tabledata'><input type='text' style='width:70px;' name='hrdvalue_dis[]' id='hrdvalue_dis1_".$z."'  readonly='true' value='".$discountTotal_amt."'  /></td>";
$data3.="<td class='tabledata'><input type='checkbox' name='addhrdAmt[]' value='0' id='addhrdAmt_".$z."' 
onclick='showrecordsdata(".$z.")'  /></td>";
$data3.="</tr>";
 $k++;$z++;} 
$data3.="</table>";
$data3.="<input type='hidden' name='hrdcount1' id='hrdcount1' value='".$z."'>";

$data4=$totalexciableamt;
$data5=$totalnonexciableamt;


echo $data=$data1."__".$data2."__".$data3."__".$data4."__".$data5;

