<?php
session_start();
require_once("../../includes/display_internal_user_dryout.inc.php");
require_once("../../includes/functions_internal_user_dryout.inc.php");
$obj = new data();
$connect = $obj->funConnect();
$display = new display();
$display->logout_iu();
$pagename = basename($_SERVER['PHP_SELF']);
$msg = NULL;
if (!empty($_REQUEST['selFYear'])) {
	$selFYear = $_REQUEST['selFYear'];
} else {
	$selFYear = $obj->getLatestAccountingFyearid();
}
$fyearstartdate = $obj->getAccountingFyearDataBasedOnFyearid($selFYear, 'FSTARTDATE');
$fyearenddate = $obj->getAccountingFyearDataBasedOnFyearid($selFYear, 'FENDDATE');

$vouchertype = array(1);
$vouchertype1 = array(2);
$liabilitiesum = 0;
$debitamount = 0;
$assetssum = 0;
$creditamount = 0;
?>

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title><?php $display->title(); ?></title>
	<?php $display->favicon(); ?>
	<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
	<?php $display->css(); ?>
	<link href="../../css/datatables/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />
	<?php $display->js(); ?>
	<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
	<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
	<!--[if lt IE 9]>
  <script src="../js/html5shiv.js"></script>
  <script src="../js/respond.min.js"></script>
<![endif]-->
	<style>
		.benchclass {
			background-color: #f39c12;
			color: #ffffff;
		}
	</style>
</head>

