<?php

$table='master_boq';
$id=$_REQUEST['id'];
//**************************DELETE ITEMS AT EDITATION TIME****START HERE*******AMIT******************************
	  if($_REQUEST['DelRow'])
	  {
		for($i=0;$i<count($_REQUEST['mst_boq']);$i++)
		{
			 $del_item=DB_query("delete from boq_entry_details where id='".$_REQUEST['mst_boq'][$i]."'",$db);
		}
	  }
	  
//**************************DELETE ITEMS AT EDITATION TIME****END HERE*******AMIT******************************

	  

//*******************************************EDIT FIELD******START HERE*****************************
	  
	  
	  if($_REQUEST['action']=='Edit')
	  {
	  		$r=$s->GetRecord('boq_entry',  "id='".$id."'");
			$result=DB_query($r,$db);
			$row=DB_fetch_array($result);
			$refid=$row['refid'] ;
		   $remark=$row['remark'] ;
		   $groupid=$row['groupid'] ;
		   
		    $rdetail=$s->GetRecord('boq_entry_details',  "pid='".$id."'");
			$result_detail=DB_query($rdetail,$db);
	  }
//*******************************************EDIT FIELD******END HERE*****************************


	  
	  
	  
//*******************************************ADD NEW*****START HERE******************************
	  
	  if($_REQUEST['Save']   )
	  {
	 	 $Mstdata['refid']=$_REQUEST['refid'] ;
		 $Mstdata['remark']=$_REQUEST['remark'] ;
 		 $Mstdata['posted_date']=date('Y-m-d');
	   if( $_REQUEST['action']=='Addnew')
	   {
		 $r= $s->RecordInsert('boq_entry', $Mstdata);
		 $lstid= DB_Last_Insert_ID($db,'boq_entry', $id);
 		}
		else
		{
			 $lstid=$id;
			 $condition1="id='".$id."'";
			 $r= $s->RecordEdit('boq_entry', $Mstdata,$condition1);
			 $condition="pid='".$id."'";
			 $s->DeleteRecord('boq_entry_details',  $condition);
		}		 
	  		for($i=0;$i<count($_REQUEST['boqid']);$i++)
			{
			$data['pid']=$lstid;
			$data['groupid']=$_REQUEST['groupid'][$i] ;
			$data['boqid']=$_REQUEST['boqid'][$i];
			$data['rate']=$_REQUEST['rate'][$i];
			$data['unit']=$_REQUEST['unit'][$i];	
			$data['qty']=$_REQUEST['qty'][$i];
			$data['amount']=$_REQUEST['qty'][$i]*$_REQUEST['rate'][$i];
 			
				$row= $s->RecordInsert('boq_entry_details', $data);
				 
				if(!$row) 
				{
				   die('Error'.DB_error());
				}
			}
			unset($_SESSION['ACGBOQ']);
			
			$s->pageLocation("index.php?route=PMS/Transcations/ViewBOQ".$rurl);
	  }
//*******************************************ADD NEW*****END HERE*********************************************	  
	  
	  
	  

//*******************************************UNSET THE SESSION AFTER PRESSING CENCEL BUTTON***START HERE*********
	  
if($_REQUEST['cencel'])
{
	unset($_SESSION['ACGBOQ']);
	unset($_SESSION['refid']);
	unset($_REQUEST['remark']);
	unset($_SESSION['ACGBOQEDIT']);
}	  

//*******************************************UNSET THE SESSION AFTER PRESSING CENCEL BUTTON***END HERE*********	  

if($_REQUEST['action']=='Edit')
{
	$action='Edit';
	$id=$_REQUEST['id'];
}
else
{
	$action='Addnew';
}

if($_REQUEST['Add_Sche_BOQ_Temp'])
{
	$_SESSION['refid']=$_REQUEST['refid'];
	$_SESSION['remark']=$_REQUEST['remark'];
}
if($_REQUEST['storeses']=='1')
{
	$ref_id=$_SESSION['refid'];
	$remark1=$_SESSION['remark'];
}
?>
	<link rel="stylesheet" href="css/new_mxerp.css">
	<div class="body_contain">
	<div class="tab_div">

	<!--<script src="js1/jquery-1.7.2.js"></script>-->
	<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>

<form action="" method="post" >

