<?php

/* $Revision: 1.38 $ */


$PageSecurity = 5;

include('includes/DefinePaymentClass.php');
include('includes/session.inc');

$title = _('Payment Entry');

include('includes/header.inc');

include('includes/SQL_CommonFunctions.inc');

if (isset($_POST['PaymentCancelled'])) {
	prnMsg(_('Payment Cancelled since cheque was not printed'), 'warning');
	include('includes/footer.inc');
	exit();
}

if (isset($_GET['NewPayment']) and $_GET['NewPayment']=='Yes'){
	unset($_SESSION['PaymentDetail']->GLItems);
	unset($_SESSION['PaymentDetail']);
}

if (!isset($_SESSION['PaymentDetail'])){
	$_SESSION['PaymentDetail'] = new Payment;
	$_SESSION['PaymentDetail']->GLItemCounter = 1;
}

//note this is already linked from this page
//echo "<a href='" . $rootpath . '/SelectSupplier.php?' . SID . "'>" . _('Back to Suppliers') . '</a><br>';

echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/transactions.png" title="' . _('Payment Entry') . '" alt="">' . ' ' . _('Payment Entry') . '</p>';
echo '<div class="page_help_text">' . _('Use this screen to enter payments FROM your bank account.  <br>Note: To enter a payment FROM a supplier, first select the Supplier, click Enter a Payment to, or Receipt from the Supplier, and use a negative Payment amount on this form.') . '</div><br><div class="centre">';

if (isset($_GET['SupplierID'])){
	/*The page was called with a supplierID check it is valid and default the inputs for Supplier Name and currency of payment */

	unset($_SESSION['PaymentDetail']->GLItems);
	unset($_SESSION['PaymentDetail']);
	$_SESSION['PaymentDetail'] = new Payment;
	$_SESSION['PaymentDetail']->GLItemCounter = 1;


	$SQL= "SELECT suppname,
			address1,
			address2,
			address3,
			address4,
			address5,
			address6,
			currcode,
			factorcompanyid
		FROM suppliers
		WHERE supplierid='" . $_GET['SupplierID'] . "'";

	$Result = DB_query($SQL, $db);
	if (DB_num_rows($Result)==0){
		prnMsg( _('The supplier code that this payment page was called with is not a currently defined supplier code') . '. ' . _('If this page is called from the selectSupplier page then this assures that a valid supplier is selected'),'warn');
		include('includes/footer.inc');
		exit;
	} else {
		$myrow = DB_fetch_array($Result);
		if ($myrow['factorcompanyid'] == 1) {
			$_SESSION['PaymentDetail']->SuppName = $myrow['suppname'];
			$_SESSION['PaymentDetail']->Address1 = $myrow['address1'];
			$_SESSION['PaymentDetail']->Address2 = $myrow['address2'];
			$_SESSION['PaymentDetail']->Address3 = $myrow['address3'];
			$_SESSION['PaymentDetail']->Address4 = $myrow['address4'];
			$_SESSION['PaymentDetail']->Address5 = $myrow['address5'];
			$_SESSION['PaymentDetail']->Address6 = $myrow['address6'];
			$_SESSION['PaymentDetail']->SupplierID = $_GET['SupplierID'];
			$_SESSION['PaymentDetail']->Currency = $myrow['currcode'];
		} else {
			$factorsql= "SELECT coyname,
			 					address1,
			 					address2,
			 					address3,
			 					address4,
			 					address5,
			 					address6
							FROM factorcompanies
							WHERE id='" . $myrow['factorcompanyid'] . "'";

			$FactorResult = DB_query($factorsql, $db);
			$myfactorrow = DB_fetch_array($FactorResult);
			$_SESSION['PaymentDetail']->SuppName = $myrow['suppname'] . _(' care of ') . $myfactorrow['coyname'];
			$_SESSION['PaymentDetail']->Address1 = $myfactorrow['address1'];
			$_SESSION['PaymentDetail']->Address2 = $myfactorrow['address2'];
			$_SESSION['PaymentDetail']->Address3 = $myfactorrow['address3'];
			$_SESSION['PaymentDetail']->Address4 = $myfactorrow['address4'];
			$_SESSION['PaymentDetail']->Address5 = $myfactorrow['address5'];
			$_SESSION['PaymentDetail']->Address6 = $myfactorrow['address6'];
			$_SESSION['PaymentDetail']->SupplierID = $_GET['SupplierID'];
			$_SESSION['PaymentDetail']->Currency = $myrow['currcode'];
			$_POST['Currency'] = $_SESSION['PaymentDetail']->Currency;
		}
	}
}

if (isset($_POST['BankAccount']) and $_POST['BankAccount']!=''){
	$_SESSION['PaymentDetail']->Account=$_POST['BankAccount'];
	/*Get the bank account currency and set that too */
	$ErrMsg = _('Could not get the currency of the bank account');
	$result = DB_query('SELECT currcode FROM bankaccounts WHERE accountcode =' . $_POST['BankAccount'],$db,$ErrMsg);
	$myrow = DB_fetch_row($result);
	$_SESSION['PaymentDetail']->AccountCurrency=$myrow[0];

} else {
	$_SESSION['PaymentDetail']->AccountCurrency =$_SESSION['CompanyRecord']['currencydefault'];
}
if (isset($_POST['DatePaid']) and $_POST['DatePaid']!='' AND Is_Date($_POST['DatePaid'])){
	$_SESSION['PaymentDetail']->DatePaid=$_POST['DatePaid'];
}
if (isset($_POST['ExRate']) and $_POST['ExRate']!=''){
	$_SESSION['PaymentDetail']->ExRate=$_POST['ExRate']; //ex rate between payment currency and account currency
}
if (isset($_POST['FunctionalExRate']) and $_POST['FunctionalExRate']!=''){
	$_SESSION['PaymentDetail']->FunctionalExRate=$_POST['FunctionalExRate']; //ex rate between payment currency and account currency
}
if (isset($_POST['Paymenttype']) and $_POST['Paymenttype']!=''){
	$_SESSION['PaymentDetail']->Paymenttype = $_POST['Paymenttype'];
}


