<?php
$page_title='Estimation Sheet BOM';
$prefix=$s->GetFieldValue('prefix_tbl',"prefix_name='".$page_title."'",'prefix_code');
if($_REQUEST['prepare'])
{
	$codeSQL=DB_query("select sheet_no as sheetid from cstest_estimation_bom order by id desc limit 0,1",$db);
	if(DB_num_rows($codeSQL)>0)
	{
	$Code1=DB_fetch_array($codeSQL);
	$exp=explode("/",$Code1['sheetid']);
	$maxval=$exp[1]+1;
	$finalcode=$prefix.'/'.str_pad($maxval,5,"0",STR_PAD_LEFT);
	}
	else
	{
		$finalcode=$prefix.'/'.'00001';
	}
	
	
	$data['branch']=$_REQUEST['site'];
	$data['sheet_no']=$finalcode;
	$data['sheet_date']=FormatDateFORSQL($_REQUEST['sheet_date']);
	$data['against']=$_REQUEST['against'];
	$data['client']=$_REQUEST['client'];
	$data['lead_ref']=$_REQUEST['lead_ref_no'];
	$data['lead_ref_date']=FormatDateFORSQL($_REQUEST['lead_ref_date']);
	$data['type']=$_REQUEST['lead_ref_type_hid'];
	$data['sub_group']=$_REQUEST['sub_group'];
	$data['primary_qty']=$_REQUEST['primary_qty'];
	$data['secondary_qty']=$_REQUEST['secondary_qty'];
	$data['grossamt']=$_REQUEST['grossamt'];
	$data['remark']=$_REQUEST['remark'];
	$data['prepared_by']=$_SESSION['UserID'];
	$data['CompanyId']=$_SESSION['ACGCompanyId'];
	$data['created_date']=date('Y-m-d');
	$data['created_by']=$_SESSION['UserID'];
	echo "<pre> 1 ";
	print_r($data);
	echo "<br>****************<br /> 2 ";
	//$s->RecordInsert('cstest_estimation_bom',$data);
	//$getlastid1=DB_Last_Insert_ID($db,'cstest_estimation_bom', 'id');
	
		for($j=0;$j<$_REQUEST['countitems'];$j++)
		{
			//if($_REQUEST['mst_itemid'][$j]!=''){
				$datahard['master_id']=$getlastid1;
				$datahard['mst_itemid']=$_REQUEST['mst_itemid'][$j];
				$datahard['item_size']=$_REQUEST['item_size'][$j];
				$datahard['item_prim_qty']=$_REQUEST['item_prim_qty'][$j];
				$datahard['item_sec_qty']=$_REQUEST['item_sec_qty'][$j];
				$datahard['item_rate']=$_REQUEST['item_rate'][$j];
				$datahard['item_amount']=$_REQUEST['item_amount'][$j];
				print_r($datahard);
				echo "<br>****************<br /> 3 ";
				//$s->RecordInsert('cstest_estimation_bom_details', $datahard);
				//$getlastid2=DB_Last_Insert_ID($db,'cstest_estimation_bom_details', 'id');
				//echo "count".$_REQUEST['counthw'];
					for($n=0; $n<$_REQUEST['counthw']; $n++)
					{
						if($_REQUEST['mst_asmbly_item_id'][$n]!=''){
						$assmbly_array=array(
									'details_mst_id'=>$getlastid2,
									'mst_itemid'=>$_REQUEST['mst_itemidasmbly'][$j],
									'item_size'=>$_REQUEST['item_size'][$j],
									'mst_asmbly_cat_id'=>$_REQUEST['mst_asmbly_cat_id'][$n],
									'mst_asmbly_item_id'=>$_REQUEST['mst_asmbly_item_id'][$n],
									'asmbly_basicrate'=>$_REQUEST['asmbly_basicrate'][$n],
									'asmbly_item_qty'=>$_REQUEST['asmbly_item_qty'][$n][$j],
									'asmbly_item_amt'=>$_REQUEST['asmbly_item_amt'][$n][$j],
						);}else{$assmbly_array='';}
						print_r($assmbly_array);
					}
					
					echo "<br>********END********<br />";
			//}
		}
		
	
	exit;
	$s->pageLocation("index.php?route=CostEstimation/estimation_sheet_bom_index".$rurl);
}?>

