<script>
function calculate_amount(str)
{
	//alert(str);
	var edit_rate = $('#edit_rate'+str).val();
	var edit_quantity = $('#edit_quantity'+str).val();
	var edit_amount = edit_rate*edit_quantity;
	$('#edit_amount'+str).val(edit_amount);
	//alert(edit_amount);
}
</script>

<script>

function validate(str)
{
	var xyz = 0;
	var flag = 0;
	var flag1 = 0;
	
	for(i=0;i<str;i++)
	{
	if($('#description'+i).val() == "")
		{
			xyz = 1;
		}
	}
	
	$('.validate').each(function()
	{
		if($(this).val() == '') 
		{
			flag = 1 ;	
		}
	});
	
	
	$('.datepicker').each(function(){
		if($(this).val() == "")
		{
			flag1 = 1;
		}
	});
	
	if(flag1 == 1 || flag == 1 || xyz == 1)
	{
		alert('Fill Mandatery Fields');
		return false;
	}
}
function get_amount(i)
{
	var quanty=document.getElementById("complQty_"+i).value;
	if(quanty==''){quanty=0;}
	var pQty=document.getElementById("ActQuantity_"+i).value;
	var quanty1=document.getElementById("compldQty_"+i).value;
	if(quanty1==''){quanty1=0;}
	var tqty=parseInt(quanty)+parseInt(quanty1);
	document.getElementById("tcomplQty_"+i).value=tqty;	
	if(tqty>pQty)
	{
	alert('Total Completed Quantity Cannot BE greater Than Project Quantity');
	document.getElementById("complQty_"+i).value=document.getElementById("complQty1_"+i).value;
	document.getElementById("tcomplQty_"+i).value='';
	document.getElementById("complQty_"+i).focus();
	return false;
	}
	
	var tquanty=document.getElementById("tcomplQty_"+i).value;
	
	
	
	

	//var total=rate*quanty;
	//var cAmt = document.getElementById("cmpAmt_"+i).value=total.toFixed(2);
	
	if(tquanty>pQty)
	{
	alert('Complete Quantity Cannot BE greater Than Project Quantity');
	document.getElementById("complQty_"+i).value=0;
	//document.getElementById("cmpAmt_"+i).value=0;
	document.getElementById("complQty_"+i).focus();
	return false;
	}
	
	var total1=pQty-tquanty;
	document.getElementById("pendQuty_"+i).value=total1.toFixed(2);
	
	//var pAmt=document.getElementById("ActAmount_"+i).value;
	//var pendAmt=pAmt-cAmt;
	//document.getElementById("pendAmt_"+i).value=pendAmt.toFixed(2);
	
	var complete = (tquanty/pQty)*100;
	document.getElementById("complted_"+i).value=complete.toFixed(2);
	
	var pend = 100-complete;
	document.getElementById("pending_"+i).value=pend.toFixed(2);
	
	//var totalAm=document.getElementById("total_amount").value;
	
	//if(totalAm!='')
	//{
		//var amt1=document.getElementById("total_amount").value;
		//totalAm=parseFloat(totalAm)+parseFloat(total);
		
	//}
	//else
	//{
		//totalAm=parseFloat(total);
	//}
	//document.getElementById("total_amount").value=totalAm;
	
	
	
		//var totalPendAm=document.getElementById("total_pend_amount").value;
		//if(totalPendAm!='')
	//{
		//var amt1=document.getElementById("total_amount").value;
		//totalPendAm=parseFloat(totalPendAm)+parseFloat(pendAmt);
		
	//}
	//else
	//{
	//	totalPendAm=parseFloat(pendAmt);
	//}
	//document.getElementById("total_pend_amount").value=totalPendAm;

	
}
function get_tot(i)
{

	var quanty=document.getElementById("complQty_"+i).value;
	if(quanty==''){quanty=0;}
	var quanty1=document.getElementById("compldQty_"+i).value;
	if(quanty1==''){quanty1=0;}
	var pQty=document.getElementById("ActQuantity_"+i).value;

	var tqty=parseInt(quanty)+parseInt(quanty1);

	document.getElementById("tcomplQty_"+i).value=tqty;	
	if(tqty>pQty)
	{
	alert('Total Completed Quantity Cannot BE greater Than Project Quantity');
	document.getElementById("complQty_"+i).value=document.getElementById("complQty1_"+i).value;
	document.getElementById("tcomplQty_"+i).value=document.getElementById("tcomplQty1_"+i).value;
	document.getElementById("tcomplQty_"+i).focus();
	document.getElementById("complQty_"+i).focus();
	return false;
	}
	var tquanty=document.getElementById("tcomplQty_"+i).value;
	
	
	
	

	//var total=rate*quanty;
	//var cAmt = document.getElementById("cmpAmt_"+i).value=total.toFixed(2);
	
	if(tquanty>pQty)
	{
	alert('Complete Quantity Cannot BE greater Than Project Quantity');
	document.getElementById("complQty_"+i).value=0;
	//document.getElementById("cmpAmt_"+i).value=0;
	document.getElementById("complQty_"+i).focus();
	return false;
	}
	
	var total1=pQty-tquanty;
	document.getElementById("pendQuty_"+i).value=total1.toFixed(2);
	
	var complete = (tquanty/pQty)*100;
	document.getElementById("complted_"+i).value=complete.toFixed(2);
	
	var pend = 100-complete;
	document.getElementById("pending_"+i).value=pend.toFixed(2);
	
}
</script>
<?php 
	  if($_REQUEST['action']=='Edit')
	  {
	  
	 if($_REQUEST['savedata'])
	  {//echo "<pre>";print_r($_REQUEST);
	  	$data7['boq_id'] = $_REQUEST['bq_id'];
		$data7['branch'] = $_REQUEST['branch'];
		$data7['pcs_no'] = $_REQUEST['pcs_no1'];
		$data7['boq_no'] = $_REQUEST['boq_no'];
		$data7['boq_date'] = FormatDateForSQL($_REQUEST['project_date']);
		$data7['assigned_to'] = $_REQUEST['assign_to'];
		$data7['sales_ord_no'] = $_REQUEST['order_no'];
		$data7['client'] = $_REQUEST['clients'];
		$data7['handled_by'] = $_REQUEST['handle_by'];
		$data7['slip_no'] = $_REQUEST['slip_no'];
		$data7['slip_date'] = FormatDateForSQL($_REQUEST['slip_date']);
		$data7['modified_by'] = $_SESSION['ACGCompanyId'];
		$data7['created_by'] = $_SESSION['UserID'];
		 $condition7="id='".$_REQUEST['pcs_id']."'"; 
		$s->RecordEdit('project_completion',$data7,$condition7);
		
		$delete = DB_query("delete from project_completion_details where
		        master_id = '".$_REQUEST['pcs_id']."'",$db);
		for($i=0;$i<count($_REQUEST['description']);$i++)
	{
		$datarray1['master_id']=$_REQUEST['mastr_id'][$i];
		$datarray1['salesorder_id']=$_REQUEST['s_id'][$i];
		$datarray1['complete_qty']=$_REQUEST['complQty'][$i];
		//$datarray1['complete_amt']=$_REQUEST['cmpAmt'][$i];
		$datarray1['complete_date']=FormatDateForSQL($_REQUEST['complDate'][$i]);
		$datarray1['pending_qty']=$_REQUEST['pendQuty'][$i];
		$datarray1['remarks']=$_REQUEST['compl_remarks'][$i];
		$datarray1['itemcode'] = $_REQUEST['itemCode'][$i];
		$datarray1['act_group'] = $_REQUEST['act_group'][$i];
		$datarray1['act_name'] = $_REQUEST['act_name'][$i];
		$datarray1['unit'] = $_REQUEST['act_unit'][$i];
		//$datarray1['rate'] = $_REQUEST['act_rate'][$i];
		$datarray1['p_qty'] = $_REQUEST['ActQuantity'][$i];
		//$datarray1['p_amt'] = $_REQUEST['ActAmount'][$i];
		$datarray1['work_desc'] = $_REQUEST['description'][$i];
		$datarray1['p_start_date'] = FormatDateFORSQL($_REQUEST['fromdate1'][$i]);
		$datarray1['pending_amt'] = $_REQUEST['pendAmt'][$i];
		$datarray1['completed'] = $_REQUEST['complted'][$i];
		$datarray1['pending'] = $_REQUEST['pending'][$i];
		$datarray1['p_end_date'] = FormatDateFORSQL($_REQUEST['endDate'][$i]);
		$datarray1['level'] = $_REQUEST['level'][$i];
		$datarray1['bom_main_id'] = $_REQUEST['bom_main_id'][$i];
		$datarray1['boq_id'] = $_REQUEST['bq_id'];
		//print_r($datarray1);exit();
		$s->RecordInsert('project_completion_details',$datarray1);
	}
		$s->pageLocation("index.php?route=PMS/Transcations/ConvertBOQ".$rurl);

	  }
	  }
	  
  
if($_REQUEST['cencel'])
{
	unset($_SESSION['ACGBOQ']);
	unset($_SESSION['refid']);
	unset($_REQUEST['remark']);
	unset($_SESSION['ACGBOQEDIT']);
}	  
?>
<link href="css/new_mxerp.css" rel="stylesheet" type="text/css">
<div class="body_contain">
<div class="tab_div">
	<!--<script src="js1/jquery-1.7.2.js"></script>-->
	<script src="../../Inventory/transation/js1/jquery.ui.widget.js"></script>
	<script src="../../Inventory/transation/js1/jquery.ui.tabs.js"></script>
<script>
	$(function() {
		$( "#tabs" ).tabs();
	});
	
	function getdate(i)
	{
	
 	 a=document.getElementById('qty_'+i).value;
 	 b=document.getElementById('rate_'+i).value;
 	 c=a*b;
 	 document.getElementById('amount_'+i).value=c;
	}
	</script>
	<script>
	$( document ).ready(function() {
$("#fade").fadeOut(4000);
});
</script>
<!---------------POP UP WINDOW FOR ADD ITEM IN EDIT CASE-------START HERE-----BY AMIT----------------------------------------->

<script>	
<!---------------POP UP WINDOW FOR ADD ITEM IN EDIT CASE-------END HERE-----BY AMIT----------------------------------------->



<!---------------POP UP WINDOW FOR ADD ITEM IN EDIT CASE-------START HERE-----BY AMIT----------------------------------------->


//function openboqtemp()
//{
//window.open("pms_popu_boq_entry1.php?action=Edit&rid=<?php //echo $id;?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>","item_popup_window","height=500,width=900,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
//	
//}	

<!---------------POP UP WINDOW FOR ADD ITEM IN EDIT CASE-------END HERE--------------------------------------------------------->




<!---------------POP UP WINDOW FOR ADD ITEM IN ADD CASE-------START HERE-----BY AMIT----------------------------------------->


<!---------------POP UP WINDOW FOR ADD ITEM IN ADD CASE-------END HERE-----BY AMIT----------------------------------------->
</script>
<form action="" method="post" >
<?php
if($_REQUEST['action']=='Edit' && $_REQUEST['cmpID']!="")
{  
 $query = "select project_completion.id as main_id,project_completion.boq_id as boqq_id, project_completion.*,boq_entry.salesorder_id as soid from project_completion inner join  boq_entry on project_completion.boq_id=boq_entry.id where project_completion.id='".$_REQUEST['cmpID']."' and project_completion.CompanyId='".$_SESSION['ACGCompanyId']."'";
$res = db_query($query,$db) or die(DB_error());
$result = DB_fetch_array($res);
extract($result);
$i=0;
  ?>
<h1>Project Complete Template Entry </h1>
<!-- Body Content Start -->

<div id="body_content">
	
<!-- TAble 1 Start -->
<div style="overflow:auto; margin-left:auto; margin-right:auto;" width="100%">
	
	<table width="100%" border="0" cellspacing="0" cellpadding="3" class="">
	<input type="hidden" name="pcs_id" value="<?php echo $_REQUEST['cmpID']; ?>">
	<input type="hidden" name="bq_id" value="<?php echo $boqq_id; ?>">
	<input type="hidden" name="pcs_no1" value="<?php echo $pcs_no; ?>">
   	<tr class="row1">	
		<td width="13%" height="33" ><strong>Branch</strong></td>
   		  <td width="20%"><input type="text" name="branch1" style="width:80%;" value="<?php echo $s->GetfieldValue('locations',"loccode='".$branch."'",'locationname');  ?>" readonly /><input type="hidden" name="branch" value="<?php echo $branch;  ?>" readonly /></td>
   		  <td width="13%"  ><strong>PCS No</strong></td>
	 	<td width="20%" ><input type="text" name="pcs_no" value="<?php echo $pcs_no;?>" style="width:80%;" readonly /></td>
	 	<td width="13%" ><strong>Date</strong></td>
		<td><input name="cur_date" type="text" style="width:80%;" value="<?php echo ConvertSQLDate($date); ?>" readonly /></td>
	</tr>
	
	<tr class="row1">
		 <td ><strong>BOQ No</strong></td>
		 <td><input type="text" name="boq_no" style="width:80%;" value="<?php echo $boq_no; ?>" readonly/></td>
		 <td ><strong>BOQ Date</strong></td>
		 <td><input type="text" name="project_date" value="<?php echo ConvertSQLDate($boq_date); ?>" style="width:80%;" readonly/></td>
		 <td ><strong>Assigned To</strong></td>
		 <td>
		 <input type="text" name="assign_to" style="width:80%;" value="<?php 
		 $techID = $s->GetfieldValue('project_work_assignment',"boq_id='".$boq_id."'",'tech_id');
		 if($techID!="")
		 { echo $s->GetfieldValue('technicians',"id='".$techID."'",'technician'); } 
		 else { $vendID = $s->GetfieldValue('project_assign_contractor',"boq_id='".$boq_id."'",'vendor_id');
		 echo $s->GetfieldValue('suppliers',"id='".$vendID."'",'vendor_name'); }
		 ?>" readonly> </td>
	</tr>
	<tr class="row1"> 
		 <td ><strong>Advice No</strong></td>
		 <td><input type="text" name="order_no" style="width:80%;"  value="<?php echo $sales_ord_no ?>" readonly /></td>
		 <td ><strong>Advice Date</strong></td>
		 <td><input type="text" name="so_date" style="width:80%;" value="<?php echo ConvertSQLDate($s->GetfieldValue('manufacturing_advice',"advice_no='".$sales_ord_no."'",'advice_date')); ?>" readonly/></td>
		 <td ><strong>Project</strong></td>
		 <td><input type="text" name="clients" value="<?php echo $client; ?>" style="width:80%;" readonly></td>
  </tr>
  	<tr class="row1"> 
		 <td ><strong>Handled By</strong></td>
		 <td><input type="text" name="handle_by" style="width:80%;"  value="<?php echo $handled_by; ?>" readonly /></td>
		 <td ><strong>Slip No.</strong> <font style="color:#FF0000;">*</font></td>
		 <td><input type="text" style="width:80%;" name="slip_no" value="<?php echo $slip_no; ?>" /></td>
		 <td ><strong>Slip Date </strong><font style="color:#FF0000;">*</font></td>
		 <td><input type="text" class="datepicker" name="slip_date" style="width:80%;" value="<?php echo ConvertSQLDate($slip_date); ?>" ></td>
  </tr>
</table>

	</div>
	<br />
<!-- TAble 2 Start -->
<!-- TAble 2 Close -->
</div><!-- Body Content Close -->
<div style="width:1200px; overflow:scroll; margin:auto;">
<table border="1" width="100%" cellpadding="3" cellspacing="0" style="border-collapse:collapse;">
  <tr bgcolor="#CCCCCC">
    <!--<td class=""><strong>Item Name</strong></td>-->
    <td><strong>Activity Group</strong></td>
    <td class=""><strong>Activity Name</strong></td>
    <td class="" style="width:23px;"><strong>Description</strong></td>
    <td class=""><strong>Unit</strong></td>
    <!--<td class=""><strong>Project Rate</strong></td>-->
    <td class=""><strong>Project Quantity</strong></td>
    <!--<td class=""><strong>Project Amount</strong></td>-->
    <td class=""><strong>Project Start Date</strong></td>
    <td class="label"><strong>Complete Quantity</strong></td>
    <td class="label"><strong>Total Complete Quantity</strong></td>
    <!--<td class=""><strong>Complete Amount</strong></td>-->
    <td class=""><strong>Completion Date</strong></td>
    <td class=""><strong>Pending Quantity</strong></td>
	<!--<td class=""><strong>Pending Amount</strong></td>-->
    <td class=""><strong>completed(%)</strong></td>
    <td class=""><strong>Pending(%)</strong></td>
    <td class=""><strong>End Date</strong></td>
    <td class=""><strong>Remark</strong></td>
  </tr>
<?php 
$salesorder_id=$s->GetFieldVAlue('manufacturing_advice',"advice_no='".$sales_ord_no."'",'id');
$order_det=DB_fetch_assoc(DB_query("SELECT * FROM `manufacturing_advice` WHERE id='".$salesorder_id."' ",$db));	
$query1 = "SELECT * from manufacturing_advice_items WHERE advice_id='".$order_det['id']."'";
                $res1 = DB_query($query1,$db) or die(DB_error());
				$y=0;
                while($result1 = DB_fetch_array($res1)){

?>
<tr class="row1">
                    <td colspan="3" style="color:#0000FF;"><?php echo "<b>".$s->GetFieldVAlue('item_master',"id='".$result1['item_code']."'",'item_name')."</b>"; ?></td>
                    <td><b><?php $unit=$s->GetFieldVAlue('item_master',"id='".$result1['item_code']."'",'mst_unitid');  echo $s->GetFieldVAlue('unitsofmeasure',"unitid='".$unit."'",'unitname'); ?></b></td>
                    <td colspan="7"><b><?php  echo $tqty=$result1['Qty'];?></b></td>
                </tr>

  <?php 
  $query = "select * from project_completion_details where master_id='$main_id' and level='".$result1['id']."' and itemcode='".$result1['item_code']."'";
 
	$res = DB_query($query,$db) or die(DB_error());
	DB_num_rows($res);
	$amo = array();
	$m=0;
	$y=0;
	while($result = DB_fetch_array($res)){
	$boqid=$s->GetFieldVAlue('project_completion',"id='".$main_id."'",'boq_id');
	$querychk = "SELECT SUM(B.complete_qty) AS cqty from project_completion AS A Left Join project_completion_details AS B ON A.id=B.master_id WHERE A.boq_id='".$boqid."' and itemcode='".$result['itemcode']."' and level='".$result1['id']."' and act_name='".$result['act_name']."' and B.id<>'".$result['id']."'";
	$reschk = DB_query($querychk,$db) or die(DB_error());
	$resultchk = DB_fetch_array($reschk);
	 $x = $result['itemcode'];
	$m++;
	$amo[] = $m;
	 $y++;
	?>
  <input type="hidden" name="id[]" value="<?php echo $id;?>" />
  <input type="hidden" name="mastr_id[]" value="<?php echo $result['master_id'];?>" />
  <input type="hidden" name="s_id[]" value="<?php echo $soid;?>" />
  <input type="hidden" name="itemCode[]" value="<?php echo $result['itemcode']; ?>"/>
	<input type="hidden" name="act_group[]" value="<?php echo $result['act_group']; ?>"/>
	<input type="hidden" name="act_name[]" value="<?php echo $result['act_name']; ?>"/>
<input type="hidden" name="level[]" value="<?php echo $result1['id']; ?>"/>
<input type="hidden" name="bom_main_id[]" value="<?php echo $salesorder_id; ?>"/>
  <tr>
    <!--<td><?php echo $s->GetFieldVAlue('item_master',"id='".$result['itemcode']."'",'item_name'); ?></td>-->
    <td><?php echo $s->GetFieldVAlue('sub_activity_group',"id='".$result['act_group']."'",'group_name'); ?></td>
    <td><?php echo $s->GetFieldVAlue('sub_activity_defination',"id='".$result['act_name']."'",'activity_name'); ?></td>
    <td><textarea id="description<?php echo $m;?>"  readonly name="description[]" rows="1" style="width:120px;"> <?php echo $result['work_desc']; ?></textarea></td>
    <td ><input type="text"  name="act_unit[]" value="<?php echo $result['unit']; ?>" readonly style="width:80px;"/></td>
    <!--<td><input type="text" name="act_rate[]" id="act_rate_<?php echo $y; ?>" value="<?php echo $result['rate']; ?>" readonly style="width:80px;"/></td>-->
    <td><input type="text" name="ActQuantity[]" id="ActQuantity_<?php echo $y; ?>" style="width:80px;" class="validate" readonly value = "<?php echo $result['p_qty']; ?>" /></t>
    <!--<td><input type="text" name="ActAmount[]" id="ActAmount_<?php echo $y; ?>" value = "<?php echo $result['p_amt']; ?>" readonly style="width:80px;" /></td>-->
    <td><input name="fromdate1[]" type="text" readonly value = "<?php echo ConvertSQLDate($result['p_start_date']); ?>"  style="width:80px;" /></td>
    <!--<td><input name="complQty[]" id="complQty_<?php echo $y; ?>" type="text" value="<?php echo $result['complete_qty']; ?>" onkeyup="get_amount('<?php echo $y;?>');" style="width:80px;"/></td>-->
<td><input name="complQty[]" id="complQty_<?php echo $y; ?>" type="text" onkeyup="get_tot('<?php echo $y;?>');" value="<?php echo $result['complete_qty']; ?>"   style="width:100%;height:20px;"/><input id="complQty1_<?php echo $y; ?>" type="hidden" value="<?php echo $result['complete_qty']; ?>"/>
	<input name="compldQty[]" id="compldQty_<?php echo $y; ?>" type="hidden" value="<?php echo $resultchk['cqty'];?>"   style="width:100%;height:20px;" readonly='true'/></td>
<?php $tqty1=$resultchk['cqty']+$result['complete_qty'];?>
	<td><input name="tcomplQty[]" id="tcomplQty_<?php echo $y; ?>" type="text" value="<?php echo $tqty1;?>" onfocus="get_amount('<?php echo $y;?>');"   style="width:100%;height:20px;" readonly='true'/><input id="tcomplQty1_<?php echo $y; ?>" type="hidden" value="<?php echo $tqty1;?>" readonly='true'/></td>
    <!--<td><input type="text" name="cmpAmt[]"  id="cmpAmt_<?php echo $y; ?>" value="<?php echo $result['complete_amt']; ?>" style="width:80px;" /></td>-->
    <td><input name="complDate[]" id="complDate_<?php echo $m; ?>" type="text" value = "<?php echo ConvertSQLDate($result['complete_date']); ?>" class="datepicker" style="width:80px;"/></td>
    <td><input name="pendQuty[]" id="pendQuty_<?php echo $y; ?>" type="text" value="<?php echo $result['pending_qty']; ?>" style="width:80px;"/></td>
    <!--<td><input name="pendAmt[]" id="pendAmt_<?php echo $y; ?>" type="text" value="<?php echo $result['pending_amt']; ?>" style="width:80px;"/></td>-->
    <td><input name="complted[]" id="complted_<?php echo $y; ?>" value="<?php echo $result['completed']; ?>" type="text" style="width:80px;"/></td>
    <td><input name="pending[]" id="pending_<?php echo $y; ?>" value="<?php echo $result['pending']; ?>" type="text" style="width:80px;"/></td>
    <td><input name="endDate[]" type="text" readonly value = "<?php echo ConvertSQLDate($result['p_end_date']); ?>"  style="width:80px;" /></td>
    <td><textarea rows="1" id="compl_remarks_<?php echo $m;?>" name="compl_remarks[]"><?php echo $result['remarks']; ?></textarea></td>
  </tr>
  <?php
}
$bom=$s->getField('bill_material','part_id',$result1['item_code'],'id');
                    if($bom!='')								
                    {
                        materialshow($bom,$spacebar,$tqty,$j,$result1['item_code'],$function_filter,$y,$result1['id'],$salesorder_id,$main_id,'');
                    }

 }
//$string1 = implode('_', $amo); 

//$tot=DB_fetch_assoc(DB_query("select sum(complete_amt) as totcom from  project_completion_details where master_id='".$_REQUEST['cmpID']."' ",$db));

//$ptot=DB_fetch_assoc(DB_query("select grand_total_amount,grand_pend_amt from  project_completion where id='".$_REQUEST['cmpID']."' ",$db));

?>

  <!--<tr class="row1">
  <td colspan="7" style="text-align:right">Total Project Amount </td>
  <td><?php echo $ptot['grand_total_amount']; ?></td>
    <td colspan="2" style="text-align:right"> Total complete Amount </td>
    <td><div id = "amt" > </div><?php echo $tot['totcom']; ?>    </td>
	<td colspan="2" style="text-align:right"> Total Pending Amount </td>
    <td><div id = "amt" > </div><?php echo $ptot['grand_pend_amt']; ?>    </td>
	<td> Prepared By </td>
  <td><?php
   echo $s->GetFieldValue('www_users',"id='".$_SESSION['UserID']."'",'userid'); ?></td>
   <td colspan="10"></td>
  </tr>-->
  
</table>
</div>
<br />
<table cellpadding="0" width="100%" cellspacing="0" border="0">
	<tr class="row1">
    <td colspan="18" style="text-align:center;"><input type="submit" name="savedata" value="save" class="search_y" onclick="return validate1('<?php echo $m; ?>');" /></td>
  </tr>
</table>
</form>

<?php } 
function materialshow($PcateID, $dashVal,$totalqty1,$j,$sopstatus,$function_filter,$y,$did,$salesorder_id,$main_id,$tree)
{	
    global $db;
    $spacebar="<img src='images/pms-arrow.jpg'/>";
	$spacebar = $spacebar.$dashVal;
	$totalqty1=$totalqty1;
	$s=new db_query(); 		
	$sopstatus=$sopstatus;
    
    $sqlCate = "select * from bill_material  where  id  = '".$PcateID."'";
	$rsCate  = DB_query($sqlCate,$db);	
 	if(DB_num_rows($rsCate)>0)
	{
	  $rowCate = DB_fetch_array($rsCate);
if($tree!=''){
		$tree=$tree.",".$rowCate['part_id'];
	}else{
		$tree=$rowCate['part_id'];
	}
            $query_comprole=DB_query("select * from bill_raw_material where bill_material_id='".$rowCate['id']."'",$db);
 	    while($row_comprole=DB_fetch_array($query_comprole))
            {  
$totalqty1=$totalqty1*$row_comprole['bom_qty'];
$queryval = "select * from project_completion_details where master_id='$main_id' and level='".$did."' and itemcode='".$row_comprole['mst_itemid']."' and bom_main_id='".$PcateID."'";
	$resval = DB_query($queryval,$db) or die(DB_error());
	if(DB_num_rows($resval)>0){
?>
<tr class="row1">
                    <td colspan="3"><?php 
$tree1=explode(",",$tree);
$newspace="<img src='images/pms-arrow.jpg'/>";
for($mm=1;$mm<count($tree1);$mm++){
echo "<b>".$newspace."&nbsp;".$s->GetFieldVAlue('item_master',"id='".$tree1[$mm]."'",'item_name')."</b>"; 
$newspace=$newspace."<img src='images/pms-arrow.jpg'/>";
}
echo "<b>".$spacebar."&nbsp;".$s->GetFieldVAlue('item_master',"id='".$row_comprole['mst_itemid']."'",'item_name')."</b>"; 
?>
</td>
                    <td><b><?php $unit=$s->GetFieldVAlue('item_master',"id='".$row_comprole['mst_itemid']."'",'mst_unitid');  echo $s->GetFieldVAlue('unitsofmeasure',"unitid='".$unit."'",'unitname'); ?></b></td>
                    <td colspan="7"><b><?php echo $totalqty1; ?></b></td>
                </tr>
<?php
}

 $query = "select * from project_completion_details where master_id='$main_id' and level='".$did."' and itemcode='".$row_comprole['mst_itemid']."' and bom_main_id='".$PcateID."'";
 
	$res = DB_query($query,$db) or die(DB_error());
	DB_num_rows($res);
	$amo = array();
	$m=0;
	$y=0;
	while($result = DB_fetch_array($res)){
	if($result['itemcode']==$row_comprole['mst_itemid']){
	$boqid=$s->GetFieldVAlue('project_completion',"id='".$main_id."'",'boq_id');
	$querychk = "SELECT SUM(B.complete_qty) AS cqty from project_completion AS A Left Join project_completion_details AS B ON A.id=B.master_id WHERE A.boq_id='".$boqid."' and itemcode='".$result['itemcode']."' and level='".$did."' and act_name='".$result['act_name']."' and B.id<>'".$result['id']."'";
	$reschk = DB_query($querychk,$db) or die(DB_error());
	$resultchk = DB_fetch_array($reschk);
	 $x = $result['itemcode'];
	$m++;
	$amo[] = $m;
	 $y++;
	?>
  <input type="hidden" name="id[]" value="<?php echo $id;?>" />
  <input type="hidden" name="mastr_id[]" value="<?php echo $result['master_id'];?>" />
  <input type="hidden" name="s_id[]" value="<?php echo $soid;?>" />
  <input type="hidden" name="itemCode[]" value="<?php echo $result['itemcode']; ?>"/>
	<input type="hidden" name="act_group[]" value="<?php echo $result['act_group']; ?>"/>
	<input type="hidden" name="act_name[]" value="<?php echo $result['act_name']; ?>"/>
<input type="hidden" name="level[]" value="<?php echo $did; ?>"/>
<input type="hidden" name="bom_main_id[]" value="<?php echo $PcateID; ?>"/>
  <tr>
    <!--<td><?php echo $s->GetFieldVAlue('item_master',"id='".$result['itemcode']."'",'item_name'); ?></td>-->
    <td><?php echo $s->GetFieldVAlue('sub_activity_group',"id='".$result['act_group']."'",'group_name'); ?></td>
    <td><?php echo $s->GetFieldVAlue('sub_activity_defination',"id='".$result['act_name']."'",'activity_name'); ?></td>
    <td><textarea id="description<?php echo $m;?>"  readonly name="description[]" rows="1" style="width:120px;"> <?php echo $result['work_desc']; ?></textarea></td>
    <td ><input type="text"  name="act_unit[]" value="<?php echo $result['unit']; ?>" readonly style="width:80px;"/></td>
    <!--<td><input type="text" name="act_rate[]" id="act_rate_<?php echo $y; ?>" value="<?php echo $result['rate']; ?>" readonly style="width:80px;"/></td>-->
    <td><input type="text" name="ActQuantity[]" id="ActQuantity_<?php echo $y; ?>" style="width:80px;" class="validate" readonly value = "<?php echo $result['p_qty']; ?>" /></t>
    <!--<td><input type="text" name="ActAmount[]" id="ActAmount_<?php echo $y; ?>" value = "<?php echo $result['p_amt']; ?>" readonly style="width:80px;" /></td>-->
    <td><input name="fromdate1[]" type="text" readonly value = "<?php echo ConvertSQLDate($result['p_start_date']); ?>"  style="width:80px;" /></td>
    <!--<td><input name="complQty[]" id="complQty_<?php echo $y; ?>" type="text" value="<?php echo $result['complete_qty']; ?>" onkeyup="get_amount('<?php echo $y;?>');" style="width:80px;"/></td>-->
<td><input name="complQty[]" id="complQty_<?php echo $y; ?>" type="text" onkeyup="get_tot('<?php echo $y;?>');" value="<?php echo $result['complete_qty']; ?>"   style="width:100%;height:20px;"/><input id="complQty1_<?php echo $y; ?>" type="hidden" value="<?php echo $result['complete_qty']; ?>"/>
	<input name="compldQty[]" id="compldQty_<?php echo $y; ?>" type="hidden" value="<?php echo $resultchk['cqty'];?>"   style="width:100%;height:20px;" readonly='true'/></td>
<?php $tqty1=$resultchk['cqty']+$result['complete_qty'];?>
	<td><input name="tcomplQty[]" id="tcomplQty_<?php echo $y; ?>" type="text" value="<?php echo $tqty1;?>" onfocus="get_amount('<?php echo $y;?>');"   style="width:100%;height:20px;" readonly='true'/><input id="tcomplQty1_<?php echo $y; ?>" type="hidden" value="<?php echo $tqty1;?>" readonly='true'/></td>
    <!--<td><input type="text" name="cmpAmt[]"  id="cmpAmt_<?php echo $y; ?>" value="<?php echo $result['complete_amt']; ?>" style="width:80px;" /></td>-->
    <td><input name="complDate[]" id="complDate_<?php echo $m; ?>" type="text" value = "<?php echo ConvertSQLDate($result['complete_date']); ?>" class="datepicker" style="width:80px;"/></td>
    <td><input name="pendQuty[]" id="pendQuty_<?php echo $y; ?>" type="text" value="<?php echo $result['pending_qty']; ?>" style="width:80px;"/></td>
    <!--<td><input name="pendAmt[]" id="pendAmt_<?php echo $y; ?>" type="text" value="<?php echo $result['pending_amt']; ?>" style="width:80px;"/></td>-->
    <td><input name="complted[]" id="complted_<?php echo $y; ?>" value="<?php echo $result['completed']; ?>" type="text" style="width:80px;"/></td>
    <td><input name="pending[]" id="pending_<?php echo $y; ?>" value="<?php echo $result['pending']; ?>" type="text" style="width:80px;"/></td>
    <td><input name="endDate[]" type="text" readonly value = "<?php echo ConvertSQLDate($result['p_end_date']); ?>"  style="width:80px;" /></td>
    <td><textarea rows="1" id="compl_remarks_<?php echo $m;?>" name="compl_remarks[]"><?php echo $result['remarks']; ?></textarea></td>
  </tr>
  <?php
}
}

$bom=$s->getField('bill_material','part_id',$row_comprole['mst_itemid'],'id');
                    if($bom!='')								
                    {
                        materialshow($bom,$spacebar,$totalqty1,$j,$row_comprole['mst_itemid'],$function_filter,$y,$did,$salesorder_id,$main_id,$tree);
                    }
}
} 

}