if (isset($_POST['Currency']) and $_POST['Currency']!=''){
	$_SESSION['PaymentDetail']->Currency=$_POST['Currency']; //payment currency
	/*Get the exchange rate between the functional currency and the payment currency*/
	$result = DB_query("SELECT rate FROM currencies WHERE currabrev='" . $_SESSION['PaymentDetail']->Currency . "'",$db);
	$myrow = DB_fetch_row($result);
	$tableExRate = $myrow[0]; //this is the rate of exchange between the functional currency and the payment currency

	if ($_POST['Currency']==$_SESSION['PaymentDetail']->AccountCurrency){
		$_POST['ExRate']=1;
		$_SESSION['PaymentDetail']->ExRate=$_POST['ExRate']; //ex rate between payment currency and account currency
		$SuggestedExRate=1;
	}
	if ($_SESSION['PaymentDetail']->AccountCurrency==$_SESSION['CompanyRecord']['currencydefault']){
		$_POST['FunctionalExRate']=1;
		$_SESSION['PaymentDetail']->FunctionalExRate=$_POST['FunctionalExRate'];
		$SuggestedFunctionalExRate =1;
		$SuggestedExRate = $tableExRate;

	} else {
		/*To illustrate the rates required
			Take an example functional currency NZD payment in USD from an AUD bank account
			1 NZD = 0.80 USD
			1 NZD = 0.90 AUD
			The FunctionalExRate = 0.90 - the rate between the functional currency and the bank account currency
			The payment ex rate is the rate at which one can purchase the payment currency in the bank account currency
			or 0.8/0.9 = 0.88889
		*/

		/*Get suggested FunctionalExRate */
		$result = DB_query("SELECT rate FROM currencies WHERE currabrev='" . $_SESSION['PaymentDetail']->AccountCurrency . "'",$db);
		$myrow = DB_fetch_row($result);
		$SuggestedFunctionalExRate = $myrow[0];

		/*Get the exchange rate between the functional currency and the payment currency*/
		$result = DB_query("select rate FROM currencies WHERE currabrev='" . $_SESSION['PaymentDetail']->Currency . "'",$db);
		$myrow = DB_fetch_row($result);
		$tableExRate = $myrow[0]; //this is the rate of exchange between the functional currency and the payment currency
		/*Calculate cross rate to suggest appropriate exchange rate between payment currency and account currency */
		$SuggestedExRate = $tableExRate/$SuggestedFunctionalExRate;

	}
}


if (isset($_POST['Narrative']) and $_POST['Narrative']!=''){
	$_SESSION['PaymentDetail']->Narrative=$_POST['Narrative'];
}
if (isset($_POST['Amount']) and $_POST['Amount']!=""){
	$_SESSION['PaymentDetail']->Amount=$_POST['Amount'];
} else {
	if (!isset($_SESSION['PaymentDetail']->Amount)) {
		$_SESSION['PaymentDetail']->Amount=0;
	}
}
if (isset($_POST['Discount']) and $_POST['Discount']!=''){
	$_SESSION['PaymentDetail']->Discount=$_POST['Discount'];
} else {
	if (!isset($_SESSION['PaymentDetail']->Discount)) {
	  $_SESSION['PaymentDetail']->Discount=0;
  }
}


$msg='';

