<?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 href="css/tender_project.css" rel="stylesheet" type="text/css" />

	<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" >
<div class="tender_main_page">
<h1>BOQ Template Entry</h1>
<!-- Body Content Start -->

<div id="body_content">
	<!-- TAble 1 Start --><div style="width:810px; overflow:auto; margin-left:auto; margin-right:auto;">
	
	<table width="100%" border="0" cellspacing="0" cellpadding="3">
   <tr class="row1">
    <td class="tabledata">Ref. No.</td>
    <td colspan="3">
	 <input name="refid" type="text" value="<?php if($_REQUEST['storeses']=='1'){ echo $ref_id; }else{ echo $refid; }s?>" /></td>
  </tr>
  <tr class="row1">
    <td class="tabledata">Remarks</td>
     <td colspan="3"><textarea class="input_border"  name="remark" style="width:600px; height:70px;"><?php if($_REQUEST['storeses']=='1') { echo $remark1;} else{echo $remark;}s?></textarea></td>
  </tr>
  <tr class="row1">
    <td class="tabledata">&nbsp;</td>
    <td colspan="3" style="text-align:right;" class="tabledata"><input name="Add_Sche_BOQ_Temp" type="submit" class="submit_button" value="Add Schedule BOQ Template" onclick="openboqtemp()" />
  &nbsp;&nbsp;&nbsp;&nbsp;
	<input type="button" value="Add New BOQ Schedule" class="submit_button" onclick="openboq()" />
	</td>
  </tr>
</table>
	</div>
	<br />
<!-- TAble 2 Start -->
<?php  //print_r($_SESSION['ACGBOQ']);?>
<input name="DelRow" value="DelRow" type="submit" />
<div style="width:800px; overflow:auto;">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr class="row2">
    <td class="tabledata">Remove</td>
	<td class="tabledata">BOQ Group</td>
 	<td class="tabledata">BOQ Name</td>
 	<td class="tabledata">Qty</td>
	<td class="tabledata">Rate</td>
	<td class="tabledata">Unit</td>
	<td class="tabledata">Amount</td>
</tr>
<?php 
if($_REQUEST['action']=='Addnew')
{
//print_r($_SESSION['ACGBOQ']);
 $boq=explode("#",$_SESSION['ACGBOQ']);
  for($i=0;$i<count($boq);$i++)
  {
  	$sql=DB_query("Select * from master_boq where  parentid!=0 and  id='".$boq[$i]."'",$db); 	
	 $myrow=DB_fetch_array($sql);
   ?>
  <tr bgcolor="#FFFFFF" onmouseover="this.style.backgroundColor='#fffeb3'; this.style.color='#00f';" onmouseout="this.style.backgroundColor='#FFF'; this.style.color='#000000';">
    <td class="tabledata1">
	<input name="groupid[]" type="hidden" value="<?php echo $s->GetFieldValue('master_boq',"id='".$myrow['parentid']."'" ,'id')?>"  />
	<input  name="mst_boq[]"  type="checkbox"  value="<?php  echo $myrow['id'];?>"/></td>
    <td class="tabledata1">
	<input  name="boqid[]"  type="hidden"  value="<?php  echo $myrow['id'];?>"/>
	<?php 
 	echo  $s->GetFieldValue('master_boq',"id='".$myrow['parentid']."'" ,'group_title');
	// echo $myrow['id'];?></td>
    <td class="tabledata1"><?php  echo $myrow['group_title'];?></td>
    <td class="tabledata1"><?php  echo $myrow['qty'];?><input name="qty[]"  id="qty_<?php echo $i ?>" type="hidden" value="<?php  echo $myrow['qty'];?>" /></td>
    <td class="tabledata1"><?php  echo $myrow['rate'];?><input name="rate[]"  id="rate_<?php echo $i ?>"  type="hidden" value="<?php  echo $myrow['rate'];?>" /></td>
    <td class="tabledata1"><?php  echo $s->GetFieldValue('unitsofmeasure',"unitid='".$myrow['unit']."'",'unitname');?>
	<input name="unit[]"  id="unit_<?php echo $i ?>"  type="hidden" value="<?php  echo $myrow['unit'];?>" /></td>
    <td class="tabledata1"><?php  echo $myrow['amount'];?><input name="amount[]" id="amount_<?php echo $i ?>"  type="hidden" value="<?php  echo $myrow['amount'];?>"  onfocus="getdate(<?php echo $i;?>)"/></td>
   </tr>  
  <?php } 
 } 
