<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("Functions_Trans.php");
include("Forms_DataGrid.php");
include("function.inc.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");


if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$querylog="SELECT FI_PreReceipt as loginaccess 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);
}
/*
if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");
	*/
	
//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);
}

$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());

$querycashtype=" select CashType_Code, CashType_Description, CostCent_Code, Auto as CashTypeId from cashtype where Journal_Type = '".$TransType."'".
               " order by CashType_Description"; 
$resultcashtype=mysql_query($querycashtype,$connection) or die(mysql_error());


 if ($TransType == 'RV') {
	$ModulePostTrx = $Module_AcctrxPostRV;
	$FA_PostTrx = $FI_RVPostTransaction;
	$ModuleRestrictCashType = $Module_RestrictCashTypeRV;
	$ModuleInvoiceGrid = $Module_InvoiceGridRV;
	$FA_Insert = $FA_ReceiptApp;
	$ModuleCashBank = $Module_CashBankRV;
}
if ($TransType == 'PV') {
	$ModulePostTrx = $Module_AcctrxPostPV;
	$FA_PostTrx = $FI_PVPostTransaction;
	$ModuleRestrictCashType = $Module_RestrictCashTypePV;
	$ModuleInvoiceGrid = $Module_InvoiceGridPV;
	$FA_Insert = $FA_PaymentApp;
	$ModuleCashBank = $Module_CashBankPV;
}	

if (empty($mainaction)) 	$mainaction = 'insert';
if (empty($SetCurrency))	$SetCurrency = 0;	
	

$message = ereg_replace("'"," ",$message);

if ($TransType == 'RV') {  $Title = "Pre-Receipt";      if(empty($Type_dbcd)) $Type_dbcd = 'C';  }

if ($TransType == 'PV') {  $Title = "Pre-Payment";      if(empty($Type_dbcd)) $Type_dbcd = 'D';  }

$Type_dbcd1 = $Type_dbcd;
if ($Type_dbcd1 == 'D') {
	$Type_dbcd2 = 'C'; 
	$Type_Label1 = $DebitAccount; 
	$Type_Label2 = $CreditAccount; 
} else {
	$Type_dbcd2 = 'D';
	$Type_Label1 = $CreditAccount; 
	$Type_Label2 = $DebitAccount;
}	

$HeaderTitle = $Title;

print <<<HERE
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<link type="text/css" rel="stylesheet" href="js/calendar/dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="js/calendar/dhtmlgoodies_calendar.js?random=20060118"></script>

<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>

<link type="text/css" href="js/calendar/jquery.datepick.css" rel="stylesheet">
<script type="text/javascript" src="js/calendar/jquery.datepick.js"></script>

<link type="text/css" href="js/grid/scripts/jquery-ui.css" rel="stylesheet">
<script type="text/javascript" src="js/grid/scripts/jquery-ui.min.js"></script>

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

function showlistLedger(frm){
 	var x = document.pretrans;
	var TransType = x.TransType.value;
	var showCustomer = 1;
	
	XPos = (screen.availHeight - 500) / 2;
	YPos = (screen.availWidth - 780) / 2;
	
	var keyword = encodeURIComponent(document.getElementById('fromledgernb').value);
		
	window.open('List_Agents_orderbycode.php?str='+frm+'&index=&CashBank=&TransType='+TransType+'&showCustomer='+showCustomer+'&PRef='+keyword+'&ShowDetails=all','mywindow','width=780,height=500,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,left='+YPos+',top='+XPos);
}

function PostChanges(){
	var x = document.pretrans;
	var disableprepaymentacc = document.getElementById('disableprepaymentacc').value;
	
	if(x.txtRepresent.value == ''){
		alert('Representative is Empty!');
		x.txtRepresent.focus();
		return 0;
	} else if(x.txtCurr.value == ''){
		alert('Currency is Empty!');
		x.txtCurr.focus();
		return 0;
	} else if(x.txtTotalAmount.value == ''){
		alert('Amount is Empty!');
		x.txtTotalAmount.focus();
		return 0;
	} else if(x.txtCashType.value == ''){
		alert('Cash Type is Empty!');
		x.txtCashType.focus();
		return 0;
	} else if(x.fromledgernb.value=='' && disableprepaymentacc == 0){
		alert('Account Number Type is Empty!');
		x.fromledgernb.focus();
		return 0;
	}
	
	x.action = 'Form_PreTrans_Actions.php?mainmode=Post';
	x.submit();	
}
function printCheck(){
	
	document.getElementById('print_check').value = 1;
	PostChanges();

	
}
function ChangeCurrency(){
	document.getElementById('precurrency').value = document.getElementById('txtCurr').value;
}
function setAmount(){
	document.getElementById('preamount').value = document.getElementById('txtTotalAmount').value;
}