<link href="css/dashboard1.css" rel="stylesheet" type="text/css" />	
<link rel="stylesheet" href="css/new_mxerp.css">
<center> <h2>Estimation Sheet Against BOM </h2>
</center>


<form name="esbom" method="post" >
<table width="100%" align="center" border="0" cellpadding="2" cellspacing="1" style="border: solid 1px #999999;">
  <tr class="row1">
    <td width="13%" class="tabledata"><strong>Branch</strong></td>
    <td width="20%"><select name="site" id="site_id" style="width:100%;">
    <option value="">All Branches</option>
     <?php 
		$site_sql = "Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')";
		$site_result = DB_query($site_sql,$db);
		while($site_row = DB_fetch_array($site_result))
		{
		?>
         <option value="<?php echo $site_row['loccode']; ?>" ><?php echo $site_row['locationname']; ?></option>
            <?php } ?>
    </select></td>
    <td width="13%" class="tabledata"><strong>Sheet No.</strong></td>
    <td width="20%"><input name="sheet_no" id="sheet_no" type="text" class="" style="width:100%;" value="Auto Generated" readonly="true" /></td>
    <td class="tabledata"><strong>Date</strong></td>
    <td width="20%"><input name="sheet_date" id="sheet_date" type="text" class="cap datepicker" style="width:100%;" readonly="true"  value="<?php echo date('d/m/Y');?>"/></td>
  </tr>
  <tr class="row1">
  <td class="tabledata"><strong>Against</strong></td>
    <td><select style="width:100%;" name="against" id="against_id"  onchange="activateDeactivate(this.value)">
      <option value="">--Select--</option>
      <option value="lead">LEAD</option>
	  <option value="none">NONE</option>
    </select></td>
  	<td class="tabledata"><strong>Party </strong></td>
    <td><select name="client" id="client_id" style="width:100%" onchange="getLeads(this.value)">
	<option value="">Select Party</option>
	<?php 
	$sql1=DB_query("Select * from clientmaster",$db);
	while($row1=DB_fetch_array($sql1))
	{?>
		<option value="<?php echo $row1['id'];?>"><?php echo $row1['c_name'];?></option>
	<?php 
	}
	?>
	
	</select></td>
	
	<td class="tabledata"><strong>Lead/Ref. No.</strong></td>
    <td id="div_lead"><input type="text" name="lead_ref_no" id="lead_ref_id" value=""  style="width:100%;" /></td>
  </tr>
  <tr class="row1">
  	<td id="test" class="tabledata"><strong>Lead/Ref. Date</strong></td>
    <td><input type="text" name="lead_ref_date" id="lead_ref_date_id" value=""  style="width:100%;" class="datepicker" /></td>
    <td class="tabledata"><strong>Type </strong></td>
    <td><input type="text" name="lead_ref_type" id="lead_ref_type_id" value=""  style="width:100%;" /><input type="hidden" name="lead_ref_type_hid" id="lead_ref_type_id_hid" value=""  style="width:100%;" />
	</td>
    <td class="tabledata"><strong>Material Sub Group</strong></td>
    <td><select style="width:100%;" name="sub_group" id="sub_group">
      <option value="">--Select Material Group--</option>
		<?php $materialSQL=DB_query("SELECT * FROM stockcategory",$db); 
		while($material=DB_fetch_array($materialSQL)){?>
		 <option value="<?php echo $material['id'];?>"><?php echo $material['categorydescription'];?></option>
		<?php }?>
    </select></td>
  </tr>
</table>
<br />
<fieldset>
<legend style="font-size:13px;"><strong>Finished Product Details</strong></legend>
<p align="left">
			<input type="button" onclick="addRow('dataTable')" value="Add More" id="addmore" class="search" />&nbsp;
			<input type="button" class="search"  onclick="deleteRow('dataTable')" value="Delete" id="delete" />
			&nbsp;&nbsp;
          <input type="button" name="profile_popup" value="Pick Finished Product" id="profile_popup"  onclick="open_items()" class="search" />&nbsp;&nbsp;<input type="button" id="expandbom111" value="Expand BOM" onclick="return expandbom()" class="search">&nbsp;&nbsp;<input type="button" id="collapse" value="Collapse BOM" onclick="return collapsebom()" class="search">&nbsp;&nbsp;<input type="button" id="addhardwareid" value="Add Hardware" onclick="displayHardwareBox()" class="search">
			<input type="hidden" id="hiditemcount" name="hiditemcount" value="" />
			
		</p>
		<br/>
