<?php
if($_REQUEST['save'])
{
	if($_REQUEST['approve']!='')
	{
	$data['project']=$_REQUEST['project'];
	$data['contractor']=$_REQUEST['contactor'];
	$data['wo_adv_req_no']=$_REQUEST['wo_adv_req_no'];
	$data['advance_date']=FormatDateForSQL($_REQUEST['advance_date']);
	$data['advance_mode']=$_REQUEST['advance_mode'];
	$data['work_order_no']=$_REQUEST['work_order_no'];
	$data['work_order_date']=$_REQUEST['work_order_date'];
	$data['total_wo_amt']=$_REQUEST['total_amt'];
	$data['total_bill_amt']=$_REQUEST['bill_amt'];
	$data['advance_amt']=$_REQUEST['advance_amount'];
	$data['approve_advance_amt']=$_REQUEST['aprove_amt'];
	$data['balance_wo_amt']=$_REQUEST['balance_amt'];
	$s->RecordInsert('sub_adv_request_approval',$data);
	$cond="id='".$_REQUEST['adr']."'";
	$dataArray['status']='Approved';
	$s->RecordEdit('sub_advance_posting_details', $dataArray, $cond);
	$s->pageLocation("index.php?route=SubContractors/workOrder/work_order_advance_request_index".$rurl);	
	}
	else
	{
	echo "<script>alert('Please Check Approve Button');</script>";
	}

}





//***********************FETCH DATA FROM TABLES*************************START HERE**********************************
$condition="id='".$_REQUEST['adr']."'";
$record=$s->GetRecordById('sub_advance_posting_details',$condition);
$recordRow=DB_fetch_array($record);



$condition1="id='".$recordRow['parentid']."'";
$record1=$s->GetRecordById('sub_advance_posting',$condition1);
$recordRow1=DB_fetch_array($record1);



$work_ordr=DB_query("select * from sub_preparation where contractor='".$recordRow['contractor']."' and project='".$recordRow['project']."'",$db);
$work_ordrRow=DB_fetch_array($work_ordr);

$workchild=DB_query("SELECT SUM(net_amt) as TOTAL_AMT FROM sub_preparation_child WHERE parentid='".$work_ordrRow['id']."'",$db);
$workchildRow=DB_fetch_array($workchild);


//***********************FETCH DATA FROM TABLES*************************END HERE**********************************

?>

<script type="text/javascript" language="javascript">

function calculate()
{
	var a=parseFloat(document.getElementById("total_amt").value);
	var b=parseFloat(document.getElementById("aprove_amt").value);
	var d=parseFloat(document.getElementById("advance_amount").value);
	if(b=='')
	{
	alert("Please Enter The Ammout For Approval");
	return false;
	}
	if(b>d)
	{
	alert("You Can't Take Approval Ammont Greater Than Advance Ammount");
	document.getElementById("aprove_amt").value='';
	document.getElementById("balance_amt").value='';
	return false;
	}
	var c=a-b;
	document.getElementById("balance_amt").value=c;
}



function checkReqDate()
{

var curr_date2='';	
var curr_month2='';
var d = new Date();
//alert(d.format("m/dd/yy"));
var curr_date = d.getDate();
var curr_month1 = d.getMonth();
var curr_year = d.getFullYear();
var curr_month=curr_month1+1;
var t=curr_month;
var d=curr_date;
if(t!=10 || t!=11 ||t!=12 )
{
 curr_month2="0" + curr_month;	
}
else
{
 curr_month2=curr_month;	
}
if(d==1 || d==2 || d==3 || d==4 || d==5 || d==6 || d==7 || d==8 || d==9 )
{
 curr_date2="0" + curr_date;		
}
else
{
 curr_date2=curr_date;		
}
var datedat=(curr_date2 + "/" + curr_month2 
+ "/" + curr_year);

var date1=document.getElementById("advance_date").value;
var str1 = date1;
var str2 = datedat;
    var dt1  = parseInt(str1.substring(0,2),10);
    var mon1 = parseInt(str1.substring(3,5),10);
    var yr1  = parseInt(str1.substring(6,10),10);
    var dt2  = parseInt(str2.substring(0,2),10);
    var mon2 = parseInt(str2.substring(3,5),10);
    var yr2  = parseInt(str2.substring(6,10),10);
    var date1 = new Date(yr1, mon1, dt1);
    var date2 = new Date(yr2, mon2, dt2);
    if(date2 < date1)
    {
        alert("Advance Date Can't Be Greater than Current Date");
        return false;
    }
    else
    {
        return true;
        
    } 
}


