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

	
$size1=explode(",",$_REQUEST['size']);
$itemarray=explode(",",$_REQUEST['itemarray']);	
$qtyarray=explode(",",$_REQUEST['qty']);
for($t=0;$t<count($size1);$t++)
{
	$item_array[$t]['id']=$size1[$t];
	$item_array[$t]['size']=$itemarray[$t];
	$item_array[$t]['qty']=$qtyarray[$t];
	
	
}
//echo "SELECT * FROM `cstest_estimation_bom_details` where master_id='".$_REQUEST['SID']."'";
$sql4=DB_query("SELECT * FROM `cstest_estimation_bom_details` where master_id='".$_REQUEST['SID']."'",$db);
$kk=0;
while($row4=DB_fetch_array($sql4))
{
	$item_array[$kk]['id']=$row4['mst_itemid'];
	$item_array[$kk]['size']=$row4['item_size'];
	$item_array[$kk]['qty']=$row4['item_prim_qty'];
	$kk++;
}

$tbodyid="'dataTable1'";
$hardwaretable='<p align="left"><input type="button" onclick="addRow1('.$tbodyid.')" value="Add More" id="addmore" class="search" /></p><table width="100%" cellpadding="2" cellspacing="2">
<tr class="row1">
  <td colspan="3" rowspan="2" class="tabledata"><strong>Assembly Details</strong></td>
<td  align="right" class="tabledata"><strong>Item </strong></td>';

foreach($item_array as $key=>$v)
   {

   $hardwaretable.='<td class="tabledata" colspan="2" style="font-size:11px;">'.$s->GetFieldValue('item_master',"id='".$v['id']."'",'item_name').'<input type="hidden" name="mst_itemidasmbly['.$key.']" value="'.$v['id'].'"/><input type="hidden" name="item_size['.$key.']" value="'.$v['size'].'" id="item_size_'.$key.'"/></td>';
  
   }
  $hardwaretable.='</tr><tr class="row1">
  	<td align="right" class="tabledata"><strong>Size </strong></td>';
	foreach($item_array as $key=>$v)
   {

 $hardwaretable.='<td class="tabledata" colspan="2" style="font-size:11px;">'.$s->GetFieldValue('size_master',"id='".$v['size']."'",'size').'</td>';
 } 	





$hardwaretable.='</tr>';








/////////////////////Assembly Items like frame and shutter///////////////////////////
$hardwaretable.='<tr class="row1">
<td class="tabledata"><strong>Sub Group</strong></td>
<td class="tabledata"><strong>Items</strong></td>
<td class="tabledata"><strong>Basic Rate</strong></td>
<td class="tabledata"><strong>UOM</strong></td>'; 
for($i=0; $i<count($item_array); $i++)
{
$hardwaretable.='<td class="tabledata"><strong>Qty</strong></td>
<td class="tabledata"><strong>Amt</strong></td>';
}
$hardwaretable.='</tr>';


$hardwaretable.='<tbody id="dataTable1">';