<?php
if(isset($_REQUEST['action']) && isset($_REQUEST['rid'])):
$DOCsql=DB_query("SELECT salesorder.branch,salesorder.id,salesorder.orderDate,salesorder.orderNo,salesorder.client,salesorder.customerPODate,salesorder.PORefNo,salesorder_details.Amount, salesorder_details.ApprovalStatus FROM salesorder INNER JOIN salesorder_details ON salesorder.id=salesorder_details.masterId WHERE salesorder_details.ApprovalStatus=3
and salesorder.CompanyId='".$_SESSION['ACGCompanyId']."' and salesorder.project_status='1' ",$db);
if(DB_num_rows($DOCsql)>0){
$i=0;
	  $Doc=DB_fetch_array($DOCsql);
	  extract($Doc);
	  //echo $id
	  
	 $auto_boqno = "BOQ-00".$_REQUEST['rid'];
  ?>
<h1>BOQ 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="1" cellspacing="0" cellpadding="3" class="">
   	<tr class="row1">
   		<td class="label">Branch</td>
   		<td><input type="text" name="branch" value="<?php echo $branch; ?>"></td>
    	<td class="label" >BOQ No</td>
	 	<td ><input type="text" name="boq_no" value="<?php echo $auto_boqno; ?>" readonly /></td>
	 	<td class="label">Date</td>
		<td><input name="boq_date" type="text" value="<?php echo $_REQUEST['fromdate']; ?>"  class="datepicker"/></td>
	</tr>
	
	<tr>
	 <td class="label">Order No</td>
	 <td><input type="text" name="order_no" value="<?php echo $orderNo; ?>" readonly/></td>
	 <td class="label">Order Date</td>
	 <td><input type="text" name="order_date" value="<?php echo $orderDate; ?>" readonly/></td>
	 <td class="label">Client Name</td>
	 <td><input type="text" name="boq_client" value="<?php echo $s->GetfieldValue('clientmaster',"id='".$client."'",'c_name');?>"></td>
	</tr>
	<tr> 
	 <td class="label">Po No</td>
	 <td><input type="text" name="po_no"  value="<?php echo $PORefNo; ?>" readonly /></td>
	 <td class="label">Po Date</td>
	 <td><input type="text" name="po_date" value="<?php echo $customerPODate; ?>" readonly/></td>
	 <td class="label">Handled By</td>
	 <td><input type="text" name="handeled_by" value="" readonly></td>
  </tr>
  <tr class="row1">
    <td colspan="16" style="text-align:right;" class="tabledata"><input name="Add_Sche_BOQ_Temp" type="submit" class="submit_button" value="Add BOQ Template" onclick="openboqtemp()" />
  &nbsp;&nbsp;&nbsp;&nbsp;
	</td>
  </tr>
</table>
<?php } ?>
<?php endif; ?>





	</div>
	<br />
<!-- TAble 2 Start -->
<!-- TAble 2 Close -->
</div><!-- Body Content Close -->
</form>

<!---------------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----------------------------------------->

<?php if($_REQUEST['action']=='convert'){?>

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>
<?php } ?>
<table border="1" width="100%" cellpadding="2" cellspacing="4">
	<tr>
		<td class="label">Item</td>
		<td class="label">Activity Group</td>
		<td class="label">Activity Name</td>
		<td></td>
	</tr>
<?php 
		$query = "SELECT salesorder.id,salesorder_details.masterId,salesorder_details.ItemCode FROM salesorder INNER JOIN salesorder_details ON salesorder.id=salesorder_details.masterId WHERE salesorder_details.ApprovalStatus='3' AND salesorder.CompanyId='".$_SESSION['ACGCompanyId']."'";

	$res = DB_query($query,$db) or die(DB_error());
	//$result = DB_fetch_array($res);
	while($result = DB_fetch_array($res)){
	$item_name = $s->GetfieldValue('item_master',"id='".$result['ItemCode']."'",'item_name');
	$category_id = $s->GetfieldValue('item_master',"id='".$result['ItemCode']."'",'CategoryID');
	if(isset($item_name))
	{
	?>
		<tr>
		<td><?php echo $item_name; ?></td>
		<td><?php echo $category_id; ?></td>
		<td></td>
		<td></td>
		</tr>
		
		<tr>
		<td></td>
		<td></td>
		<td>product Items</td>
		<td></td>
		</tr>
		
<?php } }?>
</table>