<?php

session_start();

if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");
include("Functions_Trans.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");


//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From PaymentSetup Table							
$querypaysetup= " Select DefaultCreditAccount From PaymentSetup ";
$resultpaysetup=mysql_query($querypaysetup,$connection) or die(mysql_error());
while($rowpaysetup=mysql_fetch_array($resultpaysetup)){
	extract($rowpaysetup);
}

//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ifNull(Login_Daily,0) As Login_Daily, ifNull(Login_Weekly,0) As Login_Weekly, ".
		  " ifNull(Login_Monthly,0) As Login_Monthly, ifNull(Login_Limit,0) As Login_Limit, ifNull(Branch_Manager,0) As Branch_Manager, ".
		  " ifnull(PaymentRqstApproved_Level,0) as PaymentRqstApproved_Level ".
		  " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_ReferenceByCostcenter,0) As Module_ReferenceByCostcenter, ifNull(Module_ReferenceByProject,0) As Module_ReferenceByProject, ".
			 " ifNull(Module_Delivery,0) As Module_Delivery, ifNull(Module_PaymentLimit,0) as Module_PaymentLimit ".
			 " From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$querymodule=" Select ifNull(paymentrqstNoDt,0) As paymentrqstNoDt, ifnull(toBeApprovedByHOD,0) as toBeApprovedByHOD ".
			 " From paymentsetup";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}
					
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<script type="text/javascript" language="javascript">


</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>
<body>
<form name='inserttrans' method=''>
<input type='text' name='mode' value='$mode'>
<input type='text' name='mainmode' value='$mainmode'>
<input type='text' name='STransId' value='$STransId'>
<input type='text' name='STransDtId' value='$STransDtId'>
<input type='text' name='TransType' value='$TransType' >
<input type='text' name='SAccountType' value='$SAccountType'>
<input type='text' name='SCstRqstId' value='$SCstRqstId' >
<input type='text' name='SChkRqstId' value='$SChkRqstId' >
<input type='text' name='SelectedRec' value='$SelectedRec' >
HERE;

$message='';



if($mode == "saverecord"){
	$ErrorExists		= 0;
	
	$PTransCurr 		= $txtCurr;
	$PTransDate 		= $txtDate;
	$PTransRemark 		= $txtRemark;
	$PTransApprovedBy 	= $txtApprovedBy;
	$PTransManualNo 	= $txtManualNo;
	$PTransRepresent 	= $txtRepresent;
	$PTransConfirmed	= $chkConfirmed;
	$PTransApproved 	= $txtApproved;
	$txtTotalAmount 	= str_replace( ',', '' , $txtTotalAmount);
	$PTransPaymentmode 	= $txtpaymentmode;
	$PTransFunds	   	= $txtfunds;
	
	if (!isset($PTransCurr))
	   $PTransCurr = $base1;
	   
	$txtCostCentCode = "".$txtCostCentCode;   
	if ($txtCostCentCode == '')	$CostCentCode = 'Null';
	else		$CostCentCode = "'".$txtCostCentCode."'";
	
	$txtProjectCode = "".$txtProjectCode;	
	if ($txtProjectCode == '')	$ProjectCode = 'Null';
	else		$ProjectCode = "'".$txtProjectCode."'";
	
	if(empty($PTransApproved))  $PTransApproved = 0;
	if(empty($PTransConfirmed)) $PTransConfirmed = 0;
	
	if(empty($txtcash)) $txtcash = 0;
	if(empty($txtcheck)) $txtcheck = 0;
	if(empty($txtcostshare)) $txtcostshare = 0;
	if(empty($txtguarantee)) $txtguarantee = 0;
	
	if ($Module_PaymentLimit == 0 && $toBeApprovedByHOD == 0) $PTransConfirmed = 1;

	if ( $Login_Limit > 0 && $Login_Limit != $amount && $toBeApprovedByHOD == 1){
		$ErrorExists = 1;
		$message = "Amount Requested > Limit Amount. Payment Request Cannot be Approved/Confirmed";		
	}	
    
	if ($toBeApprovedByHOD == 1 && $PTransConfirmed == 1 && $PTransApproved == 0 ){
		$ErrorExists = 1;
		$message = "Payment Request Cannot be Confirmed. Payment Request Should be Approved First";
	}
	if ($toBeApprovedByHOD == 1 && $PTransApproved == 1 && strtoupper($PTransApprovedBy) != strtoupper($_SESSION['useraccount'])){
		$ErrorExists = 1;
		$message = "Payment Request Cannot be Approved. To be Approved user does not match logged in User!";
	}
	if ($toBeApprovedByHOD == 1 && $PTransConfirmed == 1 && strtoupper($txtConfirmedBy) != strtoupper($_SESSION['useraccount'])){
		$ErrorExists = 1;
		$message = "Payment Request Cannot be Confirmed. To be Confirm user does not match logged in User!";
	}
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$PTransCurr."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$PTransCurr."' )";
	$result = selectData($connection, $query);
	$rate_dt = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base1."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base1."' )";
	$result = selectData($connection, $query);
	$rate_dt1 = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base2."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base2."' )";
	$result = selectData($connection, $query);
	$rate_dt2 = $result[0];
	
	$PaymentRqst_amount1  = ConvertCurrency($PTransCurr, $base1, insertMySQLDate($PTransDate), $txtTotalAmount, $connection); 
	$PaymentRqst_amount2  = ConvertCurrency($PTransCurr, $base2, insertMySQLDate($PTransDate), $txtTotalAmount, $connection); 
		
	//	exit;
	//Insert Master Table
	if ($STransId == "" && $ErrorExists == 0){
	
				
		fbegin();	
		$vTransId = get_nextsequence('PaymentRqst_id','',$connection);	
		fcommit();
	 
		fbegin();
		$query = " INSERT INTO PaymentRqst (PaymentRqst_Id, PaymentRqst_RefGovernment, PaymentRqst_Date, PaymentRqst_ValueDate, PaymentRqst_Remark, PaymentRqst_Name, ".
				 " PaymentRqst_ManualNo, PaymentRqst_Desc, PaymentRqst_App, Touser_account, PaymentRqst_Confirmed, User_Confirmed, Project_Code, Costcent_code, PaymentRqst_Posted, ".
				 " Currency_Code, PaymentRqst_AMOUNT, PaymentRqst_amount1, PaymentRqst_amount2, PaymentRqst_BelongsToGrp, Synchronized, User_Account,".
				 " paymentrqst_cash,paymentrqst_check,paymentrqst_costshare,paymentrqst_guarantee, paymentmode_code, sourceFund_Code ) ".
				 " VALUES ( ".$vTransId.", Null, '".insertMySQLDate($PTransDate)."', now(), '".ereg_replace("'", "\'", trim($PTransRemark))."', ".
				 "          '".ereg_replace("'", "\'", trim($PTransRepresent))."', '".ereg_replace("'", "\'", trim($PTransManualNo))."', ".
				 "          Null, $PTransApproved, '".ereg_replace("'", "\'", trim($PTransApprovedBy))."', 0, Null, $ProjectCode, $CostCentCode, 0, ".
				 "          '".$PTransCurr."', $txtTotalAmount, $PaymentRqst_amount1, $PaymentRqst_amount2, $FI_BelongsToGrp, 0, '".$_SESSION['useraccount']."', ".
				 " '$txtcash','$txtcheck','$txtcostshare','$txtguarantee', '".ereg_replace("'", "\'", trim($PTransPaymentmode))."', '".ereg_replace("'", "\'", trim($PTransFunds))."')";
					 
		//echo $query."<br>";exit;	 
		$result=mysql_query($query,$connection);
			
		
		if ($result){
			$STransId = $vTransId;
			fcommit();
		}else{
			$STransId = "";
			$message  = mysql_error(); 
			$ErrorExists = 1;
			frollback();
		}
	
	} 
	if($STransId != '' && $ErrorExists == 0){
	
	$vTransAmountDb1  = ConvertCurrency($PTransCurr, $base1, insertMySQLDate($PTransDate), $txtTotalAmount, $connection); 
	$vTransAmountDb2  = ConvertCurrency($PTransCurr, $base2, insertMySQLDate($PTransDate), $txtTotalAmount, $connection); 
		
		$vTransAmountcd1 = $vTransAmountDb1;
		$vTransAmountcd2 = $vTransAmountDb2;
				
		$sub_query1 = " UPDATE PaymentRqst SET ".
						  " PaymentRqst_AmountD1 = $vTransAmountDb1, PaymentRqst_AmountD2 = $vTransAmountDb2, ".
						  " PaymentRqst_AmountC1 = $vTransAmountcd1, PaymentRqst_AmountC2 = $vTransAmountcd2, ".
						  " Currency_Code = '".$PTransCurr."', PaymentRqst_Date = '".insertMySQLDate($PTransDate)."', ".
						  " PaymentRqst_Remark = '".ereg_replace("'", "\'", trim($PTransRemark))."', ".
						  " PaymentRqst_Name = '".ereg_replace("'", "\'", trim($PTransRepresent))."',  ".
						  " PaymentRqst_ManualNo = '".ereg_replace("'", "\'", trim($PTransManualNo))."',  ".
						  " PaymentRqst_App = $PTransApproved, Touser_account = '".ereg_replace("'", "\'", trim($PTransApprovedBy))."',  ".
						  " PaymentRqst_Confirmed = $PTransConfirmed, User_Confirmed = if($PTransConfirmed=1,'".$_SESSION['useraccount']."',Null),  ".
						  " Project_Code = $ProjectCode, Costcent_code = $CostCentCode ".
						  " WHERE PaymentRqst_Id = ".$STransId;
		//	echo $sub_query1;exit;	 
		$sub_result1=mysql_query($sub_query1,$connection);
		$query   = " SELECT PaymentRqst_RefGovernment, Costcent_code, Project_Code FROM PaymentRqst WHERE PaymentRqst_Id = $STransId ";
				$result  = selectData($connection, $query);
				$vTransRef     = $result[0];
				$vCostCentCode = $result[1];
				$vProjectCode  = $result[2];
					
				if ($vTransRef == '' || ($vOldProjCode != $txtProjectCode && $Module_ReferenceByProject == 1) ||
					 ($vOldCostCentCode != $txtCostCentCode && $Module_ReferenceByCostcenter == 1)) {
						 
					if ($Module_ReferenceByCostcenter == 1)   $vProjCostCent = $vCostCentCode;
					else if ($Module_ReferenceByProject == 1) $vProjCostCent = $vProjectCode;
					else $vProjCostCent = '';
								
						
					fbegin();
					$vTransRef = get_nextsequence('PRqst_RefGovernment',$vProjCostCent,$connection);	
					fcommit();
							
					$query = " UPDATE PaymentRqst SET ".
							 " PaymentRqst_RefGovernment = $vTransRef ".
							 " WHERE PaymentRqst_Id = $STransId ";
							
					if (!updateData($connection, $query, true)) 
						$message  = mysql_error();
							
					}
		if($PTransApproved == 1 && $PTransConfirmed == 0){
			
			$queryuser = "select user_account from ".$_SESSION['dbLabel'].".login
						where ifnull(FI_PRequestConfirmed,0) = 1 and upper(login_schema) = Upper('".$_SESSION['dbName']."') ";
			$resuser = mysql_query($queryuser,$connection);
			while($rwuser=mysql_fetch_row($resuser)){	
				$useracc = $rwuser[0];
				$query 	= "select notification_reference from notification where notification_type='PVR' and trans_id = $STransId and notification_table = 'PaymentRqst' and notification_field = 'PaymentRqst_Confirmed' 
							and upper(notification_createduser) = ('$useracc') ";//echo $query; 	
				$res 	= mysql_query($query,$connection);
				$rw 	= mysql_num_rows($res);
				if($rw == 0){
					$vNotifRef = get_nextsequence('notification_reference',$vProjCostCent,$connection);	
					$query_notify = "insert into notification (notification_type, notification_action, notification_description, 
							notification_reference, notification_createduser, notification_level, notification_date, 
							notification_done, notification_checked, trans_id, notification_table,notification_field, createdby_user
							) values ('PVR', 'To Confirm','Payment Request $vTransRef Approved By $PTransApprovedBy'
							,'$vNotifRef','$useracc','5','".insertMySQLDate($PTransDate)."',0,0,
							$STransId,'PaymentRqst','PaymentRqst_Confirmed', '".$_SESSION['useraccount']."')";
					//echo $query_notify; 			
					$res_notify = mysql_query($query_notify,$connection);										
				}
			}// exit;
		}		
		if($PTransApproved == 0){
		fbegin();
		$vNotifRef = get_nextsequence('notification_reference',$vProjCostCent,$connection);	
		fcommit();
		$query 	= "select notification_reference from notification where notification_type='PVR' and trans_id = $STransId and notification_table = 'PaymentRqst' and notification_field = 'PaymentRqst_App' 
							and upper(notification_createduser) = ('$PTransApprovedBy') ";
		$res 	= mysql_query($query,$connection);
		$rw 	= mysql_num_rows($res);
		if($rw == 0){
		$query_notify = "insert into notification (notification_type, notification_action, notification_description, 
						notification_reference, notification_createduser, notification_level, notification_date, 
						notification_done, notification_checked, trans_id, notification_table,notification_field, createdby_user
						) values ('PVR', 'Approval',
						Concat('".ereg_replace("'", "\'", trim($PTransRemark))."',if($txtcash=1,'/ Cash /',
						if($txtcheck,'<br>Cheque /','')),if($txtcostshare=1,'Costshare /',
						if($txtguarantee,'Guarantee /','')),'$txtTotalAmount',' ','$PTransCurr')
						,'$vNotifRef','$PTransApprovedBy','5','".insertMySQLDate($PTransDate)."',0,0,
						$STransId,'PaymentRqst','PaymentRqst_App', '".$_SESSION['useraccount']."')";
		//echo $query_notify; exit;				
		$res_notify = mysql_query($query_notify,$connection);
		}
		}
	}
}
else if($mode == "editrecord" ){
	$ErrorExists		= 0;
	
	$PTransCurr 		= $txtCurr;
	$PTransDate 		= $txtDate;
	$PTransRemark 		= $txtRemark;
	$PTransApprovedBy 	= $txtApprovedBy;
	$PTransApproved 	= $txtApproved;
	$PTransManualNo 	= $txtManualNo;
	$PTransRepresent 	= $txtRepresent;
	$PTransConfirmed	=$chkConfirmed;
	$txtTotalAmount 	= str_replace( ',', '' , $txtTotalAmount);
	$PTransPaymentmode 	= $txtpaymentmode;
	$PTransFunds	   	= $txtfunds;
	
	if (!isset($PTransCurr))
	   $PTransCurr = $base1;
	   
	$txtCostCentCode = "".$txtCostCentCode;   
	if ($txtCostCentCode == '')	$CostCentCode = 'Null';
	else		$CostCentCode = "'".$txtCostCentCode."'";
	
	$txtProjectCode = "".$txtProjectCode;	
	if ($txtProjectCode == '')	$ProjectCode = 'Null';
	else		$ProjectCode = "'".$txtProjectCode."'";
	
	if(empty($PTransApproved))  $PTransApproved = 0;
	if(empty($PTransConfirmed)) $PTransConfirmed = 0;
	
	if(empty($txtcash)) $txtcash = 0;
	if(empty($txtcheck)) $txtcheck = 0;
	if(empty($txtcostshare)) $txtcostshare = 0;
	if(empty($txtguarantee)) $txtguarantee = 0;
	
	if ($Module_PaymentLimit == 0 && $toBeApprovedByHOD == 0) $PTransConfirmed = 1;

	if ( $Login_Limit > 0 && $Login_Limit != $amount && $toBeApprovedByHOD == 1){
		$ErrorExists = 1;
		$message = "Amount Requested > Limit Amount. Payment Request Cannot be Approved/Confirmed";		
	}	
    if ($toBeApprovedByHOD == 1 && $PTransConfirmed == 1 && $PTransApproved == 0 ){
		$ErrorExists = 1;
		$message = "Payment Request Cannot be Confirmed. Payment Request Should be Approved First";
	}
	if ($toBeApprovedByHOD == 1 && $PTransApproved == 1 && strtoupper($PTransApprovedBy) != strtoupper($_SESSION['useraccount'])){
		$ErrorExists = 1;
		$message = "Payment Request Cannot be Approved. To be Approved user does not match logged in User!";
	}
	if ($toBeApprovedByHOD == 1 && $PTransConfirmed == 1 && strtoupper($txtConfirmedBy) != strtoupper($_SESSION['useraccount'])){
		$ErrorExists = 1;
		$message = "Payment Request Cannot be Confirmed. To be Confirm user does not match logged in User!";
	}
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$PTransCurr."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$PTransCurr."' )";
	$result = selectData($connection, $query);
	$rate_dt = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base1."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base1."' )";
	$result = selectData($connection, $query);
	$rate_dt1 = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base2."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base2."' )";
	$result = selectData($connection, $query);
	$rate_dt2 = $result[0];
	
				 
	$PaymentRqst_amount1  = ConvertCurrency($PTransCurr, $base1, insertMySQLDate($PTransDate), $txtTotalAmount, $connection); 
	$PaymentRqst_amount2  = ConvertCurrency($PTransCurr, $base2, insertMySQLDate($PTransDate), $txtTotalAmount, $connection); 
	
	$vTransAmountDb1  = ConvertCurrency($PTransCurr, $base1, insertMySQLDate($PTransDate), $txtTotalAmount, $connection); 
	$vTransAmountDb2  = ConvertCurrency($PTransCurr, $base2, insertMySQLDate($PTransDate), $txtTotalAmount, $connection); 
		
	$vTransAmountcd1 = $vTransAmountDb1;
	$vTransAmountcd2 = $vTransAmountDb2;
	
	if ($ErrorExists == 0) {
		$sub_query1 = " UPDATE PaymentRqst SET ".
						  " PaymentRqst_Date = '".insertMySQLDate($PTransDate)."', PaymentRqst_Remark= '".ereg_replace("'", "\'", trim($PTransRemark))."', ".
						  " PaymentRqst_Posted=0, ".
						  "  PaymentRqst_AMOUNT=$txtTotalAmount, PaymentRqst_amount1=$PaymentRqst_amount1, ". 
						  " PaymentRqst_amount2=$PaymentRqst_amount2, PaymentRqst_BelongsToGrp=$FI_BelongsToGrp, ".
						  " paymentrqst_cash=$txtcash,paymentrqst_check=$txtcheck,paymentrqst_costshare=$txtcostshare,paymentrqst_guarantee=$txtguarantee, ".
						  " PaymentRqst_AmountD1 = $vTransAmountDb1, PaymentRqst_AmountD2 = $vTransAmountDb2, ".
						  " PaymentRqst_AmountC1 = $vTransAmountcd1, PaymentRqst_AmountC2 = $vTransAmountcd2, ".
						  " Currency_Code = '".$PTransCurr."', PaymentRqst_Date = '".insertMySQLDate($PTransDate)."', ".
						  " PaymentRqst_Remark = '".ereg_replace("'", "\'", trim($PTransRemark))."', ".
						  " PaymentRqst_Name = '".ereg_replace("'", "\'", trim($PTransRepresent))."',  ".
						  " PaymentRqst_ManualNo = '".ereg_replace("'", "\'", trim($PTransManualNo))."',  ".
						  " PaymentRqst_App = $PTransApproved, Touser_account = '".ereg_replace("'", "\'", trim($PTransApprovedBy))."',  ".
						  " PaymentRqst_Confirmed = $PTransConfirmed, ";
		if ($toBeApprovedByHOD == 1)
			$sub_query1 .=	  " User_Confirmed = '".ereg_replace("'", "\'", trim($txtConfirmedBy))."',  ";
		else 		
			$sub_query1 .=	  " User_Confirmed = if($PTransConfirmed=1,'".$_SESSION['useraccount']."',Null),  ";
		
		$sub_query1 .=	  " Project_Code = $ProjectCode, Costcent_code = $CostCentCode, sourceFund_Code = '".ereg_replace("'", "\'", trim($PTransFunds))."', ".
						  " paymentmode_code = '".ereg_replace("'", "\'", trim($PTransPaymentmode))."' ".
						  " WHERE PaymentRqst_Id = ".$STransId;	
		//echo $sub_query1;exit;					  
		$sub_result1=mysql_query($sub_query1,$connection);
		$query_del = "delete from temp_paymentrqst where PaymentRqst_Id = ".$STransId;
		$res_del = mysql_query($query_del,$connection);
		
		$query   = " SELECT PaymentRqst_RefGovernment, Costcent_code, Project_Code FROM PaymentRqst WHERE PaymentRqst_Id = $STransId ";
		$result  = selectData($connection, $query);
		$vTransRef     = $result[0];
		if($PTransApproved == 1 && $PTransConfirmed == 0 && (($txtConfirmedBy != '' && $toBeApprovedByHOD == 1) || $toBeApprovedByHOD == 0) ){
				if ($toBeApprovedByHOD == 1)
				$queryuser = "select '$txtConfirmedBy' ";	
				else 
				$queryuser = "select user_account from ".$_SESSION['dbLabel'].".login
							where ifnull(FI_PRequestConfirmed,0) = 1 and upper(login_schema) = Upper('".$_SESSION['dbName']."') ";
				$resuser = mysql_query($queryuser,$connection);
				while($rwuser=mysql_fetch_row($resuser)){	
					$useracc = $rwuser[0];
					$query 	= "select notification_reference from notification where notification_type='PVR' and trans_id = $STransId and notification_table = 'PaymentRqst' and notification_field = 'PaymentRqst_Confirmed' 
								and upper(notification_createduser) = upper('$useracc') ";
					$res 	= mysql_query($query,$connection);
					$rw 	= mysql_num_rows($res);
					if($rw == 0){
						$vNotifRef = get_nextsequence('notification_reference',$vProjCostCent,$connection);	
						$query_notify = "insert into notification (notification_type, notification_action, notification_description, 
								notification_reference, notification_createduser, notification_level, notification_date, 
								notification_done, notification_checked, trans_id, notification_table,notification_field
								) values ('PVR', 'To Confirm','Payment Request $vTransRef Approved By $PTransApprovedBy'
								,'$vNotifRef','$useracc','5','".insertMySQLDate($PTransDate)."',0,0,
								$STransId,'PaymentRqst','PaymentRqst_Confirmed')";
						//echo $query_notify; exit;				
						$res_notify = mysql_query($query_notify,$connection);										
					}
				}
			}		
			if($PTransApproved == 0){
			fbegin();
			$vNotifRef = get_nextsequence('notification_reference',$vProjCostCent,$connection);	
			fcommit();
			$query 	= "select notification_reference from notification where notification_type='PVR' and trans_id = $STransId and notification_table = 'PaymentRqst' and notification_field = 'PaymentRqst_App' 
								and upper(notification_createduser) = ('$PTransApprovedBy') ";
			$res 	= mysql_query($query,$connection);
			$rw 	= mysql_num_rows($res);
			if($rw == 0){
			$query_notify = "insert into notification (notification_type, notification_action, notification_description, 
							notification_reference, notification_createduser, notification_level, notification_date, 
							notification_done, notification_checked, trans_id, notification_table,notification_field
							) values ('PVR', 'Approval',
							Concat('".ereg_replace("'", "\'", trim($PTransRemark))."',if($txtcash=1,'/ Cash /',
							if($txtcheck,'<br>Cheque /','')),if($txtcostshare=1,'Costshare /',
							if($txtguarantee,'Guarantee /','')),'$txtTotalAmount',' ','$PTransCurr')
							,'$vNotifRef','$PTransApprovedBy','5','".insertMySQLDate($PTransDate)."',0,0,
							$STransId,'PaymentRqst','PaymentRqst_App')";
			//echo $query_notify; exit;				
			$res_notify = mysql_query($query_notify,$connection);
			}
			}

	}	
} 

