<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

		


// fetch Prefix
	$prefixCode = $s->GetFieldValue('prefix_tbl',"prefix_id='16'",'prefix_code');
		$retrieve = "SELECT * FROM boq_entry";
		$resultret = DB_query($retrieve,$db);
		if(DB_num_rows($resultret)>0)
		{
		$re = "SELECT MAX(id) as id FROM boq_entry";
		$exe = DB_query($re,$db);
		$maximumID=DB_fetch_array($exe);
		$pad =$maximumID['id']+1;
		$autoBoqNo = $prefixCode.'-00'.$pad;
		}
		else
		{
		$autoBoqNo = $prefixCode.'-001';
		}

	  if($_REQUEST['savedata'])
	  { 

	  	$data1['salesorder_id'] = $_REQUEST['so_id'];
		$data1['boq_no'] = $autoBoqNo;
		$data1['project_date'] = date('Y-m-d');
		$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'];
		$r= $s->RecordInsert('boq_entry', $data1);
		$last_id=DB_Last_Insert_Id($db,"boq_entry","id");
		if(DB_affected_rows($db)>0)
		{
		$flag = 1;
		$data6['project_status'] = '1';	
		$condition6="id='".$_REQUEST['sod_id']."'";
	 	$s->RecordEdit('salesorder_details',$data6,$condition6);
		}
		//die;
		
	  	$description = $_REQUEST['description'];
		$ActQuantity = $_REQUEST['ActQuantity'];
		$ActAmount = $_REQUEST['ActAmount'];
		$text = $_REQUEST['text'];
		
	  	for($i=0;$i<count($_REQUEST['noOfRows']);$i++)
		{ 
		$data['salesorder_id'] = $_REQUEST['so_id'];
		$data['work_description'] = $description[$i];
		$data['quantity'] = $ActQuantity[$i];
		$data['amount'] = $ActAmount[$i];
		$data['startdate'] =FormatDateFORSQL($_REQUEST['fromdate1'][$i]);
		$data['enddate'] =FormatDateFORSQL($_REQUEST['enddate1'][$i]);
		$data['remarks'] = $text[$i];
		$data['master_id'] = $last_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['grade'] = $_REQUEST['grade'][$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);
});