if (isset($_POST['CommitBatch'])){

  /* once the GL analysis of the payment is entered (if the Creditors_GLLink is active),
  process all the data in the session cookie into the DB creating a banktrans record for
  the payment in the batch and SuppTrans record for the supplier payment if a supplier was selected
  A GL entry is created for each GL entry (only one for a supplier entry) and one for the bank
  account credit.

  NB allocations against supplier payments are a separate exercice

  if GL integrated then
  first off run through the array of payment items $_SESSION['Payment']->GLItems and
  create GL Entries for the GL payment items
  */

	/*First off  check we have an amount entered as paid ?? */
	$TotalAmount =0;
	foreach ($_SESSION['PaymentDetail']->GLItems AS $PaymentItem) {
		$TotalAmount += $PaymentItem->Amount;
	}

	if ($TotalAmount==0 AND
		($_SESSION['PaymentDetail']->Discount + $_SESSION['PaymentDetail']->Amount)/$_SESSION['PaymentDetail']->ExRate ==0){
		prnMsg( _('This payment has no amounts entered and will not be processed'),'warn');
		include('includes/footer.inc');
		exit;
	}

	/*Make an array of the defined bank accounts */
	$SQL = 'SELECT bankaccounts.accountcode
			FROM bankaccounts,
				chartmaster
		WHERE bankaccounts.accountcode=chartmaster.accountcode';
	$result = DB_query($SQL,$db);
	$BankAccounts = array();
	$i=0;

	while ($Act = DB_fetch_row($result)){
		$BankAccounts[$i]= $Act[0];
		$i++;
	}

	$PeriodNo = GetPeriod($_SESSION['PaymentDetail']->DatePaid,$db);

	// first time through commit if supplier cheque then print it first
	if ((!isset($_POST['ChequePrinted']))
		AND (!isset($_POST['PaymentCancelled']))
		AND ($_SESSION['PaymentDetail']->Paymenttype == 'Cheque')) {
	// it is a supplier payment by cheque and haven't printed yet so print cheque

		echo '<br><a href="' . $rootpath . '/PrintCheque.php?' . SID . '&ChequeNum=' . $_POST['ChequeNum'] . '">' . _('Print Cheque using pre-printed stationery') . '</a><br><br>';

		echo '<form method=post action="' . $_SERVER['PHP_SELF'] . '">';
		echo _('Has the cheque been printed') . '?<br><br>';
		echo '<input type="hidden" name="CommitBatch" VALUE="' . $_POST['CommitBatch'] . '">';
		echo '<input type="submit" name="ChequePrinted" VALUE="' . _('Yes / Continue') . '">&nbsp;&nbsp;';
		echo '<input type="submit" name="PaymentCancelled" VALUE="' . _('No / Cancel Payment') . '">';
	} else {

		//Start a transaction to do the whole lot inside
		$SQL = 'BEGIN';
		$result = DB_query($SQL,$db);


		if ($_SESSION['PaymentDetail']->SupplierID=='') {

		//its a nominal bank transaction type 1

			$TransNo = GetNextTransNo( 1, $db);
			$Transtype = 1;

			if ($_SESSION['CompanyRecord']['gllink_creditors']==1){ /* then enter GLTrans */
				$TotalAmount=0;
				foreach ($_SESSION['PaymentDetail']->GLItems as $PaymentItem) {

					 /*The functional currency amount will be the
					 payment currenct amount  / the bank account currency exchange rate  - to get to the bank account currency
					 then / the functional currency exchange rate to get to the functional currency */
					if ($PaymentItem->cheque=='') $PaymentItem->cheque=0;
					$SQL = 'INSERT INTO gltrans (type,
								typeno,
								trandate,
								periodno,
								account,
								narrative,
								amount,
								chequeno,
								tag) ';
				 	$SQL= $SQL . "VALUES (1,
						" . $TransNo . ",
						'" . FormatDateForSQL($_SESSION['PaymentDetail']->DatePaid) . "',
						" . $PeriodNo . ",
						" . $PaymentItem->GLCode . ",
						'" . $PaymentItem->Narrative . "',
						" . ($PaymentItem->Amount/$_SESSION['PaymentDetail']->ExRate/$_SESSION['PaymentDetail']->FunctionalExRate) . ",
						'". $PaymentItem->cheque ."',
						'" . $PaymentItem->tag . "'
						)";
					$ErrMsg = _('Cannot insert a GL entry for the payment using the SQL');
					$result = DB_query($SQL,$db,$ErrMsg,_('The SQL that failed was'),true);

					$TotalAmount += $PaymentItem->Amount;
				}
				$_SESSION['PaymentDetail']->Amount = $TotalAmount;
				$_SESSION['PaymentDetail']->Discount=0;
			}

			//Run through the GL postings to check to see if there is a posting to another bank account (or the same one) if there is then a receipt needs to be created for this account too

			foreach ($_SESSION['PaymentDetail']->GLItems as $PaymentItem) {

				if (in_array($PaymentItem->GLCode, $BankAccounts)) {

					/*Need to deal with the case where the payment from one bank account could be to a bank account in another currency */

					/*Get the currency and rate of the bank account transferring to*/
					$SQL = 'SELECT currcode, rate
							FROM bankaccounts INNER JOIN currencies
							ON bankaccounts.currcode = currencies.currabrev
							WHERE accountcode=' . $PaymentItem->GLCode;
					$TrfToAccountResult = DB_query($SQL,$db);
					$TrfToBankRow = DB_fetch_array($TrfToAccountResult) ;
					$TrfToBankCurrCode = $TrfToBankRow['currcode'];
					$TrfToBankExRate = $TrfToBankRow['rate'];

					if ($_SESSION['PaymentDetail']->AccountCurrency == $TrfToBankCurrCode){
					/*Make sure to use the same rate if the transfer is between two bank accounts in the same currency */
						$TrfToBankExRate = $_SESSION['PaymentDetail']->FunctionalExRate;
					}

					/*Consider an example
					 functional currency NZD
					 bank account in AUD - 1 NZD = 0.90 AUD (FunctionalExRate)
					 paying USD - 1 AUD = 0.85 USD  (ExRate)
					 to a bank account in EUR - 1 NZD = 0.52 EUR

					 oh yeah - now we are getting tricky!
					 Lets say we pay USD 100 from the AUD bank account to the EUR bank account

					 To get the ExRate for the bank account we are transferring money to
					 we need to use the cross rate between the NZD-AUD/NZD-EUR
					 and apply this to the

					 the payment record will read
					 exrate = 0.85 (1 AUD = USD 0.85)
					 amount = 100 (USD)
					 functionalexrate = 0.90 (1 NZD = AUD 0.90)

					 the receipt record will read

					 amount 100 (USD)
					 exrate    (1 EUR =  (0.85 x 0.90)/0.52 USD)
					 					(ExRate x FunctionalExRate) / USD Functional ExRate
					 functionalexrate =     (1NZD = EUR 0.52)

				*/

					$ReceiptTransNo = GetNextTransNo( 2, $db);
					$SQL= 'INSERT INTO banktrans (transno,
								type,
								bankact,
								ref,
								exrate,
								functionalexrate,
								transdate,
								banktranstype,
								amount,
								currcode)
						VALUES (' . $ReceiptTransNo . ',
							2,
							' . $PaymentItem->GLCode . ", '"
							. _('Act Transfer From ') . $_SESSION['PaymentDetail']->Account . ' - ' . $PaymentItem->Narrative . " ',
							" . (($_SESSION['PaymentDetail']->ExRate * $_SESSION['PaymentDetail']->FunctionalExRate)/$TrfToBankExRate). ",
							" . $TrfToBankExRate . ",
							'" . FormatDateForSQL($_SESSION['PaymentDetail']->DatePaid) . "',
							'" . $_SESSION['PaymentDetail']->Paymenttype . "',
							" . $PaymentItem->Amount . ",
							'" . $_SESSION['PaymentDetail']->Currency . "'
						)";
					$ErrMsg = _('Cannot insert a bank transaction because');
					$DbgMsg =  _('Cannot insert a bank transaction with the SQL');
					$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);

				}
			}
		} else {
	  /*Its a supplier payment type 22 */
			$CreditorTotal = (($_SESSION['PaymentDetail']->Discount + $_SESSION['PaymentDetail']->Amount)/$_SESSION['PaymentDetail']->ExRate)/$_SESSION['PaymentDetail']->FunctionalExRate;

			$TransNo = GetNextTransNo(22, $db);
			$Transtype = 22;

			/* Create a SuppTrans entry for the supplier payment */
			$SQL = "INSERT INTO supptrans (transno,
						type,
						supplierno,
						trandate,
						suppreference,
						rate,
						ovamount,
						transtext) ";
			$SQL = $SQL . 'VALUES (' . $TransNo . ",
					22,
					'" . $_SESSION['PaymentDetail']->SupplierID . "',
					'" . FormatDateForSQL($_SESSION['PaymentDetail']->DatePaid) . "',
					'" . $_SESSION['PaymentDetail']->Paymenttype . "',
					" . ($_SESSION['PaymentDetail']->ExRate/$_SESSION['PaymentDetail']->FunctionalExRate) . ",
					" . (-$_SESSION['PaymentDetail']->Amount-$_SESSION['PaymentDetail']->Discount) . ",
					'" . $_SESSION['PaymentDetail']->Narrative . "'
				)";

			$ErrMsg =  _('Cannot insert a payment transaction against the supplier because');
			$DbgMsg = _('Cannot insert a payment transaction against the supplier using the SQL');
			$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);

			/*Update the supplier master with the date and amount of the last payment made */
			$SQL = "UPDATE suppliers SET
				lastpaiddate = '" . FormatDateForSQL($_SESSION['PaymentDetail']->DatePaid) . "',
				lastpaid=" . $_SESSION['PaymentDetail']->Amount ."
				WHERE suppliers.supplierid='" . $_SESSION['PaymentDetail']->SupplierID . "'";



			$ErrMsg = _('Cannot update the supplier record for the date of the last payment made because');
			$DbgMsg = _('Cannot update the supplier record for the date of the last payment made using the SQL');
			$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);

			$_SESSION['PaymentDetail']->Narrative = $_SESSION['PaymentDetail']->SupplierID . "-" . $_SESSION['PaymentDetail']->Narrative;

			if ($_SESSION['CompanyRecord']['gllink_creditors']==1){ /* then do the supplier control GLTrans */
			/* Now debit creditors account with payment + discount */

				$SQL="INSERT INTO gltrans ( type,
							typeno,
							trandate,
							periodno,
							account,
							narrative,
							amount) ";
				$SQL=$SQL . "VALUES (
						22,
						" . $TransNo . ",
						'" . FormatDateForSQL($_SESSION['PaymentDetail']->DatePaid) . "',
						" . $PeriodNo . ",
						" . $_SESSION['CompanyRecord']['creditorsact'] . ",
						'" . $_SESSION['PaymentDetail']->Narrative . "',
						" . $CreditorTotal . "
					)";
				$ErrMsg = _('Cannot insert a GL transaction for the creditors account debit because');
				$DbgMsg = _('Cannot insert a GL transaction for the creditors account debit using the SQL');
				$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);

				if ($_SESSION['PaymentDetail']->Discount !=0){
					/* Now credit Discount received account with discounts */
					$SQL="INSERT INTO gltrans ( type,
								typeno,
								trandate,
								periodno,
								account,
								narrative,
								amount) ";
					$SQL=$SQL . "VALUES (22,
						" . $TransNo . ",
						'" . FormatDateForSQL($_SESSION['PaymentDetail']->DatePaid) . "',
						" . $PeriodNo . ",
						" . $_SESSION['CompanyRecord']["pytdiscountact"] . ",
						'" . $_SESSION['PaymentDetail']->Narrative . "',
						" . (-$_SESSION['PaymentDetail']->Discount/$_SESSION['PaymentDetail']->ExRate/$_SESSION['PaymentDetail']->FunctionalExRate) . "
					  )";
					$ErrMsg = _('Cannot insert a GL transaction for the payment discount credit because');
					$DbgMsg = _('Cannot insert a GL transaction for the payment discount credit using the SQL');
					$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
				} // end if discount
			} // end if gl creditors
		} // end if supplier

		if ($_SESSION['CompanyRecord']['gllink_creditors']==1){ /* then do the common GLTrans */

			if ($_SESSION['PaymentDetail']->Amount !=0){
				/* Bank account entry first */
				$SQL = "INSERT INTO gltrans ( type,
							typeno,
							trandate,
							periodno,
							account,
							narrative,
							amount) ";
				$SQL = $SQL . 'VALUES (' . $Transtype . ',
						' . $TransNo . ",
						'" . FormatDateForSQL($_SESSION['PaymentDetail']->DatePaid) . "',
						" . $PeriodNo . ",
						" . $_SESSION['PaymentDetail']->Account . ",
						'" . $_SESSION['PaymentDetail']->Narrative . "',
						" . (-$_SESSION['PaymentDetail']->Amount/$_SESSION['PaymentDetail']->ExRate/$_SESSION['PaymentDetail']->FunctionalExRate) . "
					)";

				$ErrMsg =  _('Cannot insert a GL transaction for the bank account credit because');
				$DbgMsg =  _('Cannot insert a GL transaction for the bank account credit using the SQL');
				$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);

			}
		}

		/*now enter the BankTrans entry */
		if ($Transtype==22) {
			$SQL="INSERT INTO banktrans (transno,
					type,
					bankact,
					ref,
					exrate,
					functionalexrate,
					transdate,
					banktranstype,
					amount,
					currcode) ";
			$SQL= $SQL . "VALUES (" . $TransNo . ",
				" . $Transtype . ",
				" . $_SESSION['PaymentDetail']->Account . ",
				'" . $_SESSION['PaymentDetail']->Narrative . "',
				" . $_SESSION['PaymentDetail']->ExRate . " ,
				" . $_SESSION['PaymentDetail']->FunctionalExRate . ",
				'" . FormatDateForSQL($_SESSION['PaymentDetail']->DatePaid) . "',
				'" . $_SESSION['PaymentDetail']->Paymenttype . "',
				" . -$_SESSION['PaymentDetail']->Amount . ",
				'" . $_SESSION['PaymentDetail']->Currency . "'
			)";

			$ErrMsg = _('Cannot insert a bank transaction because');
			$DbgMsg = _('Cannot insert a bank transaction using the SQL');
			$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
		} else {
			foreach ($_SESSION['PaymentDetail']->GLItems as $PaymentItem) {
				$SQL="INSERT INTO banktrans (transno,
					type,
					bankact,
					ref,
					exrate,
					functionalexrate,
					transdate,
					banktranstype,
					amount,
					currcode) ";
				$SQL= $SQL . "VALUES (" . $TransNo . ",
					" . $Transtype . ",
					" . $_SESSION['PaymentDetail']->Account . ",
					'" . $_SESSION['PaymentDetail']->Narrative . "',
					" . $_SESSION['PaymentDetail']->ExRate . " ,
					" . $_SESSION['PaymentDetail']->FunctionalExRate . ",
					'" . FormatDateForSQL($_SESSION['PaymentDetail']->DatePaid) . "',
					'" . $_SESSION['PaymentDetail']->Paymenttype . "',
					" . -$PaymentItem->Amount . ",
					'" . $_SESSION['PaymentDetail']->Currency . "'
				)";

				$ErrMsg = _('Cannot insert a bank transaction because');
				$DbgMsg = _('Cannot insert a bank transaction using the SQL');
				$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
			}
		}

		$SQL = "COMMIT";
		$ErrMsg = _('Cannot commit the changes because');
		$DbgMsg = _('The commit of the database transaction failed');
		$result= DB_query($SQL,$db,$ErrMsg,$DbgMsg);

		prnMsg(_('Payment') . ' ' . $TransNo . ' ' . _('has been successfully entered'),'success');

		$lastSupplier = ($_SESSION['PaymentDetail']->SupplierID);

		unset($_POST['BankAccount']);
		unset($_POST['DatePaid']);
		unset($_POST['ExRate']);
		unset($_POST['Paymenttype']);
		unset($_POST['Currency']);
		unset($_POST['Narrative']);
		unset($_POST['Amount']);
		unset($_POST['Discount']);
		unset($_SESSION['PaymentDetail']->GLItems);
		unset($_SESSION['PaymentDetail']);

		/*Set up a newy in case user wishes to enter another */
		echo '<br><a href="' . $_SERVER['PHP_SELF'] . '?' . SID . '">' . _('Enter a General Ledger Payment') . '</a><br>';
		echo '<br><a href="' . $rootpath . '/Payments.php?SupplierID=' . $lastSupplier . '">' . _('Enter another Payment for this Supplier') . '</a>';
	}

	include('includes/footer.inc');
	exit;

} elseif (isset($_GET['Delete'])){
  /* User hit delete the receipt entry from the batch */
	$_SESSION['PaymentDetail']->Remove_GLItem($_GET['Delete']);
} elseif (isset($_POST['Process'])){ //user hit submit a new GL Analysis line into the payment

	$ChequeNoSQL='select account from gltrans where chequeno="'.$_POST['cheque'].'"';
	$ChequeNoResult=DB_query($ChequeNoSQL, $db);

	if ($_POST['GLManualCode']!="" AND is_numeric($_POST['GLManualCode'])){

		$SQL = "select accountname
			FROM chartmaster
			WHERE accountcode=" . $_POST['GLManualCode'];

		$Result=DB_query($SQL,$db);

		if (DB_num_rows($Result)==0){
			prnMsg( _('The manual GL code entered does not exist in the database') . ' - ' . _('so this GL analysis item could not be added'),'warn');
			unset($_POST['GLManualCode']);
		} else if (DB_num_rows($ChequeNoResult)!=0 and $_POST['cheque']!=''){
			prnMsg( _('The Cheque/Voucher number has already been used') . ' - ' . _('This GL analysis item could not be added'),'error');
		} else {
			$myrow = DB_fetch_array($Result);
			$_SESSION['PaymentDetail']->add_to_glanalysis($_POST['GLAmount'],
								$_POST['GLNarrative'],
								$_POST['GLManualCode'],
								$myrow['accountname'],
								$_POST['tag'],
								$_POST['cheque']);
		}
	} else if (DB_num_rows($ChequeNoResult)!=0 and $_POST['cheque']!=''){
		prnMsg( _('The cheque number has already been used') . ' - ' . _('This GL analysis item could not be added'),'error');
	} else {
		$SQL = "select accountname FROM chartmaster WHERE accountcode=" . $_POST['GLCode'];
		$Result=DB_query($SQL,$db);
		$myrow=DB_fetch_array($Result);
		$_SESSION['PaymentDetail']->add_to_glanalysis($_POST['GLAmount'],
							$_POST['GLNarrative'],
							$_POST['GLCode'],
							$myrow['accountname'],
							$_POST['tag'],
							$_POST['cheque']);
	}

	/*Make sure the same receipt is not double processed by a page refresh */
	$_POST['Cancel'] = 1;
}