<div>
<table width="100%" align="center" border="0" cellpadding="2" cellspacing="1" id="div_items" style="border:1px solid #666666; ">
  <tr class="row1">
  	<td width="5%" class="tabledata">&nbsp;</td>
	<td width="5%" class="tabledata">&nbsp;</td>
    <td width="15%" class="tabledata"><strong>Item Code</strong></td>
    <td width="15%" class="tabledata"><strong>Item Name</strong></td>
    <td width="15%" class="tabledata"><strong>Size</strong></td>
    <td width="8%" class="tabledata"><strong>Unit</strong></td>
    <td width="8%" class="tabledata"><strong>Quantity (QA)</strong></td>
    <td width="8%" class="tabledata"><strong>Secondary Qty (QB)</strong></td>
    <td width="8%" class="tabledata"><strong>Unit (Secondary)</strong></td>
    <td width="5%" class="tabledata"><strong>Rate</strong></td>
    <td width="8%" class="tabledata"><strong>Amount</strong></td>
  </tr>
  <tbody id="dataTable">
  <tr class="row1">
    <td><input name="bompro[]" id="bompro" type="checkbox" value=""></td>
	<td><input name="mst_itemid[]" id="itemid" type="hidden" value=""></td>
    <td><input name="itemcode[]" id="itemcode" type="text" value=""></td>
	<td><input name="itemname[]" id="itemname" type="text" value=""></td>
    <td><select name="bomsize" id="bomsize" onchange="getAreaAndCalculate(this.id,this.value)" style="width:70px"><option value="">Select</option>									<?php $sql4=DB_query("Select * from size_master",$db);
while($row4=DB_fetch_array($sql4))
{
if($row3['size']==$row4['id'])$selected="selected"; else $selected="";?>
<option value="<?php echo $row4['id'];?>" <?php echo $selected;?>><?php echo $row4['size'];?></option>
<?php }?>
</select>
</td>
    <td><input name="unit[]" id="unit" type="text" value=""></td>
	<td><input type="text" name="item_qty" id="item_qty" value="" onkeyup="calculateItemAmount2(this.id,this.value)"style="width:70px"/></td>
    <td><input typ="text" name="area" id="area" value="" readonly/></td>
    <td><input typ="text" name="secarea" id="secarea" value="" readonly/></td>
    <td><input typ="text" name="rate" id="rate" value="" readonly style="width:70px"/></td>
    <td><input typ="text" name="amt" id="amt" value="" readonly style="width:70px"/></td>
  </tr>
  </tbody>
</table>
</div>
</fieldset>
<br />



<div style="display:none;" id="showhardware">
<fieldset>
<legend style="font-size:13px;"><strong>Hardware Assembly Details </strong></legend>
<div id="hardware_id">




</div>
</fieldset>
</div>

<br />

<div style="display:none" id="showbomid">
<fieldset>
<legend style="font-size:13px;"><strong>Assembly Details (BOM) </strong></legend>
<div id="bom_item">

</div>

</fieldset>
</div>
<br />


<br />
<table width="100%" align="center" border="0" cellpadding="2" cellspacing="1" style="border: solid 1px #999999;">
<tr>
    <th>Remarks</th>
    <td colspan="5">
	<textarea rows="3" name="remark" style="width:100%"></textarea>
	</td>
  </tr>
</table>
<p align="center">
	<input type="submit" name="prepare" value="Prepare"/><!-- onclick="return checkBom();"--></p>
</form>
<script language="javascript" type="text/javascript">
function displayHardwareBox()
{
	document.getElementById("showhardware").style.display='block';
}

function activateDeactivate(str)
{
	if(str=='lead')
	{
		document.getElementById("addmore").disabled=true;
		document.getElementById("delete").disabled=true;
		document.getElementById("profile_popup").disabled=true;
	}
	else if(str=='none')
	{
		document.getElementById("addmore").disabled=false;
		document.getElementById("delete").disabled=false;
		document.getElementById("profile_popup").disabled=false;
	}
}