if($mode == "cancelrecord" ){
	$query_del = "delete from temp_paymentrqst where PaymentRqst_Id = ".$STransId;
	$res_del = mysql_query($query_del,$connection);
	
}
else if ($mode == "submitrecord" ){
	
	
	
	if (!isset($PTransCurr))
	   $PTransCurr = $base1;
	   
	if (empty($PTransCostCentCode))	$CostCentCode = 'Null';
	else		$CostCentCode = "'".$PTransCostCentCode."'";
			
	if (empty($PTransProjectCode))	$ProjectCode = 'Null';
	else		$ProjectCode = "'".$PTransProjectCode."'";
	
	if(empty($PTransApproved))  $PTransApproved = 0;
	if(empty($PTransConfirmed)) $PTransConfirmed = 0;
	
	if(empty($txtcash)) $txtcash = 0;
	if(empty($txtcheck)) $txtcheck = 0;
	if(empty($txtcostshare)) $txtcostshare = 0;
	if(empty($txtguarantee)) $txtguarantee = 0;
	
	if ($Module_PaymentLimit == 0) $PTransConfirmed = 1;

    
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$PTransCurr."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$PTransCurr."' )";
	$result = selectData($connection, $query);
	$rate_dt = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base1."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base1."' )";
	$result = selectData($connection, $query);
	$rate_dt1 = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base2."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base2."' )";
	$result = selectData($connection, $query);
	$rate_dt2 = $result[0];
	
	//Insert Master Table
	if ($STransId == ""){
	
				
		fbegin();	
		$vTransId = get_nextsequence('PaymentRqst_id','',$connection);	
		fcommit();
	 
		fbegin();
		$query = " INSERT INTO Temp_PaymentRqst (PaymentRqst_Id, PaymentRqst_RefGovernment, PaymentRqst_Date, PaymentRqst_ValueDate, PaymentRqst_Remark, PaymentRqst_Name, ".
				 " PaymentRqst_ManualNo, PaymentRqst_Desc, PaymentRqst_App, Touser_account, PaymentRqst_Confirmed, User_Confirmed, Project_Code, Costcent_code, PaymentRqst_Posted, ".
				 " Currency_Code, PaymentRqst_AmountD1, PaymentRqst_AmountD2, PaymentRqst_AmountC1, PaymentRqst_AmountC2, PaymentRqst_BelongsToGrp, Synchronized, User_Account, Logged_Code,".
				 " paymentrqst_cash,paymentrqst_check,paymentrqst_costshare,paymentrqst_guarantee ) ".
				 " VALUES ( ".$vTransId.", Null, '".insertMySQLDate($PTransDate)."', now(), '".ereg_replace("'", "\'", trim($PTransRemark))."', ".
				 "          '".ereg_replace("'", "\'", trim($PTransRepresent))."', '".ereg_replace("'", "\'", trim($PTransManualNo))."', ".
				 "          Null, $PTransApproved, '".ereg_replace("'", "\'", trim($PTransApprovedBy))."', 0, Null, $ProjectCode, $CostCentCode, 0, ".
				 "          '".$PTransCurr."', 0, 0, 0, 0, $FI_BelongsToGrp, 0, '".$_SESSION['useraccount']."', '".$_SESSION['useraccount']."', ".
				 " '$txtcash','$txtcheck','$txtcostshare','$txtguarantee')";
					 
		//echo $query."<br>";exit;	 
		$result=mysql_query($query,$connection);
			
		
		if ($result){
			$STransId = $vTransId;
			fcommit();
		}else{
			$STransId = "";
			$message  = mysql_error(); 
			frollback();
		}
	
	}
	
	//Insert Detail Tables
	if ($STransId != "" && $STransDtId == "" ){
	
		if (empty($txtTransDtAmount_D))
			$txtTransDtAmount_D = 0;
		if (empty($txtTransDtAmountB1_D))
			$txtTransDtAmountB1_D = 0;
		if (empty($txtTransDtAmountB2_D))
			$txtTransDtAmountB2_D = 0;
			
			
		$vTransDtdbcd  = '';
		$vLedgerNo     = '';
		$vDetailCode   = '';
		$vTransDtDesc  = '';
		$vTransDtDesc1 = '';
		$vTransDtCurr  = '';
		$vTransDtAmount    = 0;
		$vTransDtAmountB1  = 0;
		$vTransDtAmountB2  = 0;
		$vTransDtValueDate = '';
		
		$vTransAmountDb1 = 0;
		$vTransAmountDb2 = 0;
		$vTransAmountcd1 = 0;
		$vTransAmountcd2 = 0;
			
			
		$vTransDtdbcd  = $txtTransDtdbcd;
		$vLedgerNo     = $txtLedgerNo;
		$vTransDtDesc  = $txtTransDtDesc;
		$vTransDtDesc1 = $txtTransDtDesc1;
		$vTransDtCurr  = $txtTransDtCurr;
		$vTransDtAmount    = $txtTransDtAmount_D;
		$vTransDtAmountB1  = $txtTransDtAmountB1_D;
		$vTransDtAmountB2  = $txtTransDtAmountB2_D;
		$vTransDtValueDate = $txtTransDtValueDate;
		
		if (empty($txtClientDtCode)) $vDetailCode = 'Null';
		else	$vDetailCode = "'".$txtClientDtCode."'";	
					
					
		fbegin();
		$vTransDtId = get_nextsequence('PaymentRqstdt_id','',$connection);	
		fcommit();

		fbegin();
		$query = " INSERT INTO Temp_PaymentRqstDt (PaymentRqstDt_Id, PaymentRqst_Id, PaymentRqst_Date, Ledger_Number, PaymentRqstDt_Desc, PaymentRqstDt_Desc1, ". 
				 " PaymentRqstDt_ValueDate, Currency_Code, PaymentRqstDt_Amount, PaymentRqstDt_Amount1, PaymentRqstDt_Amount2, PaymentRqstDt_dbcd, PaymentRqstDt_Posted, ".
				 " Synchronized, Logged_Code )  ".
				 " VALUES ( ".$vTransDtId.", ".$STransId.", '".insertMySQLDate($PTransDate)."', '".$vLedgerNo."', '".ereg_replace("'", "\'", trim($vTransDtDesc))."', ".
				 "          '".ereg_replace("'", "\'", trim($vTransDtDesc1))."', '".insertMySQLDate($vTransDtValueDate)."', '".$vTransDtCurr."', ".
				 "          ".$vTransDtAmount.",".$vTransDtAmountB1.",".$vTransDtAmountB2.", '".$vTransDtdbcd."', 0, 0, '".$_SESSION['useraccount']."') ";
			 
		//echo $query."<br>";exit;				
		$result=mysql_query($query,$connection);
		
		if ($result){
		
			$vCstRqstDtPer   = 100;
			$vCstRqstDtAmt   = $vTransDtAmount;
			$vCstRqstDtAmt1  = $vTransDtAmountB1;
			$vCstRqstDtAmt2  = $vTransDtAmountB2;
			$vProjectCodeDt	 = $ProjectCode;
			$vCostCentCodeDt = $CostCentCode;
			
			//Case Header Project or Costcenter Is Not Empty Automatically Insert Into CstRqst	
			if ($PTransProjectCode != '' || $PTransCostCentCode != '' ) {
		
				$query = " INSERT INTO Temp_CstRqst (PaymentRqstDt_Id, Project_code, Costcent_Code, CstRqst_Prcnt, CstRqst_Amount, CstRqst_Amount1, CstRqst_Amount2, ".
						 " Synchronized, CstRqst_Posted, Logged_Code ) ". 
						 " VALUES ( ".$vTransDtId.", $vProjectCodeDt, $vCostCentCodeDt, $vCstRqstDtPer, $vCstRqstDtAmt, $vCstRqstDtAmt1, $vCstRqstDtAmt2, ".
						 "          0, 0, '".$_SESSION['useraccount']."'	) ";
						 
				//echo $query."<br>";exit;				
				$result=mysql_query($query,$connection);
			}

			
			$sub_query = " Select Sum(if(PaymentRqstDt_dbcd='D', ifNull(PaymentRqstDt_Amount1,0), 0)) As TotalDebitAmount1, Sum(if(PaymentRqstDt_dbcd='D', ifNull(PaymentRqstDt_Amount2,0), 0)) As TotalDebitAmount2, ".
						 " Sum(if(PaymentRqstDt_dbcd='C', ifNull(PaymentRqstDt_Amount1,0), 0)) As TotalCreditAmount1, Sum(if(PaymentRqstDt_dbcd='C', ifNull(PaymentRqstDt_Amount2,0), 0)) As TotalCreditAmount2 ".
						 " From Temp_PaymentRqstDt ".
						 " Where PaymentRqst_Id = ".$STransId;
			$sub_result = selectData($connection, $sub_query);
			
			$vTransAmountDb1 = $sub_result[0];
			$vTransAmountDb2 = $sub_result[1];
			$vTransAmountcd1 = $sub_result[2];
			$vTransAmountcd2 = $sub_result[3];
				 
			$sub_query1 = " UPDATE Temp_PaymentRqst SET ".
						  " PaymentRqst_AmountD1 = $vTransAmountDb1, PaymentRqst_AmountD2 = $vTransAmountDb2, ".
						  " PaymentRqst_AmountC1 = $vTransAmountcd1, PaymentRqst_AmountC2 = $vTransAmountcd2, ".
						  " Currency_Code = '".$PTransCurr."', PaymentRqst_Date = '".insertMySQLDate($PTransDate)."', ".
						  " PaymentRqst_Remark = '".ereg_replace("'", "\'", trim($PTransRemark))."', ".
						  " PaymentRqst_Name = '".ereg_replace("'", "\'", trim($PTransRepresent))."',  ".
						  " PaymentRqst_ManualNo = '".ereg_replace("'", "\'", trim($PTransManualNo))."',  ".
						  " PaymentRqst_App = $PTransApproved, Touser_account = '".ereg_replace("'", "\'", trim($PTransApprovedBy))."',  ".
						  //" PaymentRqst_Confirmed = $PTransConfirmed, User_Confirmed = if($PTransConfirmed=1,'".$_SESSION['useraccount']."',Null),  ".
						  " Project_Code = $ProjectCode, Costcent_code = $CostCentCode ".
						  " WHERE PaymentRqst_Id = ".$STransId;
				 
			$sub_result1=mysql_query($sub_query1,$connection);
			//echo $sub_query1."<br>";exit;
						 
			fcommit();
		} else{
			$message  = mysql_error(); 
			frollback();
		}	
		
		
	} // End Case Insert In Detail Tables
	

} 
	