if (isset($_POST['Cancel'])){
	unset($_POST['GLAmount']);
	unset($_POST['GLNarrative']);
	unset($_POST['GLCode']);
	unset($_POST['AccountName']);
}

/*set up the form whatever */
if (!isset($_POST['DatePaid'])) {
	$_POST['DatePaid'] = '';
}

if (isset($_POST['DatePaid']) and ($_POST['DatePaid']=="" OR !Is_Date($_SESSION['PaymentDetail']->DatePaid))){
	$_POST['DatePaid']= Date($_SESSION['DefaultDateFormat']);
	$_SESSION['PaymentDetail']->DatePaid = $_POST['DatePaid'];
}

if ($_SESSION['PaymentDetail']->Currency=='' AND $_SESSION['PaymentDetail']->SupplierID==''){
	$_SESSION['PaymentDetail']->Currency=$_SESSION['CompanyRecord']['currencydefault'];
}


if (isset($_POST['BankAccount']) AND $_POST['BankAccount']!='') {
	$SQL = "SELECT bankaccountname
			FROM bankaccounts,
				chartmaster
		WHERE bankaccounts.accountcode= chartmaster.accountcode
		AND chartmaster.accountcode=" . $_POST['BankAccount'];

	$ErrMsg = _('The bank account name cannot be retrieved because');
	$DbgMsg = _('SQL used to retrieve the bank account name was');

	$result= DB_query($SQL,$db,$ErrMsg,$DbgMsg);

	if (DB_num_rows($result)==1){
		$myrow = DB_fetch_row($result);
		$_SESSION['PaymentDetail']->BankAccountName = $myrow[0];
		unset($result);
	} elseif (DB_num_rows($result)==0){
		prnMsg( _('The bank account number') . ' ' . $_POST['BankAccount'] . ' ' . _('is not set up as a bank account with a valid general ledger account'),'error');
	}
}


