<?php //session_name('CAKEPHP'); session_start();
$PageSecurity = 4;
include('includes/session.inc');
include('includes/function_lib.php');
global $db;
?>
<?php
if($_REQUEST['lead']!='')//Getting lead details into the estimation sheet (Manvi 23-04-2013)
{
	$sql1=DB_fetch_array(DB_query("Select leads_date, lead_Category from crm_leads where leadid=".$_REQUEST['lead']."",$db));
	$sql2=DB_fetch_array(DB_query("Select name from type_master where id = ".$sql1['lead_Category']."",$db));
	$sql3=DB_query("SELECT * FROM `crm_lead_items` where mst_leadid=".$_REQUEST['lead']."",$db);
	$num_of_items=DB_num_rows($sql3);
	//$itemdata='';
	$itemdata.=
	'<table width="100%" align="center" border="0" cellpadding="2" cellspacing="1" id="div_items" style="border:1px solid #666666; background:#999999">
  		<tr class="row1">
		<td width="5%" class="tabledata"><input name="bompro" type="checkbox" id="bompro" onclick="LeadData()" /></td>
    	<td width="8%" class="tabledata"><strong>Item Code</strong></td>
    	<td width="24%" class="tabledata"><strong>Item Name</strong></td>
    	<td width="10%" class="tabledata"><strong>Size</strong></td>
    	
		<td width="5%" class="tabledata" colspan="4" style="text-align:center"><strong>Quantity</strong></td>
    	
		<td width="8%" class="tabledata" colspan="2" style="text-align:center"><strong>Unit Rate</strong></td>
    	<td width="8%" class="tabledata"><strong>Amount</strong></td>
  	</tr>';
	$i=0;
	while($row3=DB_fetch_array($sql3))
	{
		$item_code=$s->GetFieldValue('item_master',"id='".$row3['item_code']."'",'StockID');
		$item_name=$s->GetFieldValue('item_master',"id='".$row3['item_code']."'",'item_name');
		$unit=$s->GetFieldValue('unitsofmeasure',"unitid='".$s->GetFieldValue('item_master',"id='".$row3['item_code']."'",'mst_unitid')."'",'unitname');
		$area=$s->GetFieldValue('size_master',"id='".$row3['size']."'",'area');
		$total_area=$row3['stock_qty']*$area;
		
		
		
		
		$secondaryunit=$s->GetFieldValue('unitsofmeasure',"unitid='".$s->GetFieldValue('item_master',"id='".$row3['item_code']."'",'conv_unit')."'",'unitname');
		
		//If the item has a BOM created for itself, the total production rate (according to the size) will be fetched as rate in this table
		//$sql5=DB_query("Select total_production_rate from bill_material where part_id=".$row3['item_code']." and product_size=".$row3['size']."",$db);
		$sql5=DB_query("Select total_production_rate,uom from bill_material where part_id=".$row3['item_code']."",$db);
		$num5=DB_num_rows($sql5);
		if($num5>0)
		{
			$row5=DB_fetch_array($sql5);
			$rate1=$row5['total_production_rate'];
			$amount1=$row3['stock_qty']*($rate1*$area);
			$rate=$rate1.'<input type="hidden" name="item_rate[]" id="item_rate_id_'.$i.'" value="'.$rate1.'" readonly style="width:70px"/>';
			$amount=$amount1.'<input type="hidden" name="item_amount[]" id="item_amount_id_'.$i.'" value="'.$amount1.'" readonly style="width:70px" />';
			$bom_unit=$s->GetFieldValue('unitsofmeasure',"unitid='".$row5['uom']."'",'unitname');
		}
		else
		{
			$rate1='';
			$amount1='';
			$rate='<input type="text" name="item_rate[]" id="item_rate_id_'.$i.'" value="" readonly style="width:70px"/>';
			$amount='<input type="text" name="item_amount[]" id="item_amount_id_'.$i.'" value="" readonly style="width:70px" />';
		}
		
		//Functions appplied on rate text box. Not required as of now as text box has to be readonly.
		//onkeyup="calculateItemAmount1('.$i.',this.value)" onblur="calculateItemAmount1('.$i.',this.value)" 
		
		$itemdata.="<tr class='row1'>
					<td><input name='' id='bompro_".$i."' type='checkbox' value='".$row3['item_code']."'><input name='mst_itemid[]' id='bompro_".$i."' type='hidden' value='".$row3['item_code']."'></td>
					<td>".$item_code."</td>
    				<td>".$item_name."</td>
    				<td>
						<select name='item_size[]' id='bomsize_".$i."' onchange='getAreaAndCalculate(".$i.",this.value)' style='width:100%'>";
			$sql4=DB_query("Select * from size_master where id=".$row3['size'],$db);
			while($row4=DB_fetch_array($sql4))
			{
				if($row3['size']==$row4['id'])
				$selected="selected"; else $selected="";
				$itemdata.="<option value='".$row4['id']."' ".$selected.">".$row4['size']."</option>";	
			}
			
			$qty_array[]=$row3['stock_qty'];
			$sec_qty_array[]=$total_area;
			$amount_array[]=$amount1;
			
		$itemdata.="</select>	
					</td>
    				<td style='text-align:right'>".$row3['stock_qty']."<input type='hidden' name='item_prim_qty[]' id='item_qty_id_".$i."' value='".$row3['stock_qty']."' onkeyup='calculateItemAmount2(".$i.",this.value)' readonly style='width:70px'/></td>
					<td>".$unit."</td>
    				<td style='text-align:right'>".$total_area."<input type='hidden' name='item_sec_qty[]' id='secqty_".$i."' value='".$total_area."' readonly style='width:70px'/></td>
					<td>".$secondaryunit."</td>
					<td style='text-align:right'>".($rate1*$area)."/".$unit."</td>
    				<td style='text-align:right'>".$rate1."/".$bom_unit."</td>
    				<td style='text-align:right'>".$amount."</td>
					</tr>";
		$i++;
	}
	$itemdata.="<input type='hidden' id='bomproid' value='".$i."' name='countitems' ></table>";
	$itemdata.="<tr bgcolor='#FFFFFF'>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th>Total</th>
	<th style='text-align:right'>".array_sum($qty_array)."<input type='hidden' name='primary_qty' value='".array_sum($qty_array)."' /></th>
	<th></th>
	<th style='text-align:right'>".array_sum($sec_qty_array)."<input type='hidden' name='secondary_qty' value='".array_sum($sec_qty_array)."' /></th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th style='text-align:right'>".array_sum($amount_array)."<input type='hidden' name='grossamt' value='".array_sum($amount_array)."' /></th>
	</tr>";

$sql4=DB_query("SELECT * FROM `crm_lead_items` where mst_leadid=".$_REQUEST['lead']."",$db);
$kk=0;
while($row4=DB_fetch_array($sql4))
{
	$item_array[$kk]['id']=$row4['item_code'];
	$item_array[$kk]['size']=$row4['size'];
	$kk++;
}


	$hardwaretable='<table width="100%" cellpadding="2" cellspacing="2">
<tr class="row1">
<td class="tabledata">&nbsp;</td>
<td class="tabledata">&nbsp;</td>
<td class="tabledata">&nbsp;</td>';
foreach($item_array as $key=>$v)
{
$hardwaretable.='<td class="tabledata" colspan="2"><strong>Item - </strong>'.$s->GetFieldValue('item_master',"id='".$v['id']."'",'item_name').'<br /><strong>Size - </strong>'.$s->GetFieldValue('size_master',"id='".$v['size']."'",'size').'<input type="hidden" name="mst_itemidasmbly['.$key.']" value="'.$v['id'].'"/><input type="hidden" name="item_size['.$key.']" value="'.$v['size'].'"/></td>';
}
$hardwaretable.='</tr>
<tr class="row1">
<td class="tabledata"><strong>Hardware</strong></td>
<td class="tabledata"><strong>Items</strong></td>
<td class="tabledata"><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;
$ProdManuSQL=DB_query("SELECT * FROM product_manufacture where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
while($prodManuf=DB_fetch_array($ProdManuSQL)){
  $hardwaretable.='<tr class="row1">
    <td>'.$prodManuf['manufacturer'].'<input type="hidden" name="mst_asmbly_cat_id['.$k.']" id="manufac" value="'.$prodManuf['id'].'" /></td>
    <td>';
	$GetItemsSQL=DB_query("select * from item_master where make='".$prodManuf['id']."'",$db);
	$hardwaretable.='<select style="width:150px;" name="mst_asmbly_item_id['.$k.']" id="manufact_'.$k.'">
      <option value="">--Select--</option>';
	  while($GetItemsFors=DB_fetch_array($GetItemsSQL)){
	  $hardwaretable.='<option value="'.$GetItemsFors['id'].'">'.$GetItemsFors['item_name'].'</option>';
	  }
	  $hardwaretable.='</select></td>
	<td><input type="text" name="asmbly_basicrate['.$k.']" id="basicrate_'.$k.'" style="width:60px;" /></td>';
	for($i=0; $i<count($item_array); $i++)
		{
			$hardwaretable.='<td><input type="text" name="asmbly_item_qty['.$k.']['.$i.']" id="asmbly_item_qty_'.$k.'_'.$i.'" value="" 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="" readonly /></td>'; 	
		}
  $hardwaretable.='</tr>';
  $k++;
  }
  $hardwaretable.='<tr class="row1">
  <td></td>
  <td></td>
  <td></td>';
  for($i=0; $i<count($item_array); $i++)
  {
  		$hardwaretable.='<td>&nbsp;</td>
						<td class="tabledata"><input type="text" name="" value="" id="total_hw_amt_id_'.$i.'" /></td>';	
  }
  $hardwaretable.='</tr>';
  $hardwaretable.='<input type="hidden" id="count" value="'.$k.'" name="counthw" /></table>';
	
	
	
	
	echo ConvertSQLDate($sql1['leads_date'])."__".$sql2['name']."__".$sql1['lead_Category']."__".$itemdata."__".$hardwaretable;
	
}
else{

$SQLtype=DB_query("SELECT * FROM crm_leads where leadid='".$_REQUEST['branch']."'",$db);
$typeAS=DB_fetch_array($SQLtype);
$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['lead_Category'])
$selty='selected';
$data1.="<option value='".$Type['id']."' ".$selty.">".$Type['name']."</option>";
 }
