<?php
/**
* @last-update: Zul Zolkaffly (24/08/2016)
*								- Changed layout to follow AdminLTE markup
*								- All variables and calculation algorithms are not changed
*/
?>
<div class="box box-warning">
	<div class="box-header with-border">
		<h3 class="box-title"><?php echo $this->lang->line('statFinPos') ?></h3>
		<div class="box-tools pull-right">
			<button class="btn btn-success btn-sm" onClick="do_print()"><?php echo $this->lang->line('print') ?></button>
			<button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
		</div>
	</div>
	
	<div class="box-body">
		<table class="table table-bordered table-striped datatables" cellpadding="0" cellspacing="0" width="100%">
			<thead>
				<tr>
					<th><?php echo $this->lang->line('description') ?></th>
					<th width="20%" class="tar"><h4><?php echo $selyear;//$this->lang->line('thisyear') ?></h4></th>
					<th class="<?php echo $activeTab == 1?'dn':''; ?>" width="10px">&nbsp;</th>
					<th class="tar <?php echo $activeTab == 1?'dn':''; ?>" width="20%"><h4><?php echo $selyear-1;//$this->lang->line('lastyear') ?></h4></th>
				</tr>
			</thead>
			
			<tbody>
				<?php
				if (isset($accountGroup) && !empty($accountdetails)) {
					foreach ($accountGroup as $datatbl) {
						$total_thisyear = 0;
						$total_lastyear = 0;
						$thisyear_amount = 0;
						$lastyear_amount = 0;
						$tyear_amount = 0;
						$lyear_amount = 0;
						
						if ($datatbl->title == 1) {
							?>
							<tr>
								<td colspan="4"><h5><?php echo $this->lang->line($datatbl->acctGroupName) ?></h5></td>
							</tr>
							<?php
						}
						
						foreach ($groupdetails as $rowdet) {
							$rowdet_thisyeartotal = 0;
							$rowdet_lastyeartotal = 0;
							if ($rowdet->parentID == $datatbl->ID) {
								if ($rowdet->title == 1) {
									?>
									<tr>
										<td colspan="4">&nbsp;&nbsp;&nbsp;<strong><?php echo $this->lang->line($rowdet->acctGroupName) ?></strong></td>
									</tr>
									<?php
								}

								foreach ($groupdetails as $rowdet1) {
									$rowdet1_thisyeartotal = 0;
									$rowdet1_lastyeartotal = 0;
									if ($rowdet1->parentID == $rowdet->ID) {
										if ($rowdet1->title == 1) {
											?>
											<tr>
												<td colspan="4">&nbsp;&nbsp;&nbsp;<strong><?php echo $this->lang->line($rowdet1->acctGroupName) ?></strong></td>
											</tr>
											<?php
										}
										
										if ($accountdetails) {
											foreach ($accountdetails as $row) {
												if ($row->acctGroupID == $rowdet1->ID) {
													if ($row->lastyeardebit == 0 && $row->lastyearcredit == 0 && $row->currentyeardebit == 0 && $row->currentyearcredit == 0) {
													} else {
														$tyear_amount = ($row->lastyeardebit - $row->lastyearcredit) + ($row->currentyeardebit - $row->currentyearcredit);
														$lyear_amount = $row->lastyeardebit - $row->lastyearcredit;
														
														if (floatval($tyear_amount) == 0 && floatval($lyear_amount) == 0) continue;
														?>
														<tr>
															<td style="display:none" ><?php echo $row->ID; ?></td>
															<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="<?php echo base_url(); ?>glreports/accountDetailsfc/<?php echo $row->ID; ?>/<?php echo $selyear; ?>/<?php echo $selperiod; ?>" title="<?php echo $this->lang->line('details'); ?>" class="btn btn-link edit"><?php echo substr($row->acctCode, 0, 4) . '-' . substr($row->acctCode, 4, 3) . '-' . substr($row->acctCode, 7, 2) . ' [ ' . $row->acctName . ' ]' ?></td>
															<?php
															if ($rowdet1->status == 1) {
																?>
																<td class="tar"><?php echo ($tyear_amount <= 0 ) ? number_format($tyear_amount * -1, 2) : '(' . number_format($tyear_amount, 2) . ')' ?></td>
																<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
																<td class="tar <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($lyear_amount <= 0 ) ? number_format($lyear_amount * -1, 2) : '(' . number_format($lyear_amount, 2) . ')' ?></td>
																<?php
															} else {
																?>
																<td class="tar"><?php echo ($tyear_amount >= 0 ) ? number_format($tyear_amount, 2) : '(' . number_format($tyear_amount * -1, 2) . ')' ?></td>
																<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
																<td class="tar <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($lyear_amount >= 0 ) ? number_format($lyear_amount, 2) : '(' . number_format($lyear_amount * -1, 2) . ')' ?></td>
																<?php
															}
															?>
														</tr>
														
														<?php
															$rowdet1_thisyeartotal = $rowdet1_thisyeartotal + ($row->lastyeardebit - $row->lastyearcredit) + ($row->currentyeardebit - $row->currentyearcredit);
															$rowdet1_lastyeartotal = $rowdet1_lastyeartotal + ($row->lastyeardebit - $row->lastyearcredit);
															$rowdet_thisyeartotal = $rowdet_thisyeartotal + ($row->lastyeardebit - $row->lastyearcredit) + ($row->currentyeardebit - $row->currentyearcredit);
															$rowdet_lastyeartotal = $rowdet_lastyeartotal + ($row->lastyeardebit - $row->lastyearcredit);
															$total_thisyear = $total_thisyear + ($row->lastyeardebit - $row->lastyearcredit) + ($row->currentyeardebit - $row->currentyearcredit);
															$total_lastyear = $total_lastyear + ($row->lastyeardebit - $row->lastyearcredit);
													} 
												}
											}
										}
										?>
										
										
										<tr>
											<td>&nbsp;&nbsp;&nbsp;<?php echo $this->lang->line('subtotal') ?> <?php echo $this->lang->line($rowdet1->acctGroupName) ?> </td>
											<?php
											if ($rowdet1->status == 1) {
												?>
												<td class="tar sep"><?php echo ($rowdet1_thisyeartotal <= 0 ) ? number_format($rowdet1_thisyeartotal * -1, 2) : '(' . number_format($rowdet1_thisyeartotal, 2) . ')' ?></td>
												<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
												<td class="tar sep <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($rowdet1_lastyeartotal <= 0 ) ? number_format($rowdet1_lastyeartotal * -1, 2) : '(' . number_format($rowdet1_lastyeartotal, 2) . ')' ?></td>
												<?php 
											} else { 
												?>
												<td class="tar sep"><?php echo ($rowdet1_thisyeartotal >= 0 ) ? number_format($rowdet1_thisyeartotal, 2) : '(' . number_format($rowdet1_thisyeartotal * -1, 2) . ')' ?></td>
												<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
												<td class="tar sep <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($rowdet1_lastyeartotal >= 0 ) ? number_format($rowdet1_lastyeartotal, 2) : '(' . number_format($rowdet1_lastyeartotal * -1, 2) . ')' ?></td>
												<?php 
											}
											?>
										</tr>
										<tr><td colspan="4">&nbsp;</td></tr>
										<?php
									}
								}
								
								if ($accountdetails) {
									foreach ($accountdetails as $row) {
										if ($row->acctGroupID == $rowdet->ID) {
											if ($row->acctCode == '130000000') {
												$thisyear_amount = $currentretained;
												$lastyear_amount = $lastyearretained;
												
												if (floatval($thisyear_amount) == 0 && floatval($lastyear_amount) == 0) continue;
												?>
												<tr>
													<td style="display:none" ><?php echo $row->ID; ?></td>
													<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="<?php echo base_url(); ?>glreports/accountDetailsfc/<?php echo $row->ID; ?>/<?php echo $selyear; ?>/<?php echo $selperiod; ?>" title="<?php echo $this->lang->line('details'); ?>" class="btn btn-mini btn-link edit"><?php echo substr($row->acctCode, 0, 4) . '-' . substr($row->acctCode, 4, 3) . '-' . substr($row->acctCode, 7, 2) . ' [ ' . $row->acctName . ' ]' ?></a></td>
													<?php
													if ($rowdet->status == 1) {
														?>
														<td class="tar"><?php echo ($thisyear_amount <= 0 ) ? number_format($thisyear_amount * -1, 2) : '(' . number_format($thisyear_amount, 2) . ')' ?></td>
														<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
														<td class="tar <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($lastyear_amount <= 0 ) ? number_format($lastyear_amount * -1, 2) : '(' . number_format($lastyear_amount, 2) . ')' ?></td>
														<?php
													} else {
														?>
														<td class="tar"><?php echo ($thisyear_amount >= 0 ) ? number_format($thisyear_amount, 2) : '(' . number_format($thisyear_amount * -1, 2) . ')' ?></td>
														<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
														<td class="tar <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($lastyear_amount >= 0 ) ? number_format($lastyear_amount, 2) : '(' . number_format($lastyear_amount * -1, 2) . ')' ?></td>
														<?php
													}
												?>
												</tr>
												<?php
												$total_thisyear = $total_thisyear + $thisyear_amount;
												$total_lastyear = $total_lastyear + $lastyear_amount;
												$rowdet_thisyeartotal = $rowdet_thisyeartotal + ($row->lastyeardebit - $row->lastyearcredit) + $thisyear_amount;
												$rowdet_lastyeartotal = $rowdet_lastyeartotal + $lastyear_amount;
											} elseif ($row->acctCode == '120000000') {
												$lastyear_amount = $lastRetained;
												$thisyear_amount = $currRetain;
												
												if (floatval($thisyear_amount) == 0 && floatval($lastyear_amount) == 0) continue;
												?>
												<tr>
													<td style="display:none" ><?php echo $row->ID; ?></td>
													<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="<?php echo base_url(); ?>glreports/accountDetailsfc/<?php echo $row->ID; ?>/<?php echo $selyear; ?>/<?php echo $selperiod; ?>" title="<?php echo $this->lang->line('details'); ?>" class="btn btn-mini btn-link edit"><?php echo substr($row->acctCode, 0, 4) . '-' . substr($row->acctCode, 4, 3) . '-' . substr($row->acctCode, 7, 2) . ' [ ' . $row->acctName . ' ]' ?></a></td>
													<?php
													if ($rowdet->status == 1) {
														?>
														<td class="tar"><?php echo ($thisyear_amount <= 0 ) ? number_format($thisyear_amount * -1, 2) : '(' . number_format($thisyear_amount, 2) . ')' ?></td>
														<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
														<td class="tar <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($lastyear_amount <= 0 ) ? number_format($lastyear_amount * -1, 2) : '(' . number_format($lastyear_amount, 2) . ')' ?></td>
														<?php
													} else {
														?>
														<td class="tar"><?php echo ($thisyear_amount >= 0 ) ? number_format($thisyear_amount, 2) : '(' . number_format($thisyear_amount * -1, 2) . ')' ?></td>
														<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
														<td class="tar <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($lastyear_amount >= 0 ) ? number_format($lastyear_amount, 2) : '(' . number_format($lastyear_amount * -1, 2) . ')' ?></td>
														<?php
													}
													?>
												</tr>
												<?php
												$total_thisyear = $total_thisyear + $thisyear_amount;
												$total_lastyear = $total_lastyear + $lastyear_amount; 
												$rowdet_thisyeartotal = $rowdet_thisyeartotal + $thisyear_amount;
												$rowdet_lastyeartotal = $rowdet_lastyeartotal + $lastyear_amount; ?>
												<?php
											}
											
											if ($row->lastyeardebit == 0 && $row->lastyearcredit == 0 && $row->currentyeardebit == 0 && $row->currentyearcredit == 0) {
											} elseif ($row->acctCode != '130000000' && $row->acctCode != '120000000') {
												$tyear_amount = ($row->lastyeardebit - $row->lastyearcredit) + ($row->currentyeardebit - $row->currentyearcredit);
												$lyear_amount = $row->lastyeardebit - $row->lastyearcredit;
												
												if (floatval($tyear_amount) == 0 && floatval($lyear_amount) == 0) continue;
												?>
												<tr>
												<td style="display:none" ><?php echo $row->ID; ?></td>
												<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="<?php echo base_url(); ?>glreports/accountDetailsfc/<?php echo $row->ID; ?>/<?php echo $selyear; ?>/<?php echo $selperiod; ?>" title="<?php echo $this->lang->line('details'); ?>" class="btn btn-mini btn-link edit"><?php echo substr($row->acctCode, 0, 4) . '-' . substr($row->acctCode, 4, 3) . '-' . substr($row->acctCode, 7, 2) . ' [ ' . $row->acctName . ' ]' ?></td>
												<?php
												if ($rowdet->status == 1) {
													?>
													<td class="tar"><?php echo ($tyear_amount <= 0 ) ? number_format($tyear_amount * -1, 2) : '(' . number_format($tyear_amount, 2) . ')' ?></td>
													<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
													<td class="tar <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($lyear_amount <= 0 ) ? number_format($lyear_amount * -1, 2) : '(' . number_format($lyear_amount, 2) . ')' ?></td>
													<?php
												} else {
													?>
													<td class="tar"><?php echo ($tyear_amount >= 0 ) ? number_format($tyear_amount, 2) : '(' . number_format($tyear_amount * -1, 2) . ')' ?></td>
													<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
													<td class="tar <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($lyear_amount >= 0 ) ? number_format($lyear_amount, 2) : '(' . number_format($lyear_amount * -1, 2) . ')' ?></td>
													<?php
												}
												?>
												</tr>
												<?php
												$total_thisyear = $total_thisyear + ($row->lastyeardebit - $row->lastyearcredit) + ($row->currentyeardebit - $row->currentyearcredit);
												$total_lastyear = $total_lastyear + ($row->lastyeardebit - $row->lastyearcredit);
												$rowdet_thisyeartotal = $rowdet_thisyeartotal + ($row->lastyeardebit - $row->lastyearcredit) + ($row->currentyeardebit - $row->currentyearcredit);
												$rowdet_lastyeartotal = $rowdet_lastyeartotal + ($row->lastyeardebit - $row->lastyearcredit);
											}
										}
									}
								}
								?>
								       
								<tr>
								<?php
								if ($rowdet->ID == '39') {
								?>
									<td>&nbsp;&nbsp;&nbsp;<strong><?php echo $this->lang->line($rowdet->acctGroupName) ?></strong></td>
								<?php
								} else {
								?>
									<td>&nbsp;&nbsp;&nbsp;<?php echo $this->lang->line('subtotal') ?> <?php echo $this->lang->line($rowdet->acctGroupName) ?> </td>
								<?php
								}
								
								if ($rowdet->status == 1) {
									?>
									<td class="tar sep"><?php echo ($rowdet_thisyeartotal <= 0 ) ? number_format($rowdet_thisyeartotal * -1, 2) : '(' . number_format($rowdet_thisyeartotal, 2) . ')' ?></td>
									<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
									<td class="tar sep <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($rowdet_lastyeartotal <= 0 ) ? number_format($rowdet_lastyeartotal * -1, 2) : '(' . number_format($rowdet_lastyeartotal, 2) . ')' ?></td>
									<?php
								} else {
									?>
									<td class="tar sep"><?php echo ($rowdet_thisyeartotal >= 0 ) ? number_format($rowdet_thisyeartotal, 2) : '(' . number_format($rowdet_thisyeartotal * -1, 2) . ')' ?></td>
									<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
									<td class="tar sep <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($rowdet_lastyeartotal >= 0 ) ? number_format($rowdet_lastyeartotal, 2) : '(' . number_format($rowdet_lastyeartotal * -1, 2) . ')' ?></td>
									<?php
								}
								?>
								</tr>
								<tr><td colspan="4">&nbsp;</td></tr>
								<?php
							}
						}
						
						foreach ($accountdetails as $row) {
							if ($row->acctGroupID == $datatbl->ID) {
								if ($row->acctCode == '130000000') {
									$thisyear_amount = $currentretained * -1;
									$lastyear_amount = $lastyearretained * -1;
								} else {
									$thisyear_amount = ($row->lastyeardebit - $row->lastyearcredit) + ($row->currentyeardebit - $row->currentyearcredit);
									$lastyear_amount = $row->lastyeardebit - $row->lastyearcredit;
								}
								
								if (floatval($thisyear_amount) == 0 && floatval($lastyear_amount) == 0) continue;
								?>
								<tr>
								<td style="display:none" ><?php echo $row->ID . 'hello'; ?></td>
								<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="<?php echo base_url(); ?>glreports/accountDetailsfc/<?php echo $row->ID; ?>/<?php echo $selyear; ?>/<?php echo $selperiod; ?>" title="<?php echo $this->lang->line('details'); ?>" class="btn btn-mini btn-link edit"><?php echo substr($row->acctCode, 0, 4) . '-' . substr($row->acctCode, 4, 3) . '-' . substr($row->acctCode, 7, 2) . ' [ ' . $row->acctName . ' ]' ?></td>
								<?php
								if ($datatbl->status == 1) {
									?>
									<td class="tar"><?php echo ($thisyear_amount <= 0 ) ? number_format($thisyear_amount * -1, 2) : '(' . number_format($thisyear_amount, 2) . ')' ?></td>
									<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
									<td class="tar <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($lastyear_amount <= 0 ) ? number_format($lastyear_amount * -1, 2) : '(' . number_format($lastyear_amount, 2) . ')' ?></td>
									<?php
								} else {
									?>
									<td class="tar"><?php echo ($thisyear_amount >= 0 ) ? number_format($thisyear_amount, 2) : '(' . number_format($thisyear_amount * -1, 2) . ')' ?></td>
									<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
									<td class="tar <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($lastyear_amount >= 0 ) ? number_format($lastyear_amount, 2) : '(' . number_format($lastyear_amount * -1, 2) . ')' ?></td>
									<?php
								}
								?>
								</tr>
								<?php
								$total_thisyear = $total_thisyear + $thisyear_amount;
								$total_lastyear = $total_lastyear + $lastyear_amount;
							}
						}
						?>
						<tr>
							<td><h4><?php echo $this->lang->line('total') ?> <?php echo $this->lang->line($datatbl->acctGroupName) ?> </h4></td>
							<?php
							if ($datatbl->status == 1) {
								?>
								<td class="tar tots"><?php echo ($total_thisyear <= 0 ) ? number_format($total_thisyear * -1, 2) : '(' . number_format($total_thisyear, 2) . ')' ?></td>
								<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
								<td class="tar tots <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($total_lastyear <= 0 ) ? number_format($total_lastyear * -1, 2) : '(' . number_format($total_lastyear, 2) . ')' ?></td>
								<?php
							} else {
								?>
								<td class="tar tots"><?php echo ($total_thisyear >= 0 ) ? number_format($total_thisyear, 2) : '(' . number_format($total_thisyear * -1, 2) . ')' ?></td>
								<td class="<?php echo $activeTab == 1?'dn':''; ?>">&nbsp;</td>
								<td class="tar tots <?php echo $activeTab == 1?'dn':''; ?>"><?php echo ($total_lastyear >= 0 ) ? number_format($total_lastyear, 2) : '(' . number_format($total_lastyear * -1, 2) . ')' ?></td>
								<?php
							}
							?>
						</tr>
						<tr><td colspan="4">&nbsp;</td></tr>
						<?php
					}
				}
				?>
			</tbody>
		</table>
	</div>
	
</div>

			
			