</script>
<h3>Work Order Advance Request Approval</h3>
<form action="" method="post">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td><table width="100%" border="0" cellspacing="2" cellpadding="2">
      <tr>
        <th width="21%">Project</th>
        <td width="35%"><?php echo $s->GetFieldValue('locations',"loccode='".$recordRow['project']."'",'locationname'); ?>
		<input type="hidden" name="project" id="project" value="<?php echo $recordRow['project']; ?>" />
		</td>
        <th width="16%">Contractor</th>
        <td width="28%"><?php echo $s->GetFieldValue('sub_contractor_definition',"id='".$recordRow['contractor']."'",'contractor_name'); ?>
		<input type="hidden" name="contactor" id="contactor" value="<?php echo $recordRow['contractor']; ?>" />
		</td>
      </tr>
      <tr>
        <th>WO Adv Req No.</th>
        <td><?php echo $recordRow1['wo_adv_req_no']; ?>
		<input type="hidden" name="wo_adv_req_no" id="wo_adv_req_no" value="<?php echo $recordRow1['wo_adv_req_no']; ?>" />
		</td>
	 <th >Advance Date</th>
        <td>
		<input type="text" name="advance_date" id="advance_date" value="<?php echo date('d/m/Y'); ?>" class="datepicker" style="width:150px;" onchange="checkReqDate()" />
		</td>
        <td>&nbsp;</td>
      </tr>
      <!--<tr>
        <th>Advance Date</th>
        <td><input type="text" name="advance_date" id="advance_date" value="<?php echo date('d/m/Y'); ?>" class="datepicker" /></td>
        <td >&nbsp;</td>
        <td>&nbsp;</td>
      </tr>-->
    </table>
	<br />
	<table align="center" width="100%" border="0" cellspacing="2" cellpadding="2">
      <tr>
        <td width="36%"></td>
        <th>Advance Mode</th>
		<td><input type="radio" name="advance_mode" value="Mobilisation" <?php if($advance_mode=='Mobilisation') echo "checked"; ?>/> Mobilisation <input type="radio" name="advance_mode" value="Adhoc" <?php if($advance_mode=='Adhoc') echo "checked"; ?>/>Adhoc</td>
        <td width="26%"></td>
        <td width="6%"><input type="button" name="show" value="Show" /></td>
      </tr>
    </table>
	<br />
	<table width="100%" border="0" cellspacing="2" cellpadding="2">
      <tr>
       <!-- <th width="5%">Select</th>-->
        <th width="10%"><div align="left">Work Order No.</div></th>
        <th width="7%"><div align="left">WO Date</div></th>
        <th width="12%"><div align="left">Total WO Amount</div></th>
        <th width="11%"><div align="left">Total Bill Amount</div></th>
		<th width="10%"><div align="left">Advance Amount</div></th>
        <th width="14%"><div align="left">Approval Adv Amount</div></th>
       <!-- <th width="4%">Tax</th>
        <th width="13%">Net Amount Payable</th>-->
        <th width="14%"><div align="left">Balance WO Amount</div></th>
      </tr>
      <tr>
      <!--  <td>&nbsp;</td>-->
        <td><center><?php echo $work_ordrRow['work_order_no']; ?></center>
		<input type="hidden" name="work_order_no" id="work_order_no" value="<?php echo $_REQUEST['wo_no']; ?>" />
		</td>
        <td><center><?php echo ConvertSQLDate($work_ordrRow['work_order_date']); ?></center>
		<input type="hidden" name="work_order_date" id="work_order_date" value="<?php echo $work_ordrRow['work_order_date']; ?>" />
		</td>
        <td><center><?php echo $workchildRow['TOTAL_AMT']; ?></center>
		<input type="hidden" name="total_amt" id="total_amt" value="<?php echo $workchildRow['TOTAL_AMT']; ?>" />
		</td>
        <td>
		<input type="hidden" name="bill_amt" id="bill_amt" value="" />
		</td>
        <td><center><?php echo $recordRow['amount']; ?></center>
		<input type="hidden" name="advance_amount" id="advance_amount" value="<?php echo $recordRow['amount']; ?>" />
		</td>
        <td><input type="aprove_amt" name="aprove_amt" id="aprove_amt" style="width:150px;" /></td>
<!--        <td>&nbsp;</td>
        <td>&nbsp;</td>-->
        <td><input type="balance_amt" name="balance_amt" id="balance_amt" style="width:150px;" onfocus="calculate()"  readonly="" /></td>
      </tr>
    </table></td>
  </tr>
</table>
<p align="center"> 
<input type="radio" name="approve" />Approve
<input type="submit" value="Save" name="save">
<input type="reset" name="cancel" value="Reset" /></p>
</form>