else if($_REQUEST['action']=='Edit'){
 $j=0;
while($row_detail=DB_fetch_array($result_detail))
{
//echo $j;
   ?>
  <tr bgcolor="#e7e7e7">
    <td class="tabledata1"><input  name="mst_boq[]"  type="checkbox"  value="<?php  echo $row_detail['id'];?>"/>
	</td>
    <td class="tabledata1">
	<input name="groupid[]" type="hidden" value="<?php echo $row_detail['groupid']?>"  />
	<input  name="boqid[]"  type="hidden"  value="<?php  echo $row_detail['boqid'];?>"/>
	<?php 
  $gpid=	$s->GetFieldValue('master_boq',"id='".$row_detail['boqid']."'" ,'parentid');
	 
 	echo  $s->GetFieldValue('master_boq',"id='".$gpid."'" ,'group_title');
	
	// echo $myrow['id'];?></td>
    <td class="tabledata1"><?php  echo  $s->GetFieldValue('master_boq',"id='".$row_detail['boqid']."'" ,'group_title');// echo $row_detail['group_title'];?>
	
 </td>
    <td class="tabledata1"><input name="qty[]"  id="qty_<?php echo $j; ?>" type="text" value="<?php  echo $row_detail['qty'];?>" /></td>
    <td class="tabledata1"><input name="rate[]"  id="rate_<?php echo $j; ?>"  type="text" value="<?php  echo $row_detail['rate'];?>" /></td>
    <td class="tabledata1"><input name="unit[]"  id="unit_<?php echo $j; ?>"  type="text" value="<?php  echo $row_detail['unit'];?>" /></td>
    <td class="tabledata1"><input name="amount[]" id="amount_<?php echo $j; ?>"  type="text" value="<?php  echo $row_detail['amount'];?>"  onfocus="getdate(<?php echo $j;?>)"/></td>
   </tr>  
 
<?php
$j++; 
} ?>
<?php
if(isset($_SESSION['ACGBOQEDIT']))
{
$boq=explode("#",$_SESSION['ACGBOQEDIT']);
  for($l=0,$k=$j;$l<count($boq);$l++,$k++)
  {
  //echo "Select * from master_boq where  parentid!=0 and  id='".$boq[$l]."'";
  	$sql=DB_query("Select * from master_boq where  parentid!=0 and  id='".$boq[$l]."'",$db); 	
	 $myrow=DB_fetch_array($sql);
   ?>
  <tr bgcolor="#e7e7e7">
    <td class="tabledata1">
	<input name="groupid[]" type="hidden" value="<?php echo $s->GetFieldValue('master_boq',"id='".$myrow['parentid']."'" ,'id')?>"  />
	<input  name="mst_boq[]"  type="checkbox"  value="<?php  echo $myrow['id'];?>"/></td>
    <td class="tabledata1">
	<input  name="boqid[]"  type="hidden"  value="<?php  echo $myrow['id'];?>"/>
	<?php 
 	echo  $s->GetFieldValue('master_boq',"id='".$myrow['parentid']."'" ,'group_title');
	
	// echo $myrow['id'];?></td>
    <td class="tabledata1"><?php  echo $myrow['group_title'];?>
	
 </td>
    <td class="tabledata1"><input name="qty[]"  id="qty_<?php echo $k; ?>" type="text" value="<?php  echo $myrow['qty'];?>" /></td>
    <td class="tabledata1"><input name="rate[]"  id="rate_<?php echo $k; ?>"  type="text" value="<?php  echo $myrow['rate'];?>" /></td>
    <td class="tabledata1"><input name="unit[]"  id="unit_<?php echo $k; ?>"  type="text" value="<?php  echo $myrow['unit'];?>" /></td>
    <td class="tabledata1"><input name="amount[]" id="amount_<?php echo $k; ?>"  type="text" value="<?php  echo $myrow['amount'];?>"  onfocus="getdate(<?php echo $k;?>)"/></td>
   </tr>  
  <?php }  ?>
<?php  	}
 }   
 ?>
</table>
</div>
<!-- TAble 2 Close -->
</div><!-- Body Content Close -->
 <div style="height:30px;"></div>

<div class="button_nav">
  <input name="Save" type="submit" class="submit_button" value="Save" />
  &nbsp;&nbsp;&nbsp;&nbsp;
	<input type="submit" name="cencel" value="Cancel" class="submit_button" />


</div>
</div>
</form>

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

<script>	
function openboq()
{
window.open("Addboq_sechedule_pms_html.php?action=add&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-----BY AMIT----------------------------------------->



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

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

function openboqtemp()
{
window.open("pms_popu_boq_entry.php?action=Edit&id=<?php echo $_REQUEST['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----------------------------------------->

<?php } else {?>
function openboqtemp()
{
window.open("pms_popu_boq_entry.php?action=Addnew&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");
	
}	
<?php } ?>


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