<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;
	
	
	
	
}
</script>
<?php
// fetch Prefix
	$prefixCode = $s->GetFieldValue('prefix_tbl',"prefix_id='18'",'prefix_code');
		$retrieve = "SELECT * FROM job_bill";
		$resultret = DB_query($retrieve,$db);
		if(DB_num_rows($resultret)>0)
		{
		$re = "SELECT MAX(id) as id FROM job_bill";
		$exe = DB_query($re,$db);
		$maximumID=DB_fetch_array($exe);
		$pad =$maximumID['id']+1;
		$autojobNo = $prefixCode.'-00'.$pad;
		}
		else
		{
		$autojobNo = $prefixCode.'-001';
		}

	  if($_REQUEST['savedata'])
	  { 
		$data1['branch'] =$_REQUEST['branchs'];
		$data1['voucher_no'] = $autojobNo;
		$data1['date'] = date('Y-m-d');
		$data1['contractor'] = $_REQUEST['assign_to'];
		$data1['bill_no'] = $_REQUEST['bill_no'];
		$data1['bill_date'] = FormatDateFORSQL($_REQUEST['bill_date']);
		$data1['companyId'] = $_SESSION['ACGCompanyId'];
		$data1['created_by'] = $_SESSION['UserID'];
		$data1['grand_amount'] = $_REQUEST['grand_sum'];
		$data1['Remarks'] = $_REQUEST['Remarks'];
		$data1['contract_id'] = $_REQUEST['contr_id'];
		$r= $s->RecordInsert('job_bill', $data1);
		$last_id=DB_Last_Insert_Id($db,"job_bill","id");
		
		if(DB_affected_rows($db)>0)
		{
		$flag = 1;
		
	  	for($i=0;$i<count($_REQUEST['loop']);$i++)
		{ 
		$data6['project_status'] = '2';	
		$condition6="id='".$_REQUEST['loop'][$i]."'";
		$s->RecordEdit('project_completion',$data6,$condition6);
		$data['master_id'] = $last_id;
		$data['boq_no'] = $_REQUEST['boq_nos'][$i];
		$data['client'] = $_REQUEST['clients'][$i];
		$data['boq_date'] = FormatDateForSql($_REQUEST['boq_dates'][$i]);
		$data['pcs_no'] = $_REQUEST['pcs_nos'][$i];
		$data['pcs_date'] =FormatDateForSql($_REQUEST['proj_dates'][$i]);
		$data['total_amount'] = $_REQUEST['tot_amt'][$i];
			$r= $s->RecordInsert('job_bill_details', $data);
			if(DB_affected_rows($db)>0)
			{
				$flag1 = 1;
			}
		}
		$s->pageLocation("index.php?route=PMS/Transcations/job_bill_creation".$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.bill_creation.bill_no.value=='')
	{
		alert("Please Enter Bill No.");
		(document.bill_creation.bill_no).focus();
		return false;
	}
	else if(document.bill_creation.bill_date.value=='')
	{
		alert("Please Enter Bill Date");
		(document.bill_creation.bill_date).focus();
		return false;
	}
	}
</script>

<form action="" name="bill_creation" method="post" >
<?php
 if($_REQUEST['addCont'])
	  { 
$mm=implode(",",$_REQUEST['arrayItem']);  
$DOCsql=DB_query("select project_completion.id as main_id,project_completion.*,project_assign_contractor.vendor_id as v_id from project_completion inner join  project_assign_contractor on project_completion.boq_id=project_assign_contractor.boq_id where  project_completion.approval_status='1' and project_completion.companyID='".$_SESSION['ACGCompanyId']."' and project_completion.id IN (".$mm.")",$db);
if(DB_num_rows($DOCsql)>0){
$i=0;
	  $Doc=DB_fetch_array($DOCsql);
	  extract($Doc);
  ?>
<h1>Job Bill Creation Entry</h1>
<!-- Body Content Start -->
<input type="hidden" name="contr_id" value="<?php echo $_REQUEST['cont_id']; ?>"/>
<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="">
   		<td class="label">Branch</td>
   		  <td><input type="text" name="branchs" value="<?php echo $branch; ?>" readonly/></td>
   		  <td class="label" >Voucher No.</td>
	 	<td ><input type="text" name="vo_no" value="will be auto generated" readonly /></td>
	 	<td class="label">Date</td>
		<td><input name="job_date" type="text" value="<?php echo ConvertSQLDate(date('Y-m-d')); ?>" class="datepicker"/></td>
	</tr>
	
	<tr>
		 <td class="label">Contractor</td>
		 <td><input type="text" name="assign_to" value="<?php echo $assigned_to; ?>" readonly/></td>
		 <td class="label">Bill No <font style="color:#FF0000;">*</font></td>
		 <td><input type="text" name="bill_no" /></td>
		 <td class="label">Bill Date <font style="color:#FF0000;">*</font></td>
		 <td><input type="text" name="bill_date" class="datepicker"/></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>
    <th height="29" class="label">BOQ No</th>
    <th class="label">BOQ Date</th>
    <th class="label">PCS No</th>
    <th class="label">PCS Date</th>
    <th class="label">Project</th>
    <th class="label">Amount</th>
  </tr>
  <?php 
	$query = "select project_completion.*,project_completion.id as idd from project_completion inner join  project_assign_contractor on project_completion.boq_id=project_assign_contractor.boq_id where  project_completion.approval_status='1' and project_completion.project_status is null and project_completion.companyID='".$_SESSION['ACGCompanyId']."' and project_assign_contractor.vendor_id='".$v_id."' and project_completion.id IN (".$mm.") order by project_completion.date ASC";
	$res = DB_query($query,$db) or die(DB_error());
	$m=0;
	$sum=array();
	while($result = DB_fetch_array($res)){
	$m++;
	?>
  <input type="hidden" name="loop[]" value="<?php echo $result['idd']; ?>"/>
  <tr class="rowdata">
    <td><b><input type="hidden" name="boq_nos[]" value="<?php echo $result['boq_id']; ?>"/>
        <?php echo $result['boq_no']; ?></b></td>
    <td><b><input type="hidden" name="boq_dates[]" value="<?php echo $result['boq_date']; ?>"/>
        <?php echo ConvertSQLDate($result['boq_date']); ?></b></td>
    <td><b><input type="hidden" name="pcs_nos[]" value="<?php echo $result['pcs_no']; ?>"/>
        <?php echo $result['pcs_no']; ?></b></td>
    <td><b><input type="hidden" name="proj_dates[]" value="<?php echo ConvertSQLDate($result['date']); ?>"/>
        <?php echo ConvertSQLDate($result['date']); ?></b></td>
		<td><b><input type="hidden" name="clients[]" value="<?php echo $result['client']; ?>"/><?php echo $result['client']; ?></b></td>
	<td>&nbsp;</td>
	</tr>
	<tr class="rowdata">
		<td height="29">Activity Group</td>
		<td>Activity Code</td>
		<td>Activity Name</td>
		<td>Quantity</td>
		<td>Rate</td>
		<td>Amount</td>
	</tr>
	<?php $query1 = "select * from project_completion_details where master_id='".$result['idd']."'";
	$res1 = DB_query($query1,$db) or die(DB_error()); 
	$mm=1;
	$sum1=array();
	while($result1 = DB_fetch_array($res1)){
	?>
	<tr>
		<td><?php echo $s->GetFieldValue('sub_activity_group','id="'.$result1['act_group'].'"','group_name');?></td>
		<td><?php echo $s->GetFieldValue('sub_activity_defination','id="'.$result1['act_name'].'"','activity_code');?></td>
		<td><?php echo $s->GetFieldValue('sub_activity_defination','id="'.$result1['act_name'].'"','activity_name');?></td>
		<td><?php echo $result1['complete_qty'];?></td>
		<?php 
		$vendor_id=$s->GetFieldValue('project_assign_contractor','boq_id="'.$result['boq_id'].'"','vendor_id');
		$query2 = "select mrp_rate from pms_price_list AS PL LEFT JOIN pms_price_list_items AS PLI ON PL.id=PLI.price_list_id where PLI.activity_id='".$result1['act_name']."' and PL.from_date <= '".date('Y-m-d')."' and PL.to_date >= '".date('Y-m-d')."' and  PL.particular='".$vendor_id."' and PL.price_for='C'";
		$res2 = DB_query($query2,$db) or die(DB_error());
		$result2 = DB_fetch_array($res2);
		?>
		<td><?php echo $result2['mrp_rate'];?></td>
		<td><?php echo $result1['complete_qty']*$result2['mrp_rate'];
		$sum1[]=$result1['complete_qty']*$result2['mrp_rate'];
		?></td>
	</tr>
	<?php } ?>
	<tr>
	<td colspan="4" height="29">&nbsp;</td>
	<td>Total Amount</td>
    <td><input type="hidden" name="tot_amt[]" value="<?php echo array_sum($sum1); ?>"/>
        <?php echo $result['grand_compl_amt']=array_sum($sum1); ?></td>
  </tr>
  <?php
$sum[]=$result['grand_compl_amt']; 
 } ?>
  <tr>
  <td colspan="4" height="29"></td>
    <td >Grand Total Amount</td>
    <td><input type="hidden" name="grand_sum" value="<?php echo array_sum($sum); ?>"/>
        <?php echo array_sum($sum); ?></td>
  </tr>
  <tr>
  <td> Remarks </td>
  <td><textarea name="Remarks"></textarea></td>
   <td colspan="2"></td>
  <td> Prepared By </td>
  <td><?php
   echo $s->GetFieldValue('www_users',"id='".$_SESSION['UserID']."'",'userid'); ?></td>
  </tr>
  <tr>
    <td colspan="13" style="text-align:center;"><input type="submit" name="savedata" value="save" class="search" onClick="return validates()" /></td>
  </tr>
</table>
<?php } 
 } ?>
</form>

