<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)
{
//alert(i);total_amount

	var rate=document.getElementById("act_rate_"+i).value;
	var quanty=document.getElementById("ActQuantity_"+i).value;
	
	var total=rate*quanty;
	var amount = document.getElementById("ActAmount_"+i).value=total.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;
	
	
	
	
}
// total amount

</script>
<?php
$m=0;

if($_REQUEST['action']=='edit' && $_REQUEST['boqIDD']!="")
{ 
	  if($_REQUEST['savedata'])
	  {
	  	$data1['salesorder_id'] = $_REQUEST['so_id'];
		$data1['boq_no'] = $_REQUEST['boqno'];
		$data1['project_date'] =  FormatDateFORSQL($_REQUEST['boq_date']);
		$data1['project_handeled_by'] = $_REQUEST['handeled_by'];
		$data1['project_status'] = '1';
		$data1['companyId'] = $_SESSION['ACGCompanyId'];
		//$data1['total_amount'] = $_REQUEST['total_amount'];
		$data1['created_by'] = $_SESSION['UserID'];
		$condition1="id='".$_REQUEST['boq_id']."'";
	 	$s->RecordEdit('boq_entry',$data1,$condition1);

		$data7['project_status'] = '1';	
		$condition7="id='".$_REQUEST['so_id']."'";
 		$s->RecordEdit('manufacturing_advice',$data7,$condition7);	
		
		
		$delete = DB_query("delete from boq_entry_details where
		        master_id = '".$_REQUEST['boq_id']."'",$db);
		
	  	
		for($i=0;$i<count($_REQUEST['ActQuantity']);$i++)
		{ 
			if($_REQUEST['ActQuantity'][$i]!=''){
				$flag = 1;
				$data6['project_status'] = '1';	
				$condition6="id='".$_REQUEST['so_det_id'][$i]."'";
			 	$s->RecordEdit('manufacturing_advice_items',$data6,$condition6);

				$data['salesorder_id'] = $_REQUEST['so_id'];
				$data['work_description'] = $_REQUEST['description'][$i];
				$data['quantity'] = $_REQUEST['ActQuantity'][$i];
				//$data['amount'] = $_REQUEST['ActAmount'][$i];
				$data['startdate'] =FormatDateForSql($_REQUEST['fromdate1'][$i]);
				$data['enddate'] =FormatDateForSql($_REQUEST['enddate1'][$i]);
				$data['remarks'] = $_REQUEST['text'][$i];
				$data['master_id'] = $_REQUEST['boq_id'];
				$data['itemCode'] = $_REQUEST['itemCode'][$i];
				$data['activity_group'] = $_REQUEST['act_group'][$i];
				$data['activity_name'] = $_REQUEST['act_name'][$i];
				$data['unit'] = $_REQUEST['act_unit'][$i];
				//$data['rate'] = $_REQUEST['act_rate'][$i];
				$data['level'] = $_REQUEST['so_det_id'][$i];
				$data['bom_main_id'] = $_REQUEST['bom_main_id'][$i];

				$r= $s->RecordInsert('boq_entry_details', $data);
				if(DB_affected_rows($db)>0)
				{
					$flag1 = 1;
				}
			}
		}
		
		$s->pageLocation("index.php?route=PMS/Transcations/ViewBOQ".$rurl);
			
			
			if($flag == 1 && $flag1 == 1)
			{
				echo "<div id = 'fade' style='color:green;'>Successfully Insearted</div>";
			}
	  }
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
	<script src="js1/jquery.ui.widget.js"></script>
	<script src="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>
<form action="" method="post" >
<?php
if($_REQUEST['action']=='edit' && $_REQUEST['boqIDD']!="")
{
$DOCsql=DB_query("SELECT * FROM boq_entry WHERE id='".$_REQUEST['boqIDD']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
if(DB_num_rows($DOCsql)>0){
$i=0;
	  $Doc=DB_fetch_array($DOCsql);
	  extract($Doc);	  
$order_det=DB_fetch_assoc(DB_query("SELECT * FROM `manufacturing_advice` WHERE id='".$salesorder_id."' ",$db));	    
  ?>
<h1>Project Acivities Planning</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="boq_id" value="<?php echo $_REQUEST['boqIDD']; ?>">
	<input type="hidden" name="so_id" value="<?php echo $salesorder_id; ?>">
	<input type="hidden" name="boqno" value="<?php echo $boq_no; ?>">
   	<tr class="row1">	
		<td class=""><strong>Branch</strong> </td>
   		<td><input type="text" readonly="true"  style="width:80%;" name="branch" value="<?php echo $s->GetfieldValue('locations',"loccode='".$order_det['branchid']."'",'locationname'); ?>"></td>
    	<td class="" ><strong>PPC No</strong></td>
	 	<td ><input type="text" name="boq_no" value="<?php echo $boq_no; ?>" readonly style="width:80%;" /></td>
	 	<td class=""><strong>Date</strong></td>
		<td><input name="boq_date" type="text" style="width:80%;" value="<?php echo ConvertSQLDate($project_date); ?>"  class=""  readonly="true" /></td>
	</tr>
	
	<tr class="row1">
		 <td class=""><strong>Advice No</strong></td>
		 <td><input type="text" style="width:80%;" name="order_no" value="<?php echo $s->GetfieldValue('manufacturing_advice',"id='".$salesorder_id."'",'advice_no'); ?>" readonly/></td>
		 <td class=""><strong>Advice Date</strong></td>
		 <td><input type="text" name="order_date" value="<?php echo ConvertSQLDate($s->GetfieldValue('manufacturing_advice',"id='".$salesorder_id."'",'advice_date')); ?>" style="width:80%;" readonly/></td>
		 <td class=""><strong>Project Name</strong></td>
		 <td><input type="text" name="boq_client" style="width:80%;" readonly value="<?php echo $s->GetfieldValue('manufacturing_advice',"id='".$salesorder_id."'",'project_name');?>"></td>
	</tr>
	<tr class="row1"> 
		 <td class=""><strong>Handled By</strong></td>
		 <td><input type="text"  readonly="true"  name="handeled_by" style="width:80%;" value="<?php echo $project_handeled_by; ?>" ></td>
		 <td colspan="4">&nbsp;</td>
  </tr>
</table>
	</div>
	<br />
</div>

<table border="1" width="100%" cellpadding="2" cellspacing="0">
	<tr bgcolor="#CCCCCC" height="25">
	<td class="" width="10%"><strong>Activity Group</strong></td>
	<td class="" width="10%"><strong>Activity Name</strong></td>
	<td width="25%" class=""><strong>Description</strong></td>
	<td width="6%" class=""><strong>Unit</strong></td>
	<!--<td class=""><strong>Rate</strong></td>-->
	<td width="6%" class=""><strong>Quantity</strong></td>
	<!--<td class=""><strong>Amount</strong></td>-->
	<td width="7%" class=""><strong>Start Date</strong></td>
	<td width="7%" class=""><strong>End Date</strong></td>
	<td class=""><strong>Remark</strong></td>
	<!--<td class=""><strong>Delete</strong></td>-->
</tr>
<?php 
$query = "SELECT * from manufacturing_advice_items WHERE advice_id='".$salesorder_id."'";
                $res = DB_query($query,$db) or die(DB_error());

                while($result = DB_fetch_array($res)){
	
	 $x = $result['item_code'];
$Subcat=$s->GetFieldVAlue('item_master',"id='".$result['item_code']."'",'SubCategoryID');
                $sqlactivityGroup=DB_query("select * from sub_activity_defination where inventory_group_id='".$Subcat."' and status='Active'",$db);
		if(DB_num_rows($sqlactivityGroup)>0){
		$m++;                
		?>

	<tr class="row1">
                    <td colspan="2"><?php echo "<b>".$s->GetFieldVAlue('item_master',"id='".$result['item_code']."'",'item_name')."</b>"; ?></td>
                    <td ></td>
                    <td><b><?php $unit=$s->GetFieldVAlue('item_master',"id='".$result['item_code']."'",'mst_unitid');  echo $s->GetFieldVAlue('unitsofmeasure',"unitid='".$unit."'",'unitname'); ?></b></td>
                    <td colspan="7"><b><?php  echo $tqty=$result['Qty'];?></b></td>
    </tr>
<?php } ?>
	
	<?php
	 while($AcGroup=DB_fetch_array($sqlactivityGroup)){
	

	$queryval = "SELECT * from boq_entry_details WHERE master_id='".$id."' and itemCode='".$result['item_code']."' and activity_group='".$AcGroup['group_name']."' and activity_name='".$AcGroup['id']."' and level='".$result['id']."'";
	$resval = DB_query($queryval,$db) or die(DB_error());
	$resultval = DB_fetch_array($resval);
	?>
    <input type="hidden" name="itemCode[]" value="<?php echo $result['item_code']; ?>"/>
    <input type="hidden" name="so_det_id[]" value="<?php echo $result['id']; ?>"/>
	<input type="hidden" name="bom_main_id[]" value="<?php echo $salesorder_id; ?>"/>
	<input type="hidden" name="noOfRows[]" value="<?php echo $m; ?>"/>
	<input type="hidden" name="act_group[]" value="<?php echo $AcGroup['group_name']; ?>"/>
	<input type="hidden" name="act_name[]" value="<?php echo $AcGroup['id']; ?>"/>
		<tr class="row1">
			<td align="left" valign="top"><?php echo $s->GetFieldValue('sub_activity_group',"id='".$AcGroup['group_name']."'",'group_name'); ?></td>
			<td align="left" valign="top"><?php echo $AcGroup['activity_name']; ?></td>
		  <td align="left" valign="top"><textarea id="description<?php echo $m;?>" name="description[]" rows="1" style="width:100%;"><?php echo $resultval['work_description']; ?></textarea></td>
		  <td align="left" valign="top" ><input type="text" name="act_unit[]" value="<?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$AcGroup['unit']."'",'unitname'); ?>" readonly style="width:100%;height:20px;"/></td>
			<!--<td><input type="text" name="act_rate[]" id="act_rate_<?php echo $m; ?>" value="<?php echo $AcGroup['rate']; ?>" readonly style="width:80px;height:20px;"/></td>-->
		  <td align="left" valign="top"><input type="text" name="ActQuantity[]" onchange="get_amount('<?php echo $m;?>');" value="<?php echo $resultval['quantity']; ?>" id="ActQuantity_<?php echo $m; ?>" style="width:100%;height:20px;" class="validate"/></td>
			<!--<td><input type="text" name="ActAmount[]" id="ActAmount_<?php echo $m; ?>" value="<?php echo $result['amount']; ?>" readonly style="width:80px;height:20px;" /></td>-->
		  <td align="left" valign="top"><input name="fromdate1[]" value="<?php if($resultval['startdate']!=''){echo ConvertSQLDate($resultval['startdate']);}else{echo date('d/m/Y');} ?>" type="text"  class="datepicker" style="width:100%; height:20px;" /></td>
		  <td align="left" valign="top"><input name="enddate1[]" value="<?php if($resultval['enddate']!=''){ echo ConvertSQLDate($resultval['enddate']);}else{echo date('d/m/Y');} ?>" type="text"  class="datepicker" style="width:100%; height:20px;"/></td>
		  <td align="left" valign="top"><textarea id="act_r_<?php echo $m; ?>" name="text[]" style="width:100%;" rows="1"><?php echo $resultval['remarks']; ?></textarea></td>
			<!--<td><a href="index.php?route=PMS/Transcations/boq_templates_entry<?php echo $rurl;?>&action=delete&opn=<?php echo $id; ?>" onclick="return confirm('Are you sure you want to delete?')"> <img src="images/Icon_Delete.png" /></a></td>-->
		</tr>
		<?php  } ?>
		 <?php
                
                    $bom=$s->getField('bill_material','part_id',$result['item_code'],'id');
                    if($bom!='')								
                    {
                        materialshow($bom,$spacebar,$tqty,$j,$result['item_code'],$function_filter,$m,$result['id'],$_REQUEST['boqIDD']);
                    }
                ?>
<?php } ?>
<!--<tr class="row1">
	<td colspan="5"></td>
	<td style="text-align:center;">Total Project Amount</td>
	<?php 
$main_data=DB_fetch_assoc(DB_query("select Sum(amount) as total from boq_entry_details where master_id='".$_REQUEST['boqIDD']."' ",$db));	
	?>
	<td style="text-align:center;"><input type = "text"  readonly="true" name= "total_amount" value="<?php echo $main_data['total'] ?>" style="width:80px;" id="total_amount"  /
	 /> </td>
	 <td>&nbsp;</td>
	 <td colspan="4">&nbsp;</td>
  </tr>-->
  <tr class="row1">
  <td colspan="12" style="text-align:center; font-weight:bold; ">
   Prepared By 
  <?php
   echo $s->GetFieldValue('www_users',"id='".$_SESSION['UserID']."'",'userid'); ?>
   </td>
  </tr>
<tr> 
	<td colspan="13" style="text-align:center;">
	<input type="submit" name="savedata" value="save" class="search" onClick="return validates()"></td>
</tr>

</table>
<?php } 
 } ?>
</form>
<?php
function materialshow($PcateID, $dashVal,$totalqty1,$j,$sopstatus,$function_filter,$m,$did,$bid)
{	
    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);
            $query_comprole=DB_query("select * from bill_raw_material where bill_material_id='".$rowCate['id']."'",$db);
 	    while($row_comprole=DB_fetch_array($query_comprole))
            {
	    $Subcat=$s->GetFieldVAlue('item_master',"id='".$row_comprole['mst_itemid']."'",'SubCategoryID');
            $sqlactivityGroup=DB_query("select * from sub_activity_defination where inventory_group_id='".$Subcat."' and status='Active'",$db);
	    if(DB_num_rows($sqlactivityGroup)>0){
            $m++;?>
            <tr class="row1">
                <td colspan="2" style="color:#0000FF;"><?php echo "<b><img src='images/pms-folder.jpg'/>".$spacebar."&nbsp;".$s->GetFieldVAlue('item_master',"id='".$row_comprole['mst_itemid']."'",'item_name')."</b>"; ?></td>
                <td ></td>
                <td><?php $unit=$s->GetFieldVAlue('item_master',"id='".$row_comprole['mst_itemid']."'",'mst_unitid');  echo $s->GetFieldVAlue('unitsofmeasure',"unitid='".$unit."'",'unitname'); ?></td>
                <td colspan="7"><?php echo $totalqty1=$totalqty1*$row_comprole['bom_qty']; ?></td>
            </tr>
            <?php
	    }
            while($AcGroup=DB_fetch_array($sqlactivityGroup)){
            $m++;
			$queryval = "SELECT * from boq_entry_details WHERE master_id='".$bid."' and itemCode='".$row_comprole['mst_itemid']."' and activity_group='".$AcGroup['group_name']."' and activity_name='".$AcGroup['id']."' and level='".$did."'";
			$resval = DB_query($queryval,$db) or die(DB_error());
			$resultval = DB_fetch_array($resval);
            ?>
            <input type="hidden" name="itemCode[]" value="<?php echo $row_comprole['mst_itemid']; ?>"/>
            <input type="hidden" name="noOfRows[]" value="<?php echo $m; ?>"/>
            <input type="hidden" name="so_det_id[]" value="<?php echo $did; ?>"/>
			<input type="hidden" name="bom_main_id[]" value="<?php echo $PcateID; ?>"/>
            <input type="hidden" name="act_group[]" value="<?php echo $AcGroup['group_name'];
            $s->GetFieldValue('sub_activity_group',"id='".$AcGroup['group_name']."'",'group_name'); ?>"/>
            <input type="hidden" name="act_name[]" value="<?php echo $AcGroup['id']; ?>"/>
        	<tr class="row1">
			<td align="left" valign="top"><?php echo $s->GetFieldValue('sub_activity_group',"id='".$AcGroup['group_name']."'",'group_name'); ?></td>
			<td align="left" valign="top"><?php echo $AcGroup['activity_name']; ?></td>
			<td align="left" valign="top"><textarea id="description<?php echo $m;?>" name="description[]" rows="1" style="width:100%;"><?php echo $resultval['work_description']; ?></textarea></td>
			<td align="left" valign="top"><input type="text" name="act_unit[]" value="<?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$AcGroup['unit']."'",'unitname'); ?>" readonly style="width:100%;height:20px;"/></td>
			<!--<td><input type="text" name="act_rate[]" id="act_rate_<?php echo $m; ?>" value="<?php echo $AcGroup['rate']; ?>" readonly style="width:80px;height:20px;"/></td>-->
			<td align="left" valign="top"><input type="text" name="ActQuantity[]" onchange="get_amount('<?php echo $m;?>');" value="<?php echo $resultval['quantity']; ?>" id="ActQuantity_<?php echo $m; ?>" style="width:100%;height:20px;" class="validate"/></td>
			<!--<td><input type="text" name="ActAmount[]" id="ActAmount_<?php echo $m; ?>" value="<?php echo $result['amount']; ?>" readonly style="width:80px;height:20px;" /></td>-->
			<td align="left" valign="top"><input name="fromdate1[]" value="<?php if($resultval['startdate']!=''){echo ConvertSQLDate($resultval['startdate']);}else{echo date('d/m/Y');} ?>" type="text"  class="datepicker" style="width:100%; height:20px;" /></td>
			<td align="left" valign="top"><input name="enddate1[]" value="<?php if($resultval['enddate']!=''){ echo ConvertSQLDate($resultval['enddate']);}else{echo date('d/m/Y');} ?>" type="text"  class="datepicker" style="width:100%; height:20px;"/></td>
			<td align="left" valign="top"><textarea id="act_r_<?php echo $m; ?>" name="text[]" style="width:100%;" rows="1"><?php echo $resultval['remarks']; ?></textarea></td>
			<!--<td><a href="index.php?route=PMS/Transcations/boq_templates_entry<?php echo $rurl;?>&action=delete&opn=<?php echo $id; ?>" onclick="return confirm('Are you sure you want to delete?')"> <img src="images/Icon_Delete.png" /></a></td>-->
		</tr>
            <?php }  ?>
            <?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,$m,$did,$bid);
            }//else{
            //    materialshow1($row_comprole['mst_itemid'], $spacebar,$totalqty1,$j,$_REQUEST['pid'],$function_filter,$m);
            //}
       }
	}
} 
?>
