<?php
$PageSecurity = 1;
include('config.php');
include('includes/ConnectDB.inc');
include('includes/function_lib.php');
include('includes/LanguageSetup.php');
//include('includes/DateFunctions.inc');
include('includes/session.inc');


$sql=DB_query("select * from cstest_estimation_bom where id='".$_REQUEST['viewid']."'",$db);
$row=DB_fetch_array($sql);
extract($row);


 ?>
 <script type="text/javascript" language="javascript">
  function toggle_visibility(id) {
       var e = document.getElementById("print");
	       e.style.display = 'none';
		       //alert(e.style.display);
			   window.print();
    }
</script>

<style>
/*.row1 {
	background-color:#e7e7e7;
	border:#b6b6b6 solid 1px;
	height:30px;
}*/
.tdf11{font-family:Arial; font-size:11px; color:#000000;}

.tdf12{font-family:Arial; font-size:12px; color:#000000;}

.border1{border-bottom:#666666 1px solid; border-right:#666666 1px solid;}

.border2{border-bottom:#666666 1px solid;}

</style> 

<link href="css/dashboard1.css" rel="stylesheet" type="text/css" />	
<link href="css/new_mxerp.css" rel="stylesheet" type="text/css" />




<center> <h2>Estimation Sheet Against BOM </h2>
</center>


<table width="95%" align="center" border="0" cellpadding="3" cellspacing="0" style="border:solid 1px #666666; border-bottom:none;">
  <tr class="row1">
    <td width="13%" class="border1 tdf12"><strong>Branch</strong></td>
    <td width="20%" class="tdf11 border1"><?php echo $s->getField('locations','loccode',$branch,'loccode') ; ?></td>
    <td width="13%" class="tdf12 border1"><strong>Sheet No.</strong></td>
    <td width="20%" class="tdf11 border1"><?php echo $sheet_no; ?></td>
    <td class="border1 tdf12"><strong>Date</strong></td>
    <td width="20%" class="tdf11 border2"><?php echo  ConvertSQLDate($sheet_date); ?></td>
  </tr>
  <tr class="row1">
  <td class="tdf12 border1"><strong>Against</strong></td>
    <td class="tdf11 border1"><?php echo  $against; ?></td>
  	<td class="tdf12 border1"><strong>Party </strong></td>
    <td class="tdf11 border1"><?php echo $s->getField('clientmaster','id',$client,'c_name'); ?></td>
	
	<td class="tdf12 border1"><strong>Lead/Ref. No.</strong></td>
    <td class="tdf11 border2" id="div_lead"><?php echo $s->getField('crm_leads','leadid',$lead_ref,'leadcode'); ?></td>
  </tr>
  <tr class="row1">
  	<td id="test" class="border1 tdf12"><strong>Lead/Ref. Date</strong></td>
    <td class="tdf11 border1"><?php  echo ConvertSQLDate($lead_ref_date); ?></td>
    <td class="tdf12 border1"><strong>Type </strong></td>
    <td class="tdf11 border1"><?php echo $s->getField('type_master','id',$type,'name'); ?>	</td>
    <td class="tdf12 border1"><strong>Material Sub Group</strong></td>
    <td class="tdf11 border2"><?php echo $s->getField('stockcategory','id',$sub_group,'categorydescription'); ?></td>
  </tr>
</table>
<br />
<fieldset style="width:93%; margin:auto;">
<legend style="font-size:13px;"><strong>Finished Product Details</strong></legend>

<div>
<table width="100%" align="center" border="0" cellpadding="3" cellspacing="0" id="div_items" style="border:1px solid #666666; border-bottom:none; ">
  <tr class="row1">
  	<td class="tdf12 border1"><strong>Item Code</strong></td>
    <td  class="tdf12 border1"><strong>Item Name</strong></td>
    <td  class="tdf12 border1"><strong>Size</strong></td>
     <td  class="tdf12 border2"  colspan="5"  style="text-align:center" ><strong>Quantity</strong></td>
    
  </tr>
  <?php 
  $i=0;
  $finish_qty=array();
  $finish_primay_qty=array();
  $finish_secondary_qty=array();
  $unit_primary='';
  $unit_secondary='';
  $sql_details=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['viewid']."'",$db);
  while($row1=DB_fetch_array($sql_details))
  {
      if($unit_primary=='')
	  $unit_primary=$s->getField('item_master','id',$row1['mst_itemid'],'mst_unitid');
	  if($unit_secondary=='')
	  $unit_secondary=$s->getField('item_master','id',$row1['mst_itemid'],'conv_unit');
	  $unit=$s->getField('item_master','id',$row1['mst_itemid'],'mst_unitid');
	  $secondunit=$s->getField('item_master','id',$row1['mst_itemid'],'conv_unit');
	  $finish_qty[$i]=$row1['item_prim_qty'];
  
  
  $finish_primay_qty[]=$row1['item_prim_qty'];
  $finish_secondary_qty[]=$row1['item_sec_qty'];
  ?>
  <tr class="row1">
    <td class="tdf11 border1"><?php echo $s->getField('item_master','id',$row1['mst_itemid'],'StockID'); ?></td>
	 <td class="tdf11 border1"><?php echo $s->getField('item_master','id',$row1['mst_itemid'],'item_name'); ?></td>
	   <td class="tdf11 border1"><?php  echo $s->getField('size_master','id',$row1['item_size'],'size'); ?></td>
       	  <td class="tdf11 border1"><?php echo $row1['item_prim_qty']; ?></td>
		   <td class="tdf11 border1"><?php echo $s->getField('unitsofmeasure','unitid',$unit,'unitname'); ?></td>
			 <td class="tdf11 border1"><?php echo $row1['item_sec_qty']; ?></td>
			  <td class="tdf11 border2"><?php echo $s->getField('unitsofmeasure','unitid',$secondunit,'unitname'); ?></td>
			   
  </tr>
  <?php $i++; } ?>
</table>
</div>
</fieldset>
<br />


<table width="95%" border="0" cellpadding="3" cellspacing="0" align="center" style="border:solid 1px #666666; border-bottom:none;">
  <tr class="row1">
    <td colspan="3" rowspan="2" class="tdf12 border1"><strong>Assembly Details</strong></td>
    <td class="tdf12 border1"><strong>Item</strong></td>
	<?php 
	$sql4=DB_query("SELECT * FROM `cstest_estimation_bom_details` where master_id=".$_REQUEST['viewid']."",$db);
	$kk=0;
	while($row4=DB_fetch_array($sql4))
	{
		$item_array[$kk]['id']=$row4['mst_itemid'];
		$item_array[$kk]['size']=$row4['item_size'];
		$kk++;
	}
	
	foreach($item_array as $key=>$v)
   {
?>
    <td class="tdf12 border1" colspan="2" style="font-size:11px; font-family:Arial;"><?php echo  $s->GetFieldValue('item_master',"id='".$v['id']."'",'item_name'); ?></td>
  <?php } ?>
  </tr>
  <tr class="row1">
    <td class="tdf12 border1"><strong>Size</strong></td>
    <?php
	
	foreach($item_array as $key=>$v)
   {
?>
<td class="tdf12 border1" colspan="2"><?php echo $s->GetFieldValue('size_master',"id='".$v['size']."'",'size'); ?> </td>
<?php } ?>	
  </tr>
  <?php 
  
  
  ?>
  <tr class="row1">
    <td class="tdf12 border1"><strong>Sub Group </strong></td>
    <td class="tdf12 border1"><strong>Items</strong></td>
    <td class="tdf12 border1"><strong>Basic Rate</strong></td>
    <td class="tdf12 border1"><strong>UOM</strong></td>
 <strong><?php 	foreach($item_array as $key=>$v)
   { ?></strong>
    <td class="tdf12 border1"><strong>QTY</strong></td><td class="tdf12 border1"><strong>Amount</strong></td>
	<?php } ?>
  </tr>
  
  <?php
  $assemamt=array();
  $item2=array(); 
  $items=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['viewid']."'",$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);
			  
			  
 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=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++;
		}
  
  
  ?>
  <tr class="row1">
    <td class="tdf11 border1"><?php echo $s->getField('stockcategory','id',$assem_row['mst_asmbly_cat_id'],'categorydescription'); ?></td>
    <td class="tdf11 border1"><?php echo $s->getField('item_master','id',$itemid,'item_name'); ?></td>
    <td class="tdf11 border1"><?php echo $basic; ?> </td>
    <td class="tdf11 border1"><?php echo $s->getField('unitsofmeasure','unitid',$unit,'unitname'); ?>&nbsp;</td>
 <?php
 foreach($item_array1 as $key=>$v)
   { 
		if($assemamt[$key]==$key)
		{
		
		$assemamt[$key][]=$assemamt[$key]+$v['asmbly_item_amt'];
		}
		else
		{
		$assemamt[$key][]=$v['asmbly_item_amt'];
		}
   ?>
    <td class="tdf11 border1"><?php echo $v['asmbly_item_qty']; ?>&nbsp;</td><td class="tdf11 border1"><?php echo $v['asmbly_item_amt']; ?>&nbsp;</td>
	<?php } ?>
  </tr>
  
  <?php } ?>
  
  <tr class="row1">
    <td colspan="4" class="tdf12 border1"><strong>Sub Total (A) </strong> </td>
    <?php 
 
