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

global $db;
$bomex = explode(",", $_REQUEST['bomitem']);
if($bomex[count($bomex)-1]==''){
	$bomsize = count($bomex)-1;
}else{
	$bomsize = count($bomex);
}
for($ii=0; $ii<$bomsize; $ii++){
	$bomex1 = explode("__", $bomex[$ii]);
	//echo $billsql="SELECT id FROM bill_material where part_id='".$bomex1[0]."' AND product_size='".$bomex1[1]."'";
	$billsql="SELECT id FROM bill_material where part_id='".$bomex1[0]."'";
	$billrs=DB_query($billsql,$db);
	$billrow=DB_fetch_array($billrs);
	
	$billid=$billrow['id'];
	
	$billname=$s->getField('item_master','id',$bomex1[0],'item_name');?>
	<div style="text-align:left; font-size:14px; font-weight:bold;"><?php echo $billname;?>(<?php echo $s->getField('item_master','id',$bomex1[0],'StockID');?>)</div>
	<table width="100%" align="center" border="0" cellpadding="2" cellspacing="2" style="border:1px solid #666666; background:#999999">
	<tr bgcolor="#FFFFFF">
		<th colspan="2">Item Name (Item Code)</th>
		<th colspan="2" style="text-align:center">Quantity</th>
		<th width="8%">Rate</th>
		<th width="8%">Amount</th>
    </tr>
	<?php 
	
	$getAreaFromSize=DB_fetch_array(DB_query("Select frame,frame_uom,frame_category,sutter,shutter_uom,shutter_category from size_master where id=".$bomex1[1],$db));
	//echo "<br />".$getAreaFromSize;

	$frame_area=$getAreaFromSize['frame'];
	$frame_cat=$getAreaFromSize['frame_category'];
	$frame_uom=$getAreaFromSize['frame_uom'];
	$shutter_area=$getAreaFromSize['sutter'];
	$shutter_cat=$getAreaFromSize['shutter_category'];
	$shutter_uom=$getAreaFromSize['shutter_uom'];
	
	

	$sql4="SELECT * FROM bill_raw_material where bill_material_id='".$billid."'  ";
	$sql5=DB_query($sql4,$db);
	if(DB_num_rows($sql5)>0){
	while($query1=DB_fetch_array($sql5)) 
	{	
		$bom1=$s->getField('bill_material','part_id',$query1['mst_itemid'],'id');
		
		$sub_cat=$s->getField('item_master','id',$query1['mst_itemid'],'SubCategoryID');
		if($sub_cat==$frame_cat)
		{$qty_as_per_door=$frame_area; $uom=$frame_uom;}
		elseif($sub_cat==$shutter_cat)
		{$qty_as_per_door=$shutter_area; $uom=$shutter_uom;}
		
		
	?>	
	 <tr bgcolor="#FFFFFF">
			<td colspan="2"><img src="images/bent-arrow1.jpg" height="20" width="15" />&nbsp;<?php 
			echo $s->getField('item_master','id',$query1['mst_itemid'],'item_name');?>(<?php echo $s->getField('item_master','id',$query1['mst_itemid'],'StockID');?>)</td>
			<td width="8%"><?php 
			if($uom==$s->getField('item_master','id',$query1['mst_itemid'],'mst_unitid'))
			{echo $qty_as_per_door."&nbsp;".$s->GetFieldValue('unitsofmeasure',"unitid='".$uom."'",'unitname');}
			else{			
			echo "(".$query1['bom_qty'].")&nbsp;&nbsp;"; echo $query1['bom_qty']*$bomex1[2]; echo "&nbsp;"; echo $s->GetFieldValue('unitsofmeasure',"unitid='".$s->getField('item_master','id',$query1['mst_itemid'],'mst_unitid')."'",'unitname'); }?></td>

			<td width="8%"><?php 
			//echo $uom.$s->getField('item_master','id',$query1['mst_itemid'],'conv_unit')."<br/>";
			if($uom==$s->getField('item_master','id',$query1['mst_itemid'],'conv_unit'))
			{echo $qty_as_per_door."&nbsp;".$s->GetFieldValue('unitsofmeasure',"unitid='".$uom."'",'unitname');}
			else{
			echo "(".$query1['bom_sec_qty'].")&nbsp;&nbsp;"; echo $query1['bom_sec_qty']*$bomex1[2]; echo "&nbsp;"; echo $s->GetFieldValue('unitsofmeasure',"unitid='".$s->getField('item_master','id',$query1['mst_itemid'],'conv_unit')."'",'unitname'); }?></td>
			<td><?php echo $query1['bom_costlanded_price'];?></td>
			<td><?php echo "(".$query1['bom_amount'].")&nbsp;&nbsp;"; echo $query1['bom_qty']*$bomex1[2]*$query1['bom_costlanded_price'];;?></td>
	  </tr>
	<?php	
		if($bom1!='')								
     	{	$spacebar = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    		materialshow2($bom1,$query1['product_size1'],$spacebar);
    	}	
	}
	}else{ ?>
	<tr>
				<td colspan="9">No BOM for This product.</td>
			  </tr>
	<?php }?>
	</table>
	<br /><br />
	<?php	
					
}
?>
 