echo '<form action=' . $_SERVER['PHP_SELF'] . '?' . SID . ' method=post>';


echo '<font size=3 color=BLUE>' . _('Payment');

if ($_SESSION['PaymentDetail']->SupplierID!=""){
	echo ' ' . _('to') . ' ' . $_SESSION['PaymentDetail']->SuppName;
}

if ($_SESSION['PaymentDetail']->BankAccountName!=""){
	echo ' ' . _('from the') . ' ' . $_SESSION['PaymentDetail']->BankAccountName;
}

echo ' ' . _('on') . ' ' . $_SESSION['PaymentDetail']->DatePaid . '</font>';

// Note this is duplicated
//echo '<div class="page_help_text">' . _('Note: To enter a payment FROM ') . $_SESSION['PaymentDetail']->SuppName . _(' use a negative Payment amount.');
?>

<div  style="border:5px solid #999999;" >
<table width="860" >
<tr>
<td width="123" height="47" align="left"><div align="left">Cash Payment No:</div></td>
<td width="140" align="left"><div align="left">125465</div></td> 
<td width="402" align="right">&nbsp;</td>
<td width="175" align="right">Date: 
  <input type="text" value="" name="paydate" id="paydate"  style="width:90px;"/></td>
</tr>
</table>
<div align="right" style="border:2px solid #000000;">
    <table width="960" >
      <tr>
        <td width="668"><strong>Particulars</strong></td>
	    <td width="181"><div align="right"><strong>Debit</strong></div></td>
	    <td width="95"><div align="right"><strong>Crerdit</strong></div></td>
		<td width="95"><div align="right"><strong>Action</strong></div></td>
      </tr>
    </table>
  </div>
  <div align="right" style="border:2px solid #000000;">
    <table width="960" >
      <tr>
        <td width="745"><strong>Particulars</strong></td>
	    <td width="99"><div align="right"><strong>Debit</strong></div></td>
	    <td width="93"><div align="right"><strong>Crerdit</strong></div></td>
		 <td width="93"><div align="right"><strong>Edit/Delet</strong></div></td>
      </tr>
    </table>
  </div>
</div>
<div align="right">
<table align="right" style=" border:#676F83 1px solid;">
<tr>
	<td>Amount Type</td>
	<td><select name="amount_type">
	<option value="Dr">Debit</option>
	<option value="Cr">Credit</option>
	</select>
	
	</td>
	<td>Amount</td>
	<td><input  type="text" name="amount" /></td>
