<?php 
if(isset($_POST['Save']))
{
	if($_REQUEST['work_order_date']=='')
	{
	$work_order_date1=date('Y-m-d');
	}
	else
	{
	$work_order_date1=$_REQUEST['work_order_date'];
	}
	$data['project']=$_REQUEST['project'];
	$data['block']=$_REQUEST['block'];
	$data['contractor_category']=$_REQUEST['contractor_category'];
	$data['contractor']=$_REQUEST['contractor'];
	$data['work_order_date']=FormatDateForSQL($work_order_date1);
	$data['work_order_no']=$_REQUEST['work_order_no'];
	$data['approval_status']='Pending';
	$data['work_order_start_date']=FormatDateForSQL($_REQUEST['work_order_start_date']);
	$data['work_order_completion_date']=FormatDateForSQL($_REQUEST['work_order_completion_date']);
	$saved=$s->RecordInsert('pms_sub_preparation',$data);
	$lastId=DB_Last_Insert_ID($db,'pms_sub_preparation','id');
	if($lastId!='')
	{
	for($k=0;$k<count($_REQUEST['groupid']);$k++)
	{
	$datarray['parentid']=$lastId;
	$datarray['group_id']=$_REQUEST['groupid'][$k];
	$datarray['activity_id']=$_REQUEST['activity_id'][$k];
	$datarray['unit']=$_REQUEST['unit'][$k];
	$datarray['qty']=$_REQUEST['Item_qty'][$k];
	$datarray['rates']=$_REQUEST['Item_rate'][$k];
	$datarray['amt']=$_REQUEST['Item_amount'][$k];
	$datarray['net_amt']=$_REQUEST['Net_Amount'][$k];
	$datarray['text']=$_REQUEST['Item_des'][$k];
	$insert=$s->RecordInsert('pms_sub_preparation_child',$datarray);
	//print_r($datarray);
	
	}
	
	}
	unset($_SESSION['Item_List1']);
	unset($_SESSION['text']);
	unset($_SESSION['project']);
	unset($_SESSION['block']);
	unset($_SESSION['contractor_category']);
	unset($_SESSION['contractor']);
	unset($_SESSION['work_order_date']);
	unset($_SESSION['work_order_no']);
	$_SESSION['success_msg']="Saved Successfull";
	$s->pageLocation("index.php?route=PMS/Transcations/work_order_preparation_index".$rurl);
	}


if($_REQUEST['add_works'])
{
	$_SESSION['project']=$_REQUEST['project'];
	$_SESSION['block']=$_REQUEST['block'];
	$_SESSION['contractor_category']=$_REQUEST['contractor_category'];
	$_SESSION['contractor']=$_REQUEST['contractor'];
	$_SESSION['work_order_date']=$_REQUEST['work_order_date'];
	$_SESSION['work_order_no']=$_REQUEST['work_order_no'];
	$_SESSION['work_order_start_date']=$_REQUEST['work_order_start_date'];
	$_SESSION['work_order_completion_date']=$_REQUEST['work_order_completion_date'];
}
if(!empty($_SESSION['Item_List1']))
	{
	$project=$_SESSION['project'];
	$block=$_SESSION['block'];
	$contractor_category=$_SESSION['contractor_category'];
	$contractor=$_SESSION['contractor'];
	$work_order_date=$_SESSION['work_order_date'];
	$Work_Order_No=$_SESSION['work_order_no'];
	$work_order_start_date=$_SESSION['work_order_start_date'];
	$work_order_completion_date=$_SESSION['work_order_completion_date'];
	
}
//if(isset($_POST['save']) && $_REQUEST['action']=='add')
if($_REQUEST['DeleteRow'])		 
{
	$arr=array();
	// print_r($_SESSION['CRMLeadItem']);
	$ListItem=explode('#',$_SESSION['Item_List1']);
	//print_r($ListItem);
	for($i=0;$i<count($_REQUEST['delrow']);$i++)
	{
	//echo $_REQUEST['delitem'][$i].'<br>';  
	$key = array_search($_REQUEST['delrow'][$i], $ListItem );		 
	if($key!=false || $key==0)
	unset($ListItem[$key]);
	$itemlist1=implode("#",$ListItem);
	$_SESSION['Item_List1']=$itemlist1;
	} 
	// print_r($_SESSION['Item_List1']);
}

if($_REQUEST['cancel'])
{
	unset($_SESSION['Item_List1']);
}
?>
<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();
	});
	</script>