function PrintTrans(TransId, TransRef) {
	var str = '';
	x = document.pretrans;
	var vTransType = x.TransType.value;
	var txtCashType=document.getElementById('txtCashType').value;
	
	if (confirm ( "Do you want to Print current Transaction with reference "+TransRef+" ?"))
	{	str = 'Form_PreTransPreview.php?TransId='+TransId+'&TransType='+vTransType;
		window.open(str);
	}	
	
	
}
function PrintTrans2(TransId, TransRef) {
	x = document.pretrans;
	var vTransType = x.TransType.value;
	var str = "Form_CheckPreview.php?TransId="+TransId+"&TransType="+vTransType;
	window.open(str,'_blank');
	
}

function ClearPage(TransType){
	var x = document.pretrans;
	x.TransId.value='';
	x.message.value='';
	x.txtRepresent.value='';
	x.txtCurr.value='';
	x.txtTotalAmount.value='';
	x.txtRemark.value='';
	x.txtCashType.value='';
	x.bankname.value='';
	x.chknbr.value='';
	x.fromledgernb.value='';
	x.fromledgernanme.value='';
	x.fromledgerdesc.value='';
	x.precurrency.value='';
	x.preamount.value='';
	x.action = 'Form_PreTrans.php?TransType='+TransType;
	x.submit();
}

function CurrBank(){
	var txtCashType = document.getElementById('txtCashType').value;
	var str = "Form_PreTrans_Actions.php?actionmode=currbank&txtCashType="+txtCashType;
	var xhttp = new XMLHttpRequest();
	//alert(str);exit;
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
		var data = xhttp.responseText.split('[BRK]');
		//document.write(data[0]);
		document.getElementById('bankname').value = data[0];
		document.getElementById('txtCurr').value = data[1];
		document.getElementById('precurrency').value = data[1];
		}
  };
  xhttp.open("GET", str, true);
  xhttp.send();
}
function goNextField(fieldName, e){
	if(e.keyCode == 13){
	document.getElementById(fieldName).focus();
	}
	//alert(document.getElementById('txtRepresent').value);
	if(fieldName == "fromdesc"){
		if(document.getElementById('txtRepresent').value == ""){
			document.getElementById('txtRepresent').value = document.getElementById('fromledgername').value;
			}
	}
}
function goNextField2(fieldName){
	document.getElementById(fieldName).focus();
}
function chkNumExists(e){
	if(e.keyCode == 13){
	var ledger_number = document.getElementById('fromledgernb').value;
	//alert("test");
	var TransType = document.getElementById('TransType').value;
	var str = "Form_PreTrans_Actions.php?actionmode=chknumberexists&ledger_number="+ledger_number+"&TransType="+TransType;
	var xhttp = new XMLHttpRequest();
	//alert(str);exit;
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
		var data = xhttp.responseText.split('[BRK]');
		if(data[0] == 0){
			showlistLedger(31);
		}
		else{
			document.getElementById('fromledgernb').value = data[1];
			document.getElementById('fromledgername').value = data[2];
			if(document.getElementById('txtRepresent').value == ""){
			document.getElementById('txtRepresent').value = data[2];
			}
			goNextField('fromledgername', e);
		}
		}
  };
  xhttp.open("GET", str, true);
  xhttp.send();
	}
}
</script>
<title>$HeaderTitle</title>
</head>
<body>
<form name='pretrans' id='pretrans' method='post' action='' enctype='multipart/form-data'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='TransType' id='TransType' value='$TransType' >
<input type='hidden' name='CashBank' value='$CashBank' >
<input type='hidden' name='TransId' value='$TransId' >

<input type='hidden' name='message' value='$message' size='200'>

