<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 rate=document.getElementById("act_rate_"+i).value;
	var quanty=document.getElementById("complQty_"+i).value;
	
	var total=rate*quanty;
	document.getElementById("cmpAmt_"+i).value=total.toFixed(2);
	
}
function get_pend_amount(i)
{ 
	var pAmt=document.getElementById("ActAmount_"+i).value;
	var cAmt=document.getElementById("cmpAmt_"+i).value;
	
	var total=pAmt-cAmt;
	document.getElementById("pendQuty_"+i).value=total.toFixed(2);
	
}
function chk_qty(i)
{ 
	var pQty=document.getElementById("ActQuantity_"+i).value;
	var cQty=document.getElementById("complQty_"+i).value;
	if(cQty>pQty)
	{
	alert('Complete Quantity Cannot BE greater Than Project Quantity');
	}
}

</script>
<?php 
	  if($_REQUEST['savedata'])
	  {
	  	// pcs no.
		$substr = $_REQUEST['boq_id'];
	  	$data1['boq_id'] = $_REQUEST['boq_id'];
		$data1['branch'] = $_REQUEST['branch'];
		$data1['pcs_no'] = "BOQ-00".$substr;
		$data1['boq_no'] = $_REQUEST['boq_no'];
		$data1['boq_date'] = $_REQUEST['project_date'];
		$data1['assigned_to'] = $_REQUEST['assign_to'];
		$data1['sales_ord_no'] = $_REQUEST['order_no'];
		$data1['client'] = $_REQUEST['clients'];
		$data1['handled_by'] = $_REQUEST['handle_by'];
		$data1['slip_no'] = $_REQUEST['slip_no'];
		$data1['slip_date'] = FormatDateForSQL($_REQUEST['slip_date']);
		$data1['date'] = FormatDateForSQL($_REQUEST['cur_date']);
		$data1['companyID'] = $_SESSION['ACGCompanyId'];
		$s->RecordInsert('project_completion', $data1);
		$lstid= DB_Last_Insert_ID($db,'project_completion', "id");
		
		$data6['project_status'] = '1';	
		$condition6="boq_id='".$_REQUEST['boq_id']."'";
		$s->RecordEdit('project_completion',$data6,$condition6);
		
		$data7['project_status'] = '2';	
	$condition7="orderNo='".$_REQUEST['order_no']."'";
	 $s->RecordEdit('salesorder',$data7,$condition7);
		
		for($i=0;$i<count($_REQUEST['description']);$i++)
	{
		$datarray['master_id']=$lstid;
		$datarray['salesorder_id']=$_REQUEST['id'][$i];
		$datarray['complete_qty']=$_REQUEST['complQty'][$i];
		$datarray['complete_amt']=$_REQUEST['cmpAmt'][$i];
		$datarray['complete_date']=FormatDateForSQL($_REQUEST['complDate'][$i]);
		$datarray['pending_qty']=$_REQUEST['pendQuty'][$i];
		$datarray['remarks']=$_REQUEST['compl_remarks'][$i];
		$s->RecordInsert('project_completion_details',$datarray);
	}
		$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 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>
	<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']=='complete' && $_REQUEST['opn']!="")
{
$query = "select SO.id,SO.branch,SO.client,SO.orderNo,SO.orderDate,BED.id as boq_id,BED.project_handeled_by ,BED.boq_no,BED.project_date,CM.c_name from boq_entry_details as BED inner join salesorder as SO on BED.salesorder_id = SO.id inner join clientmaster as CM on CM.id = SO.client where  SO.project_status ='1' and BED.id='".$_REQUEST['opn']."' and SO.CompanyId='".$_SESSION['ACGCompanyId']."'";
$res = db_query($query,$db) or die(DB_error());
$result = DB_fetch_array($res);
extract($result);
$i=0;
  ?>
<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="">
	<input type="hidden" name="boq_id" value="<?php echo $_REQUEST['opn']; ?>">
   		<td height="33" class="label">Branch</td>
   		  <td><input type="text" name="branch" value="<?php echo $branch; ?>" /></td>
   		  <td class="label" >PCS No</td>
	 	<td ><input type="text" name="pcs_no" value="will be auto generated" readonly /></td>
	 	<td class="label">Date</td>
		<td><input name="cur_date" type="text" value="<?php echo ConvertSQLDate(date('Y-m-d')); ?>" readonly /></td>
	</tr>
	
	<tr>
		 <td class="label">BOQ No</td>
		 <td><input type="text" name="boq_no" value="<?php echo $boq_no; ?>" readonly/></td>
		 <td class="label">BOQ Date</td>
		 <td><input type="text" name="project_date" value="<?php echo ConvertSQLDate($project_date); ?>" readonly/></td>
		 <td class="label">Assigned To</td>
		 <td><input type="text" name="assign_to" 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'); }
		 ?>"> </td>
	</tr>
	<tr> 
		 <td class="label">Sales Order No</td>
		 <td><input type="text" name="order_no"  value="<?php echo $orderNo; ?>" readonly /></td>
		 <td class="label">SO Date</td>
		 <td><input type="text" name="so_date" value="<?php echo ConvertSQLDate($orderDate); ?>" readonly/></td>
		 <td class="label">Client</td>
		 <td><input type="text" name="clients" value="<?php echo $s->GetfieldValue('clientmaster',"id='".$client."'",'c_name');?>" readonly>		 </td>
  </tr>
  	<tr> 
		 <td class="label">Handled By</td>
		 <td><input type="text" name="handle_by"  value="<?php echo $project_handeled_by; ?>" /></td>
		 <td class="label">Slip No. <font style="color:#FF0000;">*</font></td>
		 <td><input type="text" name="slip_no"/></td>
		 <td class="label">Slip Date <font style="color:#FF0000;">*</font></td>
		 <td><input type="text" class="datepicker" name="slip_date" ></td>
  </tr>
</table>

	</div>
	<br />
<!-- TAble 2 Start -->
<!-- TAble 2 Close -->
</div><!-- Body Content Close -->
<table border="1" width="100%" cellpadding="2" cellspacing="4">
  <tr class="rowdata">
    <td class="label">Item Name</td>
    <td class="label">Activity Group</td>
    <td class="label">Activity Name</td>
    <td class="label" style="width:23px;">Description</td>
    <td class="label">Unit</td>
    <td class="label">Project Rate</td>
    <td class="label">Project Quantity</td>
    <td class="label">Project Amount</td>
    <td class="label">Project Start Date</td>
    <td class="label">Complete Quantity</td>
    <td class="label">Complete Amount</td>
    <td class="label">Completion Date</td>
    <td class="label">Pending Quantity</td>
    <td class="label">Remark</td>
  </tr>
  <?php 
 $query = "SELECT salesorder.*,boq_entry.*, boq_entry.id as mainID FROM salesorder INNER JOIN boq_entry ON salesorder.id=boq_entry.salesorder_id WHERE salesorder.CompanyId='".$_SESSION['ACGCompanyId']."' and boq_entry.salesorder_id='$id'";
	$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)){
	 $x = $result['ItemCode'];
	$m++;
	$amo[] = $m;
	
	?>
  <input type="hidden" name="salesorder_table_id;" value="<?php echo $salesorder_table_id;?>" />
  <?php
	$Subcat=$s->GetFieldVAlue('item_master',"id='".$result['ItemCode']."'",'SubCategoryID');
	$sqlactivityGroup=DB_query("select * from sub_activity_defination where inventory_group_id='".$Subcat."'",$db);
		while($AcGroup=DB_fetch_array($sqlactivityGroup)){
			$SQLActivitys=DB_query("select * from sub_activity_defination where group_name='".$AcGroup['group_name']."'",$db);
			
			while($ActiviesDeta=DB_fetch_array($SQLActivitys)) {
			 $y++;
	?>
  <input type="hidden" name="id[]" value="<?php echo $id;?>" />
  <tr class="rowdata">
    <td><?php echo $s->GetFieldVAlue('item_master',"id='".$result['ItemCode']."'",'item_name'); ?></td>
    <td><?php echo $s->GetFieldValue('sub_activity_group',"id='".$AcGroup['group_name']."'",'group_name') ;?></td>
    <td><?php echo $ActiviesDeta['activity_name']; ?></td>
    <td><textarea id="description<?php echo $m;?>" readonly name="description[]"> </textarea></td>
    <td ><input type="text"  name="act_unit[]" value="<?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$ActiviesDeta['unit']."'",'unitname'); ?>" readonly style="width:80px;height:50px;"/></td>
    <input type="hidden" name="item_id[];" value="<?php echo $x; ?>" />
    <input type="hidden" name="activity_name_id[]" value="<?php echo $ActiviesDeta['id'];?>" />
    <input type="hidden" name="activity_group[];" value="<?php echo $AcGroup['group_name'];?>" />
    <input type="hidden" name="product_group[]" value="<?php echo $AcGroup['group_name']; ?>" />
    <input type="hidden" name="activityunit[]" value="<?php echo $ActiviesDeta['unit']; ?>" />
    <td><input type="text" name="act_rate[]" id="act_rate_<?php echo $y; ?>" value="<?php echo $ActiviesDeta['rate']; ?>" readonly style="width:80px;height:50px;"/></td>
    <td><input type="text" name="ActQuantity[]" id="ActQuantity_<?php echo $y; ?>" style="width:80px;height:50px;" class="validate"/ readonly value = "<?php echo $result['quantity']; ?>" /></td>
    <td><input type="text" name="ActAmount[]" id="ActAmount_<?php echo $y; ?>" value = "<?php echo $result['amount']; ?>" readonly style="width:80px;height:50px;" /></td>
    <td><input name="fromdate1[]" type="text" readonly value = "<?php echo ConvertSQLDate($result['startdate']); ?>"  style="width:80px;height:50px;" /></td>
    <td><input name="complQty[]" id="complQty_<?php echo $y; ?>" type="text"  onkeyup="chk_qty('<?php echo $y;?>');" style="width:80px;height:50px;"/></td>
    <td><input type="text" name="cmpAmt[]"  id="cmpAmt_<?php echo $y; ?>"  style="width:80px;height:50px;" onfocus="get_amount('<?php echo $y;?>');"/></td>
    <td><input name="complDate[]" id="complDate_<?php echo $m; ?>" type="text" class="datepicker" style="width:80px;height:50px;"/></td>
    <td><input name="pendQuty[]" id="pendQuty_<?php echo $y; ?>" type="text" onfocus="get_pend_amount('<?php echo $y;?>');"  style="width:80px;height:50px;"/></td>
    <td><textarea id="compl_remarks_<?php echo $m;?>" name="compl_remarks[]"> </textarea></td>
  </tr>
  <?php }?>
  <?php }  ?>
  <?php }
$string1 = implode('_', $amo); ?>
  <tr>
    <td> Total Amount </td>
    <td><div id = "amt" > </div>
        <input type = "text" id="totAmount" name = "totAmount"  onfocus="get_tot_amount('<?php echo $amo;?>');" style="width:80px;height:50px;"/>
    </td>
  </tr>
  <tr>
    <td colspan="13" style="text-align:center;"><input type="submit" name="savedata" value="save" class="search" onclick="return validate1('<?php echo $m; ?>');" /></td>
  </tr>
</table>
</form>
<?php } ?>