<script type="text/javascript">
function calc_amt(ctr)
{
	var qty=document.getElementById("Item_qty"+ctr).value;
	var rate=document.getElementById("Item_rate"+ctr).value;
	var amt=parseInt(qty*rate);
	document.getElementById("Item_amount"+ctr).value=amt; 
	document.getElementById("Net_Amount"+ctr).value=amt;
}
function getblock(str)
{
	//alert(str);
	var xmlhttp;
	if(str=='')
	{
	document.getElementById("BId").innerHTML='';
	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)
	{
	var data=xmlhttp.responseText;
	document.getElementById("BId").innerHTML=data;
	}
	}
	xmlhttp.open("GET","block_ajax1.php?q="+str,true);
	xmlhttp.send();
}
			
			
function getcontractor(str)
{
	var site=document.getElementById("project").value;
	var xmlhttp;
	if(str=='')
	{
	document.getElementById("C_id").innerHTML='';
	document.getElementById("Work_id").innerHTML='';
	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)
	{
	var data=xmlhttp.responseText.split("__");	 
	//alert(data);
	document.getElementById("C_id").innerHTML=data[0];
	document.getElementById("Work_id").innerHTML=data[1];
	}
	}
	xmlhttp.open("GET","contractor_ajax.php?r="+str+"&site="+site,true);
	xmlhttp.send();
}
			