$assemamt=array();
  $item2=array(); 
  $items=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['SID']."'",$db);
  while($item1=DB_fetch_array($items)) 	  {	  $item2[]=$item1['id'];	  }   $itemsdata=implode("','",$item2);
  
 
  $assem=DB_query("select distinct(mst_asmbly_cat_id) from cstest_estimation_bom_details_asmbly 
			  where details_mst_id in('".$itemsdata."') and 
			  hardware_flag=0",$db);
			  
$t=0;			  
 while($assem_row=DB_fetch_array($assem))
  {
   
	  $itemid=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'
	   and  details_mst_id in('".$itemsdata."')",'mst_asmbly_item_id'); 
  
	   $basic=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'
	   and  details_mst_id in('".$itemsdata."')",'asmbly_basicrate'); 
   
	
		   $sql_4=DB_query("SELECT * FROM `cstest_estimation_bom_details_asmbly` 
			where details_mst_id in('".$itemsdata."') and   hardware_flag=0 
			and mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'",$db);
			
	    $kk1=0;
		while($row_4=DB_fetch_array($sql_4))
		{
			$item_array1[$kk1]['asmbly_item_qty']=$row_4['asmbly_item_qty'];
			$item_array1[$kk1]['asmbly_item_amt']=$row_4['asmbly_item_amt'];
			$kk1++;
		}
		
		
		$unit= $s->getField('item_master','id',$itemid,'mst_unitid');	 

		
		
		
	if($t==0)
	 {	
		
$hardwaretable.='<tr class="row1">
<td>';
$maincatsql=DB_fetch_array(DB_query("Select id from stockmaincategory where stockmaincategoryid='FG' and CompanyId=".$_SESSION['ACGCompanyId'],$db));
$catsql=DB_query("Select * from stockcategory where stockmaincategory=".$maincatsql['id'],$db);
$hardwaretable.='<select name="itemsubcat[]" id="itemsubcat" onchange="getItemList(this.value,this.id)" style="width:120px">';
while($catrow=DB_fetch_array($catsql))
{
$g='';
if($catrow['id']==$assem_row['mst_asmbly_cat_id'])

$g='selected="selected"';

	$hardwaretable.='<option value="'.$catrow['id'].'" '.$g.'  >'.$catrow['categorydescription'].'</option>';	
}
$hardwaretable.='</select></td>
<td><select name="itemdata[]" id="item" onchange="itemBomDetails(this.value,this.id)">
<option value="">Select Item</option>';
$catsql1=DB_query("Select * from item_master where id=".$itemid,$db);
while($catrow1=DB_fetch_array($catsql1))
{

$hardwaretable.='<option value="'.$catrow1['id'].'" selected="selected"   >'.$catrow1['item_name'].'</option>';	
}


$hardwaretable.='</select></td>
<td><input type="text" name="basicrateA[]" id="basicrateA" style="width:50px;" readonly value="'.$basic.'" /></td>
<td><input type="text" name="" id="uomA" style="width:50px;" readonly value="'.$s->getField('unitsofmeasure','unitid',$unit,'unitname').'" /></td>'; 

for($i=0; $i<count($item_array); $i++)
{
if($assemamt[$i]==$i)
{
$assemamt[$i][]=$assemamt[$i]+$item_array1[$i]['asmbly_item_amt'];
}
else
{
$assemamt[$i][]=$item_array1[$i]['asmbly_item_amt'];
}

$hardwaretable.='<td><input type="text" name="asmbly_qty[0]['.$i.']" id="asmbly_item_qtyA_'.$i.'" 
value="'.$item_array1[$i]['asmbly_item_qty'].'" readonly  /></td>
				 <td><input type="text" name="asmbly_amt[0]['.$i.']" id="asmbly_item_amtA_'.$i.'" 
				 value="'.$item_array1[$i]['asmbly_item_amt'].'" readonly /></td>';
}
$hardwaretable.='<input type="hidden" value="'.$i.'" id="fassmblycnt" /></tr>';



}
else
{
$hardwaretable.='<tr class="row1">
<td>';
$maincatsql=DB_fetch_array(DB_query("Select id from stockmaincategory where stockmaincategoryid='FG' and CompanyId=".$_SESSION['ACGCompanyId'],$db));
$catsql=DB_query("Select * from stockcategory where stockmaincategory=".$maincatsql['id'],$db);
$hardwaretable.='<select name="itemsubcat[]" id="itemsubcat_'.$t.'" onchange="getItemList(this.value,this.id)" style="width:120px">';
while($catrow=DB_fetch_array($catsql))
{
$g='';
if($catrow['id']==$assem_row['mst_asmbly_cat_id'])

$g='selected="selected"';

	$hardwaretable.='<option value="'.$catrow['id'].'" '.$g.'  >'.$catrow['categorydescription'].'</option>';	
	
}
$hardwaretable.='</select></td>
<td><select name="itemdata[]" id="item_'.$t.'" onchange="itemBomDetails(this.value,this.id)">
<option value="">Select Item</option>';
$catsql1=DB_query("Select * from item_master where id=".$itemid,$db);
while($catrow1=DB_fetch_array($catsql1))
{



	$hardwaretable.='<option value="'.$catrow1['id'].'" selected="selected"   >'.$catrow1['item_name'].'</option>';	
}

$hardwaretable.='</select></td>

<td><input type="text" name="basicrateA[]" id="basicrateA_'.$t.'" style="width:50px;" readonly value="'.$basic.'" /></td>
<td><input type="text" name="" id="uomA_'.$t.'" style="width:50px;" readonly  value="'.$s->getField('unitsofmeasure','unitid',$unit,'unitname').'"   /></td>'; 
for($i=0; $i<count($item_array); $i++)
{
if($assemamt[$i]==$i)
$assemamt[$i][]=$assemamt[$i]+$item_array1[$i]['asmbly_item_amt'];
else
$assemamt[$i][]=$item_array1[$i]['asmbly_item_amt'];

$hardwaretable.='<td><input type="text" name="asmbly_qty['.$t.']['.$i.']" id="asmbly_item_qtyA_'.$i.'_'.$t.'" 
value="'.$item_array1[$i]['asmbly_item_qty'].'" readonly  /></td>
<td><input type="text" name="asmbly_amt['.$t.']['.$i.']" id="asmbly_item_amtA_'.$i.'_'.$t.'" 
value="'.$item_array1[$i]['asmbly_item_amt'].'" readonly /></td>';
}
$hardwaretable.='<input type="hidden" value="'.$t.'" id="fassmblycnt" /></tr>';


}

$t++; }
	

$hardwaretable.='</tbody>';
$hardwaretable.='<tr class="row1"><td><b>Sub Total (A)</b></<td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
for($i=0; $i<count($item_array); $i++)
{

$value=array_sum($assemamt[$i]);
if(!empty($assemamt[$i]))
$value1=$value;
else
$value1=0;

$hardwaretable.='<td>&nbsp;</td>
				 <td class="tabledata"><input type="text" name="total[]" id="totalA_'.$i.'" value="'.$value1.'" readonly /></td>';
}
$hardwaretable.='<tr>';


//////////////////////////////////////////////////////////////


//onfocus="calculateAllAmt(this.id)"






//////////////////Hardware Items/////////////////////////////
$coun= count($item_array);
  $coun1=($coun*2)+4;

$hardwaretable.='<tr class="row1"><td class="tabledata" colspan='.$coun1.'><strong>Sub Assembly Details</strong></td></tr>';


$hardwaretable.='<tr class="row1">
<td class="tabledata"><strong>Sub Group</strong></td>
<td class="tabledata"><strong>Items</strong></td>
<td class="tabledata" colspan=2><strong>Basic Rate</strong></td>'; 
for($i=0; $i<count($item_array); $i++)
{
$hardwaretable.='<td class="tabledata"><strong>Qty</strong></td>
<td class="tabledata"><strong>Amt</strong></td>';
}
$hardwaretable.='</tr>';

 
		  
  


$k=0;
 $item2=array(); 
  $hrdamt=array();
  $items=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['SID']."'",$db);
		  while($item1=DB_fetch_array($items))
		  {
		  $item2[]=$item1['id'];
		  }
           $itemsdata=implode("','",$item2);
  
 
		  $assem=DB_query("select distinct(mst_asmbly_cat_id) from cstest_estimation_bom_details_asmbly 
		  where details_mst_id in('".$itemsdata."') and 
		  hardware_flag=1",$db);
  while($assem_row=DB_fetch_array($assem))
  {
   
	   $itemid=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'
	   and  details_mst_id in('".$itemsdata."')",'mst_asmbly_item_id'); 
	  
	   $basic=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'
	   and  details_mst_id in('".$itemsdata."')",'asmbly_basicrate'); 
	   
	   //$unit=$s->GetFieldValue('cstest_estimation_bom_details_asmbly',"mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'
	  // and  details_mst_id in('".$itemsdata."')",'mst_asmbly_unit_id'); 
  
		   $sql_4=DB_query("SELECT * FROM `cstest_estimation_bom_details_asmbly` 
			where details_mst_id in('".$itemsdata."') and   hardware_flag=1 
			and mst_asmbly_cat_id='".$assem_row['mst_asmbly_cat_id']."'",$db);
	$kk1=0;
			while($row_4=DB_fetch_array($sql_4))
			{
				$item_array1[$kk1]['asmbly_item_qty']=$row_4['asmbly_item_qty'];
				$item_array1[$kk1]['asmbly_item_amt']=$row_4['asmbly_item_amt'];
				$kk1++;
			}


//$ProdManuSQL=DB_query("SELECT * FROM product_manufacture where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
//while($prodManuf=DB_fetch_array($ProdManuSQL)){




  $hardwaretable.='<tr class="row1">
    <td>'.$s->getField('product_manufacture','id',$assem_row['mst_asmbly_cat_id'],'manufacturer').'<input type="hidden" name="mst_asmbly_cat_id['.$k.']" id="manufac" value="'.$assem_row['mst_asmbly_cat_id'].'" /></td>
    <td>';
	$GetItemsSQL=DB_query("select * from item_master where make='".$assem_row['mst_asmbly_cat_id']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	$hardwaretable.='<select style="width:150px;" name="mst_asmbly_item_id['.$k.']" id="manufact_'.$k.'" onchange="showbascicamt(this.value,this.id)">
      <option value="">--Select--</option>';
	  while($GetItemsFors=DB_fetch_array($GetItemsSQL))
	  {
	  
		$l='';
		if($GetItemsFors['id']==$itemid)
		
		$l='selected="selected"';

	  $unit= $s->getField('item_master','id',$itemid,'mst_unitid');	
	  
	  
	  $hardwaretable.='<option value="'.$GetItemsFors['id'].'" '.$l.'    >'.$GetItemsFors['item_name'].'</option>';
	  }
	  $hardwaretable.='</select></td>
	<td ><input type="text" name="asmbly_basicrate['.$k.']" id="basicrate_'.$k.'" style="width:50px;" value="'.$basic.'" /></td>
	<td ><input type="text" name="" id="uomaT_'.$k.'" style="width:50px;" readonly  value="'. $s->getField('unitsofmeasure','unitid',$unit,'unitname').'"  /></td>';
	for($i=0; $i<count($item_array); $i++)
		{
		if($hrdamt[$i]==$i)
		{ 	$hrdamt[$i][]=$hrdamt[$i]+$item_array1[$i]['asmbly_item_amt'];}
		else {	$hrdamt[$i][]=$item_array1[$i]['asmbly_item_amt'];	}
   
		
		
		$hardwaretable.='<td><input type="text" name="asmbly_item_qty['.$k.']['.$i.']" id="asmbly_item_qty_'.$k.'_'.$i.'" 
		value="'.$item_array1[$i]['asmbly_item_qty'].'" onkeyup="calcItemAmt1(this.value,'.$k.','.$i.')" onblur="calcItemAmt1(this.value,'.$k.','.$i.')"/></td>
							<td><input type="text" name="asmbly_item_amt['.$k.']['.$i.']" id="asmbly_item_amt_'.$k.'_'.$i.'" value="'.$item_array1[$i]['asmbly_item_amt'].'" readonly /></td>'; 	
		}
  $hardwaretable.='</tr>';
  $k++;
  }
 
  $hardwaretable.='<tr class="row1">
  <td><b>Sub Total (B)</b></td>
  <td></td>
  <td colspan=2></td>';
  for($i=0; $i<count($item_array); $i++)
  {
  		$hardwaretable.='<td></td>
						<td class="tabledata"><input type="text" name="" 
						value="'.array_sum($hrdamt[$i]).'" id="total_hw_amt_id_'.$i.'" readonly /></td>';	
  }
  $hardwaretable.='</tr>';
  $hardwaretable.='<tr class="row1">
  <td colspan="2"><b>Total Amount(C=A+B)</b></td>
  <td colspan=2></td>';
  for($i=0; $i<count($item_array); $i++)
  {
  $sum=array_sum($hrdamt[$i])+array_sum($assemamt[$i]);
  		$hardwaretable.='<td></td>
						<td class="tabledata"><input type="text" name="" 
						value="'. $sum.'" id="totalC_'.$i.'" readonly/></td>';	
  }
  $hardwaretable.='</tr>';
   $hardwaretable.='<tr class="row1">
  <td colspan="2"><b>Grand Total (D=C*Q)</b></td>
  <td colspan=2></td>';
  for($i=0; $i<count($item_array); $i++)
  { 
 

  $totalamt[]=(array_sum($hrdamt[$i])+array_sum($assemamt[$i]))*($item_array[$i]['qty']);
	 $sum1=(array_sum($hrdamt[$i])+array_sum($assemamt[$i]))*($item_array[$i]['qty']);
  		$hardwaretable.='<td></td>
						<td class="tabledata"><input type="text" name="" value="'.$sum1.'" id="totalD_'.$i.'" readonly/>
						<input type="hidden" name="totalqtyabc" id="totalqtyabc_'.$i.'" value="'.$item_array[$i][qty].'" ></td>';	
  }
  $hardwaretable.='</tr>';
  
  $hardwaretable.='<input type="hidden" id="count" value="'.$k.'" name="counthw" /></table>';
  echo $hardwaretable;
	

?>