</tr>
<tr>
	<td >Select GL Account :</td>
	<td colspan="3"><select style="width:260px;" onchange="return assignComboToInput(this,GLManualCode)" name="GLCode"><option value="1">1 - s</option><option value="101">101 - Vat Input Credit</option><option value="201">201 - Vat Recievable</option><option value="1010">1010 - Petty Cash</option><option value="1020">1020 - Cash on Hand</option><option value="1030">1030 - Cheque Accounts</option><option value="1040">1040 - Savings Accounts</option><option value="1050">1050 - Payroll Accounts</option><option value="1060">1060 - Special Accounts</option><option value="1070">1070 - Money Market Investments</option><option value="1080">1080 - Short-Term Investments (&lt; 90 days)</option><option value="1090">1090 - Interest Receivable</option><option value="1100">1100 - Accounts Receivable</option><option value="1150">1150 - Allowance for Doubtful Accounts</option><option value="1200">1200 - Notes Receivable</option><option value="1250">1250 - Income Tax Receivable</option><option value="1300">1300 - Prepaid Expenses</option><option value="1350">1350 - Advances</option><option value="1400">1400 - Supplies Inventory</option><option value="1420">1420 - Raw Material Inventory</option><option value="1440">1440 - Work in Progress Inventory</option><option value="1460">1460 - Finished Goods Inventory</option><option value="1500">1500 - Land</option><option value="1550">1550 - Bonds</option><option value="1600">1600 - Buildings</option><option value="1620">1620 - Accumulated Depreciation of Buildings</option><option value="1650">1650 - Equipment</option><option value="1670">1670 - Accumulated Depreciation of Equipment</option><option value="1700">1700 - Furniture &amp; Fixtures</option><option value="1710">1710 - Accumulated Depreciation of Furniture &amp; Fixtures</option><option value="1720">1720 - Office Equipment</option><option value="1730">1730 - Accumulated Depreciation of Office Equipment</option><option value="1740">1740 - Software</option><option value="1750">1750 - Accumulated Depreciation of Software</option><option value="1760">1760 - Vehicles</option><option value="1770">1770 - Accumulated Depreciation Vehicles</option><option value="1780">1780 - Other Depreciable Property</option><option value="1790">1790 - Accumulated Depreciation of Other Depreciable Prop</option><option value="1800">1800 - Patents</option><option value="1850">1850 - Goodwill</option><option value="1900">1900 - Future Income Tax Receivable</option><option value="2010">2010 - Bank Indedebtedness (overdraft)</option><option value="2020">2020 - Retainers or Advances on Work</option><option value="2050">2050 - Interest Payable</option><option value="2100">2100 - Accounts Payable</option><option value="2150">2150 - Goods Received Suspense</option><option value="2200">2200 - Short-Term Loan Payable</option><option value="2230">2230 - Current Portion of Long-Term Debt Payable</option><option value="2250">2250 - Income Tax Payable</option><option value="2300">2300 - Sale Tax Payable</option><option value="2310">2310 - Sale Tax Recievable</option><option value="2320">2320 - PST Payable</option><option value="2330">2330 - PST Recoverable (commission)</option><option value="2340">2340 - Payroll Tax Payable</option><option value="2350">2350 - Withholding Income Tax Payable</option><option value="2360">2360 - Other Taxes Payable</option><option value="2400">2400 - Employee Salaries Payable</option><option value="2410">2410 - Management Salaries Payable</option><option value="2420">2420 - Director / Partner Fees Payable</option><option value="2450">2450 - Health Benefits Payable</option><option value="2460">2460 - Pension Benefits Payable</option><option value="2470">2470 - Canada Pension Plan Payable</option><option value="2480">2480 - Employment Insurance Premiums Payable</option><option value="2500">2500 - Land Payable</option><option value="2550">2550 - Long-Term Bank Loan</option><option value="2560">2560 - Notes Payable</option><option value="2600">2600 - Building &amp; Equipment Payable</option><option value="2700">2700 - Furnishing &amp; Fixture Payable</option><option value="2720">2720 - Office Equipment Payable</option><option value="2740">2740 - Vehicle Payable</option><option value="2760">2760 - Other Property Payable</option><option value="2800">2800 - Shareholder Loans</option><option value="2900">2900 - Suspense</option><option value="3100">3100 - Capital Stock</option><option value="3200">3200 - Capital Surplus / Dividends</option><option value="3300">3300 - Dividend Taxes Payable</option><option value="3400">3400 - Dividend Taxes Refundable</option><option value="3500">3500 - Retained Earnings</option><option value="4100">4100 - Product / Service Sales</option><option value="4200">4200 - Sales Exchange Gains/Losses</option><option value="4500">4500 - Consulting Services</option><option value="4600">4600 - Rentals</option><option value="4700">4700 - Finance Charge Income</option><option value="4800">4800 - Sales Returns &amp; Allowances</option><option value="4900">4900 - Sales Discounts</option><option value="5000">5000 - Cost of Sales</option><option value="5100">5100 - Production Expenses</option><option value="5200">5200 - Purchases Exchange Gains/Losses</option><option value="5500">5500 - Direct Labour Costs</option><option value="5600">5600 - Freight Charges</option><option value="5700">5700 - Inventory Adjustment</option><option value="5800">5800 - Purchase Returns &amp; Allowances</option><option value="5900">5900 - Purchase Discounts</option><option value="6100">6100 - Advertising</option><option value="6150">6150 - Promotion</option><option value="6200">6200 - Communications</option><option value="6250">6250 - Meeting Expenses</option><option value="6300">6300 - Travelling Expenses</option><option value="6400">6400 - Delivery Expenses</option><option value="6500">6500 - Sales Salaries &amp; Commission</option><option value="6550">6550 - Sales Salaries &amp; Commission Deductions</option><option value="6590">6590 - Benefits</option><option value="6600">6600 - Other Selling Expenses</option><option value="6700">6700 - Permits, Licenses &amp; License Fees</option><option value="6800">6800 - Research &amp; Development</option><option value="6900">6900 - Professional Services</option><option value="7020">7020 - Support Salaries &amp; Wages</option><option value="7030">7030 - Support Salary &amp; Wage Deductions</option><option value="7040">7040 - Management Salaries</option><option value="7050">7050 - Management Salary deductions</option><option value="7060">7060 - Director / Partner Fees</option><option value="7070">7070 - Director / Partner Deductions</option><option value="7080">7080 - Payroll Tax</option><option value="7090">7090 - Benefits</option><option value="7100">7100 - Training &amp; Education Expenses</option><option value="7150">7150 - Dues &amp; Subscriptions</option><option value="7200">7200 - Accounting Fees</option><option value="7210">7210 - Audit Fees</option><option value="7220">7220 - Banking Fees</option><option value="7230">7230 - Credit Card Fees</option><option value="7240">7240 - Consulting Fees</option><option value="7260">7260 - Legal Fees</option><option value="7280">7280 - Other Professional Fees</option><option value="7300">7300 - Business Tax</option><option value="7350">7350 - Property Tax</option><option value="7390">7390 - Corporation Capital Tax</option><option value="7400">7400 - Office Rent</option><option value="7450">7450 - Equipment Rental</option><option value="7500">7500 - Office Supplies</option><option value="7550">7550 - Office Repair &amp; Maintenance</option><option value="7600">7600 - Automotive Expenses</option><option value="7610">7610 - Communication Expenses</option><option value="7620">7620 - Insurance Expenses</option><option value="7630">7630 - Postage &amp; Courier Expenses</option><option value="7640">7640 - Miscellaneous Expenses</option><option value="7650">7650 - Travel Expenses</option><option value="7660">7660 - Utilities</option><option value="7700">7700 - Ammortization Expenses</option><option value="7750">7750 - Depreciation Expenses</option><option value="7800">7800 - Interest Expense</option><option value="7900">7900 - Bad Debt Expense</option><option value="8100">8100 - Gain on Sale of Assets</option><option value="8200">8200 - Interest Income</option><option value="8300">8300 - Recovery on Bad Debt</option><option value="8400">8400 - Other Revenue</option><option value="8500">8500 - Loss on Sale of Assets</option><option value="8600">8600 - Charitable Contributions</option><option value="8900">8900 - Other Expenses</option><option value="9010">9010 - Sales Against Form H</option><option value="9020">9020 - Sales Local</option><option value="9100">9100 - Excise Duty Basic On Sales</option><option value="9110">9110 - Education Cess On Excise Duty (Sales)</option><option value="9120">9120 - Higher Education Cess On Excise Duty (Sales)</option></select></td>
</tr>
<tr>
<td valign="top">Narration :</td>
<td colspan="3"><textarea  cols="30" name="narration"></textarea></td>
</tr>

<tr>
<td colspan="4" align="right">
  <div align="right">
    <input type="button" name="Add"  value="Add"/>
    &nbsp;&nbsp;
    <input type="button" name="Reset" value="Reset" />
    &nbsp;&nbsp;
    <input type="submit" name="save"  value="Save" />
  </div></td>

</tr>
</table>
</div>
<?php

echo '<p><table>';

$SQL = 'SELECT bankaccountname,
		bankaccounts.accountcode,
		bankaccounts.currcode
		FROM bankaccounts,
		chartmaster
		WHERE bankaccounts.accountcode=chartmaster.accountcode';

$ErrMsg = _('The bank accounts could not be retrieved because');
$DbgMsg = _('The SQL used to retrieve the bank accounts was');
$AccountsResults = DB_query($SQL,$db,$ErrMsg,$DbgMsg);

echo '<tr><td>' . _('Bank Account') . ':</td><td><select name="BankAccount">';

if (DB_num_rows($AccountsResults)==0){
	echo '</select></td></tr></table><p>';
	prnMsg( _('Bank Accounts have not yet been defined. You must first') . ' <a href="' . $rootpath . '/BankAccounts.php">' . _('define the bank accounts') . '</a> ' . _('and general ledger accounts to be affected'),'warn');
	include('includes/footer.inc');
	exit;
} else {
	while ($myrow=DB_fetch_array($AccountsResults)){
	/*list the bank account names */
		if (!isset($_POST['BankAccount']) AND $myrow['currcode']==$_SESSION['CompanyRecord']['currencydefault']){
			$_POST['BankAccount']=$myrow['accountcode'];
		}
		if ($_POST['BankAccount']==$myrow['accountcode']){
			echo '<option selectED VALUE="' . $myrow['accountcode'] . '">' . $myrow['bankaccountname'] . ' - ' . $myrow['currcode'];
		} else {
			echo '<option VALUE="' . $myrow['accountcode'] . '">' . $myrow['bankaccountname'] . ' - ' . $myrow['currcode'];
		}
	}
	echo '</select></td></tr>';
}


