<style>
	table.datatables{ order: 2px solid #444 !important; }
	table.datatables th, .datatables td { border: 1px solid #444 !important; }
	table.datatables th { background-color: #F9F9F9 !important; }
	table.datatables th.header { background-color: #F4F4F4 !important; }
	table.datatables tr.total td,
	table.datatables tr.total th { font-weight: bold !important; }
</style>
<script>
$(document).ready(function() {
	$('td.blk5').prepend('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
	$('td.blk10').prepend('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
});
</script>

<?php
if (!$years) {
	$years[0] = date("Y");
	$years[1] = date("Y") - 1;
}

foreach($years as $y=>$year) {
	$debitTotal[$year] = 0;
	$creditTotal[$year] = 0;
	$debitGrandTotal[$year] = 0;
	$creditGrandTotal[$year] = 0;
	echo "<input type=\"hidden\" id=\"year".$y."\" value=\"".$year."\" />\n";
}
?>
<section class="container">
	<div class="row">
		<div class="col-xs-12 table-responsive">
		TEST
		</div>
	</div>
	
	<div class="row pagebreak">
		<div class="col-xs-12 table-responsive">
			<h3>Kedudukan Kewangan</h3>
			<table width="100%" class="datatables">
				<thead>
					<tr>
						<th>&nbsp;</th>
						<th colspan="2">Tahun : <?php echo ($years && $years[0]) ? $years[0] : NULL; ?></th>
						<th colspan="2">Tahun Sebelum : <?php echo ($years && $years[1]) ? $years[1] : NULL; ?></th>
					</tr>
					<tr>
						<th class="col-sm-8">Perkara</th>
						<?php
						foreach($years as $year) {
							$debitTotal[$year] = 0;
							$creditTotal[$year] = 0;
							?>
							<th class="col-sm-1">Debit</th>
							<th class="col-sm-1">Credit</th>
							<?php
						}
						?>
					</tr>
				</thead>
				
				<tbody>
				<?php
					if ($balance) {
						foreach($balance as $key=>$row) {
							?>
							<tr>
								<td><?php echo $row['acctName']; ?></td>
								<?php
								foreach($years as $y=>$year) {
									$db = ($row['data'][$year] && $row['data'][$year]['openingBalanceDr'] > 0) ? $row['data'][$year]['openingBalanceDr']:'';
									$cr = ($row['data'][$year] && $row['data'][$year]['openingBalanceCr'] > 0) ? $row['data'][$year]['openingBalanceCr']:'';
									$debitTotal[$year] += ($db) ? $db : 0;
									$creditTotal[$year] += ($cr) ? $cr : 0;
									?>
									<td><?php echo ($db) ? number_format($db,2) : NULL; ?></td>
									<td><?php echo ($cr) ? number_format($cr,2) : NULL; ?></td>
									<?php									
								}
								?>								
							</tr>
							<?php
						}
					}
				?>
				</tbody>
				
				<tfoot>
					<tr>
						<th class="text-right">Sub Jumlah</th>
						<?php
						foreach($years as $y=>$year) {
							?>
							<th><?php echo number_format($debitTotal[$year],2); ?></th>
							<th><?php echo number_format($creditTotal[$year],2); ?></th>
							<?php
							$debitGrandTotal[$year] += $debitTotal[$year];
							$creditGrandTotal[$year] += $creditTotal[$year];
						}
						?>
					</tr>
				</tfoot>
			</table>
		</div>
	</div>
	
	<div class="row pagebreak">
		<div class="col-xs-12 table-responsive">
			<h3>Perolehan / Hasil</h3>
			<table width="100%" class="datatables">
				<thead>
					<tr>
						<th>&nbsp;</th>
						<th colspan="2">Tahun : <?php echo ($years && $years[0]) ? $years[0] : NULL; ?></th>
						<th colspan="2">Tahun Sebelum : <?php echo ($years && $years[1]) ? $years[1] : NULL; ?></th>
					</tr>
					<tr>
						<th class="col-sm-8">Perkara</th>
						<?php
						foreach($years as $year) {
							$debitTotal[$year] = 0;
							$creditTotal[$year] = 0;
							?>
							<th class="col-sm-1">Debit</th>
							<th class="col-sm-1">Credit</th>
							<?php
						}
						?>
					</tr>
				</thead>
				
				<tbody>
				<?php
					if ($profit) {
						foreach($profit as $key=>$row) {
							?>
							<tr>
								<td><?php echo $row['acctName']; ?></td>
								<?php
								foreach($years as $y=>$year) {
									$db = ($row['data'][$year] && $row['data'][$year]['openingBalanceDr'] > 0) ? $row['data'][$year]['openingBalanceDr']:'';
									$cr = ($row['data'][$year] && $row['data'][$year]['openingBalanceCr'] > 0) ? $row['data'][$year]['openingBalanceCr']:'';
									$debitTotal[$year] += ($db) ? $db : 0;
									$creditTotal[$year] += ($cr) ? $cr : 0;
									?>
									<td><?php echo ($db) ? number_format($db,2) : NULL; ?></td>
									<td><?php echo ($cr) ? number_format($cr,2) : NULL; ?></td>
									<?php									
								}
								?>								
							</tr>
							<?php
						}
					}
				?>
				</tbody>
				
				<tfoot>
					<tr>
						<th class="text-right">Sub Jumlah</th>
						<?php
						foreach($years as $y=>$year) {
							?>
							<th><?php echo number_format($debitTotal[$year],2); ?></th>
							<th><?php echo number_format($creditTotal[$year],2); ?></th>
							<?php
							$debitGrandTotal[$year] += $debitTotal[$year];
							$creditGrandTotal[$year] += $creditTotal[$year];
						}
						?>
					</tr>
				</tfoot>
			</table>
		</div>
	</div>
	
		
	<div class="row pagebreak">
		<div class="col-xs-12 table-responsive">
			<h3>Jumlah Keseluruhan</h3>
			<table width="100%" class="datatables">
				<thead>
					<tr>
						<th class="col-sm-8">Jumlah Keseluruhan</th>
						<?php
						foreach($years as $year) {
							?>
							<th class="col-sm-1">Debit</th>
							<th class="col-sm-1">Credit</th>
							<?php
						}
						?>
					</tr>
				</thead>
				
				<tbody>
					<tr>
					<td class="col-sm-8" class="text-right">Jumlah</td>
					<?php
					foreach($years as $y=>$year) {
						?>
						<td class="col-sm-1"><?php echo number_format($debitGrandTotal[$year],2); ?></td>
						<td class="col-sm-1"><?php echo number_format($creditGrandTotal[$year],2); ?></td>
						<?php
					}
					?>
					</tr>

				</tbody>
			</table>
		</div>
	</div>
	
	
	<br><br>
	<div class="row">
		<div class="col-xs-12">
			<div class="tableTitle">
					PENGESAHAN
			</div>
			<table class="datatables" width = "100%">
					<tr>
							<th class="subheader" width="25%" colspan="2">JURUAUDIT I</th>
							<th class="subheader" width="25%" colspan="2">JURUAUDIT II</th>
							<th class="subheader" width="25%" colspan="2">DISEMAK</th>
							<th class="subheader" width="25%" colspan="2">DIPERAKUI</th>
					</tr>
					<tr>
							<td class="text-center">TANDATANGAN</td>
							<td class="text-center">TARIKH</td>
							<td class="text-center">TANDATANGAN</td>
							<td class="text-center">TARIKH</td>
							<td class="text-center">TANDATANGAN</td>
							<td class="text-center">TARIKH</td>
							<td class="text-center">TANDATANGAN</td>
							<td class="text-center">TARIKH</td>
					</tr>
					<tr>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
					</tr>
			</table>
		</div>
   </div>
		
</section>