//Delete Detail Mode
else if ($mode == "deletedt" ){

	$AllowDelete = 1;
	$ErrorExists = 0;
	$message  = '';
	
	$vTransAmountDb1 = 0;
	$vTransAmountDb2 = 0;
	$vTransAmountcd1 = 0;
	$vTransAmountcd2 = 0;
	

		
	//If No Constraints Exists On Delete
	if ($AllowDelete == 1) {

		fbegin();
		
		// Delete From Temp_PaymentRqstDt For Selected Record
		$query = " DELETE FROM Temp_PaymentRqstDt ".
				 " WHERE PaymentRqstDt_Id = ".$STransDtId;
		if (!updateData($connection, $query, true)) {
			$message  = mysql_error();
			$ErrorExists = 1;
			frollback();
		}  
		else {
			fcommit();
		}
			
			
	
			
		if ( $ErrorExists == 0 ){ 
			
			$sub_query = " Select Sum(if(PaymentRqstDt_dbcd='D', ifNull(PaymentRqstDt_Amount1,0), 0)) As TotalDebitAmount1, Sum(if(PaymentRqstDt_dbcd='D', ifNull(PaymentRqstDt_Amount2,0), 0)) As TotalDebitAmount2, ".
						 " Sum(if(PaymentRqstDt_dbcd='C', ifNull(PaymentRqstDt_Amount1,0), 0)) As TotalCreditAmount1, Sum(if(PaymentRqstDt_dbcd='C', ifNull(PaymentRqstDt_Amount2,0), 0)) As TotalCreditAmount2 ".
						 " From Temp_PaymentRqstDt ".
						 " Where PaymentRqst_Id = ".$STransId;
			$sub_result = selectData($connection, $sub_query);
			
			$vTransAmountDb1 = $sub_result[0];
			$vTransAmountDb2 = $sub_result[1];
			$vTransAmountcd1 = $sub_result[2];
			$vTransAmountcd2 = $sub_result[3];
			
			fbegin();
			$sub_query1 = " UPDATE Temp_PaymentRqst SET ".
						  " PaymentRqst_AmountD1 = $vTransAmountDb1, PaymentRqst_AmountD2 = $vTransAmountDb2, ".
						  " PaymentRqst_AmountC1 = $vTransAmountcd1, PaymentRqst_AmountC2 = $vTransAmountcd2 ".
						  " WHERE PaymentRqst_Id = ".$STransId;
				 
			$sub_result1=mysql_query($sub_query1,$connection);
			//echo $sub_query1."<br>";exit;
				
			if ( $sub_result1 ){ 
				fcommit();
			} else
			{
			   $message  = mysql_error(); 
			   frollback();
			} 
		} // End If No Error Exists 
			
			
	
	} // End If No Constraints Found On Delete
	
	
}

//Edit Detail Tables
else if ($mode == "editdt" && $STransId != "" && $STransDtId != ""){

	if (empty($txtTransDtAmount_D))
		$txtTransDtAmount_D = 0;
	if (empty($txtTransDtAmountB1_D))
		$txtTransDtAmountB1_D = 0;
	if (empty($txtTransDtAmountB2_D))
		$txtTransDtAmountB2_D = 0;
			
			
	$vTransDtdbcd  = '';
	$vLedgerNo     = '';
	$vTransDtDesc  = '';
	$vTransDtDesc1 = '';
	$vTransDtCurr  = '';
	$vTransDtAmount    = 0;
	$vTransDtAmountB1  = 0;
	$vTransDtAmountB2  = 0;
	$vTransDtValueDate = '';
		
	$vTransAmountDb1 = 0;
	$vTransAmountDb2 = 0;
	$vTransAmountcd1 = 0;
	$vTransAmountcd2 = 0;
			
			
	$vTransDtdbcd  = $txtTransDtdbcd;
	$vLedgerNo     = $txtLedgerNo;
	$vTransDtDesc  = $txtTransDtDesc;
	$vTransDtDesc1 = $txtTransDtDesc1;
	$vTransDtCurr  = $txtTransDtCurr;
	$vTransDtAmount    = $txtTransDtAmount_D;
	$vTransDtAmountB1  = $txtTransDtAmountB1_D;
	$vTransDtAmountB2  = $txtTransDtAmountB2_D;
	$vTransDtValueDate = $txtTransDtValueDate;
				
	fbegin();
	$query = " UPDATE Temp_PaymentRqstDt SET ".
			 " Currency_Code = '".$vTransDtCurr."', Ledger_number = '".$vLedgerNo."', PaymentRqstDt_Desc = '".ereg_replace("'", "\'", trim($vTransDtDesc))."', ". 
			 " PaymentRqstDt_Desc1 = '".ereg_replace("'", "\'", trim($vTransDtDesc1))."',PaymentRqstDt_ValueDate = '".insertMySQLDate($vTransDtValueDate)."', ".
			 " PaymentRqstDt_Amount = $vTransDtAmount, PaymentRqstDt_Amount1 = $vTransDtAmountB1, PaymentRqstDt_Amount2 = $vTransDtAmountB2  ".
			 " WHERE PaymentRqstDt_Id = '".$STransDtId."'";

	//echo $query."<br>";exit;				
	$result=mysql_query($query,$connection);
		
	if ($result){
		$sub_query = " Select Sum(if(PaymentRqstDt_dbcd='D', ifNull(PaymentRqstDt_Amount1,0), 0)) As TotalDebitAmount1, Sum(if(PaymentRqstDt_dbcd='D', ifNull(PaymentRqstDt_Amount2,0), 0)) As TotalDebitAmount2, ".
					 " Sum(if(PaymentRqstDt_dbcd='C', ifNull(PaymentRqstDt_Amount1,0), 0)) As TotalCreditAmount1, Sum(if(PaymentRqstDt_dbcd='C', ifNull(PaymentRqstDt_Amount2,0), 0)) As TotalCreditAmount2 ".
					 " From Temp_PaymentRqstDt ".
					 " Where PaymentRqst_Id = ".$STransId;
		$sub_result = selectData($connection, $sub_query);
			
		$vTransAmountDb1 = $sub_result[0];
		$vTransAmountDb2 = $sub_result[1];
		$vTransAmountcd1 = $sub_result[2];
		$vTransAmountcd2 = $sub_result[3];
			
		$sub_query1 = " UPDATE Temp_PaymentRqst SET ".
					  " PaymentRqst_AmountD1 = $vTransAmountDb1, PaymentRqst_AmountD2 = $vTransAmountDb2, ".
					  " PaymentRqst_AmountC1 = $vTransAmountcd1, PaymentRqst_AmountC2 = $vTransAmountcd2 ".
					  " WHERE PaymentRqst_Id = ".$STransId;
						  
		$sub_result1=mysql_query($sub_query1,$connection);
		//echo $sub_query1."<br>";exit;
		
		$sub_query2 = " UPDATE Temp_CstRqst SET ".
					  " CstRqst_Amount  = ((ifNull(CstRqst_Prcnt,0) * $vTransDtAmount)/100), ".
					  " CstRqst_Amount1 = ((ifNull(CstRqst_Prcnt,0) * $vTransDtAmountB1)/100), ".
					  " CstRqst_Amount2 = ((ifNull(CstRqst_Prcnt,0) * $vTransDtAmountB2)/100) ".
					  " WHERE PaymentRqstDt_Id = '".$STransDtId."'";
				 
		$sub_result2=mysql_query($sub_query2,$connection);
		//echo $sub_query2."<br>";exit;

				
		fcommit();
	} else{
		$message  = mysql_error(); 
		frollback();
	}

	
} 

else if ($mode == "insertcstrqst" && $STransId != "" && $STransDtId != ""){
	
	$vCstRqstId      = '';
	$vCostCentCodeDt = '';
	$vProjectCodeDt  = '';
	$vCstRqstPer     = 0;
	$vCstRqstAmt     = 0;
	$vCstRqstAmt1    = 0;
	$vCstRqstAmt2    = 0;
	
	   
	if (empty($txtCostCentCodeDt))
		$vCostCentCodeDt = 'Null';
	else
		$vCostCentCodeDt = "'".$txtCostCentCodeDt."'";
			
	if (empty($txtProjectCodeDt))
		$vProjectCodeDt = 'Null';
	else
		$vProjectCodeDt = "'".$txtProjectCodeDt."'";   

	
	if (empty($txtCstRqstPer))
		$txtCstRqstPer = 0;
	if (empty($txtCstRqstAmt))
		$txtCstRqstAmt = 0;
	if (empty($txtCstRqstAmt1))
		$txtCstRqstAmt1 = 0;
	if (empty($txtCstRqstAmt2))
		$txtCstRqstAmt2 = 0;
			
			
	
	
	$vCstRqstPer   = $txtCstRqstPer;
	$vCstRqstAmt   = $txtCstRqstAmt;
	$vCstRqstAmt1  = $txtCstRqstAmt1;
	$vCstRqstAmt2  = $txtCstRqstAmt2;	
		
				
	fbegin();
	$query = " INSERT INTO Temp_CstRqst (PaymentRqstDt_Id, Project_code, Costcent_Code, CstRqst_Prcnt, CstRqst_Amount, CstRqst_Amount1, CstRqst_Amount2, ".
			 " Synchronized, CstRqst_Posted, Logged_Code ) ". 
			 " VALUES ( ".$STransDtId.", $vProjectCodeDt, $vCostCentCodeDt, $vCstRqstPer, $vCstRqstAmt, $vCstRqstAmt1, $vCstRqstAmt2, ".
			 "          0, 0, '".$_SESSION['useraccount']."'	) ";
			 
	//echo $query."<br>";exit;				
	$result=mysql_query($query,$connection);
		
	if ($result){
		fcommit();
	} else{
		$message  = mysql_error(); 
		frollback();
	}	
	
	

} 


//Delete CstRqst
else if ($mode == "deletecstrqst" && $SCstRqstId != ""){

	$AllowDelete = 1;
	$ErrorExists = 0;
	$message  = '';
	
	
		
	//If No Constraints Exists On Delete
	if ($AllowDelete == 1) {

		
		fbegin();
		
		// Delete From TempCstRqst For Selected Record
		$query = " DELETE FROM Temp_CstRqst ".
				 " WHERE Cst_increment = '".$SCstRqstId."'";
					 
		if (!updateData($connection, $query, true)) {
			$message  = mysql_error();
			$ErrorExists = 1;
			frollback();
		}  
		else {
			fcommit();
		}
			
	
	} // End If No Constraints Found On Delete
	
	
}