HERE;
echo "<input type='hidden' name='disableprepaymentacc' id='disableprepaymentacc' value='".$config['disableprepaymentacc']."' >";
//Initializations
if (empty($mainaction))	 $mainaction = 'insert';
if (empty($txtDate))	 $txtDate = displayToday();
if (empty($txtCurr))  	 $txtCurr= $DefaultCur;

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";

echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>$Title</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";
	  

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='38'  height='34'>&nbsp;</td>
				<td valign='middle' height='34'>";

	echo"	<button type='button' id='btnClear' name='btnClear' onclick=\"ClearPage('$TransType'); return false;\" title='Clear Page'><span><span class='style1'>Clear Page</span></span></button>";

	echo"   <button type='button' name='btnPost' id='btnPost' onclick='PostChanges();return false;' title='Save Changes'><span><span>Save</span></span></button>";
	
	echo"   <button type='button' name='btnPost' id='btnPost' onclick='printCheck();return false;' title='Print'><span><span>Print</span></span></button>";
echo"   </td></tr>";
echo"	</table>";
echo"</td></tr>";

$strt = $_SESSION['StartFinancial_Date'];
$end = $_SESSION['EndFinancial_Date'];
echo"<tr><td id='content-body'>
		<table border='0' align='center'  class='tablecontent' cellpadding='0' cellspacing='0' >
		  <col width=105>
		  <col width=320>
		  <col width=70>
		  <col width=100>
		  <col width=200>
		  <col width=300>"; 

	  
echo"   <tr style='height:25px;'>
		  <td align='left' valign='center'></td>
		  <td align='left' ></td>";
echo"     <td align='left' ><span class='style1'></span></td>";		  
echo"     <td align='left' valign='center'><span class='style1'>Date</span></td>";
echo" 	  <td align='left'><span class='style1'>";
echo"     <input type='text' id='txtDate' name='txtDate' onkeyup=\"goNextField('txtCashType', event)\" value='$txtDate' maxlength='10' style='width:80px;'  >
		  </td>";
echo"     <td align='left'><span class='style1'></span></td>"; 
echo"   </tr>";	
		
echo"   <tr style='height:25px;'>
		  <td align='left' valign='center'><span class='style1'>Representative</span></td>
		  <td align='left' ><span class='style1'>
		  <input type='text' id='txtRepresent' name='txtRepresent' onkeyup=\"goNextField('txtDate', event)\" autofocus=autofocus value='".htmlspecialchars($txtRepresent, ENT_QUOTES)."'  style='width:310px;' maxlength='256' ></span></td>";
echo"     <td align='left' ><span class='style1'></span></td>";		
echo"     <td align='left' valign='center'><span class='style1'>Cash Type</span></td>";
echo"     <td align='left'><span class='style1'>
			<select name='txtCashType' id='txtCashType' onchange=\"goNextField2('txtCurr'); CurrBank();\" style='height:20px; width:180px; font-size:14px;'>";
echo"		<option></option>";
		while($ctype=mysql_fetch_row($resultcashtype)){
		echo"<option ";
			if ($txtCashType==$ctype[0])
				echo " selected ";
			echo" value='$ctype[3]' label='$ctype[2]'>$ctype[1] </option>";
		}
		echo"</select></span></td>";  

echo"   </tr>";	

echo"   <tr  height='30'>
		  <td align='left' valign='center' rowspan='2'><span class='style1'>Remark</span></td>
		  <td align='left' colspan='2' rowspan='2'><textarea name='txtRemark' rows='3' cols='36'>".htmlspecialchars($txtRemark, ENT_QUOTES)."</textarea></td>";
echo"     <td align='left' valign='center'><span class='style1'>Amount</span></td>";

echo" 	<td align='left' colspan='2'><span class='style1'><select name='txtCurr' id='txtCurr' onchange=\"goNextField2('txtTotalAmount'); ChangeCurrency()\" style='width:60px;' onChange='ChangeCurrency();' >";
			echo"<option></option>";
			while($curf=mysql_fetch_row($resultcurrency)){
			echo"<option ";
				if ($txtCurr==$curf[0])
					echo " selected ";
				echo" value='$curf[0]'>$curf[0] </option>";
			}
			echo"</select></span>";	
			
			