echo '<tr><td>' . _('Date Paid') . ':</td>
	<td><input type="text" name="DatePaid" class="date" alt="'.$_SESSION['DefaultDateFormat'].'" maxlength=10 size=11 onChange="isDate(this, this.value, '."'".$_SESSION['DefaultDateFormat']."'".')" value="' . $_SESSION['PaymentDetail']->DatePaid . '"></td>
	</tr>';


if ($_SESSION['PaymentDetail']->SupplierID==''){
	echo '<tr><td>' . _('Currency of Payment') . ':</td><td><select name="Currency">';
	$SQL = 'SELECT currency, currabrev, rate FROM currencies';
	$result=DB_query($SQL,$db);

	if (DB_num_rows($result)==0){
		echo '</select></td></tr>';
		prnMsg( _('No currencies are defined yet. Payments cannot be entered until a currency is defined'),'error');
	} else {
		while ($myrow=DB_fetch_array($result)){
		if ($_SESSION['PaymentDetail']->Currency==$myrow['currabrev']){
			echo '<option selected value=' . $myrow['currabrev'] . '>' . $myrow['currency'];
		} else {
			echo '<option value=' . $myrow['currabrev'] . '>' . $myrow['currency'];
		}
		}
		echo '</select></td><td><i>' . _('The transaction currency does not need to be the same as the bank account currency') . '</i></td></tr>';
	}
} else { /*its a supplier payment so it must be in the suppliers currency */
	echo '<tr><td>' . _('Supplier Currency') . ':</td><td>' . $_SESSION['PaymentDetail']->Currency . '</td></tr>';
	echo '<input type="hidden" name="Currency" value="' . $_SESSION['PaymentDetail']->Currency . '">';
	/*get the default rate from the currency table if it has not been set */
	if (!isset($_POST['ExRate']) OR $_POST['ExRate']==''){
		$SQL = "SELECT rate FROM currencies WHERE currabrev='" . $_SESSION['PaymentDetail']->Currency ."'";
		$Result=DB_query($SQL,$db);
		$myrow=DB_fetch_row($Result);
		$_POST['ExRate']=$myrow[0];
	}

}

if (!isset($_POST['ExRate'])){
	$_POST['ExRate']=1;
}

if (!isset($_POST['FunctionalExRate'])){
	$_POST['FunctionalExRate']=1;
}
if ($_SESSION['PaymentDetail']->AccountCurrency!=$_SESSION['PaymentDetail']->Currency AND isset($_SESSION['PaymentDetail']->AccountCurrency)){
	if (isset($SuggestedExRate)){
		$SuggestedExRateText = '<b>' . _('Suggested rate:') . ' ' . number_format($SuggestedExRate,4) . '</b>';
	} else {
		$SuggestedExRateText ='';
	}
	if ($_POST['ExRate']==1 AND isset($SuggestedExRate)){
		$_POST['ExRate'] = $SuggestedExRate;
	}
	echo '<tr><td>' . _('Payment Exchange Rate') . ':</td>
				<td><input type="text" name="ExRate" maxlength=10 size=12 value="' . $_POST['ExRate'] . '"></td>
			<td>' . $SuggestedExRateText . ' <i>' . _('The exchange rate between the currency of the bank account currency and the currency of the payment') . '. 1 ' . $_SESSION['PaymentDetail']->AccountCurrency . ' = ? ' . $_SESSION['PaymentDetail']->Currency . '</i></td></tr>';
}

if ($_SESSION['PaymentDetail']->AccountCurrency!=$_SESSION['CompanyRecord']['currencydefault']
												AND isset($_SESSION['PaymentDetail']->AccountCurrency)){
	if (isset($SuggestedFunctionalExRate)){
		$SuggestedFunctionalExRateText = '<b>' . _('Suggested rate:') . ' ' . number_format($SuggestedFunctionalExRate,4) . '</b>';
	} else {
		$SuggestedFunctionalExRateText ='';
	}
	if ($_POST['FunctionalExRate']==1 AND isset($SuggestedFunctionalExRate)){
		$_POST['FunctionalExRate'] = $SuggestedFunctionalExRate;
	}
	echo '<tr><td>' . _('Functional Exchange Rate') . ':</td><td><input type="text" name="FunctionalExRate" maxlength=10 size=12 value="' . $_POST['FunctionalExRate'] . '"></td>
			<td>' . ' ' . $SuggestedFunctionalExRateText . ' <i>' . _('The exchange rate between the currency of the business (the functional currency) and the currency of the bank account') .  '. 1 ' . $_SESSION['CompanyRecord']['currencydefault'] . ' = ? ' . $_SESSION['PaymentDetail']->AccountCurrency . '</i></td></tr>';
}
echo '<tr><td>' . _('Payment type') . ':</td><td><select name="Paymenttype">';

include('includes/GetPaymentMethods.php');
/* The array Payttypes is set up in includes/GetPaymentMethods.php
payment methods can be modified from the setup tab of the main menu under payment methods*/

foreach ($PaytTypes as $PaytType) {

	if (isset($_POST['Paymenttype']) and $_POST['Paymenttype']==$PaytType){
		echo '<option selected value="' . $PaytType . '">' . $PaytType;
	} else {
		echo '<option Value="' . $PaytType . '">' . $PaytType;
	}
} //end foreach
echo '</select></td></tr>';

if (!isset($_POST['ChequeNum'])) {
	$_POST['ChequeNum']='';
}

echo '<tr><td>' . _('Cheque Number') . ':</td>
		<td><input type="text" name="ChequeNum" maxlength=8 size=10 value="' . $_POST['ChequeNum'] . '"> ' . _('(if using pre-printed stationery)') . '</td></tr>';

if (!isset($_POST['Narrative'])) {
	$_POST['Narrative']='';
}

echo '<tr><td>' . _('Reference / Narrative') . ':</td>
			<td colspan=2><input type="text" name="Narrative" maxlength=80 size=82 value="' . $_POST['Narrative'] . '">  ' . _('(Max. length 80 characters)') . '</td></tr>';
echo '<tr><td colspan=3><div class="centre"><input type="submit" name="UpdateHeader" value="' . _('Update'). '"></td></tr>';


echo '</table><br>';