//Edit CstJoutDt Table
else if ($mode == "editcstrqst" && $SCstRqstId != ""){

	$vCstRqstId     = '';
	$vCostCentCodeDt  = '';
	$vProjectCodeDt   = '';
	$vCstRqstPer    = 0;
	$vCstRqstAmt    = 0;
	$vCstRqstAmt1   = 0;
	$vCstRqstAmt2   = 0;
	
	   
	if (empty($txtCostCentCodeDt))
		$vCostCentCodeDt = 'Null';
	else
		$vCostCentCodeDt = "'".$txtCostCentCodeDt."'";
			
	if (empty($txtProjectCodeDt))
		$vProjectCodeDt = 'Null';
	else
		$vProjectCodeDt = "'".$txtProjectCodeDt."'";   

	
	if (empty($txtCstRqstPer))
		$txtCstRqstPer = 0;
	if (empty($txtCstRqstAmt))
		$txtCstRqstAmt = 0;
	if (empty($txtCstRqstAmt1))
		$txtCstRqstAmt1 = 0;
	if (empty($txtCstRqstAmt2))
		$txtCstRqstAmt2 = 0;
			
			
	
	
	$vCstRqstPer   = $txtCstRqstPer;
	$vCstRqstAmt   = $txtCstRqstAmt;
	$vCstRqstAmt1  = $txtCstRqstAmt1;
	$vCstRqstAmt2  = $txtCstRqstAmt2;	

		
	fbegin();
	$query = " UPDATE Temp_CstRqst SET ".
			 " Project_code = $vProjectCodeDt, Costcent_Code = $vCostCentCodeDt, CstRqst_Prcnt = $vCstRqstPer, ". 
			 " CstRqst_Amount = $vCstRqstAmt, CstRqst_Amount1 = $vCstRqstAmt1, CstRqst_Amount2 = $vCstRqstAmt2  ".
			 " WHERE Cst_increment = '".$SCstRqstId."'";

	//echo $query."<br>";exit;				
	$result=mysql_query($query,$connection);
		
	if ($result){
		
		fcommit();
	} else{
		$message  = mysql_error(); 
		frollback();
	}

	
} 


// Insert ChkRqst
else if ($mode == "insertchkrqst" && $STransId != "" && $STransDtId != ""){
	
	$vChkRqstId = '';
	$vCheckNo     = '';
	$vBank        = '';
	$vValueDate   = '';
	$vCurrCode    = '';
	$vChkRqstLedger = '';
	$vChkRqstType   = '';
	$vChkRqstAmt    = 0;
	$vChkRqstAmt1   = 0;
	$vChkRqstAmt2   = 0;
	
	$vCheckExists = 0;
	


	if (empty($txtChkRqstAmt))
		$txtChkRqstAmt = 0;
	if (empty($txtChkRqstAmt1))
		$txtChkRqstAmt1 = 0;
	if (empty($txtChkRqstAmt2))
		$txtChkRqstAmt2 = 0;
			
			

	$vCheckNo     = $txtCheckNo;
	$vBank        = $txtBank;
	$vValueDate   = $txtValueDate;
	$vCurrCode    = $txtCurrCode;
	$vChkRqstLedger = $txtChkRqstLedger;
	$vChkRqstType   = $txtChkRqstType;
	$vChkRqstAmt    = $txtChkRqstAmt;
	$vChkRqstAmt1   = $txtChkRqstAmt1;
	$vChkRqstAmt2   = $txtChkRqstAmt2;	
		
	
	if ($vChkRqstType == 'A') {
		$sub_query=" select count(*) from checks ".
				   " where ledger_number = '".$vChkRqstLedger."' ". 
				   " and chk_number = '".$vCheckNo."' ";
				   
		//echo $sub_query;exit;
		$sub_result = selectData($connection, $sub_query);
		$vCheckExists = $sub_result[0];
	}
	
	if ($vChkRqstType == 'A' && $vCheckExists == 0) {
		$sub_query= " select count(*) from Temp_ChkRqst ".
		            " where ledger_number = '".$vChkRqstLedger."' ". 
					" and chk_number = '".$vCheckNo."'";
					
		//echo $sub_query;exit;
		$sub_result = selectData($connection, $sub_query);
		$vCheckExists = $sub_result[0];
	}
	
	
	if ($vCheckExists == 0) {	
		fbegin();
		$query = " INSERT INTO Temp_ChkRqst (PaymentRqstDt_Id, Chk_number, Chk_bank, Chk_ValueDate, Currency_Code, Chk_Amount, Chk_Amount1, Chk_Amount2, ".
				 " Ledger_Number, Chk_Type, Synchronized, ChkRqst_Posted, Logged_Code ) ". 
				 " VALUES ( ".$STransDtId.", '".ereg_replace("'", "\'", trim($vCheckNo))."', '".ereg_replace("'", "\'", trim($vBank))."', '".insertMySQLDate($vValueDate)."', ".
				 "          '".$vCurrCode."', $vChkRqstAmt, $vChkRqstAmt1, $vChkRqstAmt2, '".$vChkRqstLedger."', '".$vChkRqstType."', 0, 0, '".$_SESSION['useraccount']."'	) ";
				 
		//echo $query."<br>";exit;				
		$result=mysql_query($query,$connection);
			
		if ($result){
			fcommit();
		} else{
			$message  = mysql_error(); 
			frollback();
		}	
	} else {
		$message  = 'Insert not allowed. Check No. already exists.'; 
	}	
	

} 

//Edit ChkRqst Table
else if ($mode == "editchkrqst" && $SChkRqstId != ""){

	$vChkRqstId = '';
	$vCheckNo     = '';
	$vBank        = '';
	$vValueDate   = '';
	$vCurrCode    = '';
	$vChkRqstLedger = '';
	$vChkRqstType   = '';
	$vChkRqstAmt    = 0;
	$vChkRqstAmt1   = 0;
	$vChkRqstAmt2   = 0;
	


	if (empty($txtChkRqstAmt))
		$txtChkRqstAmt = 0;
	if (empty($txtChkRqstAmt1))
		$txtChkRqstAmt1 = 0;
	if (empty($txtChkRqstAmt2))
		$txtChkRqstAmt2 = 0;
			
			

	$vCheckNo     = $txtCheckNo;
	$vBank        = $txtBank;
	$vValueDate   = $txtValueDate;
	$vCurrCode    = $txtCurrCode;
	$vChkRqstLedger = $txtChkRqstLedger;
	$vChkRqstType   = $txtChkRqstType;
	$vChkRqstAmt    = $txtChkRqstAmt;
	$vChkRqstAmt1   = $txtChkRqstAmt1;
	$vChkRqstAmt2   = $txtChkRqstAmt2;	
		
			
	fbegin();
	$query = " UPDATE Temp_ChkRqst SET ".
			 " Chk_bank = '".ereg_replace("'", "\'", trim($vBank))."', Chk_ValueDate = '".insertMySQLDate($vValueDate)."', ". 
			 " Chk_Amount = $vChkRqstAmt, Chk_Amount1 = $vChkRqstAmt1, Chk_Amount2 = $vChkRqstAmt2  ".
			 " WHERE Chk_Increment = '".$SChkRqstId."'";

	//echo $query."<br>";exit;				
	$result=mysql_query($query,$connection);
		
	if ($result){
		
		fcommit();
	} else{
		$message  = mysql_error(); 
		frollback();
	}

	
} 
//Delete ChkRqst
else if ($mode == "deletechkrqst" && $SChkRqstId != ""){

	$AllowDelete = 1;
	$ErrorExists = 0;
	$message  = '';
	
	
		
	//If No Constraints Exists On Delete
	if ($AllowDelete == 1) {

		
		fbegin();
		
		// Delete From TempChkRqst For Selected Record
		$query = " DELETE FROM Temp_ChkRqst ".
				 " WHERE Chk_Increment = '".$SChkRqstId."'";
					 
		if (!updateData($connection, $query, true)) {
			$message  = mysql_error();
			$ErrorExists = 1;
			frollback();
		}  
		else {
			fcommit();
		}
			
	
	} // End If No Constraints Found On Delete
	
	
}


// Insert BillRqst
else if ($mode == "insertbillrqst" && $STransId != "" && $STransDtId != ""){
	
	$vBillRqstId = '';
	$vBillNo      = '';
	$vBank        = '';
	$vValueDate   = '';
	$vCurrCode    = '';
	$vBillRqstLedger = '';
	$vBillRqstType   = '';
	$vBillRqstAmt    = 0;
	$vBillRqstAmt1   = 0;
	$vBillRqstAmt2   = 0;
	
	$vBillExists = 0;
	


	if (empty($txtBillRqstAmt))
		$txtBillRqstAmt = 0;
	if (empty($txtBillRqstAmt1))
		$txtBillRqstAmt1 = 0;
	if (empty($txtBillRqstAmt2))
		$txtBillRqstAmt2 = 0;
			
			

	$vBillNo      = $txtBillNo;
	$vBank        = $txtBank;
	$vValueDate   = $txtValueDate;
	$vCurrCode    = $txtCurrCode;
	$vBillRqstLedger = $txtBillRqstLedger;
	$vBillRqstType   = $txtBillRqstType;
	$vBillRqstAmt    = $txtBillRqstAmt;
	$vBillRqstAmt1   = $txtBillRqstAmt1;
	$vBillRqstAmt2   = $txtBillRqstAmt2;	
		
	
	if ($vBillRqstType == 'A') {
		$sub_query=" select count(*) from bills ".
				   " where ledger_number = '".$vBillRqstLedger."' ". 
				   " and bill_number = '".$vBillNo."' ";
				   
		//echo $sub_query;exit;
		$sub_result = selectData($connection, $sub_query);
		$vBillExists = $sub_result[0];
	}
	
	if ($vBillExists == 0) {
		$sub_query= " select count(*) from Temp_BillRqst ".
		            " where ledger_number = '".$vBillRqstLedger."' ". 
					" and Bill_number = '".$vBillNo."'";
					
		//echo $sub_query;exit;
		$sub_result = selectData($connection, $sub_query);
		$vBillExists = $sub_result[0];
	}
	
	

	if ($vBillExists == 0) {	
		fbegin();
		$query = " INSERT INTO Temp_BillRqst (PaymentRqstDt_Id, Bill_Number, Bill_Bank, Bill_ValueDate, Currency_Code, Bill_Amount, Bill_Amount1, Bill_Amount2, ".
				 " Ledger_Number, Bill_Type, Synchronized, Logged_Code ) ". 
				 " VALUES ( ".$STransDtId.", '".ereg_replace("'", "\'", trim($vBillNo))."', '".ereg_replace("'", "\'", trim($vBank))."', '".insertMySQLDate($vValueDate)."', ".
				 "          '".$vCurrCode."', $vBillRqstAmt, $vBillRqstAmt1, $vBillRqstAmt2, '".$vBillRqstLedger."', '".$vBillRqstType."', 0, '".$_SESSION['useraccount']."'	) ";
				 
		//echo $query."<br>";exit;				
		$result=mysql_query($query,$connection);
			
		if ($result){
			fcommit();
		} else{
			$message  = mysql_error(); 
			frollback();
		}	
	} else
		$message  = 'Insert not allowed. Bill No. already exists.'; 
	

} 

//Edit BillRqst Table
else if ($mode == "editbillrqst" && $SBillRqstId != ""){

	$vBillRqstId = '';
	$vBillNo       = '';
	$vBank         = '';
	$vValueDate    = '';
	$vCurrCode     = '';
	$vBillRqstLedger = '';
	$vBillRqstType   = '';
	$vBillRqstAmt    = 0;
	$vBillRqstAmt1   = 0;
	$vBillRqstAmt2   = 0;
	


	if (empty($txtBillRqstAmt))
		$txtBillRqstAmt = 0;
	if (empty($txtBillRqstAmt1))
		$txtBillRqstAmt1 = 0;
	if (empty($txtBillRqstAmt2))
		$txtBillRqstAmt2 = 0;
			
			

	$vBillNo      = $txtBillNo;
	$vBank        = $txtBank;
	$vValueDate   = $txtValueDate;
	$vCurrCode    = $txtCurrCode;
	$vBillRqstLedger = $txtBillRqstLedger;
	$vBillRqstType   = $txtBillRqstType;
	$vBillRqstAmt    = $txtBillRqstAmt;
	$vBillRqstAmt1   = $txtBillRqstAmt1;
	$vBillRqstAmt2   = $txtBillRqstAmt2;	
			
	fbegin();
	$query = " UPDATE Temp_BillRqst SET ".
			 " Bill_bank = '".ereg_replace("'", "\'", trim($vBank))."', Bill_ValueDate = '".insertMySQLDate($vValueDate)."', ". 
			 " Bill_Amount = $vBillRqstAmt, Bill_Amount1 = $vBillRqstAmt1, Bill_Amount2 = $vBillRqstAmt2  ".
			 " WHERE Bill_Increment = '".$SBillRqstId."'";

	//echo $query."<br>";exit;				
	$result=mysql_query($query,$connection);
		
	if ($result){
		
		fcommit();
	} else{
		$message  = mysql_error(); 
		frollback();
	}

	
} 
//Delete BillRqst
else if ($mode == "deletebillrqst" && $SBillRqstId != ""){

	$AllowDelete = 0;
	$ErrorExists = 0;
	$message  = '';
	
	
		
	//If No Constraints Exists On Delete
	if ($AllowDelete == 0) {

		
		fbegin();
		
		// Delete From TempBillRqst For Selected Record
		$query = " DELETE FROM Temp_BillRqst ".
				 " WHERE Bill_Increment = '".$SBillRqstId."'";
					 
		if (!updateData($connection, $query, true)) {
			$message  = mysql_error();
			$ErrorExists = 1;
			frollback();
		}  
		else {
			fcommit();
		}
			
	
	} // End If No Constraints Found On Delete
	
	
}


//Delete Record Mode From Main Tables
else if ($mode == "" && $mainmode == "delete" && $STransId != ''){

	$AllowDelete = 1;
	$message  = '';
	
	//Check If Trans Is Posted
	$query = "SELECT ifNull(PaymentRqst_Posted,0) As PaymentRqstPosted FROM PaymentRqst WHERE PaymentRqst_Id = ".$STransId;
	$result = selectData($connection, $query);
	$PaymentRqstPosted = $result[0];
	
	if ($PaymentRqstPosted == 1 ){
		$message  = 'Delete not allowed, Transaction posted.';
		$AllowDelete = 0;
	}
	
	if ($AllowDelete == 1) {
		//Check If Trans Is Confirmed
		$query = "SELECT ifNull(PaymentRqst_Confirmed,0) As PaymentRqstConfirmed FROM PaymentRqst WHERE PaymentRqst_Id = ".$STransId;
		$result = selectData($connection, $query);
		$PaymentRqstConfirmed = $result[0];
		
		if ($PaymentRqstConfirmed == 1 ){
			$message  = 'Delete not allowed, Transaction Confirmed.';
			$AllowDelete = 0;
		}
	}
	
	if ($AllowDelete == 1) {
		//Check PaymentRqst If Has Related Payment
		$query = " Select Count(*) ".
				 " From Trans  ".
				 " Where Trans.PaymentRqst_Id = '".$STransId."' ".
				 " And Trans_Type = 'PV' ";

		$result = selectData($connection, $query);
		$PaymentExists = $result[0];
			
		if ($PaymentExists > 0 ){
			$message  = 'Delete not allowed, Payment Request been used.';
			$AllowDelete = 0;
		}
	}	
	
	
	
	//If No Constraints Exists On Delete
	if ($AllowDelete == 1) {
		
		
	    // Delete From CstRqst For Selected Record
		$query = " DELETE FROM CstRqst ".
				 " WHERE CstRqst.PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = '".$STransId."') ";
				 
		if (!updateData($connection, $query, true))		 
			$message  = mysql_error(); 
			
		// Delete From ChkRqst For Selected Record
		$query = " DELETE FROM ChkRqst ".
				 " WHERE ChkRqst.PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = '".$STransId."') ";
				 
		if (!updateData($connection, $query, true))		 
			$message  = mysql_error(); 	
			
		// Delete From BillRqst For Selected Record
		$query = " DELETE FROM BillRqst ".
				 " WHERE BillRqst.PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = '".$STransId."') ";
				 
		if (!updateData($connection, $query, true))		 
			$message  = mysql_error(); 	

		// Delete From PaymentRqstDt For Selected Record
		$query = " DELETE FROM PaymentRqstDt ".
				 " WHERE PaymentRqst_Id = ".$STransId;
					 
		if (updateData($connection, $query, true)) {
			// Delete From PaymentRqst For Selected Record
			$query = " DELETE FROM PaymentRqst ".
					 " WHERE PaymentRqst_Id = ".$STransId;
				
			if (!updateData($connection, $query, true))		 
				$message  = mysql_error(); 
				
		}
		else
			$message  = mysql_error(); 	
		
	}
	
	
}