$data1.="</select>";



$data2="";
$data2.="<table align='center' width='100%;' border='1' cellpadding='3' cellspacing='0'>";
$data2.="</tr>";
$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 class='tabledata'>Grade</td>";
$data2.="<td 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.="<td class='tabledata'>Total Stock</td>";
$data2.="</tr>";
$i=0; 
//echo "select * from crm_lead_items where mst_leadid='".$_REQUEST['branch']."' and del_status=0";
$getitem=DB_query("select * from crm_lead_items where mst_leadid='".$_REQUEST['branch']."' and del_status=0",$db);
while($row=DB_fetch_array($getitem))
{
$item_Codeas=$s->GetFieldValue('item_master',"id='".$row['item_code']."'",'StockID');
$item_names=$s->GetFieldValue('item_master',"id='".$row['item_code']."'",'item_name');
$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='deactivate1(".$i.")'><img src='images/delete1.gif' /></a>
		</div></td>";	
$data2.="<td class='tabledata'><input type='hidden' name='item_code[]' id='itemid1_".$i."' style='width:60px;' value='".$row['item_code']."' /></td>";
$data2.="<td class='tabledata'><input type='text' style='width:60px;' value='".$item_Codeas."'  />
<input type='hidden'   id='stockqty1_".$i."'   value='".$row['stock_qty']."'  /></td>";
$data2.="<td class='tabledata' style='white-space:nowrap'><input type='text' style='width:90px;'   id=''  value='".$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($row['size']==$sizeid['id'])
$selsize='selected';
$data2.="<option value='".$sizeid['id']."' ".$selsize.">".$sizeid['size']."</option>";
}
$data2.="</select>";
$data2.="</td>";
$data2.="<td class='tabledata' style='white-space:nowrap'>";
$data2.="<select style='width:100px;' name='grade[]' id='grade'>";
$data2.="<option>--Select--</option>";
$SQLgrade=DB_query("SELECT * FROM product_grade",$db); 
while($gradeid=DB_fetch_array($SQLgrade)){
$selgrad='';
if($row['grade']==$gradeid['id'])
$selgrad='selected';

$data2.="<option value='".$gradeid['id']."' ".$selgrad.">".$gradeid['grade']."</option>";
}
$data2.="</select>";
$data2.="</td>";
$data2.="<td class='tabledata'><input type='text' name='stock_qty[]'  id='stockqty_".$i."' style='width:50px;' value='".$row['stock_qty']."'  onkeyup='calcValue1(this.value,this.id)'/></td>";
$data2.="<td class='tabledata'><input type='text' name='stock_price[]' value='".$row['stock_price']."' 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='".$row['stock_perunit']."'/></td>";
$data2.="<td class='tabledata'><input type='text' name='stock_value[]' value='".$row['stock_value']."' id='stock_value_".$i."' style='width:60px;' onfocus='CalculateTotalLeadAmt(this.value,this.id)'/></td>";
$data2.="<td class='tabledata'><select style='width:80px;' name='PerAmo[]' id='PerAmo_".$i."' >
            <option value='Amount' >Amount</option>
            <option value='PercentAmt'>Percent Amt</option>
          </select></td>";
$data2.="<td class='tabledata'><input type='text' style='width:50px' name='Discount[]' id='Discount_".$i."' onkeyup='DiscountAmount1(this.value,this.id)' /></td>";
$data2.="<td class='tabledata'><input type='text' style='width:50px' name='perc[]' id='perc_".$i."'  readonly='true'  /></td>";
$data2.="<td class='tabledata'><input type='text' style='width:70px' name='value_dis[]' id='value_dis_".$i."'  readonly='true' /></td>";
$data2.="<td class='tabledata'><input type='text' name='total_stock[]' id='total_stock_".$i."' style='width:50px;'  value='".$row['total_stock']."'  readonly='true' /></td>";
$data2.="</tr>";
$i++;}

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

echo $data=$data1."__".$data2;
}
?>