if ($_SESSION['CompanyRecord']['gllink_creditors']==1 AND $_SESSION['PaymentDetail']->SupplierID==''){
/* Set upthe form for the transaction entry for a GL Payment Analysis item */
	
	echo '<table class="table1" border="1">
	<tr>
			<th>' . _('Prticulas').'</th>
			<th>' . _('Amount') . ' (' . $_SESSION['PaymentDetail']->Currency . ')</th>
			<th>' . _('Credit') . '</th>
			<th>' . _('Debit') . '</th>
			<th>' . _('Action') . '</th>
		</tr>';

	$PaymentTotal = 0;
	foreach ($_SESSION['PaymentDetail']->GLItems as $PaymentItem) {
		$tagsql='SELECT tagdescription from tags where tagref='.$PaymentItem->tag;
		$tagresult=DB_query($tagsql, $db);
		$tagmyrow=DB_fetch_row($tagresult);
		if ($PaymentItem->tag==0) {
			$tagname='None';
		} else {
			$tagname=$tagmyrow[0];
		}
		echo '<tr>
			<td align=left>' . $PaymentItem->cheque . '</td>
			<td align=right>' . number_format($PaymentItem->Amount,2) . '</td>
			<td>' . $PaymentItem->GLCode . ' - ' . $PaymentItem->GLActName . '</td>
			<td>' . stripslashes($PaymentItem->Narrative)  . '</td>
			<td>' . $PaymentItem->tag . ' - ' . $tagname . '</td>
			<td><a href="' . $_SERVER['PHP_SELF'] . '?' . SID . '&Delete=' . $PaymentItem->ID . '">' . _('Delete') . '</a></td>
			</tr>';
		$PaymentTotal += $PaymentItem->Amount;

	}
	echo '<tr><td></td><td align=right><b>' . number_format($PaymentTotal,2) . '</b></td><td></td><td></td><td></td></tr></table>';


	echo '<br><font size=3 color=BLUE><div class="centre">' . _('General Ledger Payment Analysis Entry') . '</div></font><br><table>';

	//Select the tag
	echo '<tr><td>' . _('Select Tag') . ':</td><td><select name="tag">';

	$SQL = 'SELECT tagref,
				tagdescription
		FROM tags
		ORDER BY tagref';

	$result=DB_query($SQL,$db);
	echo '<option value=0>0 - None';
	while ($myrow=DB_fetch_array($result)){
		if (isset($_POST['tag']) and $_POST['tag']==$myrow["tagref"]){
			echo '<option selected value=' . $myrow['tagref'] . '>' . $myrow['tagref'].' - ' .$myrow['tagdescription'];
		} else {
			echo '<option value=' . $myrow['tagref'] . '>' . $myrow['tagref'].' - ' .$myrow['tagdescription'];
		}
	}
	echo '</select></td></tr>';
// End select tag

	/*now set up a GLCode field to select from avaialble GL accounts */
	if (isset($_POST['GLManualCode'])) {
		echo '<tr><td>' . _('Enter GL Account Manually') . ':</td>
			<td><input type=Text Name="GLManualCode" Maxlength=12 size=12onChange="return inArray(this, this.value, GLCode.options,'.
		"'".'The account code '."'".'+ this.value+ '."'".' doesnt exist'."'".')"' .
			' onKeyPress="return restrictToNumbers(this, event)" VALUE='. $_POST['GLManualCode'] .'  ></td></tr>';
	} else {
		echo '<tr><td>' . _('Enter GL Account Manually') . ':</td>
			<td><input type=Text Name="GLManualCode" Maxlength=12 size=12 onChange="return inArray(this, this.value, GLCode.options,'.
		"'".'The account code '."'".'+ this.value+ '."'".' doesnt exist'."'".')"' .
			' onKeyPress="return restrictToNumbers(this, event)"></td></tr>';
	}
	echo '<tr><td>' . _('Select GL Account') . ':</td>
		<td><select name="GLCode" onChange="return assignComboToInput(this,'.'GLManualCode'.')">';

	$SQL = 'SELECT accountcode,
					accountname
			FROM chartmaster
			ORDER BY accountcode';

	$result=DB_query($SQL,$db);
	if (DB_num_rows($result)==0){
		echo '</select></td></tr>';
		prnMsg(_('No General ledger accounts have been set up yet') . ' - ' . _('payments cannot be analysed against GL accounts until the GL accounts are set up'),'error');
	} else {
		while ($myrow=DB_fetch_array($result)){
			if (isset($_POST['GLCode']) and $_POST['GLCode']==$myrow["accountcode"]){
				echo '<option selectED value=' . $myrow['accountcode'] . '>' . $myrow['accountcode'] . ' - ' . $myrow['accountname'];
			} else {
				echo '<option value=' . $myrow['accountcode'] . '>' . $myrow['accountcode'] . ' - ' . $myrow['accountname'];
			}
		}
		echo '</select></td></tr>';
	}

	echo '<tr><td>'. _('Cheque/Voucher Number') .'</td><td><input type="text" name="cheque" Maxlength=12 size=12></td></tr>';

	if (isset($_POST['GLNarrative'])) {
		echo '<tr><td>' . _('GL Narrative') . ':</td><td><input type="text" name="GLNarrative" maxlength=50 size=52 value="' . $_POST['GLNarrative'] . '"></td></tr>';
	} else {
		echo '<tr><td>' . _('GL Narrative') . ':</td><td><input type="text" name="GLNarrative" maxlength=50 size=52></td></tr>';
	}

	if (isset($_POST['GLAmount'])) {
		echo '<tr><td>' . _('Amount') . ' (' . $_SESSION['PaymentDetail']->Currency . '):</td><td><input type=Text Name="GLAmount" Maxlength=12 size=12 onKeyPress="return restrictToNumbers(this, event)"  onChange="numberFormat(this,2)" onFocus="return setTextAlign(this, '."'".'right'."'".')" VALUE=' . $_POST['GLAmount'] . '></td></tr>';
	} else {
		echo '<tr><td>' . _('Amount') . ' (' . $_SESSION['PaymentDetail']->Currency . '):</td><td><input type=Text Name="GLAmount" Maxlength=12 size=12 onKeyPress="return restrictToNumbers(this, event)"  onChange="numberFormat(this,2)" onFocus="return setTextAlign(this, '."'".'right'."'".')"></td></tr>';
	}

	echo '</table>';
	echo '<div class="centre"><input type=submit name="Process" value="' . _('Accept') . '"><input type=submit name="Cancel" value="' . _('Cancel') . '"></div>';

} else {
/*a supplier is selected or the GL link is not active then set out
the fields for entry of receipt amt and disc */


	echo '<table><tr><td>' . _('Amount of Payment') . ' ' . $_SESSION['PaymentDetail']->Currency . ':</td>
					<td><input type="text" name="Amount" maxlength=12 size=13 value=' . $_SESSION['PaymentDetail']->Amount . '></td></tr>';

	if (isset($_SESSION['PaymentDetail']->SupplierID)){ /*So it is a supplier payment so show the discount entry item */
		echo '<tr><td>' . _('Amount of Discount') . ':</td>
					<td><input type="text" name="Discount" maxlength=12 size=13 value=' . $_SESSION['PaymentDetail']->Discount . '></td></tr>';
		echo '<input type="hidden" name="SuppName" value="' . $_SESSION['PaymentDetail']->SuppName . '">';
	} else {
		echo '<input type="hidden" name="discount" Value=0>';
	}
	echo '</table>';

}
echo '<br><br><input type=submit name="CommitBatch" value="' . _('Accept and Process Payment') . '">';
echo '</form>';

include('includes/footer.inc');
?>
<script type="text/javascript">
document.getElementById('paydate').value='<?=date("m.d.y");?>';
</script>