//Delete Record Mode From Temp Tables
else if ($mode == "" && $mainmode == "deletepending" && $STransId != ''){

	$query = " DELETE FROM Temp_ChkRqst ".
			 " WHERE PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = ".$STransId." ) ";
	if (!updateData($connection, $query, true))		 
      $message  = mysql_error(); 
	  
	$query = " DELETE FROM Temp_BillRqst ".
			 " WHERE PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = ".$STransId." ) ";
	if (!updateData($connection, $query, true))		 
      $message  = mysql_error(); 
	  
	$query = " DELETE FROM Temp_CstRqst ".
			 " WHERE PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = ".$STransId." ) ";
	if (!updateData($connection, $query, true))		 
      $message  = mysql_error(); 

	$query = " DELETE FROM Temp_PaymentRqstDt ".
			 " WHERE PaymentRqst_Id = ".$STransId;
	if (!updateData($connection, $query, true))
      $message  = mysql_error(); 
	
	  
	$query = " DELETE FROM Temp_PaymentRqst ".
			 " WHERE PaymentRqst_Id = ".$STransId;
	if (!updateData($connection, $query, true))
      $message  = mysql_error();   
	

}



//Search Mode	
else if(($mainmode == "search" || $mainmode == "search2" || $mainmode == "notification") && $STransId != ''){
			 
	$ErrorExists = 0;
	$AllowEdit   = 1;
	
	if ($AllowEdit == 1) {
		//Check If Trans Is Confirmed
		$query = "SELECT ifNull(PaymentRqst_Confirmed,0) As PaymentRqstConfirmed, User_Confirmed, ifNull(PaymentRqst_App,0) FROM PaymentRqst WHERE PaymentRqst_Id = ".$STransId;
		$result = selectData($connection, $query);
		$PaymentRqstConfirmed = $result[0];
		$ConfirmedBy		  = $result[1];
		$PaymentRqstApp	  	  = $result[2];
		
		if ($PaymentRqstConfirmed == 1 ){
			$message  = 'Edit not allowed, Transaction Confirmed.';
			$AllowEdit = 0;
		}
	}
	
	if ($toBeApprovedByHOD == 1){
		$isAuth = 0;
		$isHOD  = 0;
		$existInPayment = 0;
		$AllowEdit = 1;
		$message   = "";
		$queryHod = "select distinct 1 from ".$_SESSION['dbLabel'].".HOD_Users where hod_code = '".$_SESSION['useraccount']."' ";	
		$resHod = mysql_query($queryHod, $connection);
		$rowHod = mysql_num_rows($resHod);
		if ($rowHod > 0)
			$isHOD = 1;

		$querypayment = "select 1 from trans where PaymentRqst_id = $STransId ";
		$respayment = mysql_query($querypayment, $connection);
		$rwpayment = mysql_num_rows($respayment);
		if ($rwpayment > 0) 
			$existInPayment = 1;
		
		if ($ConfirmedBy == $_SESSION['useraccount'])
			$isAuth = 1;

		if ($PaymentRqstApp == 1 && $isHOD == 0 && $isAuth == 0){
			$message  = 'Edit not allowed, Transaction Approved.';
			$AllowEdit = 0;
		}
		
		if ($PaymentRqstConfirmed == 1 && $isAuth == 0){
			$message  = 'Edit not allowed, Transaction Confirmed.';
			$AllowEdit = 0;
		}
		
		if ($existInPayment == 1){
			$message  = 'Edit not allowed, Transaction Exists in Payment.';
			$AllowEdit = 0;
		} 
			
		
	} 
	if($mainmode == "notification" && $AllowEdit == 1){
		$querychk = "select 1 from Temp_PaymentRqst WHERE PaymentRqst_Id = ".$STransId;
		$result = selectData($connection, $querychk);
		$ExistInPending = $result[0];
		if($ExistInPending == 1){
			echo "[BRK]";
			echo $message;
			exit;
		}
	}
	if ($AllowEdit == 1) {
	
		//Copy Data From Main Tables To Temp Tables
		$query = " INSERT INTO Temp_PaymentRqst (PaymentRqst_Id, PaymentRqst_RefGovernment, PaymentRqst_Date, PaymentRqst_ValueDate, PaymentRqst_Remark, PaymentRqst_Name, ".
				 " PaymentRqst_ManualNo, PaymentRqst_Desc, PaymentRqst_App, Touser_account, PaymentRqst_Confirmed, User_Confirmed, Project_Code, Costcent_code, PaymentRqst_Posted, ".
				 " Currency_Code, PaymentRqst_AmountD1, PaymentRqst_AmountD2, PaymentRqst_AmountC1, PaymentRqst_AmountC2, PaymentRqst_BelongsToGrp, Synchronized, PaymentRqst_Used, ".
				 " Porder_Reference, paymentmode_code, sourceFund_Code, User_Account, Logged_Code ".
				 " ,paymentrqst_cash,paymentrqst_check,paymentrqst_costshare,paymentrqst_guarantee,purRelated_id ) ".
				 " SELECT PaymentRqst_Id, PaymentRqst_RefGovernment, PaymentRqst_Date, PaymentRqst_ValueDate, PaymentRqst_Remark, PaymentRqst_Name, ".
				 " PaymentRqst_ManualNo, PaymentRqst_Desc, PaymentRqst_App, Touser_account, PaymentRqst_Confirmed, User_Confirmed, Project_Code, Costcent_code, PaymentRqst_Posted, ".
				 " Currency_Code, PaymentRqst_AmountD1, PaymentRqst_AmountD2, PaymentRqst_AmountC1, PaymentRqst_AmountC2, PaymentRqst_BelongsToGrp, Synchronized, PaymentRqst_Used, ".
				 " Porder_Reference, paymentmode_code, sourceFund_Code, User_Account, '".$_SESSION['useraccount']."' ".
				 " ,paymentrqst_cash,paymentrqst_check,paymentrqst_costshare,paymentrqst_guarantee,purRelated_id ".
				 " FROM PaymentRqst ".
				 " WHERE PaymentRqst_Id = ".$STransId;

		if (!updateData($connection, $query, true)) {
			$ErrorExists = 1;
			$message  = mysql_error(); 	
		}  


		if ($ErrorExists == 0) {
			//echo $message.$query; exit;				 
			$query = " INSERT INTO Temp_PaymentRqstDt (PaymentRqstDt_Id, PaymentRqst_Id, PaymentRqst_Date, Ledger_Number, PaymentRqstDt_Desc, PaymentRqstDt_Desc1, ". 
					 " PaymentRqstDt_ValueDate, Currency_Code, PaymentRqstDt_Amount, PaymentRqstDt_Amount1, PaymentRqstDt_Amount2, PaymentRqstDt_dbcd, PaymentRqstDt_Posted, ".
					 " PaymentRqstdt_dbcdAra, Synchronized, Logged_Code )  ".
					 " SELECT PaymentRqstDt_Id, PaymentRqst_Id, PaymentRqst_Date, Ledger_Number, PaymentRqstDt_Desc, PaymentRqstDt_Desc1, ". 
					 " PaymentRqstDt_ValueDate, Currency_Code, PaymentRqstDt_Amount, PaymentRqstDt_Amount1, PaymentRqstDt_Amount2, PaymentRqstDt_dbcd, PaymentRqstDt_Posted, ".
					 " PaymentRqstdt_dbcdAra, Synchronized, '".$_SESSION['useraccount']."'  ".
					 " FROM PaymentRqstDt ".
					 " WHERE PaymentRqst_Id = ".$STransId;
			
			if (!updateData($connection, $query, true))
				$message  = mysql_error(); 			


			$query = " INSERT INTO Temp_CstRqst (PaymentRqstDt_Id, Project_code, Costcent_Code, CstRqst_Prcnt, CstRqst_Amount, CstRqst_Amount1, CstRqst_Amount2, Synchronized, CstRqst_Posted, Logged_Code ) ".
					 " SELECT PaymentRqstDt_Id, Project_code, Costcent_Code, CstRqst_Prcnt, CstRqst_Amount, CstRqst_Amount1, CstRqst_Amount2, Synchronized, CstRqst_Posted, '".$_SESSION['useraccount']."' ".
					 " FROM CstRqst ".
					 " WHERE CstRqst.PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = '".$STransId."') ";
					 
			if (!updateData($connection, $query, true))
			  $message  = mysql_error(); 
			  
			  
			$query = " INSERT INTO Temp_ChkRqst (PaymentRqstDt_Id, Chk_Number, Chk_Bank, Chk_ValueDate, Currency_Code, Chk_Amount, Chk_Amount1, Chk_Amount2, Ledger_Number, Chk_Type, Synchronized, ChkRqst_Posted, Logged_Code ) ".
					 " SELECT PaymentRqstDt_Id, Chk_Number, Chk_Bank, Chk_ValueDate, Currency_Code, Chk_Amount, Chk_Amount1, Chk_Amount2, Ledger_Number, Chk_Type, Synchronized, ChkRqst_Posted, '".$_SESSION['useraccount']."' ".
					 " FROM ChkRqst ".
					 " WHERE ChkRqst.PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = '".$STransId."') ";
					 
			if (!updateData($connection, $query, true))
			  $message  = mysql_error();

			$query = " INSERT INTO Temp_BillRqst (PaymentRqstDt_Id, Bill_Number, Bill_Bank, Bill_ValueDate, Currency_Code, Bill_Amount, Bill_Amount1, Bill_Amount2, Ledger_Number, Bill_Type, Synchronized, Logged_Code ) ".
					 " SELECT PaymentRqstDt_Id, Bill_Number, Bill_Bank, Bill_ValueDate, Currency_Code, Bill_Amount, Bill_Amount1, Bill_Amount2, Ledger_Number, Bill_Type, Synchronized, '".$_SESSION['useraccount']."' ".
					 " FROM BillRqst ".
					 " WHERE BillRqst.PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = '".$STransId."') ";
					 
			if (!updateData($connection, $query, true))
			  $message  = mysql_error();  
	  
		
		}
	}	
	if( $mainmode == "notification" ) {
		echo "[BRK]";
		echo $message;
		exit;
	}
}

//Search Approve/Confirm List Mode	
else if(($mainmode == "searchapprove" || $mainmode == "searchconfirmed") && $STransId != ''){
		 
	$ErrorExists = 0;
	//Copy Data From Main Tables To Temp Tables
	$query = " INSERT INTO Temp_PaymentRqst (PaymentRqst_Id, PaymentRqst_RefGovernment, PaymentRqst_Date, PaymentRqst_ValueDate, PaymentRqst_Remark, PaymentRqst_Name, ".
	         " PaymentRqst_ManualNo, PaymentRqst_Desc, PaymentRqst_App, Touser_account, PaymentRqst_Confirmed, User_Confirmed, Project_Code, Costcent_code, PaymentRqst_Posted, ".
			 " Currency_Code, PaymentRqst_AmountD1, PaymentRqst_AmountD2, PaymentRqst_AmountC1, PaymentRqst_AmountC2, PaymentRqst_BelongsToGrp, Synchronized, User_Account, Logged_Code ".
			 " ,paymentrqst_cash,paymentrqst_check,paymentrqst_costshare,paymentrqst_guarantee ) ".
			 " SELECT PaymentRqst_Id, PaymentRqst_RefGovernment, PaymentRqst_Date, PaymentRqst_ValueDate, PaymentRqst_Remark, PaymentRqst_Name, ".
	         " PaymentRqst_ManualNo, PaymentRqst_Desc, PaymentRqst_App, Touser_account, PaymentRqst_Confirmed, User_Confirmed, Project_Code, Costcent_code, PaymentRqst_Posted, ".
			 " Currency_Code, PaymentRqst_AmountD1, PaymentRqst_AmountD2, PaymentRqst_AmountC1, PaymentRqst_AmountC2, PaymentRqst_BelongsToGrp, Synchronized, User_Account, '".$_SESSION['useraccount']."' ".
			 " ,paymentrqst_cash,paymentrqst_check,paymentrqst_costshare,paymentrqst_guarantee ".
			 " FROM PaymentRqst ".
			 " WHERE PaymentRqst_Id = ".$STransId;
	
	if (!updateData($connection, $query, true)) {
		$ErrorExists = 1;
		$message  = mysql_error(); 	
	}  


	if ($ErrorExists == 0) {
		//echo $message.$query; exit;				 
		$query = " INSERT INTO Temp_PaymentRqstDt (PaymentRqstDt_Id, PaymentRqst_Id, PaymentRqst_Date, Ledger_Number, PaymentRqstDt_Desc, PaymentRqstDt_Desc1, ". 
				 " PaymentRqstDt_ValueDate, Currency_Code, PaymentRqstDt_Amount, PaymentRqstDt_Amount1, PaymentRqstDt_Amount2, PaymentRqstDt_dbcd, PaymentRqstDt_Posted, ".
				 " PaymentRqstdt_dbcdAra, Synchronized, Logged_Code )  ".
				 " SELECT PaymentRqstDt_Id, PaymentRqst_Id, PaymentRqst_Date, Ledger_Number, PaymentRqstDt_Desc, PaymentRqstDt_Desc1, ". 
				 " PaymentRqstDt_ValueDate, Currency_Code, PaymentRqstDt_Amount, PaymentRqstDt_Amount1, PaymentRqstDt_Amount2, PaymentRqstDt_dbcd, PaymentRqstDt_Posted, ".
				 " PaymentRqstdt_dbcdAra, Synchronized, '".$_SESSION['useraccount']."'  ".
				 " FROM PaymentRqstDt ".
				 " WHERE PaymentRqst_Id = ".$STransId;
		
		if (!updateData($connection, $query, true))
			$message  = mysql_error(); 			


		$query = " INSERT INTO Temp_CstRqst (PaymentRqstDt_Id, Project_code, Costcent_Code, CstRqst_Prcnt, CstRqst_Amount, CstRqst_Amount1, CstRqst_Amount2, Synchronized, CstRqst_Posted, Logged_Code ) ".
				 " SELECT PaymentRqstDt_Id, Project_code, Costcent_Code, CstRqst_Prcnt, CstRqst_Amount, CstRqst_Amount1, CstRqst_Amount2, Synchronized, CstRqst_Posted, '".$_SESSION['useraccount']."' ".
				 " FROM CstRqst ".
				 " WHERE CstRqst.PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = '".$STransId."') ";
				 
		if (!updateData($connection, $query, true))
		  $message  = mysql_error(); 
		  
		  
		$query = " INSERT INTO Temp_ChkRqst (PaymentRqstDt_Id, Chk_Number, Chk_Bank, Chk_ValueDate, Currency_Code, Chk_Amount, Chk_Amount1, Chk_Amount2, Ledger_Number, Chk_Type, Synchronized, ChkRqst_Posted, Logged_Code ) ".
				 " SELECT PaymentRqstDt_Id, Chk_Number, Chk_Bank, Chk_ValueDate, Currency_Code, Chk_Amount, Chk_Amount1, Chk_Amount2, Ledger_Number, Chk_Type, Synchronized, ChkRqst_Posted, '".$_SESSION['useraccount']."' ".
				 " FROM ChkRqst ".
				 " WHERE ChkRqst.PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = '".$STransId."') ";
				 
		if (!updateData($connection, $query, true))
		  $message  = mysql_error();

		$query = " INSERT INTO Temp_BillRqst (PaymentRqstDt_Id, Bill_Number, Bill_Bank, Bill_ValueDate, Currency_Code, Bill_Amount, Bill_Amount1, Bill_Amount2, Ledger_Number, Bill_Type, Synchronized, Logged_Code ) ".
				 " SELECT PaymentRqstDt_Id, Bill_Number, Bill_Bank, Bill_ValueDate, Currency_Code, Bill_Amount, Bill_Amount1, Bill_Amount2, Ledger_Number, Bill_Type, Synchronized, '".$_SESSION['useraccount']."' ".
				 " FROM BillRqst ".
				 " WHERE BillRqst.PaymentRqstDt_Id In (Select PaymentRqstDt_Id From Temp_PaymentRqstDt Where PaymentRqst_Id = '".$STransId."') ";
				 
		if (!updateData($connection, $query, true))
		  $message  = mysql_error();  
  
    
	}
}

