<?php
//*****************************INSERT THE DATA************************START HERE************************************/

if(isset($_POST['SaveFinish']))
{	
	$vouchar_date=FormatDateForSQL($_REQUEST['vouchar_date']);
	for($i=0;$i<count($_REQUEST['bill_row']);$i++)
	{
	$data['vouchar_date']=$vouchar_date;
	$data['contractor']=$_REQUEST['contractor'][$i];
	$data['work_order_no']=$_REQUEST['work_order_no'][$i];
	$data['ra_billno']=$_REQUEST['ra_billno'][$i];
	$data['ra_bill_date']=$_REQUEST['ra_bill_date'][$i];
	$data['project']=$_REQUEST['project'][$i];
	$data['net_amt']=$_REQUEST['net_amt'][$i];
	$s->RecordInsert('subcontractor_rabill_posting');// INSERT RECORD
	$cond="id='".$_REQUEST['bill_row'][$i]."'";
	$dataArray['posting_status']='Posted';
	$s->RecordEdit('sub_ra_bill_preparation', $dataArray, $cond);
	}
	exit;
	$_SESSION['success_msg']="Saved Successfully";	// CONFIRMATION MSG
	$s->pageLocation("index.php?route=SubContractors/RaBill/ra_bill_posting_entry_index".$rurl); // PAGE REDIRECTION
}

//*****************************INSERT THE DATA************************END HERE************************************/


//*****************************FETCH THE DATA WHEN ACTION EDIT*******START HERE************************************/

if($_REQUEST['action']=='edit' and $_REQUEST['opn']!='')
{
$sql=DB_query("select * from subcontractor_rabill_posting where id='".$_REQUEST['opn']."'",$db);
$query=DB_fetch_array($sql);	
extract($query);	
}

//*****************************FETCH THE DATA WHEN ACTION EDIT*******END HERE************************************/


//*****************************UPDATE RECORD***********************START HERE************************************/

if(isset($_POST['Update']))
{
	$start_date=FormatDateForSQL($_REQUEST['subcontractor_rabill_posting']['vouchar_date']);
	$_REQUEST['subcontractor_rabill_posting']['vouchar_date']=$start_date;
	extract($_REQUEST['subcontractor_rabill_posting']);
	$s->RecordEdit('subcontractor_rabill_posting',$_REQUEST['subcontractor_rabill_posting'],"id=".$_REQUEST['opn']);
	$_SESSION['success_msg']="Update Successfully";
	$s->pageLocation("index.php?route=SubContractors/RaBill/ra_bill_posting_entry_index".$rurl); // PAGE REDIRECTION
}
//*****************************UPDATE RECORD***********************END HERE***************************************/


?>
<style>
.label{
background-color: #B7B7B7;
    color: #000000;
    font-size: 10.5px;
    font-weight: bold !important;
    padding: 5px 10px;
    text-align: left;
}
</style>
<form action="" method="post">
<link href="css/tender_project.css" rel="stylesheet" type="text/css" />
<div class="tender_main_page">
<h3>RA Bill Posting</h3>
<table width="100%" border="1"  cellpadding="3">
      <tr>
        <td class="tabledata">Vouchar Date</td>
        <td><input type="text" name="vouchar_date"  id="vouchar_date" style="width: 200px" class="datepicker"
        	value="<? echo date('d/m/Y'); ?>">
    	</td>
      </tr>
    </table>
	  <br /><br />
	<div style="overflow: auto; width: 810px; height: 150px">	
	<table width="100%" border="1"  cellpadding="3">
      <tr>
        <td class="tabledata">Select</td>
        <td class="tabledata">Contractor Name</td>
        <td class="tabledata">Work Order No</td>
        <td class="tabledata">RA Bill No</td>
        <td class="tabledata">Bill Date</td>
        <td class="tabledata">Project Name</td>
        <td class="tabledata">Net Amount</td>
      </tr>
	  <?  $billrec=DB_query("select * from sub_ra_bill_preparation where verify_status='Verified' and posting_status='pending'",$db); 
	  $i=0;
	  	  while($billrecRow=DB_fetch_array($billrec)){
	  ?>
      <tr>
        <td><input type="checkbox" name="bill_row[]" id="bill_row" value="<? echo $billrecRow['id']; ?>"  /></td>
        <td><? echo $s->GetFieldValue('sub_contractor_definition',"id='".$billrecRow['contractor']."'",'contractor_name'); ?>
		<input type="hidden" name="contractor[]" id="contractor" value="<? echo $billrecRow['contractor']; ?>" /></td>
        <td><? echo $s->GetFieldValue('sub_preparation',"id='".$billrecRow['work_order_no']."'",'work_order_no'); ?>
		<input type="hidden" name="work_order_no[]" id="work_order_no" value="<? echo $billrecRow['work_order_no']; ?>" /></td>
        <td><? echo $billrecRow['bill_no'] ?>
		<input type="hidden" name="ra_billno[]" id="ra_billno" value="<? echo $billrecRow['bill_no']; ?>" /></td>
        <td><? echo ConvertSQLDate($billrecRow['bill_date']); ?>
		<input type="hidden" name="ra_bill_date[]" id="ra_bill_date" value="<? echo $billrecRow['bill_date']; ?>" /></td>
        <td><? echo $s->GetFieldValue('locations',"loccode='".$billrecRow['project']."'",'locationname'); ?>
		<input type="hidden" name="project[]" id="project" value="<? echo $billrecRow['project']; ?>" /></td>
        <td><? echo $billrecRow['net_amt'] ?>
		<input type="hidden" name="net_amt[]" id="net_amt" value="<? echo $billrecRow['net_amt']; ?>" /></td>
      </tr>
	  <? $i++;  } ?>
    </table>
    </div>
	 <div align="centre"><input type="submit" name="<?php if($_REQUEST['action']=='edit') echo "Update"; else echo "SaveFinish"; ?>" value="<?php if($_REQUEST['action']=='edit') echo "Update"; else echo "Save & Finish"; ?>"><input type="submit" name="Close" value="Close"></div>
 </div> 
</div>
</form>