foreach($item_array1 as $key=>$v)
  { 
   
   ?>
    <td class="tdf12 border1" >&nbsp;</td><td class="tdf12 border1" ><?php echo  array_sum($assemamt[$key]); ?>&nbsp;</td>
	<?php } ?>
  </tr>
  

<tr class="row1">
 
   
	<?php 
 $coun= count($item_array1);
  $coun1=($coun*2)+4;
 ?>
  <td colspan="<?php echo $coun1; ?>" class="tdf12 border1"  ><strong>Sub Assembly Details</strong></td>
  </tr>
 
 
  <tr class="row1">
    <td class="tdf12 border1"><strong>Sub Group </strong></td>
    <td class="tdf12 border1"><strong>Items</strong></td>
    <td class="tdf12 border1"><strong>Basic Rate</strong></td>
    <td class="tdf12 border1"><strong>UOM</strong></td>
 <?php 
foreach($item_array as $key=>$v)
   {
    ?>
    <td class="tdf12 border1"><strong>QTY</strong></td><td class="tdf12 border1"><strong>Amount</strong></td>
	<?php } ?>
  </tr>
  
  <?php
  $item2=array(); 
  $hrdamt=array();
  $items=DB_query("select * from cstest_estimation_bom_details where master_id='".$_REQUEST['viewid']."'",$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++;
			}
		  
  
  ?>
  <tr class="row1">
    <td class="tdf11 border1"><?php echo $s->getField('product_manufacture','id',$assem_row['mst_asmbly_cat_id'],'manufacturer'); ?></td>
    <td class="tdf11 border1"><?php echo $s->getField('item_master','id',$itemid,'item_name'); ?></td>
    <td class="tdf11 border1"><?php echo $basic; ?> </td>
    <td class="tdf11 border1"><?php echo $s->getField('unitsofmeasure','unitid',$unit,'unitname'); ?>&nbsp;</td>
 <?php 
 foreach($item_array1 as $key=>$v)
   { 
       if($hrdamt[$key]==$key)
		{ 	$hrdamt[$key][]=$hrdamt[$key]+$v['asmbly_item_amt'];}
		else {	$hrdamt[$key][]=$v['asmbly_item_amt'];	}
   
   
   ?>
    <td class="tdf11 border1"><?php echo $v['asmbly_item_qty']; ?></td><td class="tdf11 border1"><?php echo $v['asmbly_item_amt']; ?>&nbsp;</td>
	<?php } ?>
  </tr>
  
  <?php } ?>
  <tr class="row1">
    <td colspan="4" class="tdf12 border1"><strong>Sub Total (B) </strong> </td>
    <?php 