//Confirm Request Mode	
else if(($mainmode == "confirmrequest" || $mainmode == "confirmrequestitem") && $STransId != ''){

	$sub_query = " Select PaymentRqst_Date, Currency_Code, Round(ifNull(PaymentRqst_AmountD1,0)) ".
				 " From PaymentRqst ".
				 " Where PaymentRqst_Id = ".$STransId;
	$sub_result = selectData($connection, $sub_query);
	$vPayRqstDate  = $sub_result[0];
	$vCurrencyCode = $sub_result[1];
	$vPayRqstAmtD1 = $sub_result[2];
	

	$sub_query = " Select Round(ifNull(Sum(ifNull(PaymentRqst_AmountD1,0)),0)) As TotalDebitAmount1 ".
				 " From PaymentRqst ".
				 " Where 1 = 1 ".
				 " And Upper(User_Confirmed) = Upper('".$_SESSION['useraccount']."') ";
				 
	if ($Login_Daily == 1){
		$sub_query = $sub_query . " And Date_Format(PaymentRqst_Date, '%Y%m%d') = Date_Format('".$vPayRqstDate."', '%Y%m%d') ";
	}
	else if ($Login_Weekly == 1){
		$sub_query = $sub_query . " And Date_Format(PaymentRqst_Date, '%x%v') = Date_Format('".$vPayRqstDate."', '%x%v') ";
	}
	else if ($Login_Monthly == 1){
		$sub_query = $sub_query . " And Date_Format(PaymentRqst_Date, '%Y%m') = Date_Format('".$vPayRqstDate."', '%Y%m') ";
	}
	//echo $sub_query; exit;
	$sub_result = selectData($connection, $sub_query);
	$vTotalDebitAmount1 = $sub_result[0];
	$vTotalDebitAmount1 = $vTotalDebitAmount1 + $vPayRqstAmtD1;
	
	if (($vTotalDebitAmount1 <= $Login_Limit && $Login_Limit > 0) || ($Login_Daily == 0 && $Login_Weekly == 0 && $Login_Monthly == 0)) {
		$sub_query1 = " UPDATE PaymentRqst SET ".
					  " PaymentRqst_Confirmed = 1, User_Confirmed = '".$_SESSION['useraccount']."'  ".
					  " WHERE PaymentRqst_Id = ".$STransId;
				 
		$sub_result1=mysql_query($sub_query1,$connection);
	}
	else {
		$message = " Confirmation not allowed. Limit Exceeded. Total: ".$vCurrencyCode."  ".number_format($vTotalDebitAmount1, $DecimalBase1)." - Allowed: ".$vCurrencyCode."  ".number_format($Login_Limit, $DecimalBase1);
	}

}

//Confirm Request Mode	
else if($mainmode == "approverequest" && $STransId != ''){

	$sub_query = " UPDATE PaymentRqst SET ".
				 " PaymentRqst_App = 1  ".
				 " WHERE PaymentRqst_Id = ".$STransId;
				 
	$sub_result=mysql_query($sub_query,$connection);
	
}

// approve and confirm 
else if ($toBeApprovedByHOD == 1 && $mainmode == 'approveConfirm' && $STransId != ''){
	$ErrorExists = 0;
	
	$query  = "select ifnull(PaymentRqst_App,0), ifnull(PaymentRqst_Confirmed,0), Touser_account, user_confirmed, PaymentRqst_refgovernment, PaymentRqst_DATE, round(PaymentRqst_amountD1,$DecimalBase)
				from PaymentRqst where PaymentRqst_Id = ".$STransId;
	$result = mysql_query($query, $connection); 
	$row = mysql_fetch_row($result);
	$approved    = $row[0];
	$confirmed   = $row[1];
	$approvedBy  = $row[2];
	$confirmedBy = $row[3];
	$vTransRef	 = $row[4];
	$TransDate	 = $row[5];
	$amount 	 = $row[6];
	
	if ( $Login_Limit > 0 && $Login_Limit < $amount){
		$ErrorExists = 1;
		$message = "Amount Requested > Limit Amount. Payment Request Cannot be Approved/Confirmed";		
	}
	
	
	if ($approved == 0 && strtoupper($approvedBy) == strtoupper($_SESSION['useraccount']) && $ErrorExists == 0){
		if ($confirmedBy == '')
			$message = "No user was assigned in 'To Be Confirmed By' ";
		else {
			$update = "update PaymentRqst set PaymentRqst_App = 1 where PaymentRqst_Id = ".$STransId;
			$result = mysql_query($update, $connection);		
		}
	} else if ($confirmed == 0 && strtoupper($confirmedBy) == strtoupper($_SESSION['useraccount']) && $ErrorExists == 0){
		if ($approved == 0)
			$message = "Payment Request has to be approved first by $approvedBy ";
		else {
			$update = "update PaymentRqst set PaymentRqst_Confirmed = 1 where PaymentRqst_Id = ".$STransId;
			$result = mysql_query($update, $connection);	
			$vNotifRef = get_nextsequence('notification_reference',$vProjCostCent,$connection);	
			$query_notify = "insert into notification (notification_type, notification_action, notification_description, 
					notification_reference, notification_createduser, notification_level, notification_date, 
					notification_done, notification_checked, trans_id, notification_table,notification_field, createdby_user
					) values ('PVR', 'To Confirm','Payment Request $vTransRef Approved By $approvedBy'
					,'$vNotifRef','$confirmedBy','5','".$TransDate."',0,0,
					$STransId,'PaymentRqst','PaymentRqst_Confirmed', '".$_SESSION['useraccount']."')";
			//echo $query_notify; 			
			$res_notify = mysql_query($query_notify,$connection);		
		}
	}
}
//Load Porder
else if($mainmode == "loadporder" && $SelectedRec != ''){  

	$sub_query = " Select Porder_reference, Porder_Date, Porder.Supplier_code, Suppliers.Ledger_Name, Porder.Project_Code, Porder.Costcent_code, Porder.Currency_code, ".
				 " Round((IfNull(Porder_Taxable,0) + IfNull(Porder_NoneTaxable,0) + IfNull(Porder_Vat,0) - IfNull(Porder_DiscountAmount,0) + ".
				 "       (ifNull(Porder_Tax1,0) + ifNull(Porder_Tax2,0) + ifNull(Porder_Tax3,0) + ifNull(Porder_Tax4,0) + ifNull(Porder_Tax5,0))), ".$DecimalBase.") As Porder_NetTotal ".
				 " From Porder ".
				 " Left Join Suppliers on Suppliers.Ledger_Number = Porder.Supplier_code ".
				 " Where Porder_Id = '".$SelectedRec."'";
	$sub_result = selectData($connection, $sub_query);
	
	
	
	$vPorderRef  = $sub_result[0];
	$vPorderDate = $sub_result[1];
	$vTransCurr  = $sub_result[6];
	
	if (empty($sub_result[5]))	$CostCentCode = 'Null';
	else $CostCentCode = "'".$sub_result[5]."'";
			
	if (empty($sub_result[4]))	$ProjectCode = 'Null';
	else $ProjectCode = "'".$sub_result[4]."'";
	
	fbegin();	
	$vTransId = get_nextsequence('PaymentRqst_id','',$connection);	
	fcommit();
	 
	fbegin();
	$query = " INSERT INTO Temp_PaymentRqst (PaymentRqst_Id, PaymentRqst_RefGovernment, Porder_Reference, PaymentRqst_Date, PaymentRqst_ValueDate, PaymentRqst_Remark, PaymentRqst_Name, ".
			 " PaymentRqst_ManualNo, PaymentRqst_Desc, PaymentRqst_App, Touser_account, PaymentRqst_Confirmed, User_Confirmed, Project_Code, Costcent_code, PaymentRqst_Posted, ".
			 " Currency_Code, PaymentRqst_AmountD1, PaymentRqst_AmountD2, PaymentRqst_AmountC1, PaymentRqst_AmountC2, PaymentRqst_BelongsToGrp, Synchronized, User_Account, Logged_Code ) ".
			 " VALUES ( ".$vTransId.", Null, '".$vPorderRef."', now(), now(), Null, Null, Null, Null, 0, Null, 0, Null, $ProjectCode, $CostCentCode, 0, ".
			 "          '".$vTransCurr."', 0, 0, 0, 0, $FI_BelongsToGrp, 0, '".$_SESSION['useraccount']."', '".$_SESSION['useraccount']."' )";
					 
			 
	$result=mysql_query($query,$connection);
	//echo $query."<br>";exit;	
		
	if ($result){
		$STransId = $vTransId;
		fcommit();
	}else{
		$STransId = "";
		$message  = mysql_error(); 
		frollback();
	}

	//Insert Detail Tables
	if ($STransId != ""){
		$ErrorExists = 0;
		
		$vTransAmountDb1 = 0;
		$vTransAmountDb2 = 0;
		$vTransAmountcd1 = 0;
		$vTransAmountcd2 = 0;
			
			
		$vTransDtdbcd  = 'D';
		$vLedgerNo     = $sub_result[2];
		$vTransDtDesc  = 'Payment For '.$sub_result[3].' On Purchase Order No: '.$sub_result[0];
		$vTransDtCurr  = $sub_result[6];
		
		$vTransDtAmount    = $sub_result[7];
		$vTransDtAmountB1  = ConvertCurrency($vTransDtCurr, $base1, $sub_result[1], $vTransDtAmount, $connection); 
		$vTransDtAmountB2  = ConvertCurrency($vTransDtCurr, $base2, $sub_result[1], $vTransDtAmount, $connection); 
					
		fbegin();
		$vTransDtId = get_nextsequence('PaymentRqstdt_id','',$connection);	
		fcommit();

		fbegin();
		$query = " INSERT INTO Temp_PaymentRqstDt (PaymentRqstDt_Id, PaymentRqst_Id, PaymentRqst_Date, Ledger_Number, PaymentRqstDt_Desc, PaymentRqstDt_Desc1, ". 
				 " PaymentRqstDt_ValueDate, Currency_Code, PaymentRqstDt_Amount, PaymentRqstDt_Amount1, PaymentRqstDt_Amount2, PaymentRqstDt_dbcd, PaymentRqstDt_Posted, ".
				 " Synchronized, Logged_Code )  ".
				 " VALUES ( ".$vTransDtId.", ".$STransId.", now(), '".$vLedgerNo."', '".ereg_replace("'", "\'", trim($vTransDtDesc))."', Null, now(), '".$vTransDtCurr."', ".
				 "          $vTransDtAmount, $vTransDtAmountB1, $vTransDtAmountB2, '".$vTransDtdbcd."', 0, 0, '".$_SESSION['useraccount']."') ";
			 
		//echo $query."<br>";exit;				
		$result=mysql_query($query,$connection);
		
		if ($result){
			fcommit();
			
			$vCstRqstDtPer   = 100;
			$vCstRqstDtAmt   = $vTransDtAmount;
			$vCstRqstDtAmt1  = $vTransDtAmountB1;
			$vCstRqstDtAmt2  = $vTransDtAmountB2;
			$vProjectCodeDt	 = $ProjectCode;
			$vCostCentCodeDt = $CostCentCode;
			
			//Case Header Project or Costcenter Is Not Empty Automatically Insert Into CstRqst	
			if ($vProjectCodeDt != 'Null' || $vCostCentCodeDt != 'Null' ) {
		
				$query = " INSERT INTO Temp_CstRqst (PaymentRqstDt_Id, Project_code, Costcent_Code, CstRqst_Prcnt, CstRqst_Amount, CstRqst_Amount1, CstRqst_Amount2, ".
						 " Synchronized, CstRqst_Posted, Logged_Code ) ". 
						 " VALUES ( ".$vTransDtId.", $vProjectCodeDt, $vCostCentCodeDt, $vCstRqstDtPer, $vCstRqstDtAmt, $vCstRqstDtAmt1, $vCstRqstDtAmt2, ".
						 "          0, 0, '".$_SESSION['useraccount']."'	) ";
						 
				//echo $query."<br>";exit;				
				$result=mysql_query($query,$connection);
			}

		} else{
			$ErrorExists = 1;
			$message  = mysql_error(); 
			frollback();
		}	
		
		
		if ($ErrorExists == 0){
			$vTransDtdbcd  = 'C';
			$vLedgerNo     = $DefaultCreditAccount;
			$vTransDtDesc  = 'Payment For '.$sub_result[3].' On Purchase Order No: '.$sub_result[0];
			$vTransDtCurr  = $sub_result[6];
			
			$vTransDtAmount    = $sub_result[7];
			$vTransDtAmountB1  = ConvertCurrency($vTransDtCurr, $base1, $sub_result[1], $vTransDtAmount, $connection); 
			$vTransDtAmountB2  = ConvertCurrency($vTransDtCurr, $base2, $sub_result[1], $vTransDtAmount, $connection); 
						
			fbegin();
			$vTransDtId = get_nextsequence('PaymentRqstdt_id','',$connection);	
			fcommit();

			fbegin();
			$query = " INSERT INTO Temp_PaymentRqstDt (PaymentRqstDt_Id, PaymentRqst_Id, PaymentRqst_Date, Ledger_Number, PaymentRqstDt_Desc, PaymentRqstDt_Desc1, ". 
					 " PaymentRqstDt_ValueDate, Currency_Code, PaymentRqstDt_Amount, PaymentRqstDt_Amount1, PaymentRqstDt_Amount2, PaymentRqstDt_dbcd, PaymentRqstDt_Posted, ".
					 " Synchronized, Logged_Code )  ".
					 " VALUES ( ".$vTransDtId.", ".$STransId.", now(), '".$vLedgerNo."', '".ereg_replace("'", "\'", trim($vTransDtDesc))."', Null, now(), '".$vTransDtCurr."', ".
					 "          $vTransDtAmount, $vTransDtAmountB1, $vTransDtAmountB2, '".$vTransDtdbcd."', 0, 0, '".$_SESSION['useraccount']."') ";
				 
			//echo $query."<br>";exit;				
			$result=mysql_query($query,$connection);
			
			if ($result){
				fcommit();
				
				$vCstRqstDtPer   = 100;
				$vCstRqstDtAmt   = $vTransDtAmount;
				$vCstRqstDtAmt1  = $vTransDtAmountB1;
				$vCstRqstDtAmt2  = $vTransDtAmountB2;
				$vProjectCodeDt	 = $ProjectCode;
				$vCostCentCodeDt = $CostCentCode;
				
				//Case Header Project or Costcenter Is Not Empty Automatically Insert Into CstRqst	
				if ($vProjectCodeDt != 'Null' || $vCostCentCodeDt != 'Null' ) {
			
					$query = " INSERT INTO Temp_CstRqst (PaymentRqstDt_Id, Project_code, Costcent_Code, CstRqst_Prcnt, CstRqst_Amount, CstRqst_Amount1, CstRqst_Amount2, ".
							 " Synchronized, CstRqst_Posted, Logged_Code ) ". 
							 " VALUES ( ".$vTransDtId.", $vProjectCodeDt, $vCostCentCodeDt, $vCstRqstDtPer, $vCstRqstDtAmt, $vCstRqstDtAmt1, $vCstRqstDtAmt2, ".
							 "          0, 0, '".$_SESSION['useraccount']."'	) ";
							 
					//echo $query."<br>";exit;				
					$result=mysql_query($query,$connection);
				}

			} else{
				$ErrorExists = 1;
				$message  = mysql_error($connection); 
				frollback();
			}
		}
		
		
		if ($ErrorExists == 0 && $STransId != ''){
			$sub_query = " Select Sum(if(PaymentRqstDt_dbcd='D', ifNull(PaymentRqstDt_Amount1,0), 0)) As TotalDebitAmount1, Sum(if(PaymentRqstDt_dbcd='D', ifNull(PaymentRqstDt_Amount2,0), 0)) As TotalDebitAmount2, ".
						 " Sum(if(PaymentRqstDt_dbcd='C', ifNull(PaymentRqstDt_Amount1,0), 0)) As TotalCreditAmount1, Sum(if(PaymentRqstDt_dbcd='C', ifNull(PaymentRqstDt_Amount2,0), 0)) As TotalCreditAmount2 ".
						 " From Temp_PaymentRqstDt ".
						 " Where PaymentRqst_Id = ".$STransId;
			$sub_result = selectData($connection, $sub_query);
			
			$vTransAmountDb1 = $sub_result[0];
			$vTransAmountDb2 = $sub_result[1];
			$vTransAmountcd1 = $sub_result[2];
			$vTransAmountcd2 = $sub_result[3];
				 
			$sub_query1 = " UPDATE Temp_PaymentRqst SET ".
						  " PaymentRqst_AmountD1 = $vTransAmountDb1, PaymentRqst_AmountD2 = $vTransAmountDb2, ".
						  " PaymentRqst_AmountC1 = $vTransAmountcd1, PaymentRqst_AmountC2 = $vTransAmountcd2 ".
						  " WHERE PaymentRqst_Id = ".$STransId;
				 
			$sub_result1=mysql_query($sub_query1,$connection);
			//echo $sub_query1."<br>";exit;
		}
		
	} // End Case Insert In Detail Tables

}

