<?php

//****************************************INSERT THE DATA*********START HERE****************************************/

if($_REQUEST['save'])
{
	if($_REQUEST['cash_bank']!='')
	{
	if($_REQUEST['cash_bank']=='Cash')
	{
	$vprefix=6;
	$ledger=6;
	}
	else
	{
	$vprefix=4;
	$ledger=$_REQUEST['cash_bank'];
	}
	$autocode=DB_query("select MAX(wo_adv_req_no) as wono from sub_advance_posting order by wono desc limit 1",$db);
	$autocodeRow=DB_fetch_array($autocode);
	if(DB_num_rows($autocode)>0)
	{
	$woa=$autocodeRow['wono'];
	$aa=substr($woa,-2);
	$final=$aa+1;
	
	}
	else
	{
	$final="1";
	}
	$string="WA/";
	$finalstring=$string.str_pad($final,4,0,STR_PAD_LEFT);
	
	$data['VPrefix']=$vprefix;
	$data['ledger']=$ledger;
	$data['voucher_date']=FormatDateForSQL($_REQUEST['vo_date']);
	$data['wo_adv_req_no']=$finalstring;
	$data['wo_adv_req_date']=FormatDateForSQL($_REQUEST['work_adv_re_date']);
	$s->RecordInsert('sub_advance_posting',$data);
	$lastId= DB_Last_Insert_ID($db,'sub_advance_posting', $id);
	//echo count($_REQUEST['project']);
	
	for($i=0;$i<count($_REQUEST['project']);$i++)
	{
	$datarray['parentid']=$lastId;
	$datarray['project']=$_REQUEST['project'][$i];
	$datarray['contractor']=$_REQUEST['contractor'][$i];
	$datarray['work_order_no']=$_REQUEST['work_order_no'][$i];
	$datarray['amount']=$_REQUEST['amount'][$i];
	$datarray['status']='Pending';
	//echo "<pre>";
	//print_r($datarray);
	$s->RecordInsert('sub_advance_posting_details',$datarray);
	}
	//exit;
	
unset($cash_bank);
unset($vo_date);
unset($work_adv_re_date);
unset($payment_mode);
unset($_SESSION['contract']);
	$_SESSION['success_msg']="Saved Successfully";	
	echo "<script>location.href='index.php?route=SubContractors/workOrder/work_order_advance_posting_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
	}
	else
	{
		echo "<script>alert('Try Again');</script>";
	}
}

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




//****************************************EDIT THE DATA*********START HERE****************************************/



if($_REQUEST['action']=='edit' && !empty($_REQUEST['pos']))
{	
	$recEdit=DB_fetch_array($s->GetRecordById('sub_advance_posting','id='.$_REQUEST['pos']));
	extract($recEdit);
}
if(isset($_REQUEST['update']))
{
	$s->RecordEdit('sub_advance_posting',$_REQUEST['sub_advance_posting'],"id=".$_REQUEST['pos']);
	$_SESSION['success_msg']="Update Successfully";
	echo "<script>location.href='index.php?route=SubContractors/workOrder/work_order_advance_posting_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}


//****************************************EDIT THE DATA*********END HERE****************************************/





//***********************************CREATE SESSION******************START HERE**********************************/


if($_REQUEST['contractors'])
{
	$_SESSION['cash_bank']=$_REQUEST['cash_bank'];
	$_SESSION['vo_date']=$_REQUEST['vo_date'];
	$_SESSION['work_adv_re_date']=$_REQUEST['work_adv_re_date'];
	$_SESSION['payment_mode']=$_REQUEST['payment_mode'];
}
if($_REQUEST['AMIT']==1)
{
	$cash_bank=$_SESSION['cash_bank'];
	$vo_date=$_SESSION['vo_date'];
	$work_adv_re_date=$_SESSION['work_adv_re_date'];
	$payment_mode=$_SESSION['payment_mode'];
}


//***********************************CREATE SESSION******************END HERE**********************************/







//**************************CANCEL SESSION***************************START HERE***************************************/

if($_REQUEST['cancel'])
{
	unset($cash_bank);
	unset($vo_date);
	unset($work_adv_re_date);
	unset($payment_mode);
	unset($_SESSION['contract']);
}


//**************************CANCEL SESSION***************************END HERE***************************************/

?>

<!--------------------------------------AJAX FILE------------------------START HERE------------------------------------>


<script type="text/javascript" language="javascript">
function getpayment1(str)
{
	//alert(str);
	var xmlhttp;
	if(str=='')
	{
	document.getElementById("payment").innerHTML='';
	return false;
	}
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	var data=xmlhttp.responseText;
	document.getElementById("payment").innerHTML=data;
	}
	}
	xmlhttp.open("GET","Getpayment.php?q="+str,true);
	xmlhttp.send();
}
</script>

<!--------------------------------------AJAX FILE------------------------END HERE------------------------------------>



<!--------------------------------------JS FILE------------------------START HERE------------------------------------>



<script type="text/javascript" language="javascript">
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("vo_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("Voucher Date Can't Be Greater than Current Date");
        return false;
    }
    else
    {
        return true;
        
    } 
}