function checkBom()
{
	var count = document.getElementById("bomproid").value;
	for(var i=0; i<count; i++)
	{
		var bomrate=document.getElementById("item_rate_id_"+i).value;
		if(bomrate=='')
		{
			alert('Some of the finished items do not have their BOM. Please create BOM of each item before proceeding');
			return false;
		}
	}
}

function getLeads(str)
{
	var against_id=document.getElementById("against_id").value;
	var site_id=document.getElementById("site_id").value;
	if(site_id=='')
	{
		alert('Please choose site'); 
		document.getElementById("client_id").value='';
		document.getElementById("site_id").focus();
		return false;
	}
	if(against_id=='')
	{
		alert('Please choose the option against'); 
		document.getElementById("against_id").focus();
		document.getElementById("client_id").value='';
	}
	
	
	if(against_id=='lead')
	{
		if (str=="")
  		{
  			document.getElementById("div_lead").innerHTML="";
 			return;
  		} 
		if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("div_lead").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","CRM_GetLeads.php?str="+against_id+"&site="+site_id+"&client_id="+str,true);
		xmlhttp.send();
	}
}

function GetLeadRecord(str)
{
	if (str=="")
	{
		document.getElementById("lead_ref_date_id").value="";
		document.getElementById("lead_ref_type_id").value="";
		document.getElementById("lead_ref_type_id_hid").value='';
		document.getElementById("div_items").innerHTML='';
		return;
	} 
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			var data_brk=xmlhttp.responseText.split('__');
			document.getElementById("lead_ref_date_id").value=data_brk[0];
			document.getElementById("lead_ref_type_id").value=data_brk[1];
			document.getElementById("lead_ref_type_id_hid").value=data_brk[2];
			document.getElementById("div_items").innerHTML=data_brk[3];
			document.getElementById("hardware_id").innerHTML=data_brk[4];
			
		}
	}
	xmlhttp.open("GET","CRM_GetLeadsRecord.php?lead="+str,true);
	xmlhttp.send();
}

function calculateItemAmount1(i,str)
{
	var item_qty=document.getElementById("item_qty_id_"+i).value;
	var amount=item_qty*str;
	document.getElementById("item_amount_id_"+i).value=amount.toFixed(2);
}
function calculateItemAmount2(i,str)
{
	var rate=document.getElementById("item_rate_id_"+i).value;
	if(rate!='')
	{
		var amount=str*rate;
		document.getElementById("item_amount_id_"+i).value=amount.toFixed(2);
	}
}
function LeadData()
{	
	var t = document.getElementById('bomproid').value;
	if(document.getElementById('bompro').checked==true)
	{	
		if(t==undefined){
			document.getElementById('bompro_'+0).checked=true;		
		}
		for (var i = 0; i < t; i++)
		{
				document.getElementById('bompro_'+i).checked=true;
		}	
	}
	else
	{
		if(t==undefined){
			document.getElementById('bompro_'+0).checked=false;		
		}
	
		for (var i = 0; i < t; i++)
		{
			document.getElementById('bompro_'+i).checked=false;	
		}
	}
}

function expandbom()
{	
	var t = document.getElementById('bomproid').value;
	
	//alert(t);
	var c = 0;
	var b='';
	for (var i = 0; i < t; i++)
	{
		if(document.getElementById('bompro_'+i).checked==true && document.getElementById('bomsize_'+i).value!=''){
			if(i==t-1){
				b += document.getElementById('bompro_'+i).value+"__"+document.getElementById('bomsize_'+i).value+"__"+document.getElementById('item_qty_id_'+i).value;
			}else{
				b += document.getElementById('bompro_'+i).value+"__"+document.getElementById('bomsize_'+i).value+"__"+document.getElementById('item_qty_id_'+i).value+",";
			}
			c = 1;
		}
	}
	
	//alert(b);
	
	if(c==0){
		alert("Please check atleast one item");
		return false;
	}
	
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("bom_item").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","CRM_GetBomsRecord.php?bomitem="+b,true);
	xmlhttp.send();
	
	document.getElementById("showbomid").style.display='block';
}
function collapsebom()
{
	document.getElementById("showbomid").style.display='none';
}