else if ($actionmode=='updateitemstatus' && $PRqstid != ''){

	$PRqstid = $_GET["PRqstid"];
	$objval = $_GET["objval"];
	$vreason = $_GET["vreason"];

	echo "[BRK]";

	if (strtoupper($objval)=='A')
		$userupdate = $_SESSION['username'];
		
		
	if ($objval == 'p') {
		$query = " UPDATE paymentrqst SET  PaymentRqst_ApprovedStatus = null, PaymentRqst_ApprovedLevel = '$PaymentRqstApproved_Level', PaymentRqst_App = 0, Touser_account = null, PaymentRqst_RejectedReason = null WHERE paymentrqst_id = $PRqstid ";
		if (!updateData($connection, $query, true)) $message  = mysql_error();		 
	} else {
		$query = " UPDATE paymentrqst SET  PaymentRqst_ApprovedStatus = '$objval', PaymentRqst_ApprovedLevel = '$PaymentRqstApproved_Level', ";

		if ($objval == 'r') {
			$query .= " PaymentRqst_App = 0, Touser_account = null, PaymentRqst_RejectedReason = '$vreason' ";
		} else {
			$query .= " PaymentRqst_App = 1, Touser_account = '".$userupdate."', PaymentRqst_RejectedReason = '$vreason' ";
		}
		
		$query .= " WHERE paymentrqst_id = $PRqstid ";
		
		if (!updateData($connection, $query, true)) $message  = mysql_error();		 
				 
	}		 
		
		
	echo $message;
	echo "[BRK]";
}

//Post Mode	
else if($mode == "post" && $STransId != ''){  

	if (!isset($txtCurr))
	   $txtCurr = $base1;
	   
	if (empty($txtCostCentCode))	$CostCentCode = 'Null';
	else		$CostCentCode = "'".$txtCostCentCode."'";
			
	if (empty($txtProjectCode))		$ProjectCode = 'Null';
	else		$ProjectCode = "'".$txtProjectCode."'";      
	
	if(empty($txtApproved))  $txtApproved = 0;
	if(empty($txtConfirmed)) $txtConfirmed = 0;
	if ($Module_PaymentLimit == 0) $txtConfirmed = 1;
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$txtCurr."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$txtCurr."' )";
	$result = selectData($connection, $query);
	$rate_dt = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base1."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base1."' )";
	$result = selectData($connection, $query);
	$rate_dt1 = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base2."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base2."' )";
	$result = selectData($connection, $query);
	$rate_dt2 = $result[0];
	
	$AllowSave = 1;
	$AllowConfirm = 1;
	
	$vCstRqstBalPer   = 0;
	$vCstRqstBalAmt   = 0;
	$vCstRqstBalAmt1  = 0;
	$vCstRqstBalAmt2  = 0;
	
	$vChkRqstBalAmt   = 0;
	$vChkRqstBalAmt1  = 0;
	$vChkRqstBalAmt2  = 0;
	
	$vTransAmountDb1 = 0;
	$vTransAmountDb2 = 0;
	$vTransAmountcd1 = 0;
	$vTransAmountcd2 = 0;
		
		
	$sub_query1 = " Select PaymentRqstDt.PaymentRqstDt_Id, (100 - Sum(ifNull(CstRqst_Prcnt,0))) As CstRqstPer, ".
	              " (Round(ifNull(PaymentRqstDt_Amount,0), ".$DecimalBase1.") - Round(Sum(ifNull(CstRqst_Amount,0)), ".$DecimalBase1.") ) As CstRqstAmount, ".
				  " (Round(ifNull(PaymentRqstDt_Amount1,0), ".$DecimalBase1.") - Round(Sum(ifNull(CstRqst_Amount1,0)), ".$DecimalBase1.") ) As CstRqstAmount1, ".
				  " (Round(ifNull(PaymentRqstDt_Amount2,0), ".$DecimalBase1.") - Round(Sum(ifNull(CstRqst_Amount2,0)), ".$DecimalBase1.") ) As CstRqstAmount2 ".
				  " From Temp_PaymentRqstDt As PaymentRqstDt Inner Join Temp_CstRqst on PaymentRqstDt.PaymentRqstDt_Id = Temp_CstRqst.PaymentRqstDt_Id ".
				  " Where PaymentRqstDt.PaymentRqst_Id = '".$STransId."' ".
				  " Group By PaymentRqstDt.PaymentRqstDt_Id ";
				  
					      
	//echo $sub_query1; exit;
	$sub_result1=mysql_query($sub_query1,$connection);
	while($sub_row1=mysql_fetch_row($sub_result1)){
	
		$vTransDtId       = $sub_row1[0];
		$vCstRqstBalPer   = $sub_row1[1];
		$vCstRqstBalAmt   = $sub_row1[2];
		$vCstRqstBalAmt1  = $sub_row1[3];
		$vCstRqstBalAmt2  = $sub_row1[4];
		
		if($vCstRqstBalPer != 0) {
			$AllowSave = 0;
			$ErrorRecId = $vTransDtId;
			$message='Save not allowed. Total of Projects/Costcenters exceeds Total Amount.';
			break;
		}
	}	
	
	
						
	
	$sub_query2 = " Select PaymentRqstDt.PaymentRqstDt_Id, ".
	              " (Round(ifNull(PaymentRqstDt_Amount,0), ".$DecimalBase1.") - Round(Sum(ifNull(Chk_Amount,0)), ".$DecimalBase1.") ) As ChkAmount, ".
				  " (Round(ifNull(PaymentRqstDt_Amount1,0), ".$DecimalBase1.") - Round(Sum(ifNull(Chk_Amount1,0)), ".$DecimalBase1.") ) As ChkAmount1, ".
				  " (Round(ifNull(PaymentRqstDt_Amount2,0), ".$DecimalBase1.") - Round(Sum(ifNull(Chk_Amount2,0)), ".$DecimalBase1.") ) As ChkAmount2 ".
				  " From Temp_PaymentRqstDt As PaymentRqstDt Inner Join Temp_ChkRqst on PaymentRqstDt.PaymentRqstDt_Id = Temp_ChkRqst.PaymentRqstDt_Id ".
				  " Where PaymentRqstDt.PaymentRqst_Id = '".$STransId."' ".
				  " Group By PaymentRqstDt.PaymentRqstDt_Id ";
				  
					      
	//echo $sub_query2; exit;
	$sub_result2=mysql_query($sub_query2,$connection);
	while($sub_row2=mysql_fetch_row($sub_result2)){
	
		$vTransDtId       = $sub_row2[0];
		$vChkRqstBalAmt   = $sub_row2[1];
		$vChkRqstBalAmt1  = $sub_row2[2];
		$vChkRqstBalAmt2  = $sub_row2[3];
		
		if($vChkRqstBalAmt != 0 || $vChkRqstBalAmt1 != 0 || $vChkRqstBalAmt2 != 0) {
			$AllowSave = 0;
			$ErrorRecId = $vTransDtId;
			$message='Save not allowed. Total of Cheques exceeds Total Amount.';
			break;
		}
	}
	
	
	$sub_query3 = " Select PaymentRqstDt.PaymentRqstDt_Id, ".
	              " (Round(ifNull(PaymentRqstDt_Amount,0), ".$DecimalBase1.") - Round(Sum(ifNull(Bill_Amount,0)), ".$DecimalBase1.") ) As BillAmount, ".
				  " (Round(ifNull(PaymentRqstDt_Amount1,0), ".$DecimalBase1.") - Round(Sum(ifNull(Bill_Amount1,0)), ".$DecimalBase1.") ) As BillAmount1, ".
				  " (Round(ifNull(PaymentRqstDt_Amount2,0), ".$DecimalBase1.") - Round(Sum(ifNull(Bill_Amount2,0)), ".$DecimalBase1.") ) As BillAmount2 ".
				  " From Temp_PaymentRqstDt As PaymentRqstDt Inner Join Temp_BillRqst on PaymentRqstDt.PaymentRqstDt_Id = Temp_BillRqst.PaymentRqstDt_Id ".
				  " Where PaymentRqstDt.PaymentRqst_Id = '".$STransId."' ".
				  " Group By PaymentRqstDt.PaymentRqstDt_Id ";
					      
	//echo $sub_query3; exit;
	$sub_result3=mysql_query($sub_query3,$connection);
	while($sub_row3=mysql_fetch_row($sub_result3)){
	
		$vTransDtId        = $sub_row3[0];
		$vBillRqstBalAmt   = $sub_row3[1];
		$vBillRqstBalAmt1  = $sub_row3[2];
		$vBillRqstBalAmt2  = $sub_row3[3];
		
		if($vBillRqstBalAmt != 0 || $vBillRqstBalAmt1 != 0 || $vBillRqstBalAmt2 != 0) {
			$AllowSave = 0;
			$ErrorRecId = $vTransDtId;
			$message='Save not allowed. Total of Bills exceeds Total Amount.';
			break;
		}
	}
	
	
	
	//Check User Access For Date Modification/ Lock Period
	$MainCompanyName=explode('_',$_SESSION['dbName']);

	$sub_query= " Select ifNull(AllowLockedPeriod,0) As AllowLockedPeriod, LockedPeriod As LockedPeriodDate, LockedPeriodDays As LockedPeriodDays ".
				" From ".$_SESSION['dbLabel'].".ListUserDir  ".
				" Where Usercode='".$_SESSION['useraccount']."' ".
				" And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
				" And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
				  
	$sub_result = mysql_query($sub_query,$connection) or die(mysql_error());
	$rowsperiod = mysql_num_rows($sub_result);

	while($sub_rows=mysql_fetch_array($sub_result)){
		extract($sub_rows);
	}	
	
	if ($rowsperiod > 0 && $AllowLockedPeriod == 1) { 
		if (!(empty($LockedPeriodDate)) && $LockedPeriodDate != '' && insertMySQLDate($txtDate) < $LockedPeriodDate) $ExceedLockPeriod = 1;
		if ($LockedPeriodDays != '' && dateDiff(insertMySQLDate(displayToday()),insertMySQLDate($txtDate)) > $LockedPeriodDays) $ExceedLockPeriod = 1;
		
		//if ($AllowSave == 0) $message='Save not allowed. Exceed Lock Period.';
		if ($ExceedLockPeriod == 1) {
			$AllowSave = 0;
			$message='Save not allowed. Exceed Lock Period.';
		}
	}
	//echo $sub_query;exit;					
			
		
						
	if ($AllowSave == 1) {	
		
		$sub_query4 = " Select Sum(if(PaymentRqstDt_dbcd='D', ifNull(PaymentRqstDt_Amount1,0), 0)) As TotalDebitAmount1, Sum(if(PaymentRqstDt_dbcd='D', ifNull(PaymentRqstDt_Amount2,0), 0)) As TotalDebitAmount2, ".
					  " Sum(if(PaymentRqstDt_dbcd='C', ifNull(PaymentRqstDt_Amount1,0), 0)) As TotalCreditAmount1, Sum(if(PaymentRqstDt_dbcd='C', ifNull(PaymentRqstDt_Amount2,0), 0)) As TotalCreditAmount2 ".
					  " From Temp_PaymentRqstDt ".
					  " Where PaymentRqst_Id = ".$STransId;
		$sub_result4 = selectData($connection, $sub_query4);
			
		$vTransAmountDb1 = $sub_result4[0];
		$vTransAmountDb2 = $sub_result4[1];
		$vTransAmountcd1 = $sub_result4[2];
		$vTransAmountcd2 = $sub_result4[3];

						  
		$sub_query5 = " UPDATE Temp_PaymentRqst SET ".
					  " PaymentRqst_AmountD1 = $vTransAmountDb1, PaymentRqst_AmountD2 = $vTransAmountDb2, ".
					  " PaymentRqst_AmountC1 = $vTransAmountcd1, PaymentRqst_AmountC2 = $vTransAmountcd2, ".
					  " Currency_Code = '".$txtCurr."', PaymentRqst_Date = '".insertMySQLDate($txtDate)."', ".
					  " PaymentRqst_Remark = '".ereg_replace("'", "\'", trim($txtRemark))."', ".
					  " PaymentRqst_Name = '".ereg_replace("'", "\'", trim($txtRepresent))."',  ".
					  " PaymentRqst_ManualNo = '".ereg_replace("'", "\'", trim($txtManualNo))."',  ".
					  " PaymentRqst_App = $txtApproved, Touser_account = '".ereg_replace("'", "\'", trim($txtApprovedBy))."',  ".
					  //" PaymentRqst_Confirmed = $txtConfirmed, User_Confirmed = if ($txtConfirmed=1, '".$_SESSION['useraccount']."', Null),  ".
					  " Project_Code = $ProjectCode, Costcent_code = $CostCentCode ".
					  " WHERE PaymentRqst_Id = ".$STransId;
			 
		$sub_result5=mysql_query($sub_query5,$connection);
		//echo $sub_query5."<br>"; exit;
		
		//InCase Confirm Checkbox is Checked
		if ($Module_PaymentLimit == 1 && $Branch_Manager == 1 && $txtConfirmed == 1) {		
	
			$sub_query = " Select Round(ifNull(Sum(ifNull(PaymentRqst_amountD1,0)),0)) As TotalDebitAmount1 ".
						 " From PaymentRqst ".
						 " Where 1 = 1 ".
						 " And Upper(User_Confirmed) = Upper('".$_SESSION['useraccount']."') ";
						 
			if ($Login_Daily == 1){
				$sub_query = $sub_query . " And Date_Format(PaymentRqst_Date, '%Y%m%d') = Date_Format('".insertMySQLDate($txtDate)."', '%Y%m%d') ";
			}
			else if ($Login_Weekly == 1){
				$sub_query = $sub_query . " And Date_Format(PaymentRqst_Date, '%x%v') = Date_Format('".insertMySQLDate($txtDate)."', '%x%v') ";
			}
			else if ($Login_Monthly == 1){
				$sub_query = $sub_query . " And Date_Format(PaymentRqst_Date, '%Y%m') = Date_Format('".insertMySQLDate($txtDate)."', '%Y%m') ";
			}
			//echo $sub_query; exit;
			$sub_result = selectData($connection, $sub_query);
			$vTotalDebitAmount1 = $sub_result[0];
			$vTotalDebitAmount1 = $vTotalDebitAmount1 + $vTransAmountDb1;
			
			if (($vTotalDebitAmount1 <= $Login_Limit && $Login_Limit > 0) || ($Login_Daily == 0 && $Login_Weekly == 0 && $Login_Monthly == 0)){
				$sub_query1 = " UPDATE Temp_PaymentRqst SET ".
							  " PaymentRqst_Confirmed = 1, User_Confirmed = '".$_SESSION['useraccount']."'  ".
							  " WHERE PaymentRqst_Id = ".$STransId;
						 
				$sub_result1=mysql_query($sub_query1,$connection);
			}
			else {
				$AllowSave = 0;
				$message = " Save not allowed. Limit Exceeded. Total: ".$txtCurr."  ".number_format($vTotalDebitAmount1, $DecimalBase1)." - Allowed: ".$txtCurr."  ".number_format($Login_Limit, $DecimalBase1);
			}
		}
	
		if ($AllowSave == 1) {
		
			if ($sub_result5){
				
				$sub_query6 = " UPDATE Temp_PaymentRqstDt SET ".
							  " PaymentRqst_Date = '".insertMySQLDate($txtDate)."' ".
							  " WHERE PaymentRqst_Id = ".$STransId;
					 
				$sub_result6=mysql_query($sub_query6,$connection);
						
				if ($mainmode == 'insert') $SMode = 'I';
				if ($mainmode == 'edit') $SMode = 'U';
				//echo $SMode;exit;
				
				$vOldProjCode  	  = '';
				$vOldCostCentCode = '';
				
				// In Edit Case, Get The Old Values For Project and Costcenter For References Issue
				$query   = " SELECT Project_Code, Costcent_code  FROM PaymentRqst WHERE PaymentRqst_Id = $STransId ";
				$result  = selectData($connection, $query);
				$vOldProjCode  	  = $result[0];
				$vOldCostCentCode = $result[1];
				
				fbegin();		
				$query = " SELECT CopyData_TempPaymentRqst('".$SMode."', ".$STransId.", '".$_SESSION['useraccount']."') FROM Dual ";
				$result = selectData($connection, $query);
				$result = $result[0];
				//echo $query; exit;
				//echo 'error='.$result; exit;
				if ($result != 0){ 
					fcommit();
					
					$query   = " SELECT PaymentRqst_RefGovernment, Costcent_code, Project_Code FROM PaymentRqst WHERE PaymentRqst_Id = $STransId ";
					$result  = selectData($connection, $query);
					$vTransRef     = $result[0];
					$vCostCentCode = $result[1];
					$vProjectCode  = $result[2];
					
					$existCode = 0;
					if ($vOldCostCentCode != $txtCostCentCode && $Module_ReferenceByCostcenter == 1 && $vTransRef != ''){
						$querychk = "select 1 from sequence where '$vOldCostCentCode' like Concat(Sequence.CostCenter,'%') and '$txtCostCentCode' like Concat(Sequence.CostCenter,'%') ";
						$reschk = mysql_query($querychk, $connection);
						$rwchk = mysql_fetch_row($reschk);
						$existCode = $rwchk[0];
					}
					if ($vOldProjCode != $txtProjectCode && $Module_ReferenceByProject == 1 && $vTransRef != ''){
						$querychk = "select 1 from sequence where '$vOldProjCode' like Concat(Sequence.Project,'%') and '$txtProjectCode' like Concat(Sequence.Project,'%') ";
						$reschk = mysql_query($querychk, $connection);
						$rwchk = mysql_fetch_row($reschk);
						$existCode = $rwchk[0];
					}	
					
					if ($vTransRef == '' || ($vOldProjCode != $txtProjectCode && $Module_ReferenceByProject == 1 && $existCode == 0) ||
						 ($vOldCostCentCode != $txtCostCentCode && $Module_ReferenceByCostcenter == 1 && $existCode == 0)) {
							 
						if ($Module_ReferenceByCostcenter == 1)   $vProjCostCent = $vCostCentCode;
						else if ($Module_ReferenceByProject == 1) $vProjCostCent = $vProjectCode;
						else $vProjCostCent = '';
									
							
						fbegin();
						$vTransRef = get_nextsequence('PRqst_RefGovernment',$vProjCostCent,$connection);	
						fcommit();
								
						$query = " UPDATE PaymentRqst SET ".
								 " PaymentRqst_RefGovernment = $vTransRef ".
								 " WHERE PaymentRqst_Id = $STransId ";
								
						if (!updateData($connection, $query, true)) 
							$message  = mysql_error();
							
					}	
						
					 
				} else{
					$message  = mysql_error($connection); 
					$message  = 'Error In CopyData_TempPaymentRqst Function.'.'<br>'.$message; 
					frollback();
				}
				
				
			} else{
				$message  = mysql_error(); 
				frollback();
			}
		}
	}
}


