<?php

/* $Id: GLBalanceSheet.php 5820 2013-02-24 04:28:04Z daintree $*/

/*Through deviousness and cunning, this system allows shows the balance sheets as at the end of any period selected - so first off need to show the input of criteria screen while the user is selecting the period end of the balance date meanwhile the system is posting any unposted transactions */


include ('includes/session.php');
$Title = _('Balance Sheet');
include('includes/SQL_CommonFunctions.php');
include('includes/AccountSectionsDef.php'); // This loads the $Sections variable


$ViewTopic= 'GeneralLedger';
$BookMark = 'BalanceSheet';

if(isset($_GET['group']))
{
	$_POST['BalancePeriodEnd'] = $_GET['periodend'];
}

if (! isset($_POST['BalancePeriodEnd']) or isset($_POST['SelectADifferentPeriod'])){

	/*Show a form to allow input of criteria for TB to show */
	include('includes/header.php');
	echo '<div class="centre"><p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/printer.png" title="'
		. _('Print') . '" alt="" />' . ' ' . _('Balance Sheet') . '</p></div>';
	echo '<div class="page_help_text">'
	. _('Balance Sheet (or statement of financial position) is a summary  of balances. Assets, liabilities and ownership equity are listed as of a specific date, such as the end of its financial year. Of the four basic financial statements, the balance sheet is the only statement which applies to a single point in time.') . '<br />'
	. _('The balance sheet has three parts: assets, liabilities and ownership equity. The main categories of assets are listed first and are followed by the liabilities. The difference between the assets and the liabilities is known as equity or the net assets or the net worth or capital of the company and according to the accounting equation, net worth must equal assets minus liabilities.') . '<br />'
	. _('EMP is an "accrual" based system (not a "cash based" system).  Accrual systems include items when they are invoiced to the customer, and when expenses are owed based on the supplier invoice date.') . '</div>';

	echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
    echo '<div>';
	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
	echo '<table class="selection">
			<tr>
				<td>'._('Select the balance date').':</td>
				<td><select name="BalancePeriodEnd">';

	$periodno=GetPeriod(Date($_SESSION['DefaultDateFormat']));
	$sql = "SELECT lastdate_in_period FROM periods WHERE periodno='".$periodno . "'";
	$result = DB_query($sql);
	$myrow=DB_fetch_array($result);
	$lastdate_in_period=$myrow[0];

	$sql = "SELECT periodno, lastdate_in_period FROM periods ORDER BY periodno DESC";
	$Periods = DB_query($sql);

	while ($myrow=DB_fetch_array($Periods)){
		if( $myrow['periodno']== $periodno){
			echo '<option selected="selected" value="' . $myrow['periodno'] . '">' . ConvertSQLDate($lastdate_in_period) . '</option>';
		} else {
			echo '<option value="' . $myrow['periodno'] . '">' . ConvertSQLDate($myrow['lastdate_in_period'])  . '</option>';
		}
	}

	echo '</select></td></tr>';

	echo '</table>';

	echo '<br />
			<div class="centre">
				<input type="submit" name="ShowBalanceSheet" value="'._('Show on Screen (HTML)').'" />
			</div>';
	echo '<br />
			<div class="centre">
				<input type="submit" name="PrintPDF" value="'._('Produce PDF Report').'" />
			</div>';
	echo '</div></form>';

	/*Now do the posting while the user is thinking about the period to select */
	include ('includes/GLPostings.php');

} elseif (isset($_POST['PrintPDF'])) {
	
	require_once('includes/tcpdf/config/tcpdf_config.php');
	require_once('includes/tcpdf/tcpdf.php');
	
	// create new PDF document
	$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	
	// set margins
	$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
	$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
	$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
	
	// set auto page breaks
	$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
	
	// set some language-dependent strings (optional)
	if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
	    require_once(dirname(__FILE__).'/lang/eng.php');
	    $pdf->setLanguageArray($l);
	}

	// set font
	// $pdf->SetFont('dejavusans', '', 10);
	
	// add a page
	$pdf->AddPage();

	$html = "";
	
	$RetainedEarningsAct = $_SESSION['CompanyRecord']['retainedearnings'];
	
	$sql = "SELECT lastdate_in_period FROM periods WHERE periodno='" . $_POST['BalancePeriodEnd'] . "'";
	$PrdResult = DB_query($sql);
	$myrow = DB_fetch_row($PrdResult);
	$BalanceDate = ConvertSQLDate($myrow[0]);


	$sql="SELECT a.sectioninaccounts, a.groupname
		FROM chartmaster c
		INNER JOIN accountgroups a
		ON c.group_ = a.groupname
		WHERE c.accountcode = '".$RetainedEarningsAct."'";
	$RetainedEarningsResult = DB_query($sql);
	$RetainedEarningsRow = DB_fetch_row($RetainedEarningsResult);
	$RetainedEarningsSection = $RetainedEarningsRow[0];
	$RetainedEarningsAccountGroup = $RetainedEarningsRow[1];

	/*Calculate B/Fwd retained earnings */

	$SQL = "SELECT Sum(CASE WHEN chartdetails.period='" . $_POST['BalancePeriodEnd'] . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS accumprofitbfwd,
			Sum(CASE WHEN chartdetails.period='" . ($_POST['BalancePeriodEnd'] - 12) . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS lyaccumprofitbfwd
		FROM chartmaster INNER JOIN accountgroups
		ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
		ON chartmaster.accountcode= chartdetails.accountcode
		WHERE accountgroups.pandl=1";

	$AccumProfitResult = DB_query($SQL,_('The accumulated profits brought forward could not be calculated by the SQL because'));

	$AccumProfitRow = DB_fetch_array($AccumProfitResult); /*should only be one row returned */

	$SQL = "SELECT accountgroups.sectioninaccounts,
			accountgroups.groupname,
			accountgroups.parentgroupname,
			chartdetails.accountcode,
			chartmaster.accountname,
			Sum(CASE WHEN chartdetails.period='" . $_POST['BalancePeriodEnd'] . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS balancecfwd,
			Sum(CASE WHEN chartdetails.period='" . ($_POST['BalancePeriodEnd'] - 12) . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS lybalancecfwd
		FROM chartmaster INNER JOIN accountgroups
		ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
		ON chartmaster.accountcode= chartdetails.accountcode
		WHERE accountgroups.pandl=0 AND accountgroups.parentgroupname = ''
		GROUP BY accountgroups.sectioninaccounts,
			accountgroups.groupname
		ORDER BY accountgroups.sectioninaccounts,
			accountgroups.sequenceintb,
			accountgroups.groupname,
			chartdetails.accountcode";
	

	$AccountsResult = DB_query($SQL,_('No general ledger accounts were returned by the SQL because'));

	$html = $html . '<div class="invoice">
			<table class="selection">
			<tr>
				<th colspan="3">
					<div class="centre"><b>' .
		_('Balance Sheet as at') . ' ' . $BalanceDate .'</b>
					</div>
				</th>
			</tr>';

	$html = $html . '<tr>
				<th></th>
				<th style="text-align:right">' . $BalanceDate . '</th>
				<th style="text-align:right">' ._('Last Year') . '</th>
			</tr>';

	$k=0; //row colour counter
	$Section='';
	$prevSection = '';
	$SectionBalance = 0;
	$SectionBalanceLY = 0;

	$LYCheckTotal = 0;
	$CheckTotal = 0;

	$ActGrp ='';
	$GroupTotal = 0;
	$LYGroupTotal = 0;


	while ($myrow=DB_fetch_array($AccountsResult)) {
		
		$Section = $myrow['sectioninaccounts'];
		$ActGrp = $myrow['groupname'];
		$GroupTotal = $myrow['balancecfwd'];
		$LYGroupTotal = $myrow['lybalancecfwd'];
		
		if($Section != $prevSection)
		{
			$html = $html . sprintf('<tr><td colspan="3"><hr></td></tr>
						<tr>
					<td><b>%s</b></td>
					<td style="text-align:right">%s</td>
					<td style="text-align:right">%s</td>
				</tr>',
				$Sections[$Section],
				locale_number_format($SectionBalance,$_SESSION['CompanyRecord']['decimalplaces']),
				locale_number_format($SectionBalanceLY,$_SESSION['CompanyRecord']['decimalplaces']));
			
			$SectionBalanceLY = 0;
			$SectionBalance = 0;
			$prevSection = $Section;
		}
		
		if ($myrow['groupname'] == $RetainedEarningsAccountGroup){
			$GroupTotal += $AccumProfitRow['accumprofitbfwd'];
			$LYGroupTotal += $AccumProfitRow['lyaccumprofitbfwd'];
		}
		
		$SectionBalanceLY +=	$LYGroupTotal;
		$SectionBalance	  +=	$GroupTotal;
		$LYCheckTotal	  +=	$LYAccountBalance;
		$CheckTotal  	  +=	$AccountBalance;
		
		$html = $html . sprintf('<tr>
				<td>%s</td>
				<td style="text-align:right">%s</td>
				<td style="text-align:right">%s</td>
			     </tr>',
			$ActGrp,
			locale_number_format($GroupTotal,$_SESSION['CompanyRecord']['decimalplaces']),
			locale_number_format($LYGroupTotal,$_SESSION['CompanyRecord']['decimalplaces'])
			);

	}
	//end of loop
	
	$html = $html . sprintf('<tr>
		<td><b>'._('Check Total').'</b></td>
		<td style="text-align:right">%s</td>
		<td style="text-align:right">%s</td>
		</tr>',
		locale_number_format($CheckTotal,$_SESSION['CompanyRecord']['decimalplaces']),
		locale_number_format($LYCheckTotal,$_SESSION['CompanyRecord']['decimalplaces']));
	

	$pdf->writeHTML($html, true, false, true, false, '');
	// output the HTML content
	$pdf->Output('example_006.pdf', 'I');
	exit;
	
} else {
	
	include('includes/header.php');
	echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
	echo '<div>';
	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
	echo '<input type="hidden" name="BalancePeriodEnd" value="' . $_POST['BalancePeriodEnd'] . '" />';

	$RetainedEarningsAct = $_SESSION['CompanyRecord']['retainedearnings'];
	
	$sql = "SELECT lastdate_in_period FROM periods WHERE periodno='" . $_POST['BalancePeriodEnd'] . "'";
	$PrdResult = DB_query($sql);
	$myrow = DB_fetch_row($PrdResult);
	$BalanceDate = ConvertSQLDate($myrow[0]);


	if(isset($_POST['ShowBalanceSheet']))
	{
		$sql="SELECT a.sectioninaccounts, a.groupname
			FROM chartmaster c
			INNER JOIN accountgroups a
			ON c.group_ = a.groupname
			WHERE c.accountcode = '".$RetainedEarningsAct."'";
		$RetainedEarningsResult = DB_query($sql);
		$RetainedEarningsRow = DB_fetch_row($RetainedEarningsResult);
		$RetainedEarningsSection = $RetainedEarningsRow[0];
		$RetainedEarningsAccountGroup = $RetainedEarningsRow[1];

		/*Calculate B/Fwd retained earnings */
	
		$SQL = "SELECT Sum(CASE WHEN chartdetails.period='" . $_POST['BalancePeriodEnd'] . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS accumprofitbfwd,
				Sum(CASE WHEN chartdetails.period='" . ($_POST['BalancePeriodEnd'] - 12) . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS lyaccumprofitbfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=1";
	
		$AccumProfitResult = DB_query($SQL,_('The accumulated profits brought forward could not be calculated by the SQL because'));
	
		$AccumProfitRow = DB_fetch_array($AccumProfitResult); /*should only be one row returned */
	
		$SQL = "SELECT accountgroups.sectioninaccounts,
				accountgroups.groupname,
				accountgroups.parentgroupname,
				chartdetails.accountcode,
				chartmaster.accountname,
				Sum(CASE WHEN chartdetails.period='" . $_POST['BalancePeriodEnd'] . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS balancecfwd,
				Sum(CASE WHEN chartdetails.period='" . ($_POST['BalancePeriodEnd'] - 12) . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS lybalancecfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=0 AND accountgroups.parentgroupname = ''
			GROUP BY accountgroups.sectioninaccounts,
				accountgroups.groupname
			ORDER BY accountgroups.sectioninaccounts,
				accountgroups.sequenceintb,
				accountgroups.groupname,
				chartdetails.accountcode";
		

		$AccountsResult = DB_query($SQL,_('No general ledger accounts were returned by the SQL because'));
		echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/preview.gif" title="' . _('Search') . '" alt="" /> ' . _('HTML View') . '</p>';
	
		echo '<div class="invoice">
				<table class="selection">
				<tr>
					<th colspan="3">
						<div class="centre"><b>' .
			_('Balance Sheet as at') . ' ' . $BalanceDate .'</b>
						</div>
					</th>
				</tr>';
	
		echo '<tr>
					<th></th>
					<th>' . $BalanceDate . '</th>
					<th>' ._('Last Year') . '</th>
				</tr>';
	
		$k=0; //row colour counter
		$Section='';
		$prevSection = '';
		$SectionBalance = 0;
		$SectionBalanceLY = 0;
	
		$LYCheckTotal = 0;
		$CheckTotal = 0;
	
		$ActGrp ='';
		$GroupTotal = 0;
		$LYGroupTotal = 0;
	
	
		while ($myrow=DB_fetch_array($AccountsResult)) {
			
			$Section = $myrow['sectioninaccounts'];
			$ActGrp = $myrow['groupname'];
			$GroupTotal = $myrow['balancecfwd'];
			$LYGroupTotal = $myrow['lybalancecfwd'];
			
			if($Section != $prevSection)
			{
				printf('<tr>
					<td><b>%s</b></td>
					<td class="number">%s</td>
					<td class="number">%s</td>
				</tr>',
				$Sections[$Section],
				locale_number_format($SectionBalance,$_SESSION['CompanyRecord']['decimalplaces']),
				locale_number_format($SectionBalanceLY,$_SESSION['CompanyRecord']['decimalplaces']));
				
				$SectionBalanceLY = 0;
				$SectionBalance = 0;
				$prevSection = $Section;
			}
			
			if ($myrow['groupname'] == $RetainedEarningsAccountGroup){
				$GroupTotal += $AccumProfitRow['accumprofitbfwd'];
				$LYGroupTotal += $AccumProfitRow['lyaccumprofitbfwd'];
			}
			
			$SectionBalanceLY +=	$LYGroupTotal;
			$SectionBalance	  +=	$GroupTotal;
			$LYCheckTotal	  +=	$LYAccountBalance;
			$CheckTotal  	  +=	$AccountBalance;
			
			printf('<tr>
					<td><a href="%s?group=%s&periodend=%s">%s</a></td>
					<td class="number">%s</td>
					<td class="number">%s</td>
				     </tr>',
				$_SERVER["PHP_SELF"],
				$ActGrp,
				$_POST['BalancePeriodEnd'],
				$ActGrp,
				locale_number_format($GroupTotal,$_SESSION['CompanyRecord']['decimalplaces']),
				locale_number_format($LYGroupTotal,$_SESSION['CompanyRecord']['decimalplaces'])
				);
	
		}
		//end of loop
		
		printf('<tr style="background-color:#ffffff">
			<td><b>'._('Check Total').'</b></td>
			<td class="number">%s</td>
			<td class="number">%s</td>
			</tr>',
			locale_number_format($CheckTotal,$_SESSION['CompanyRecord']['decimalplaces']),
			locale_number_format($LYCheckTotal,$_SESSION['CompanyRecord']['decimalplaces']));
	}
	else if(isset($_GET['group']))
	{
		$SQL = "SELECT Sum(CASE WHEN chartdetails.period='" . $_POST['BalancePeriodEnd'] . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS accumprofitbfwd,
				Sum(CASE WHEN chartdetails.period='" . ($_POST['BalancePeriodEnd'] - 12) . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS lyaccumprofitbfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=1";
	
		$AccumProfitResult = DB_query($SQL,_('The accumulated profits brought forward could not be calculated by the SQL because'));
	
		$AccumProfitRow = DB_fetch_array($AccumProfitResult); /*should only be one row returned */
	
		$SQL = "SELECT accountgroups.sectioninaccounts,
				accountgroups.groupname,
				accountgroups.parentgroupname,
				chartdetails.accountcode,
				chartmaster.accountname,
				Sum(CASE WHEN chartdetails.period='" . $_POST['BalancePeriodEnd'] . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS balancecfwd,
				Sum(CASE WHEN chartdetails.period='" . ($_POST['BalancePeriodEnd'] - 12) . "' THEN chartdetails.bfwd + chartdetails.actual ELSE 0 END) AS lybalancecfwd
			FROM chartmaster INNER JOIN accountgroups
			ON chartmaster.group_ = accountgroups.groupname INNER JOIN chartdetails
			ON chartmaster.accountcode= chartdetails.accountcode
			WHERE accountgroups.pandl=0 AND accountgroups.groupname = '".$_GET['group']."'
			GROUP BY accountgroups.groupname,
				chartdetails.accountcode,
				chartmaster.accountname,
				accountgroups.parentgroupname,
				accountgroups.sequenceintb,
				accountgroups.sectioninaccounts
			ORDER BY accountgroups.sectioninaccounts,
				accountgroups.sequenceintb,
				accountgroups.groupname,
				chartdetails.accountcode";
				
		$AccountsResult = DB_query($SQL,_('No general ledger accounts were returned by the SQL because'));
		echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/preview.gif" title="' . _('Search') . '" alt="" /> ' . _('HTML View') . '</p>';
	
		echo '<div class="invoice">
				<table class="selection">';	
		
		$TableHeader = '<tr>
					<th>'._('Account').'</th>
					<th>'._('Account Name').'</th>
					<th>' . $BalanceDate . '</th>
					<th>'._('Last Year').'</th>
				</tr>';
	
		$k=0; //row colour counter
		$Section='';
		$SectionBalance = 0;
		$SectionBalanceLY = 0;
	
		$LYCheckTotal = 0;
		$CheckTotal = 0;
	
		$ActGrp ='';
		$Level=0;
		$ParentGroups=array();
		$ParentGroups[$Level]='';
		$GroupTotal = array(0);
		$LYGroupTotal = array(0);
	
		echo $TableHeader;
		$j=0; //row counter
	
		while ($myrow=DB_fetch_array($AccountsResult)) {
			
			$AccountBalance = $myrow['balancecfwd'];
			$LYAccountBalance = $myrow['lybalancecfwd'];
	
			if ($myrow['accountcode'] == $RetainedEarningsAct){
				$AccountBalance += $AccumProfitRow['accumprofitbfwd'];
				$LYAccountBalance += $AccumProfitRow['lyaccumprofitbfwd'];
			}
	
			if ($myrow['groupname']!= $ActGrp AND $ActGrp != '') {
				if ($myrow['parentgroupname']!=$ActGrp){
					while ($myrow['groupname']!=$ParentGroups[$Level] AND $Level>0){
						if ($_POST['Detail']=='Detailed'){
							echo '<tr>
									<td colspan="2"></td>
								<td><hr /></td>
									<td></td>
									<td><hr /></td>
									<td></td>
								</tr>';
						}
						printf('<tr>
				      <td colspan="2"><I>%s</I></td>
								  <td class="number">%s</td>
								  <td></td>
								  <td class="number">%s</td>
								</tr>',
								$ParentGroups[$Level],
								locale_number_format($GroupTotal[$Level],$_SESSION['CompanyRecord']['decimalplaces']),
								locale_number_format($LYGroupTotal[$Level],$_SESSION['CompanyRecord']['decimalplaces'])
								);
						$GroupTotal[$Level] = 0;
						$LYGroupTotal[$Level] = 0;
						$ParentGroups[$Level]='';
						$Level--;
						$j++;
					}
	
					printf('<tr>
				  <td colspan="2">%s</td>
							  <td class="number">%s</td>
							  <td></td>
							  <td class="number">%s</td>
							</tr>',
							$ParentGroups[$Level],
							locale_number_format($GroupTotal[$Level],$_SESSION['CompanyRecord']['decimalplaces']),
							locale_number_format($LYGroupTotal[$Level],$_SESSION['CompanyRecord']['decimalplaces']) );
	
					$GroupTotal[$Level] = 0;
					$LYGroupTotal[$Level] = 0;
					$ParentGroups[$Level]='';
					$j++;
				}
			}
			if ($myrow['sectioninaccounts']!= $Section ){
	
				if ($Section!=''){
					if ($_POST['Detail']=='Detailed'){
						echo '<tr>
								<td colspan="2"></td>
								<td><hr /></td>
								<td></td>
								<td><hr /></td>
								<td></td>
							</tr>';
					} else {
						echo '<tr>
								<td colspan="3"></td>
								<td><hr /></td>
								<td></td>
								<td><hr /></td>
							</tr>';
					}
	
					printf('<tr>
								<td colspan="3"><h2>%s</h2></td>
								<td class="number">%s</td>
								<td></td>
								<td class="number">%s</td>
							</tr>',
					$Sections[$Section],
					locale_number_format($SectionBalance,$_SESSION['CompanyRecord']['decimalplaces']),
					locale_number_format($SectionBalanceLY,$_SESSION['CompanyRecord']['decimalplaces']));
					$j++;
				}
				$SectionBalanceLY = 0;
				$SectionBalance = 0;
				$Section = $myrow['sectioninaccounts'];
			}
	
			if ($myrow['groupname']!= $ActGrp){
	
				if ($ActGrp!='' AND $myrow['parentgroupname']==$ActGrp){
					$Level++;
				}
	
				if ($_POST['Detail']=='Detailed'){
					$ActGrp = $myrow['groupname'];
					printf('<tr>
							  <td colspan="6"><h3>%s</h3></td>
							</tr>',
							$myrow['groupname']);
					echo $TableHeader;
				}
				$GroupTotal[$Level]=0;
				$LYGroupTotal[$Level]=0;
				$ActGrp = $myrow['groupname'];
				$ParentGroups[$Level]=$myrow['groupname'];
				$j++;
			}
	
			$SectionBalanceLY +=	$LYAccountBalance;
			$SectionBalance	  +=	$AccountBalance;
			for ($i=0;$i<=$Level;$i++){
				$LYGroupTotal[$i] += $LYAccountBalance;
				$GroupTotal[$i] += $AccountBalance;
			}
			$LYCheckTotal	  +=	$LYAccountBalance;
			$CheckTotal  	  +=	$AccountBalance;
	
	
			
	
				if ($k==1){
					echo '<tr class="OddTableRows">';
					$k=0;
				} else {
					echo '<tr class="EvenTableRows">';
					$k++;
				}
	
				$ActEnquiryURL = '<a href="' . $RootPath . '/GLAccountInquiry.php?Period=' . $_POST['BalancePeriodEnd'] . '&amp;Account=' . $myrow['accountcode'] . '">' . $myrow['accountcode'] . '</a>';
	
				printf('<td>%s</td>
						<td>%s</td>
						<td class="number">%s</td>
						<td class="number">%s</td>
						</tr>',
						$ActEnquiryURL,
						htmlspecialchars($myrow['accountname'],ENT_QUOTES,'UTF-8',false),
						locale_number_format($AccountBalance,$_SESSION['CompanyRecord']['decimalplaces']),
						locale_number_format($LYAccountBalance,$_SESSION['CompanyRecord']['decimalplaces']));
				$j++;
			
		}
		//end of loop
	}
	
	echo '</table>';
	echo '</div>';
	echo '<br /><div class="centre"><input type="submit" name="SelectADifferentPeriod" value="'._('Select A Different Balance Date').'" /></div>';
	echo '</div></form>';
}

include('includes/footer.php');
?>