function addRow(tableID) 
{
	if(document)
		var table = document.getElementById(tableID);
		var rowCount = table.rows.length;
		var row = table.insertRow(rowCount);
		document.getElementById("hiditemcount").value=rowCount;
		var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {	 
            
			var newcell = row.insertCell(i);
           	//newcell.style.background='#e7e7e7';
			  newcell.className='tabledata';
			   newcell.innerHTML = table.rows[0].cells[i].innerHTML;
			   if(newcell.childNodes[0].id=='psize' && newcell.childNodes[0].type=='hidden'){
						newcell.childNodes[0].type='text';
					}
			   	switch(newcell.childNodes[0].type) 
				{
				case "text":
					newcell.childNodes[0].value = "";
					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
					
					
				break;

				case "hidden":

					newcell.childNodes[0].value = "";

					var nod_id_hidden = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "button":

					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "checkbox":

				newcell.childNodes[0].checked = false;
				var nod_id_chk = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				

				break;

				case "select-one":
				newcell.childNodes[0].selectedIndex = 0;
				var nod_id = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				break;	
			}
		}
	}
	
function deleteRow(tableID) 
{
	try 
	{
		var table = document.getElementById(tableID);
		var rowCount = table.rows.length;
	
		for(var i=0; i<rowCount; i++) 
		{
			var row = table.rows[i];
			var chkbox = row.cells[0].childNodes[0];
			if(null != chkbox && true == chkbox.checked) 
			{
				if(rowCount <= 1) 
				{
					alert("Cannot delete all the rows.");
					break;
				}
				table.deleteRow(i);
				rowCount--;
				i--;
			}
		}
		document.getElementById("hiditemcount").value=parseInt(rowCount)-1;		
	}
	
	catch(e) 
	{
		alert(e);
	}
}
function open_items()
{

	window.open("open_item.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'] ?>&action=<?php echo $_REQUEST['action']?>","itempopup_window","height=500,width=600,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}


function cal_primary(str)
{
	var str1=document.getElementById("bomproid").value;
	if(str1=="")
	{
		return;
	}
	var psum = 0;
	for(var i=0;i<str1;i++)
	{
		if(document.getElementById("item_qty_id_"+i).value==''){document.getElementById("item_qty_id_"+i).value=0;}
		psum=parseFloat(psum)+parseFloat(document.getElementById("item_qty_id_"+i).value);
	}
	document.getElementById(str).value=psum.toFixed(2);
}	

function cal_secondary(str)
{
	var str1=document.getElementById("bomproid").value;
	if(str1=="")
	{
		return;
	}
	var psum = 0;
	for(var i=0;i<str1;i++)
	{
		if(document.getElementById("secqty_"+i).value==''){document.getElementById("secqty_"+i).value=0;}
		psum=parseFloat(psum)+parseFloat(document.getElementById("secqty_"+i).value);
	}
	document.getElementById(str).value=psum.toFixed(2);
}	
function cal_gross(str)
{
	var str1=document.getElementById("bomproid").value;
	if(str1=="")
	{
		return;
	}
	var psum = 0;
	for(var i=0;i<str1;i++)
	{
		if(document.getElementById("item_amount_id_"+i).value==''){document.getElementById("item_amount_id_"+i).value=0;}
		psum=parseFloat(psum)+parseFloat(document.getElementById("item_amount_id_"+i).value);
	}
	document.getElementById(str).value=psum.toFixed(2);
}

function calcItemAmt1(str,k,i)
{
	if(document.getElementById("manufact_"+k).value=='')
	{
		alert('Please choose an item before entering its details');
		document.getElementById("manufact_"+k).focus();
		document.getElementById("asmbly_item_qty_"+k+"_"+i).value='';
		document.getElementById("asmbly_item_amt_"+k+"_"+i).value='';
		return false;
	}else
	{
		var basicrate = document.getElementById("basicrate_"+k).value
		var total = basicrate*str;
		document.getElementById("asmbly_item_amt_"+k+"_"+i).value=total.toFixed(2);
		
		var gtotal=0;
		var cnthw=document.getElementById("count").value;
		for(var k=0; k<cnthw; k++)
		{
			var amount=document.getElementById("asmbly_item_amt_"+k+"_"+i).value;
			if(amount==''){amount=0;}
			gtotal=parseFloat(gtotal)+parseFloat(amount);
		}
		document.getElementById("total_hw_amt_id_"+i).value=gtotal.toFixed(2);
	}
}
</script>