function showsitename160(str)
{
	var site=document.getElementById("project").value;
	var xmlhttp;
	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=xmlhttp.responseText.split("__");	
	document.getElementById("storedata7").innerHTML=data[0];
	document.getElementById("Work_id").innerHTML=data[1];
	}
	}
	xmlhttp.open("GET","get_contractor_code.php?r="+str+"&site="+site,true);
	xmlhttp.send();
	//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);
}
</script>
<form method="post" action="">
  <div class="tender_main_page">
    <h1>Work Order Preparation</h1>
    <!-- Body Content Start -->
    <div id="body_content">
      <!-- TAble 1 Start -->
      <div>
        <table width="100%" border="0" cellspacing="0" cellpadding="3">
          <!--  <tr class="row1">
    <td style="width:120px;" class="tabledata">Tender Project</td>
    <td><input style="width:680px;" class="input_border" /></td>
  </tr> -->
          <tr class="row1">
            <td class="tabledata">Project</td>
            <td><select name="project" id="project" onChange="getblock(this.value)"  class="input_border"  style="width:250px;">
                <option value="">---select---</option>
                <?php 
	$site="select loccode,locationname from locations";
	$result=DB_query($site,$db);		
	while($mrow=DB_fetch_array($result))
	{
		
					$sel='';
					if($project==$mrow['loccode'])
					$sel="selected";
		
		?>
                <option value="<?php echo $mrow['loccode']?>" <?php echo $sel;?>><?php echo $mrow['locationname']?></option>
                <?php } ?>
              </select></td>
            <td class="tabledata">Block</td>
            <td><div id="BId">
                <select name="block"  class="input_border" style="width:250px;" >
                  <option value="">---select---</option>
                  <?php $blockvalue="select * from sub_block_master";
			$blocks=DB_query($blockvalue,$db);
			while($myblock=DB_fetch_array($blocks))
			{
					$sel='';
					if($block==$myblock['id'])
					$sel="selected";
			?>
                  <option value="<?php echo $myblock['id']?>"<?php echo $sel;?> ><?php echo $myblock['block_name']?></option>
                  <?php } ?>
                </select>
              </div></td>
          </tr>
          <tr class="row1">
            <td class="tabledata">Contractor Name </td>
            <td><select name="contractor_category"   id="contractor_category" onChange="showsitename160(this.value)" class="input_border" style="width:250px;">
                <option value="">---select---</option>
                <?php 
		$contractorvalues="select * from sub_contractor_definition";
		$contractors=DB_query($contractorvalues,$db);
		while($mycontractors=DB_fetch_array($contractors))
		{
					$sel='';
					if($contractor_category==$mycontractors['id'])
					$sel="selected";
			
			?>
                <option value="<?php echo $mycontractors['id'];?>"<?php echo $sel;?>><?php echo $mycontractors['contractor_name']; ?></option>
                <?php }?>
              </select></td>
           <td class="tabledata">Contractor Code </td>
            <td><div id="storedata7">
                <select name="contractor" id="contractor"  onchange="generate_workorderno(this.value)"  class="input_border" style="width:250px;">
                  <option value="">---select---</option>
                  <?php 
		$cont="select * from sub_contractor_definition";
		$contract=DB_query($cont,$db);
		while($mycontractor=DB_fetch_array($contract))
		  {  
			  
					$sel='';
					if($contractor==$mycontractor['id'])
					$sel="selected";
			?>
                  <option value="<?php echo $mycontractor['id']; ?>"<?php echo $sel;?>><?php echo $mycontractor['contractor_category']; ?></option>
                  <?php  }  ?>
                </select>
              </div></td>
          </tr>
          <tr class="row1">
            <td class="tabledata">Work Order Date</td>
            <td><!--<input type="text"   style="width:250px;"  name="sub_preparation[work_order_date]" value="<?php if($_REQUEST['action']=='edit' || isset($_SESSION['Item_List1'])) echo $work_order_date ; else echo date('d-m-y');?>" id="work_order_date" class="datepicker input_border" />-->
			
			<!--<input id="work_order_date" class="datepicker hasDatepicker" type="text" value="" name="sub_preparation[work_order_date]">-->
			
			<input type="text" style="width:250px;" name="work_order_date" id="work_order_date" class="datepicker" value="<?php echo $work_order_date;?>"  />
			</td>
            <td class="tabledata">Work Order No.</td>
            <td><div id="Work_id">
			<input type="text" style="width:250px;"  name="work_order_no"  id="work_order_no"  value="<?php echo $Work_Order_No?>" />
			
                
              </div></td>
          </tr>
          <tr class="row1">
            <td class="tabledata">Work Order Start Date</td>
            <td><!--<input type="text"   style="width:250px;"  name="sub_preparation[work_order_start_date]" value="<?php if($_REQUEST['action']=='edit' || isset($_SESSION['Item_List1'])) echo  $work_order_start_date ; else echo date('d-m-y');?>" id="work_order_date" class="datepicker" />-->
			
			<input type="text" style="width:250px;" name="work_order_start_date" id="work_order_start_date" class="datepicker" value="<?php echo $work_order_start_date;?>"  />
			</td>
            <td class="tabledata">Work Order Completion Date </td>
            <td><!--<input type="text" name="sub_preparation[work_order_completion_date]" value="<?php if($_REQUEST['action']=='edit' || isset($_SESSION['Item_List1'])) echo  $work_order_completion_date ; else echo date('d-m-y');?>" id="work_order_no"   class="input_border" style="width:250px;"  />-->
			
			<input type="text" style="width:250px;" name="work_order_completion_date" id="work_order_completion_date" class="datepicker" value="<?php echo $work_order_completion_date;?>"  />
			</td>
          </tr>
        </table>
      </div>
      <!-- TAble 1 Close -->
      <br />
      <!-- TAble 2 Start -->
      <div>
        <div class="cl"></div>
        <div style="z-index:-1000em;">
          <div class="demo">
            <div id="tabs">
              <ul>
                <li>
                 <!-- <input type="submit" name="add_works" value="Add Works" onClick="popup('work_order_preparation_addworks1.php?<?php echo $rurl.'&action='.$_REQUEST['action'].'&pre='.$_REQUEST['pre']; ?>',1000,600)" class="#tabs-1"/>-->
				 
				  <!--<input type="submit" name="add_works" value="Add Works" onClick="popup('work_order_preparation_addwork.php?<?php echo $rurl.'&action='.$_REQUEST['action'].'&pre='.$_REQUEST['pre']; ?>',1000,600)" class="#tabs-1"/>-->
				  
				  
				  <a href="#tabs-1">Add Works</a>
                </li>
                <li><a href="#tabs-2">Material Recovery Details</a></li>
                <li><a href="#tabs-3">Work Order Details</a></li>
              </ul>
              <div style="width:800px; overflow:auto;" id="tabs-1">
			  <fieldset>
                <table width="100%" border="1" cellspacing="0" cellpadding="0">
				<tr>
			<?php 
  if(isset($_SESSION['Item_List1'])){?>
   <td  class="tabledata">
				<input type="submit" name="add_works" value="Add Works" onClick="popup('work_order_preparation_addwork.php?<?php echo $rurl.'&action='.$_REQUEST['action'].'&pre='.$_REQUEST['pre']; ?>',1000,600)" class="submit_button" />
				</td>
				<td class="tabledata" colspan="9">

     <input type="submit" name="DeleteRow" value="Delete" class="submit_button"   onclick="return confirm('Are you sure you want to delete?')"/>
				</td>
				 <?php } else { ?>
				 
				 <td  class="tabledata" colspan="10">
				<input type="submit" name="add_works" value="Add Works" onClick="popup('work_order_preparation_addwork.php?<?php echo $rurl.'&action='.$_REQUEST['action'].'&pre='.$_REQUEST['pre']; ?>',1000,600)" class="submit_button" />
				</td>
				 
				 <?php } ?>
				</tr>
                  <tr class="row2">
                    <td class="tabledata">Remove</td>
                   <!-- <td class="tabledata">Sl. No.</td>-->
                    <td class="tabledata">PBSE Element</td>
                    <td class="tabledata">Activity</td>
                    <td class="tabledata">Unit</td>
                    <td class="tabledata">Qty</td>
                    <td class="tabledata">Rate</td>
                    <td class="tabledata">Amount</td>
                    <td class="tabledata">Net Amount</td>
                    <td class="tabledata">Description</td>
                  </tr>
                  <?php 

		 //echo $_SESSION['Item_List1'];
		 $ItemList12=explode("#",$_SESSION['Item_List1']);
		  for($i=0;$i<count($ItemList12);$i++)
		  {
		    $q="select * from master_boq where id ='".$ItemList12[$i]."'";
			$qq=DB_query($q,$db);
			while($qqq=DB_fetch_array($qq)){
			
  ?>
                  <tr bgcolor="#e7e7e7">
                    <td style="text-align:center;"><input type="checkbox" name="delrow[]" value="<?php echo $qqq['id']?>" /></td>
                   <!-- <td style="width:10px;"><?php echo $i+1;?></td>-->
                    <td><?php echo $s->GetFieldValue('master_boq',"id='".$qqq['parentid']."'",'group_title');?>
					<input type="hidden" name="groupid[<?php echo $i;?>]" id="groupid" value="<?php echo $s->GetFieldValue('master_boq',"id='".$qqq['parentid']."'",'id');?>" />
					</td>
                    <td><?php  echo  $qqq['group_title'];
					//echo $qqq['activity_name'];?></td>
                    <td><?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$qqq['unit']."'",'unitname') ;?>
					<input type="hidden" name="unit[<?php echo $i;?>]" id="unit" value="<?php echo $qqq['unit']; ?>" />
					</td>
                    <td><input type="text" name="Item_qty[<?php echo $i;?>]"  id="Item_qty<?php echo $i;?>" class="input_border" style="width:95px;"/></td>
                    <td><input type="text" name="Item_rate[<?php echo $i;?>]"  id="Item_rate<?php echo $i;?>"class="input_border" style="width:95px;"/></td>
                    <td><input type="text" name="Item_amount[<?php echo $i;?>]"   id="Item_amount<?php echo $i;?>"class="input_border" style="width:100px;" onFocus="calc_amt(<?php echo $i;?>)"/></td>
                    <td><input type="text" name="Net_Amount[<?php echo $i;?>]"  id="Net_Amount<?php echo $i;?>"class="input_border" style="width:100px;" /></td>
                    <td><input type="text" name="Item_des[<?php echo $i;?>]"  id="Item_des<?php echo $i;?>"class="input_border" style="width:100px;"/>
                      <input type="hidden" name="activity_id[<?php echo $i;?>]" value="<?php echo $qqq['id'];?>"/>
                    </td>
                  </tr>
                  <?php 
  }
 
  }
  ?>
                </table>
				</fieldset>
              </div>
              <!-- Tab 1 Close -->
              <div id="tabs-2">
                <fieldset>
                Fielset set 2
                </fieldset>
              </div>
              <!-- Tab 2 Close -->
              <div id="tabs-3">
                <fieldset>
                Fielset set 3
                </fieldset>
              </div>
              <!-- Tab 3 Close -->
            </div>
          </div>
        </div>
      </div>
      <!-- TAble 2 Close -->
    </div>
    <!-- Body Content Close -->
    <div style="height:30px;"></div>
    <div class="button_nav">
      <input type="submit" value="Save"  name="Save"  class="submit_button" />
      &nbsp;&nbsp;&nbsp;&nbsp;
      <input type="submit" value="Cancel" name="cancel" class="submit_button" />
    </div>
  </div>
</form>
