<?php
/**
* @last-update: Zul Zolkaffly (24/08/2016)
*								- Changed layout to follow AdminLTE markup
*								- Zul testing
*/
?>
<script>

function PNLYearChange() {
	var y = $('input[name="yearCurr"], input#year0').val();
	var p = $('input[name="yearPrev"], input#year1').val();
	
	$.post('<?php echo base_url(); ?>apply/profitnlossajax',{ y:y, p:p },function(result) {
		var obj = (result) ? JSON.parse(result) : null;
		if ( $.isEmptyObject( obj ) ) return;
		
		$.each(obj, function(k,v) {
			var $elem = $('#'+k);			
			if($elem.length) $elem.html(		accounting.formatNumber(	Number(v)	,2)		);
		});
		InitCheckUnbalance();
	});
}
</script>

<div class="box box-warning">
	<div class="box-header with-border">
		<h3 class="box-title">Analisa Untung / Rugi</h3>
	</div>
	
				
	<div class="box-body">
		<table id="financialPosition" class="table table-bordered table-hover datatables">
			<thead>
				<tr>
					<th rowspan="2" class="col-sm-8">Perkara</th>
					<th class="text-center col-sm-2 year_curr"><?php echo $years[0]; ?></th>
					<th class="text-center col-sm-2 year_prev"><?php echo $years[1]; ?></th>
				</tr>
				<tr><th class="text-center col-sm-2">RM</th><th class="text-center col-sm-2">RM</th></tr>
			</thead>
			
			<tbody>
			<?php
			if($parents && $pos && $years) {
				foreach($parents as $parID=>$parDet) {
					if(isset($pos[$parID]) && $pos[$parID]) {
						foreach($pos[$parID] as $grpID=>$grpDet) {
							$total = NULL;
							?>
							<tr><th class="header" colspan="3"><?php echo $this->lang->line(strtolower($grpDet['section'])); ?></th></tr>
							<?php
							if($grpDet['details']) {
								foreach($grpDet['details'] as $accID=>$accDet) {
									?>
									<tr>
										<td class="blk10"><?php echo $accDet['name']; ?></td>
										<?php
										foreach($years as $y=>$year) {
											if(!isset($total[$year])) $total[$year] = 0;
											$total[$year] += $accDet['data'][$year];
											?><td class="text-center col-sm-2" id="pnl_item_<?php echo $parID; ?>_<?php echo $parID; ?>_<?php echo $grpID; ?>_<?php echo $accID; ?>_<?php echo $y; ?>"><?php echo number_format($accDet['data'][$year],2); ?></td><?php
										}
										?>
									</tr>
									<?php
								}
								?>
								<tr class="total">
									<td class="blk5">Jumlah</td>
									<?php
									foreach($years as $y=>$year) {
										?><td class="text-center col-sm-2" id="pnl_total_<?php echo $parID; ?>_<?php echo $parID; ?>_<?php echo $grpID; ?>_<?php echo $y; ?>"><?php echo number_format($total[$year],2); ?></td><?php
									}
									?>
								</tr>
								<?php
							}	
							?><tr><td colspan="3">&nbsp;</td></tr><?php
						}
					}
					
					if($parDet['section']) {
						foreach($parDet['section'] as $sectID=>$sectDet) {
							if(isset($pos[$sectID]) && $pos[$sectID]) {
								foreach($pos[$sectID] as $grpID=>$grpDet) {
									$total = NULL;
									?>
									<tr><th class="header" colspan="3"><?php echo $this->lang->line(strtolower($grpDet['section'])); ?></th></tr>
									<?php
									if($grpDet['details']) {
										foreach($grpDet['details'] as $accID=>$accDet) {
											?>
											<tr>
												<td class="blk10"><?php echo $accDet['name']; ?></td>
												<?php
												foreach($years as $y=>$year) {
													if(!isset($total[$year])) $total[$year] = 0;
													$total[$year] += $accDet['data'][$year];
													?><td class="text-center col-sm-2" id="pnl_item_<?php echo $parID; ?>_<?php echo $sectID; ?>_<?php echo $grpID; ?>_<?php echo $accID; ?>_<?php echo $y; ?>"><?php echo number_format($accDet['data'][$year],2); ?></td><?php
												}
												?>
											</tr>
											<?php
										}
										?>
										<tr class="total">
											<td class="blk10">Jumlah</td>
											<?php
											foreach($years as $y=>$year) {
												?><td class="text-center col-sm-2" id="pnl_total_<?php echo $parID; ?>_<?php echo $sectID; ?>_<?php echo $grpID; ?>_<?php echo $y; ?>"><?php echo number_format($total[$year],2); ?></td><?php
											}
											?>
										</tr>
										<?php
									}
								}
								?>
								<tr class="total">
									<td class="blk5"><?php echo $this->lang->line(strtolower($sectDet['name'])); ?></td>
									<?php
									foreach($years as $y=>$year) {
										?><th class="text-center col-sm-2" id="pnl_sub_<?php echo $parID; ?>_<?php echo $sectID; ?>_<?php echo $grpID; ?>_<?php echo $y; ?>"><?php echo number_format($sectDet['total'][$year],2); ?></th><?php
									}
									?>
								</tr>
								<tr><td colspan="3">&nbsp;</td></tr>
								<?php
							}
						}
					}
					?>
					<tr class="total">
						<td><?php echo $this->lang->line(strtolower($parDet['name'])); ?></td>
						<?php
						foreach($years as $y=>$year) {
							?><td class="text-center col-sm-2" id="pnl_grand_<?php echo $parID; ?>_<?php echo $y; ?>"><?php echo number_format($parDet['total'][$year],2); ?></td><?php
						}
						?>
					</tr>
					<tr><td colspan="3">&nbsp;</td></tr>
					<?php
				}
			}
			?>
			</tbody>
			
		</table>
	</div>
	
</div>
