<!DOCTYPE html>
<html>
	<head>
		<title><?php echo $title; ?></title>
		<base href="<?php echo base_url(); ?>">
		<meta charset="utf-8">
		<!-- Tell the browser to be responsive to screen width -->
		<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

		<link rel="shortcut icon" href="assets/images/favicon.png" type="image/x-icon">

		<!-- Theme style -->
		<link rel="stylesheet" href="assets/AdminLTE-3.2.0/dist/css/adminlte.min.css">

		<!-- Google Font: Source Sans Pro -->
		<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
	</head>

	<body>
		<div class="wrapper">
			<!-- Main content -->
			<section class="content">
				<div class="container-fluid">
					<!-- Small boxes (Stat box) -->
					<div class="row">
						<div class="col-md-12">
							<!-- Invoice content -->
							<div class="invoice p-3 mb-3">
								<!-- title row -->
								<div class="row mb-4">
									<div class="col-12">
										<h2 class="page-header">
											<img src="<?php echo $this->session->company_logo; ?>" width="115" class="img">
											<small class="float-right"><?php echo $this->session->company_name; ?></small>
										</h2>
										<h3 class="text-center">Balance Sheet</h3>
									</div>
								</div>

								<!-- info row -->
								<div class="row invoice-info mb-4">
									<div class="col-md-6 invoice-col">
										<strong>Closing Date:</strong> <?php echo date( 'jS M, Y ', strtotime( date_to_db( $closing_date ) ) ); ?>
									</div>
								</div>

								<!-- Table row -->
								<div class="row">
									<div class="col-md-12">
										<table id="treetable1" class="table table-striped table-bordered bootstrap-datatable datatable">
											<thead>
												<tr>
													<th colspan="4" class="left">Assets</th>
												</tr>
												<tr>
													<th class="col-md-6">Name of Account</th>
													<th class="col-md-2 text-center">Debit</th>
													<th class="col-md-2 text-center">Credit</th>
													<th class="col-md-2 text-center">Balance</th>
												</tr>
											</thead>
											<tbody>
											<?php
												$net     = 0;
												$balance = 0;
												$debit   = 0;
												$credit  = 0;
												foreach ( $assets as $a1 ) {
													//print_r($a1);
													$aa[]    = $a1;
													$debit   = sum_subarrays_by_key( $aa, "debit" );
													$credit  = sum_subarrays_by_key( $aa, "credit" );
													$balance = $debit - $credit;
													$net += $balance;
													if ( $balance > 0 ) {
														$pre = 'Dr';
													} elseif ( $balance == 0 ) {
														$pre = ' ';
													} else {
														$pre = 'Cr';
													}
												?>
												<tr data-tt-id="<?php echo $a1['id']; ?>">
													<td><?php echo $a1['name']; ?></td>
													<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
													<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
													<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
												</tr>
												<?php
													$balance = 0;
													$debit   = 0;
													$credit  = 0;
													unset( $aa );
													if ( isset( $a1['child'] ) ) {
														foreach ( $a1['child'] as $a2 ) {
															$bb[]    = $a2;
															$debit   = sum_subarrays_by_key( $bb, "debit" );
															$credit  = sum_subarrays_by_key( $bb, "credit" );
															$balance = $debit - $credit;
															if ( $balance > 0 ) {
																$pre = 'Dr';
															} elseif ( $balance == 0 ) {
																$pre = ' ';
															} else {
																$pre = 'Cr';
															}
														?>
														<tr data-tt-id="<?php echo $a2['id']; ?>" data-tt-parent-id="<?php echo $a1['id']; ?>">
															<td><?php echo $a2['name']; ?></td>
															<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
															<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
															<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
														</tr>
														<?php
															$balance = 0;
															$debit   = 0;
															$credit  = 0;
															unset( $bb );
															if ( isset( $a2['child'] ) ) {
																foreach ( $a2['child'] as $a3 ) {
																	$cc[]    = $a3;
																	$debit   = sum_subarrays_by_key( $cc, "debit" );
																	$credit  = sum_subarrays_by_key( $cc, "credit" );
																	$balance = $debit - $credit;
																	if ( $balance > 0 ) {
																		$pre = 'Dr';
																	} elseif ( $balance == 0 ) {
																		$pre = ' ';
																	} else {
																		$pre = 'Cr';
																	}
																?>
																<tr data-tt-id="<?php echo $a3['id']; ?>" data-tt-parent-id="<?php echo $a2['id']; ?>">
																	<td><?php echo $a3['name']; ?></td>
																	<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
																	<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
																	<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
																</tr>
																<?php
																	$balance = 0;
																	$debit   = 0;
																	$credit  = 0;
																	unset( $cc );
																	if ( isset( $a3['child'] ) ) {
																		foreach ( $a3['child'] as $a4 ) {
																			$dd[]    = $a4;
																			$debit   = sum_subarrays_by_key( $dd, "debit" );
																			$credit  = sum_subarrays_by_key( $dd, "credit" );
																			$balance = $debit - $credit;
																			if ( $balance > 0 ) {
																				$pre = 'Dr';
																			} elseif ( $balance == 0 ) {
																				$pre = ' ';
																			} else {
																				$pre = 'Cr';
																			}
																		?>
																		<tr data-tt-id="<?php echo $a4['id']; ?>" data-tt-parent-id="<?php echo $a3['id']; ?>">
																			<td><?php echo $a4['name']; ?></td>
																			<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
																			<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
																			<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
																		</tr>
																			<?php
																				$balance = 0;
																				$debit   = 0;
																				$credit  = 0;
																				unset( $dd );
																				if ( isset( $a4['child'] ) ) {
																					foreach ( $a4['child'] as $a5 ) {
																						$ee[]    = $a5;
																						$debit   = sum_subarrays_by_key( $ee, "debit" );
																						$credit  = sum_subarrays_by_key( $ee, "credit" );
																						$balance = $debit - $credit;
																						if ( $balance > 0 ) {
																							$pre = 'Dr';
																						} elseif ( $balance == 0 ) {
																							$pre = ' ';
																						} else {
																							$pre = 'Cr';
																						}
																					?>
																					<tr data-tt-id="<?php echo $a5['id']; ?>" data-tt-parent-id="<?php echo $a4['id']; ?>">
																						<td><?php echo $a5['name']; ?></td>
																						<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
																						<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
																						<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
																					</tr>
																					<?php
																						$balance = 0;
																						$debit   = 0;
																						$credit  = 0;
																						unset( $ee );
																					}
																				}
																			}
																		}
																	}
																}
															}
														}
													}
												?>
											</tbody>
											<tfoot>
											<?php
												if ( $net > 0 ) {
													$npre = 'Dr';
												} elseif ( $net == 0 ) {
													$npre = ' ';
												} else {
													$npre = 'Cr';
												}
												?>
												<tr>
													<td colspan="3"><b>Net Balance</b></td>
													<td class="text-right"><?php echo number_format( abs( $net ?? 0 ), 2 ) . ' ' . $npre; ?></td>
												</tr>
											</tfoot>
										</table>
									</div>
								</div>

								<!-- Table row -->
								<div class="row">
									<div class="col-md-12">
										<table id="treetable2" class="table table-striped table-bordered bootstrap-datatable datatable">
											<thead>
												<tr>
													<th colspan="4" class="left">Liability</th>
												</tr>
												<tr>
													<th class="col-md-6">Name of Account</th>
													<th class="col-md-2 text-center">Debit</th>
													<th class="col-md-2 text-center">Credit</th>
													<th class="col-md-2 text-center">Balance</th>
												</tr>
											</thead>
											<tbody>
											<?php
												$net     = 0;
												$balance = 0;
												$debit   = 0;
												$credit  = 0;
												foreach ( $liabilities as $a1 ) {
													$aa[]    = $a1;
													$debit   = sum_subarrays_by_key( $aa, "debit" );
													$credit  = sum_subarrays_by_key( $aa, "credit" );
													$balance = $debit - $credit;
													$net += $balance;
													if ( $balance > 0 ) {
														$pre = 'Dr';
													} elseif ( $balance == 0 ) {
														$pre = ' ';
													} else {
														$pre = 'Cr';
													}
												?>
												<tr data-tt-id="<?php echo $a1['id']; ?>">
													<td><?php echo $a1['name']; ?></td>
													<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
													<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
													<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
												</tr>
												<?php
													$balance = 0;
													$debit   = 0;
													$credit  = 0;
													unset( $aa );
													if ( isset( $a1['child'] ) ) {
														foreach ( $a1['child'] as $a2 ) {
															$bb[]    = $a2;
															$debit   = sum_subarrays_by_key( $bb, "debit" );
															$credit  = sum_subarrays_by_key( $bb, "credit" );
															$balance = $debit - $credit;
															if ( $balance > 0 ) {
																$pre = 'Dr';
															} elseif ( $balance == 0 ) {
																$pre = ' ';
															} else {
																$pre = 'Cr';
															}
														?>
														<tr data-tt-id="<?php echo $a2['id']; ?>" data-tt-parent-id="<?php echo $a1['id']; ?>">
															<td><?php echo $a2['name']; ?></td>
															<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
															<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
															<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
														</tr>
														<?php
															$balance = 0;
															$debit   = 0;
															$credit  = 0;
															unset( $bb );
															if ( isset( $a2['child'] ) ) {
																foreach ( $a2['child'] as $a3 ) {
																	$cc[]    = $a3;
																	$debit   = sum_subarrays_by_key( $cc, "debit" );
																	$credit  = sum_subarrays_by_key( $cc, "credit" );
																	$balance = $debit - $credit;
																	if ( $balance > 0 ) {
																		$pre = 'Dr';
																	} elseif ( $balance == 0 ) {
																		$pre = ' ';
																	} else {
																		$pre = 'Cr';
																	}
																?>
																<tr data-tt-id="<?php echo $a3['id']; ?>" data-tt-parent-id="<?php echo $a2['id']; ?>">
																	<td><?php echo $a3['name']; ?></td>
																	<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
																	<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
																	<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
																</tr>
																<?php
																	$balance = 0;
																	$debit   = 0;
																	$credit  = 0;
																	unset( $cc );
																	if ( isset( $a3['child'] ) ) {
																		foreach ( $a3['child'] as $a4 ) {
																			$dd[]    = $a4;
																			$debit   = sum_subarrays_by_key( $dd, "debit" );
																			$credit  = sum_subarrays_by_key( $dd, "credit" );
																			$balance = $debit - $credit;
																			if ( $balance > 0 ) {
																				$pre = 'Dr';
																			} elseif ( $balance == 0 ) {
																				$pre = ' ';
																			} else {
																				$pre = 'Cr';
																			}
																		?>
																		<tr data-tt-id="<?php echo $a4['id']; ?>" data-tt-parent-id="<?php echo $a3['id']; ?>">
																			<td><?php echo $a4['name']; ?></td>
																			<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
																			<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
																			<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
																		</tr>
																		<?php
																			$balance = 0;
																			$debit   = 0;
																			$credit  = 0;
																			unset( $dd );
																			if ( isset( $a4['child'] ) ) {
																				foreach ( $a4['child'] as $a5 ) {
																					$ee[]    = $a5;
																					$debit   = sum_subarrays_by_key( $ee, "debit" );
																					$credit  = sum_subarrays_by_key( $ee, "credit" );
																					$balance = $debit - $credit;
																					if ( $balance > 0 ) {
																						$pre = 'Dr';
																					} elseif ( $balance == 0 ) {
																						$pre = ' ';
																					} else {
																						$pre = 'Cr';
																					}
																				?>
																				<tr data-tt-id="<?php echo $a5['id']; ?>" data-tt-parent-id="<?php echo $a4['id']; ?>">
																					<td><?php echo $a5['name']; ?></td>
																					<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
																					<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
																					<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
																				</tr>
																				<?php
																					$balance = 0;
																					$debit   = 0;
																					$credit  = 0;
																					unset( $ee );
																				}
																			}
																		}
																	}
																}
															}
														}
													}
												}
												?>
											</tbody>
											<tfoot>
											<?php
												if ( $net > 0 ) {
													$npre = 'Dr';
												} elseif ( $net == 0 ) {
													$npre = ' ';
												} else {
													$npre = 'Cr';
												}
											?>
												<tr>
													<td colspan="3"><b>Net Balance</b></td>
													<td class="text-right"><?php echo number_format( abs( $net ?? 0 ), 2 ) . ' ' . $npre; ?></td>
												</tr>
											</tfoot>
										</table>
									</div>
								</div>

								<!-- Table row -->
								<div class="row">
									<div class="col-md-12">
										<table id="treetable3" class="table table-striped table-bordered bootstrap-datatable datatable">
											<thead>
												<tr>
													<th colspan="4" class="left">Equity/Capital</th>
												</tr>
												<tr>
													<th class="col-md-6">Name of Account</th>
													<th class="col-md-2 text-center">Debit</th>
													<th class="col-md-2 text-center">Credit</th>
													<th class="col-md-2 text-center">Balance</th>
												</tr>
											</thead>
											<tbody>
											<?php
												$net     = 0;
												$balance = 0;
												$debit   = 0;
												$credit  = 0;
												foreach ( $equities as $a1 ) {
													$aa[]    = $a1;
													$debit   = sum_subarrays_by_key( $aa, "debit" );
													$credit  = sum_subarrays_by_key( $aa, "credit" );
													$balance = $debit - $credit;
													$net += $balance;
													if ( $balance > 0 ) {
														$pre = 'Dr';
													} elseif ( $balance == 0 ) {
														$pre = ' ';
													} else {
														$pre = 'Cr';
													}
												?>
												<tr data-tt-id="<?php echo $a1['id']; ?>">
													<td><?php echo $a1['name']; ?></td>
													<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
													<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
													<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
												</tr>
												<?php
													$balance = 0;
													$debit   = 0;
													$credit  = 0;
													unset( $aa );
													if ( isset( $a1['child'] ) ) {
														foreach ( $a1['child'] as $a2 ) {
															$bb[]    = $a2;
															$debit   = sum_subarrays_by_key( $bb, "debit" );
															$credit  = sum_subarrays_by_key( $bb, "credit" );
															$balance = $debit - $credit;
															if ( $balance > 0 ) {
																$pre = 'Dr';
															} elseif ( $balance == 0 ) {
																$pre = ' ';
															} else {
																$pre = 'Cr';
															}
														?>
														<tr data-tt-id="<?php echo $a2['id']; ?>" data-tt-parent-id="<?php echo $a1['id']; ?>">
															<td><?php echo $a2['name']; ?></td>
															<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
															<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
															<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
														</tr>
														<?php
															$balance = 0;
															$debit   = 0;
															$credit  = 0;
															unset( $bb );
															if ( isset( $a2['child'] ) ) {
																foreach ( $a2['child'] as $a3 ) {
																	$cc[]    = $a3;
																	$debit   = sum_subarrays_by_key( $cc, "debit" );
																	$credit  = sum_subarrays_by_key( $cc, "credit" );
																	$balance = $debit - $credit;
																	if ( $balance > 0 ) {
																		$pre = 'Dr';
																	} elseif ( $balance == 0 ) {
																		$pre = ' ';
																	} else {
																		$pre = 'Cr';
																	}
																?>
																<tr data-tt-id="<?php echo $a3['id']; ?>" data-tt-parent-id="<?php echo $a2['id']; ?>">
																	<td><?php echo $a3['name']; ?></td>
																	<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
																	<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
																	<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
																</tr>
																<?php
																	$balance = 0;
																	$debit   = 0;
																	$credit  = 0;
																	unset( $cc );
																	if ( isset( $a3['child'] ) ) {
																		foreach ( $a3['child'] as $a4 ) {
																			$dd[]    = $a4;
																			$debit   = sum_subarrays_by_key( $dd, "debit" );
																			$credit  = sum_subarrays_by_key( $dd, "credit" );
																			$balance = $debit - $credit;
																			if ( $balance > 0 ) {
																				$pre = 'Dr';
																			} elseif ( $balance == 0 ) {
																				$pre = ' ';
																			} else {
																				$pre = 'Cr';
																			}
																		?>
																		<tr data-tt-id="<?php echo $a4['id']; ?>" data-tt-parent-id="<?php echo $a3['id']; ?>">
																			<td><?php echo $a4['name']; ?></td>
																			<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
																			<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
																			<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
																		</tr>
																		<?php
																			$balance = 0;
																			$debit   = 0;
																			$credit  = 0;
																			unset( $dd );
																			if ( isset( $a4['child'] ) ) {
																				foreach ( $a4['child'] as $a5 ) {
																					$ee[]    = $a5;
																					$debit   = sum_subarrays_by_key( $ee, "debit" );
																					$credit  = sum_subarrays_by_key( $ee, "credit" );
																					$balance = $debit - $credit;
																					if ( $balance > 0 ) {
																						$pre = 'Dr';
																					} elseif ( $balance == 0 ) {
																						$pre = ' ';
																					} else {
																						$pre = 'Cr';
																					}
																				?>
																				<tr data-tt-id="<?php echo $a5['id']; ?>" data-tt-parent-id="<?php echo $a4['id']; ?>">
																					<td><?php echo $a5['name']; ?></td>
																					<td class="text-right"><?php echo number_format( $debit ?? 0, 2 ); ?></td>
																					<td class="text-right"><?php echo number_format( $credit ?? 0, 2 ); ?></td>
																					<td class="text-right"><?php echo number_format( abs( $balance ?? 0 ), 2 ) . ' ' . $pre; ?></td>
																				</tr>
																					<?php
																						$balance = 0;
																						$debit   = 0;
																						$credit  = 0;
																						unset( $ee );
																					}
																				}
																			}
																		}
																	}
																}
															}
														}
													}
												?>
											</tbody>
											<tfoot>
												<?php
													if ( $net > 0 ) {
														$npre = 'Dr';
													} elseif ( $net == 0 ) {
														$npre = ' ';
													} else {
														$npre = 'Cr';
													}
												?>
												<tr>
													<td colspan="3"><b>Net Balance</b></td>
													<td class="text-right"><?php echo number_format( abs( $net ?? 0 ), 2 ) . ' ' . $npre; ?></td>
												</tr>
											</tfoot>
										</table>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</section>
		</div>

		<!-- jQuery 3.3.1 -->
		<script src="assets/AdminLTE-3.2.0/plugins/jquery/jquery.min.js"></script>
		<script src="assets/plugin/treetable/js/jquery.treetable.js"></script>
		<script>
			$("#treetable1").treetable({expandable: true});
			$("#treetable2").treetable({expandable: true});
			$("#treetable3").treetable({expandable: true});
		</script>
	</body>
</html>