function checkReqDate1()
{

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("work_adv_re_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 Posting Date Can't Be Greater than Current Date");
        return false;
    }
    else
    {
        return true;
        
    } 
}
function validatedata()
{
var date=document.getElementById("vo_date").value;
var from_date= document.getElementById('work_adv_re_date').value; 
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=Date.parse(date);
var datedat1=Date.parse(datedat);
var fromdate=Date.parse(from_date);
var fromdate1=Date.parse(datedat);
if(date1 > datedat1)
		{
			alert("Voucher Date Can't Be Greater than Current Date");
			document.getElementById("vo_date").focus();
			return false;
		}
		if(fromdate > fromdate1)
		{
			alert("Work Adv. Date Can't Be Greater than Current Date");
			document.getElementById("work_adv_re_date").focus();
			return false;
		}
var grand_amt=parseFloat(document.getElementById("wo_grand_total").value);
var amt=parseFloat(document.getElementById("amount").value);
if(grand_amt<amt)
{
alert("You Can't Take Ammount Greater Than Work Order Ammout");	
return false;
}
}
</script>


<!--------------------------------------JS FILE------------------------END HERE------------------------------------>




<link href="css/tender_project.css" rel="stylesheet" type="text/css" />
<div class="tender_main_page">
<h3>Work Order Advance Posting</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>
        <td width="13%"><input type="radio" name="payment_mode" onclick="getpayment1(this.value)" value="Cash" <?php if($payment_mode=='Cash') { echo "checked"; }?> />Cash</td>
        <td width="17%"><input type="radio" name="payment_mode" value="Bank" onclick="getpayment1(this.value)"  <?php if($payment_mode=='Bank') { echo "checked"; }?> />Bank</td>
        <td width="19%"><input type="radio" name="sub_advance_posting[payment_mode]" value="Debit Note"  <?php if($payment_mode=='Debit Note') { echo "checked"; }?> />Debit Note</td>
        <td width="51%">&nbsp;</td>
      </tr>
    </table>
	<br />
	<table width="100%" border="0" cellspacing="2" cellpadding="2">
      <tr>
        <th>Cash/Bank</th>
        <td>
		<div id="payment">
		<select name="cash_bank" id="cash_bank">
		<?php if(isset($_SESSION['contract'])) {
		if($cash_bank=='Cash')
		{
		$abc=$cash_bank;
		}
		else
		{
		$abc=$cash_bank;
		}
		?>
          <option value="<?php echo $abc; ?>"><?php 
		  if($cash_bank=='Cash')
		  {
		  echo $cash_bank;
		  }
		  else {
		echo  $s->GetFieldValue('accountledger',"AccountsID='".$cash_bank."'",'AccountName');
		    } ?></option>
		  
		  	<?php } else {?>
			
			<option value="0">Select</option>
			<?php } ?>
        </select></div></td>
        <th>Voucher Date</th>
        <td><input type="text" name="vo_date" id="vo_date" value="<?php if(isset($_SESSION['contract'])) { echo $vo_date;}else{echo date('d/m/Y');}?>" class="datepicker" style="width:150px;" onchange="checkReqDate()" /></td>
      </tr>
	  <tr>
	  <th>WO Adv Req No.</th>
	   <td><input type="text" name="work_adv_re_no" value="Auto Generated" readonly="true" id="work_adv_re_no" style="width:150px;" /></td>
      <th>WO Adv Req Date</th>
	   <td><input type="text" name="work_adv_re_date" id="work_adv_re_date" value="<?php if(isset($_SESSION['contract'])) { echo $work_adv_re_date;}else{echo date('d/m/Y');} ?>" class="datepicker" style="width:150px;" onchange="checkReqDate1()" /></td>
	  </tr>
    </table>
	<br />
	<br />
	<br />
	<p><input type="submit" name="contractors" value="Contractors" onclick="popup('addcontractors.php?<?php echo $rurl.'&action='.$_REQUEST['action']; ?>',1000,600)"/></p>
<table width="100%" border="0" cellspacing="2" cellpadding="2">
	<tr>
	<th>Project Name</th>
	<th>Contractor Name</th>
	<th>Work Order No.</th>
	<th>Work Order Total Amout.</th>
	<th>Amount</th>
	</tr>
	<?php 
	 if(isset($_SESSION['contract'])){
	 $listcon=explode("#",$_SESSION['contract']);
	 for($i=0;$i<count($listcon);$i++)
	 {
	 $cont="select * from sub_preparation where id='".$listcon[$i]."' AND approval_status='Approved'";
	 $cont1=DB_query($cont,$db);
	 $contRow=DB_fetch_array($cont1);
	?>
	<tr>
	<td><center><?php echo $s->GetFieldValue('locations',"loccode='".$contRow['project']."'",'locationname'); ?></center>
	<input type="hidden" name="project[]" id="project" value="<?php echo $contRow['project']; ?>" />
	</td>
	<td><center><?php echo $s->GetFieldValue('sub_contractor_definition',"id='".$contRow['contractor']."'",'contractor_name'); ?></center>
	<input type="hidden" name="contractor[]" id="contractor" value="<?php echo $contRow['contractor']; ?>" />
	</td>
	<td><center><?php echo $contRow['work_order_no']; ?><input type="hidden" name="work_order_no[]" value="<?php echo $contRow['id']; ?>" id="work_order_no" style="width:150px;" /></center></td>
	<td><center><?php echo $contRow['grand_total']; ?><input type="hidden" name="wo_grand_total[]" id="wo_grand_total" style="width:150px;" value="<?php echo $contRow['grand_total']; ?>" /></center></td>
	<td><center><input type="text" name="amount[]" id="amount" style="width:150px;" /></center></td>
	</tr>
	<?php } } ?>
	</table></td>
	</tr>
</table>
<p align="center"> <input type="submit" value="Save" name="<?php if($_REQUEST['action']=='edit') echo 'update'; else echo 'save'; ?>" onclick="return validatedata()"><input type="submit" name="cancel" value="Reset" /><a href="index.php?route=SubContractors/workOrder/work_order_advance_posting_index&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>"><input type="button" name="back" value="Cancel" /></a></p>
</form>

</div>