$message = ereg_replace(' ','%20',$message);
$WarningMsg = ereg_replace(' ','%20',$WarningMsg);

$PTransManualNo  	= ereg_replace(' ','%20',$PTransManualNo);
$PTransProjectCode 	= ereg_replace(' ','%20',$PTransProjectCode);
$PTransCostCentCode = ereg_replace(' ','%20',$PTransCostCentCode);
$PTransRemark 		= ereg_replace(' ','%20',$PTransRemark);
$PTransRepresent	= ereg_replace(' ','%20',$PTransRepresent);
$PTransApprovedBy 	= ereg_replace(' ','%20',$PTransApprovedBy);




if($mode == "saverecord" || $mode == "editrecord"){
	if ($ErrorExists == 0)
		header ("Location: ./Form_PaymentRqst.php?actionmode=post&mainaction=".$mainmode."&PrTransId=".$STransId."&PrTransRef=".$vTransRef."&message=".$message);
	else 
		header ("Location: ./Form_PaymentRqst.php?actionmode=$mode&mainaction=".$mainmode."&txtTransId=".$STransId."&PrTransRef=".$vTransRef."&message=".$message);
}
else if($mode == "post") {
	if ($AllowSave == 1)
		header ("Location: ./Form_PaymentRqst.php?actionmode=".$mode."&mainaction=".$mainmode."&PrTransId=".$STransId."&PrTransRef=".$vTransRef."&message=".$message);
	else
		header ("Location: ./Form_PaymentRqst.php?actionmode=submitrecord&mainaction=".$mainmode."&ErrorRecId=".$ErrorRecId."&txtTransId=".$STransId."&message=".$message);
}	
else if ($mode == "" && $mainmode == "delete" )	{
	header ("Location: ./Form_PaymentRqstSearch.php?message=".$message);
}	
else if ($mode == "" && $mainmode == "deletepending")	
	header ("Location: ./Form_PaymentRqstSearchPending.php?message=".$message);
else if ($mode == "deletedt" )		
	header ("Location: ./Form_PaymentRqst.php?actionmode=".$mode."&mainaction=".$mainmode."&txtTransId=".$STransId."&message=".$message);
else if ($mode == "editdt")		
	header ("Location: ./Form_PaymentRqstDetail.php?actionmode=".$mode."&mainaction=".$mainmode."&PTransId=".$STransId."&PTransDate=".$PTransDate."&PTransDtType=".$PTransDtType."&PTransDtId=".$PTransDtId."&PAccountType=".$PAccountType."&message=".$message);
else if ($mode == "" && $mainmode == "search" ){
	if ($message != "")
		header ("Location: ./Form_PaymentRqstSearch.php?PMode=0&message=".$message);
	else	
		header ("Location: ./Form_PaymentRqstSearch.php?PMode=1&message=".$message);
} 
else if ($mode == "" && $mainmode == "search2" ){
	if ($message != "")
		header ("Location: ./Form_PaymentRqstUnconfirmed.php?PMode=0&message=".$message);
	else	
		header ("Location: ./Form_PaymentRqstUnconfirmed.php?PMode=1&message=".$message);
} 
else if ($mode == "" && $mainmode == "searchapprove" ){
	if ($message != "")
		header ("Location: ./Form_PaymentRqstApproveList.php?PMode=0&message=".$message);
	else	
		header ("Location: ./Form_PaymentRqstApproveList.php?PMode=1&message=".$message);
}
else if ($mode == "" && $mainmode == "searchconfirmed" ){
	if ($message != "")
		header ("Location: ./Form_PaymentRqstConfirmList.php?PMode=0&message=".$message);
	else	
		header ("Location: ./Form_PaymentRqstConfirmList.php?PMode=1&message=".$message);
}
else if ($mode == "" && $mainmode == "confirmrequest" ){
	header ("Location: ./Form_PaymentRqstConfirmList.php?message=".$message);
}
else if ($mode == "" && $mainmode == "approverequest" ){
	header ("Location: ./Form_PaymentRqstApproveList.php?message=".$message);
}
else if ($mode == "" && $mainmode == "loadporder" ){
	if ($ErrorExists == 1)
		header ("Location: ./Form_PaymentRqst_LoadPorder.php?PMode=0&message=".$message);
	else	
		header ("Location: ./Form_PaymentRqst_LoadPorder.php?PMode=1&GeneratedId=".$STransId."&message=".$message);
}
else if ($mode == "insertcstrqst" || $mode == "editcstrqst" || $mode == "deletecstrqst" )		
	header ("Location: ./Form_PaymentRqst_CstRqst.php?actionmode=".$mode."&mainaction=".$mainmode."&PTransId=".$STransId."&PTransDtId=".$STransDtId."&PTransDtCurr=".$PTransDtCurr."&message=".$message);	
else if ($mode == "insertchkrqst" || $mode == "editchkrqst" || $mode == "deletechkrqst" )		
	header ("Location: ./Form_PaymentRqst_ChkRqst.php?actionmode=".$mode."&mainaction=".$mainmode."&PTransId=".$STransId."&PTransDtId=".$STransDtId."&PTransDtCurr=".$PTransDtCurr."&message=".$message);	
else if ($mode == "insertbillrqst" || $mode == "editbillrqst" || $mode == "deletebillrqst" )		
	header ("Location: ./Form_PaymentRqst_BillRqst.php?actionmode=".$mode."&mainaction=".$mainmode."&PTransId=".$STransId."&PTransDtId=".$STransDtId."&PTransDtCurr=".$PTransDtCurr."&message=".$message);
else if ($mode == "submitrecord" ){
	 header ("Location: ./Form_PaymentRqstDetail.php?actionmode=".$mode."&mainaction=".$mainmode."&PTransDtType=".$PTransDtType."&PTransId=".$STransId."&PAccountType=".$PAccountType.	
			"&PTransDate=".$PTransDate."&PTransCurr=".$PTransCurr."&PTransCashType=".$PTransCashType."&PTransManualNo=".$PTransManualNo."&PTransApproved=".$PTransApproved.
			"&PTransConfirmed=".$PTransConfirmed."&PTransProjectCode=".$PTransProjectCode."&PTransCostCentCode=".$PTransCostCentCode."&PTransRepresent=".$PTransRepresent.
			"&PTransRemark=".$PTransRemark."&PTransApprovedBy=".$PTransApprovedBy."&message=".$message);
} else if($mode == "cancelrecord" ){
	header ("Location: ./Form_PaymentRqst.php?clearAll=1");
}	
else if ($mode == "" && $mainmode == "approveConfirm" && $toBeApprovedByHOD == 1){
	if ($message == '')
		header("Location: ./Form_PaymentRqstUnconfirmed.php");
	else 
		header("Location: ./Form_PaymentRqstUnconfirmed.php?message=$message");
}
	

print <<<HERE
</form>
</body>

HERE;


?>