echo"     <input type='text' id='txtTotalAmount' name='txtTotalAmount' onkeyup=\"goNextField('chknbr', event); setAmount()\" value='$txtTotalAmount'  style='width: 115px; font-size:14px;' onkeypress='return (event.charCode >= 48 && event.charCode <= 57) || (event.charCode== 46) || (event.charCode >= 0 && event.charCode <= 31)'
			onkeyup=\"setAmount();\"></td>";


echo"   </tr>";

echo"   <tr  height='60px'><td ><span class='style1'>Bank Name</span></td><td>";
if($TransType="PV"){
	echo "<input type='text' name='bankname' id='bankname' readonly style='width:180px'>";
	
}
else{
echo"<input type='text' name='bankname' id='bankname' value='$bankname' style='width:180px'>";
}
echo"</td>
		<td ><span class='style1'>Check #</span>
		<input type='text' name='chknbr' id='chknbr' onkeyup=\"goNextField('txtValueDate', event);\" value='$chknbr' style='width:100px'></td>
		
		</tr>";
		$txtValueDate = date("d/m/Y");
echo "<tr>
<td></td>
<td></td>
<td></td>
<td><span class='style1'>Value Date</span></td>
<td><input type='text' id='txtValueDate' name='txtValueDate' onkeyup=\"goNextField('fromledgernb', event)\" value='$txtValueDate' maxlength='10' style='width:80px;'  ></td>
</tr>";
echo"	<tr  height='25'><td colspan='9'>";

echo "<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='0' >";
echo"	<tr bgcolor='#ffffff' height='25'>";
	echo"  		<td align='left'  style='width:235px;background: url(design/grid-label-bg.png); background-repeat: no-repeat; background-position: left;padding-left:15px;'>
				<font color='#fffffff'><b>From</b></span></td>";
	echo "<td></td>";
	echo "</tr>";
echo "</table>";
echo "</td></tr>";

echo"	<tr  height='25'><td colspan='9'>";
echo "<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='0'width='110px' >";	
echo "<col width='200px'><col width='300px'><col width='300px'><col width='120px'><col width='180px'>";	  
echo "<tr height='30px'>";
echo "<th>Account Number</th><th>Account Name</th><th>Desc.</th><th>Currency</th><th>Amount</th>";
echo"</tr>";
echo "<tr height='40px'>";
echo "<td align='center'><input type='text' name='fromledgernb' id='fromledgernb' onkeyup=\"chkNumExists(event);\" value='$fromledgernb' style='width:150px;' maxlength='10'>
	<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerCode' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" 
	onClick=\"showlistLedger('31', '$TransType');\" style='vertical-align:bottom;'/></span></label>
	</td>";
echo "<td align='center'><input type='text' name='fromledgernanme' onkeyup=\"goNextField('fromdesc', event);\" id='fromledgername' value='$fromledgername' style='width:280px;'>
		</td>";
echo "<td align='center'><input type='text' name='fromledgerdesc' id='fromdesc' value='$fromdesc' style='width:280px;'></td>";
echo "<td align='center'><input type='text' name='precurrency' id='precurrency' value='$precurrency' readonly style='width:110px;'></td>";
echo "<td align='center'><input type='text' name='preamount' id='preamount' value='$preamount' readonly style='width:170px;'></td>";
echo "</tr>";
echo"</table></td></tr>";

echo"   <tr  height='60px'><td colspan='9' align='center' >&nbsp;</td></tr>";

echo" </table></td></tr>";

echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";

echo" </table>  ";//end of last table menu

echo "<input type='hidden' name='print_check' id='print_check' value=$print_check />";
echo "<input type='hidden' name='bankname2' id='bankname2' value=$bankname2 />";
if($mainaction=='done' && $TransId != ''){
	
	echo "<script>
	if(document.getElementById('print_check').value == 1){";
	echo " PrintTrans2('".$TransId."', '".$TransType."'); ";
	echo"}
	else{";
	echo " PrintTrans('".$TransId."', '".$TransType."'); ";
	echo "}";
	echo " ClearPage('".$TransType."'); ";
	echo "</script>";
	exit;
}

print <<<HERE

</form>
</body>
<html>

HERE;

?>