function validates()
	{
	
 	if(document.project_entry.handeled_by.value=='')
	{
		alert("Please Enter Handled By");
		(document.project_entry.handeled_by).focus();
		return false;
	}
}
</script>
<form action="" name="project_entry" method="post" >
<?php
if($_REQUEST['action']=='convert' && $_REQUEST['sid']!="")
{
$DOCsql=DB_query("SELECT * FROM salesorder WHERE id='".$_REQUEST['sid']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
if(DB_num_rows($DOCsql)>0){
$i=0;
	  $Doc=DB_fetch_array($DOCsql);
	  extract($Doc);
  ?>
<h1>Project Creation 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="0" cellspacing="0" cellpadding="3" class="">
	<input type="hidden" name="so_id" value="<?php echo $_REQUEST['sid']; ?>">
	<input type="hidden" name="sod_id" value="<?php echo $_REQUEST['sodid']; ?>">
   	<tr class="row1">	
		<td class="">Branch</td>
   		<td><input type="text" name="branch" style="width:150px;" value="<?php echo $branch; ?>"></td>
    	<td class="" >BOQ No</td>
	 	<td ><input type="text" name="boq_no" style="width:150px;" value="will be auto generated" readonly /></td>
	 	<td class="">Date</td>
		<td><input name="boq_date" type="text" style="width:150px;" value="<?php echo ConvertSQLDate(date('Y-m-d')); ?>"  class="datepicker"/></td>
	</tr>
	
	<tr class="row1">
		 <td class="">Order No</td>
		 <td><input type="text" name="order_no" style="width:150px;" value="<?php echo $orderNo; ?>" readonly/>
	      </td>
		 <td class="">Order Date</td>
		 <td><input type="text" name="order_date" style="width:150px;" value="<?php echo ConvertSQLDate($orderDate); ?>" readonly/></td>
		 <td class="">Client Name</td>
		 <td><input type="text" name="boq_client" style="width:150px;" readonly value="<?php echo $s->GetfieldValue('clientmaster',"id='".$client."'",'c_name');?>"></td>
	</tr>
	<tr class="row1"> 
		 <td class="">PO No</td>
		 <td><input type="text" name="po_no" style="width:150px;" value="<?php echo $PORefNo; ?>" readonly /></td>
		 <td class="">PO Date</td>
		 <td><input type="text" name="po_date" style="width:150px;" value="<?php echo ConvertSQLDate($customerPODate); ?>" readonly/></td>
		 <td class="">Handled By <font style="color:#FF0000;">*</font></td>
		 <td><input type="text" style="width:150px;" name="handeled_by"></td>
  </tr>
</table>

	</div>
	<br />
<!-- TAble 2 Start -->
<table border="1" width="100%" cellpadding="3" cellspacing="0">
<tr class="row1">
	<th class="">Activity Group</th>
	<th class="">Activity Name</th>
	<th class="">Description</th>
	<!--<th class="">Grade</th>-->
	<th class="">Unit</th>
	<th class="">Quantity</th>
	<th class="">Rate</th>
	<th class="">Amount</th>
	<th class="">Start Date</th>
	<th class="">End Date</th>
	<th class="">Remark</th>
	<th class="">Delete</th>
</tr>
<?php
$qty=array(); 
	$query = "SELECT * from salesorder_details WHERE masterId='".$id."'";
	$res = DB_query($query,$db) or die(DB_error());
	$m=0;
	while($result = DB_fetch_array($res)){
	   $x = $result['ItemCode'];
	?>
	<tr class="row1">
	<td colspan="2"><?php echo $s->GetFieldVAlue('item_master',"id='".$result['mst_itemid']."'",'item_name'); ?></td>
	<td ><?php //  $query= "select size as total_size from salesorder_details where ItemCode='".$result['ItemCode']."' and masterId='".$id."'";
		// $sql15=DB_query($query,$db);
	//	 $row25=DB_fetch_array($sql15);
	//  echo $s->GetFieldVAlue('size_master',"id='".$row25['total_size']."'",'size'); ?></td>
	<td><?php // $query= "select sum(Approved_qty) as total_qty from salesorder_details where ItemId='".$result['mst_itemid']."' and OrderId='".$id."'";
		 //$sql15=DB_query($query,$db);
		 //$row25=DB_fetch_array($sql15);
	//echo $row25['total_qty'];
	 $unit=$s->GetFieldVAlue('item_master',"id='".$result['mst_itemid']."'",'mst_unitid');  echo $s->GetFieldVAlue('unitsofmeasure',"unitid='".$unit."'",'unitname'); ?></td>
		
	<td colspan="7"> <?php   $query1= "select Quantity from salesorder_details where mst_itemid='".$result['mst_itemid']."' and masterId='".$id."'";
		 $sql115=DB_query($query1,$db);
		 $row125=DB_fetch_array($sql115);
	echo $row125['Quantity']; ?></td>
	
	
	</tr>
	
	<input type="hidden" name="so_det_id[]" value="<?php echo $result['id']; ?>"/>
	<?php
	    $Subcat=$s->GetFieldVAlue('item_master',"id='".$result['mst_itemid']."'",'SubCategoryID');
            "select * from sub_activity_defination where inventory_group_id='".$Subcat."' and status='Active'";
	$sqlactivityGroup=DB_query("select * from sub_activity_defination where inventory_group_id='".$Subcat."' and status='Active'",$db);
	while($AcGroup=DB_fetch_array($sqlactivityGroup)){
	$m++;
	?>
	<input type="hidden" name="itemCode[]" value="<?php echo $result['mst_itemid']; ?>"/>
	<input type="hidden" name="noOfRows[]" value="<?php echo $m; ?>"/>
	<input type="hidden" name="so_det_id[]" value="<?php echo $result['id']; ?>"/>
	<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['activity_name']; ?>"/>
		<tr class="row1">
			<td><?php echo $s->GetFieldValue('sub_activity_group',"id='".$AcGroup['group_name']."'",'group_name'); ?></td>
			<td><?php echo $AcGroup['activity_name']; ?></td>
			<td><textarea id="description<?php echo $m;?>" name="description[]" style="height:20px; width:100px;"></textarea></td>
			<!--<td><select style="width:100px; height:20px;" name="grade[]" id="grade<?php echo $m;?>">
	<option value="0">Select</option>
	<?php 
	$sql_grade=DB_query("select * from product_grade where 1",$db);
	while($sq=DB_fetch_array($sql_grade))
	{
	?>
	<option value="<?php echo $sq['id']; ?>"><?php echo $sq['grade']; ?></option>
	<?php } ?>
	
	</select></td>-->
			<td ><input type="text" name="act_unit[]" value="<?php echo $s->GetFieldValue('unitsofmeasure',"unitid='".$AcGroup['unit']."'",'unitname'); ?>" readonly style="width:60px; height:20px;"/></td>
			<td><input type="text" name="ActQuantity[]" onchange="get_amount('<?php echo $m;?>');" id="ActQuantity_<?php echo $m; ?>" style="width:50px;height:20px;" class="validate"/></td>
			<td><input type="text" name="act_rate[]" id="act_rate_<?php echo $m; ?>" value="<?php $rate= DB_fetch_array(DB_query("select Rate as total_rate from salesorder_details where mst_itemid='".$result['mst_itemid']."' and masterId='".$id."'",$db));echo $rate['total_rate']; //echo $AcGroup['rate']; ?>" readonly style="width:60px;height:20px;"/></td>
			<td><input type="text" name="ActAmount[]" id="ActAmount_<?php echo $m; ?>" readonly style="width:80px;height:20px;" /></td>
			<td><input name="fromdate1[]" value="<?php echo date('d/m/Y'); ?>" type="text"  class="datepicker" style="width:80px;height:20px;" /></td>
			<td><input name="enddate1[]" value="<?php echo date('d/m/Y'); ?>" type="text"  class="datepicker" style="width:80px;height:20px;"/></td>
			<td><textarea id="act_r_<?php echo $m; ?>" name="text[]" style="height:20px; width:130px;"></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 } ?>
<tr class="row1">
	<td colspan="6" style="text-align:right;">Total Project Amount</td>
	<td colspan="6" style="text-align:left;"><input readonly type = "text" name= "total_amount" id="total_amount" style="width:100px; height:20px;" /></td>
	 </tr>
  <tr class="row1">
  <td colspan="11" style="text-align:center; font-weight:bold;">Prepared By 
  <?php
   echo $s->GetFieldValue('www_users',"id='".$_SESSION['UserID']."'",'userid'); ?></td>
  </tr>
<tr class="row1"> 
	<td colspan="11" style="text-align:center;">
	<input type="submit" name="savedata" value="save" class="search" onClick="return validates()"></td>
</tr>
</table>
<!-- TAble 2 Close -->

</div>
<!-- Body Content Close -->


<?php } 
 } ?>
</form>