foreach($item_array1 as $key=>$v)
{
  ?>
    <td class="tdf12 border1" >&nbsp;</td><td class="tdf12 border1" ><?php echo  array_sum($hrdamt[$key]); ?>&nbsp;</td>
	<?php } ?>
  </tr>
<tr class="row1">
    <td colspan="4" class="tdf12 border1"><strong>Total For Door With H/W (C=A+B)</strong> </td>
    <?php 
foreach($item_array1 as $key=>$v)
   { 
   
   ?>
    <td class="tdf12 border1">&nbsp;</td><td class="tdf12 border1"><?php echo  array_sum($hrdamt[$key])+array_sum($assemamt[$key]); ?>&nbsp;</td>
	<?php } ?>
  </tr>
  <tr class="row1">
    <td colspan="4" class="tdf12 border1"><strong>Grand Total (D=C*(Finished Product Qty))</strong> </td>
    <?php 
 
 	$totalamt=array();
foreach($item_array1 as $key=>$v)
{
 ?>
    <td class="tdf12 border1">&nbsp;</td>
    <td class="tdf12 border1"><strong><?php 
	$totalamt[]=(array_sum($hrdamt[$key])+array_sum($assemamt[$key]))*($finish_qty[$key]);
	echo   (array_sum($hrdamt[$key])+array_sum($assemamt[$key]))*($finish_qty[$key]); ?>&nbsp;</strong></td>
<?php } ?>
  </tr>
</table>






<br />
<table width="95%" border="0" align="center" cellpadding="3" cellspacing="0" style="border:solid 1px #666666; border-bottom:none;">
  <tr>
    <td width="27%" rowspan="2" align="left" class="tdf12 border1"><strong>Entire Quantity</strong></td>
    <td width="15%" align="left" style="padding:0px;" class="border1"><table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td width="50%" class="tdf12" style="border-right:#666666 1px solid;"><?php echo array_sum($finish_primay_qty); ?></td>
    <td class="tdf12"><?php  echo $s->getField('unitsofmeasure','unitid',$unit_primary,'unitname'); ?></td>
  </tr>
</table>
</td>
    <td width="15%" rowspan="3" align="left" class="tdf12 border1"><strong>Remarks</strong></td>
    <td rowspan="3" valign="top" class="tdf12 border2"><?php echo $remark; ?>	</td>
  </tr>
  <tr>
    <td align="left" class="tdf12 border1" style="padding:0px;"><table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td width="50%" class="tdf12" style="border-right:#666666 1px solid;"><?php echo array_sum($finish_secondary_qty); ?></td>
    <td class="tdf12"><?php echo $s->getField('unitsofmeasure','unitid',$unit_secondary,'unitname'); ?></td>
  </tr>
</table></td>
  </tr>
  <tr>
    <td align="left" class="tdf12 border1"><strong>Total Amount for Entire Quantity (&#931;D)</strong></td>
    <td align="left" style="font-size:13px;" class="tdf12 border1"><?php echo array_sum($totalamt); ?></td>
  </tr>
</table>

<div align="center" id="foo"  style="margin-top:20px;">
	<a href="#" onclick="toggle_visibility(this.id)" id="print"><input type="button" value="Print" id="print" /></a>
</div>