<body class="skin-blue fixed">
	<!-- header logo: style can be found in header.less -->
	<?php $display->header_tag(); ?>
	<div class="wrapper row-offcanvas row-offcanvas-left">
		<?php $display->leftmenu(20, 3); ?>
		<aside class="right-side">
			<!-- Content Header (Page header) -->
			<section class="content-header">
				<h1>
					<i class="fa fa-usd"></i>&nbsp;7Oceans Accounting&nbsp;&nbsp;&nbsp;&nbsp;<small>
						<div class="headline text-center" id="time" style="color:#000; font-weight:bold; font-size:12px;"></div>
					</small>
				</h1>
				<ol class="breadcrumb">
					<li><a href="../"><i class="fa fa-home"></i> Home</a></li>
					<li class="active">Trial balance</li>
				</ol>
			</section>

			<!-- Main content -->
			<section class="content">
				<!--   content put here..................-->

				<div class="box box-primary">
					<h3 style=" text-align:center;">Trial balance</h3>
					<form name="frm1" id="frm1" enctype="multipart/form-data" action="<?php echo $pagename; ?>" />
					<div class="row invoice-info">
						<div class="col-md-4 invoice-col" style=" text-align:center;">
							<strong>&nbsp;</strong>
						</div>
						<div class="col-md-4 invoice-col" style=" text-align:center;">
							<select name="selFYear" id="selFYear" class="form-control" onChange="getSubmit();">
								<?php $obj->getAccountingFyearList($selFYear); ?>
							</select>
						</div>
						<div class="col-md-4 invoice-col" style=" text-align:center;">
							<strong>&nbsp;</strong>
						</div>
					</div>
					<div class="row invoice-info">
						<div class="col-md-12 invoice-col">
							<div class="box-body table-responsive" style="overflow:auto;">
								<table class="table" width="100%">
									<thead>
										<tr>
											<th rowspan="3" align="left" valign="middle" width="70%">Particulars</th>
											<th colspan="2" valign="middle" style="border-left:1px solid; text-align:center;">Closing Balance</th>
										</tr>
										<tr>
											<th colspan="2" valign="middle" style="border-left:1px solid;text-align:center;"><?php echo date('d-M-Y', strtotime($fyearstartdate)) . ' To ' . date('d-M-Y', strtotime($fyearenddate)) ?></th>
										</tr>
										<tr>
											<th valign="middle" width="15%" style="border-left:1px solid; text-align:center;">Debit</th>
											<th valign="middle" width="15%" style="border-left:1px solid; text-align:center;">Credit</th>
										</tr>
									</thead>
									<tbody>
										<?php $sql = "select GROUPID, GROUP_NAME, GROUP_CODE from accounting_groups where PARENTID='2'";
										$result = mysql_query($sql);
										while ($rows = mysql_fetch_assoc($result)) {
											$groupid = $obj->getSubGroupIdsBasedToGroupID($rows['GROUPID']);
											if (empty($groupid)) {

												$href = 'ledgerbalancesheet.php?groupid=' . $rows['GROUPID'] . '&amttype=CREDIT_AMOUNT&page=7&selFYear=' . $selFYear;
											} else {
												$href = 'groupbalancesheet.php?groupid=' . $rows['GROUPID'] . '&amttype=CREDIT_AMOUNT&page=7&selFYear=' . $selFYear;
											}
											$groupid[] = $rows['GROUPID'];
											$ledgerids = $obj->getLedgerIdsBasedToGroupID($groupid, 2);

											$amount = $obj->getLedgerIdBalanceBasedOnLedgeridsArr($ledgerids, 'CREDIT_AMOUNT', $fyearstartdate, $fyearenddate, 'ALL');
											$liabilitiesum = $liabilitiesum + $amount;
											$creditamount = $creditamount + $amount;
										?>
											<tr>
												<td align="left" valign="middle" width="70%"><a href="<?= $href; ?>" style="font-size:14px;"><?= $rows['GROUP_CODE']; ?> - <?= $rows['GROUP_NAME']; ?></a></td>
												<td style="text-align:right;border-left:1px solid;" valign="middle" width="15%"></td>
												<td align="left" valign="middle" width="15%" style="border-left:1px solid;text-align:right;"><strong><?= number_format($amount, 2, '.', ','); ?></strong></td>
											</tr>
										<?php } ?>
										<?php $sql = "select GROUPID, GROUP_NAME, GROUP_CODE from accounting_groups where PARENTID='1'";
										$result = mysql_query($sql);
										while ($rows = mysql_fetch_assoc($result)) {
											$groupid = $obj->getSubGroupIdsBasedToGroupID($rows['GROUPID']);
											if (empty($groupid)) {
												$href = 'ledgerbalancesheet.php?groupid=' . $rows['GROUPID'] . '&amttype=DEBIT_AMOUNT&page=7&selFYear=' . $selFYear;
											} else {
												$href = 'groupbalancesheet.php?groupid=' . $rows['GROUPID'] . '&amttype=DEBIT_AMOUNT&page=7&selFYear=' . $selFYear;
											}
											$groupid[] = $rows['GROUPID'];
											$ledgerids = $obj->getLedgerIdsBasedToGroupID($groupid, 0);
											$ledgerids2 = $obj->getLedgerIdsBasedToGroupID($groupid, 1);
											$amount1 = $obj->getLedgerIdBalanceBasedOnLedgeridsArr($ledgerids, 'DEBIT_AMOUNT', $fyearstartdate, $fyearenddate, 'ALL');
											$amount2 = $obj->getLedgerIdBalanceBasedOnLedgeridsArr1($ledgerids2, 'DEBIT_AMOUNT', $fyearstartdate, $fyearenddate, 'ALL');
											$assetssum = $assetssum + $amount1 + $amount2;
											$debitamount = $debitamount + $amount1 + $amount2;
										?>
											<tr>
												<td align="left" valign="middle" width="70%"><a href="<?= $href; ?>" style="font-size:14px;"><?= $rows['GROUP_CODE']; ?> - <?= $rows['GROUP_NAME']; ?></a></td>
												<td style="text-align:right;border-left:1px solid;" valign="middle" width="15%"><strong><?= number_format(($amount1 + $amount2), 2, '.', ','); ?></strong></td>
												<td align="left" valign="middle" width="15%" style="border-left:1px solid;text-align:right;"></td>
											</tr>
										<?php } ?>

										<tr>
											<td align="left" valign="middle" width="70%"><a href="getP_Lledgerlist.php?type=CREDIT_AMOUNT&selFYear=<?= $selFYear; ?>&ledgerid=''&page=8" style="font-size:14px;">Direct Incomes</a></td>
											<td style="text-align:right;border-left:1px solid;" valign="middle" width="15%"></td>
											<?php
											$directincome = $obj->getDebitCreditAmount('CREDIT_AMOUNT', $fyearstartdate, $fyearenddate, $vouchertype1, '');
											$creditamount = $creditamount + $directincome;
											?>
											<td align="left" valign="middle" width="15%" style="border-left:1px solid;text-align:right;"><strong><?= number_format($directincome, 2, '.', ','); ?></strong></td>
										</tr>

										<tr>
											<td align="left" valign="middle" width="70%"><a href="getP_Lledgerlist.php?type=DEBIT_AMOUNT&selFYear=<?= $selFYear; ?>&ledgerid=''&page=8" style="font-size:14px;">Direct Expenses</a></td>
											<?php
											$directexpense = $obj->getDebitCreditAmount('DEBIT_AMOUNT', $fyearstartdate, $fyearenddate, $vouchertype, '');
											$debitamount = $debitamount + $directexpense;
											?>
											<td style="text-align:right;border-left:1px solid;" valign="middle" width="15%"><strong><?= number_format($directexpense, 2, '.', ','); ?></strong></td>
											<td align="left" valign="middle" width="15%" style="border-left:1px solid;text-align:right;"></td>
										</tr>

										<tr>
											<td align="left" valign="middle" width="70%">Difference In Opening balance</td>
											<?php
											$differenceopeningbalance = $debitamount - $creditamount;
											if ($differenceopeningbalance > 0) {
												$creditamount = $creditamount + $differenceopeningbalance; ?>
												<td style="text-align:right;border-left:1px solid;" valign="middle" width="15%"></td>
												<td align="left" valign="middle" width="15%" style="border-left:1px solid;text-align:right;"><strong><?= number_format($differenceopeningbalance, 2, '.', ','); ?></strong></td>
											<?php } else {
												$debitamount = $debitamount + ($creditamount - $debitamount); ?>
												<td style="text-align:right;border-left:1px solid;" valign="middle" width="15%"><strong><?= number_format(($creditamount - $debitamount), 2, '.', ','); ?></strong></td>
												<td align="left" valign="middle" width="15%" style="border-left:1px solid;text-align:right;"></td>
											<?php } ?>
										</tr>
									</tbody>
									<tfoot>
										<tr>
											<td width="70%"><strong>Total</strong></td>
											<td width="15%" style="text-align:right;border-left:1px solid;"><strong><?= number_format($debitamount, 2, '.', ','); ?>&nbsp;&nbsp;</strong></td>
											<td width="15%" style="text-align:right;border-left:1px solid;"><strong><?= number_format($creditamount, 2, '.', ','); ?>&nbsp;&nbsp;</strong></td>
										</tr>
									</tfoot>
								</table>
							</div>
						</div>
					</div>
					<div style="clear:both;"></div>
					</form>
				</div>


				<!--   content ends here..................-->
			</section><!-- /.content -->
		</aside><!-- /.right-side -->


	</div><!-- ./wrapper -->
	<?php $display->footer(); ?>
	<script src="../../js/timer.js" type="text/javascript"></script>
	<link href="../../css/jquery.alerts.css" rel="stylesheet" type="text/css" />
	<link href="../../css/datatables/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />
	<script src='../../js/plugins/datatables/jquery.dataTables.min.js'></script>
	<script src='../../js/jquery.autosize.js'></script>
	<script language="JavaScript" type="text/javascript" src="../../js/jquery.alerts.js"></script>
	<script type="text/javascript">
		$(document).ready(function() {

		});

		function getSubmit() {
			document.frm1.submit();
		}
	</script>

</body>

</html>