@php
	$total_pendapatan = 0;
	$total_hpp = 0;
	$total_beban_usaha = 0;

	function isNegative($number) {
		if ($number < 0) {
			return "(".currency(abs($number)).")";
		} else {
			return currency($number);
		}
	}

	function currency($number) {
		return number_format($number, 2, ',', '.');
	}
@endphp

<table>
	<thead>
		<tr>
			<td colspan="3" style="text-align: center; font-weight: bold;">CV. KETJUBUNG</td>
		</tr>
		<tr>
			<td colspan="3" style="text-align: center; font-weight: normal; font-style: italic;">Laba Rugi</td>
		</tr>
		<tr>
			<td colspan="3" style="text-align: center; font-weight: normal; font-style: italic;">Periode : {{ $periode }}</td>
		</tr>
	</thead>
</table>

<table>
	@if(sizeof($pendapatan) > 0)
		@foreach($pendapatan as $key => $value)
			@php if(sizeof($value['child'])==0) continue; @endphp
			<tr>
				<th colspan='3' style='text-align: left;'>{{$value['nama_perkiraan']}}</th>
			</tr>
			@if(sizeof($value['child']) > 0)
				@foreach($value['child'] as $keyC => $valueC)
					@php if($valueC['total_saldo']==0) continue; @endphp
					<tr>
						<td>{{$valueC['nama_perkiraan']}}</td>
						<td style='text-align : right;'>{{isNegative($valueC['total_saldo'])}}</td>
					</tr>
					@php
						if($valueC['saldo_normal']==$value['saldo_normal']){
							$total_pendapatan += $valueC['total_saldo'];
						}else{
							$total_pendapatan -= $valueC['total_saldo'];
						}
					@endphp
				@endforeach
				<tr>
	    			<th colspan='2' style='text-align: left;'>Total {{$value['nama_perkiraan']}}</th>
	    			<th style='text-align : right;'>{{isNegative($total_pendapatan)}}</th>
	    		</tr>
			@endif
		@endforeach
	@endif

	@if(sizeof($hpp) > 0)
		<tr>
			<th colspan='3' style='text-align: left;'>Harga Pokok Penjualan (HPP)</th>
		</tr>
		@foreach($hpp as $key => $value)
			@php
				if(sizeof($value['child'])==0) continue;
				$sub_total_hpp = 0;
			@endphp

			@if(sizeof($value['child']) > 0)
				@foreach($value['child'] as $keyC => $valueC)
					@php
						if($valueC['total_saldo']==0) continue;

						if($valueC['saldo_normal']==$value['saldo_normal']){
							$sub_total_hpp += $valueC['total_saldo'];
							$saldo_hpp = $valueC['total_saldo'];
						}else{
							$sub_total_hpp -= $valueC['total_saldo'];
							$saldo_hpp = $valueC['total_saldo']*-1;
						}
					@endphp
					<tr>
						<td>{{$valueC['nama_perkiraan']}}</td>
						<td style='text-align : right;'>{{isNegative($saldo_hpp)}}</td>
					</tr>
				@endforeach
				@php
					if($sub_total_hpp==0) continue;
					$total_hpp += $sub_total_hpp;
				@endphp
			@endif
		@endforeach
		<tr>
			<th colspan='2' style='text-align: left;'>Total Harga Pokok Penjualan (HPP)</th>
			<th style='text-align : right;'>{{isNegative($total_hpp)}}</th>
		</tr>
	@endif

	@if(sizeof($beban) > 0)
	@php $total_beban = 0; @endphp
		<tr>
			<th colspan='3' style='text-align: left;'>Beban Usaha</th>
		</tr>
		@foreach($beban as $key => $value)
			@php
				if(sizeof($value['child'])==0) continue;
			@endphp

			@if(sizeof($value['child']) > 0)
				@php $total_beban = 0; @endphp
				@foreach($value['child'] as $keyC => $valueC)
					@php
						if($valueC['total_saldo']==0) continue;

						if($valueC['saldo_normal']==$value['saldo_normal']){
							$total_beban += $valueC['total_saldo'];
						}else{
							$total_beban -= $valueC['total_saldo'];
						}

						if($total_beban==0) continue;
					@endphp
					<tr>
						<td>{{$valueC['nama_perkiraan']}}</td>
						<td style='text-align : right;'>{{isNegative($total_beban)}}</td>
					</tr>
				@endforeach
				@php
					$total_beban_usaha += $total_beban;
				@endphp
			@endif
		@endforeach
		<tr>
			<th colspan='2' style='text-align: left;'>Total Beban Usaha</th>
			<th style='text-align : right;'>{{isNegative($total_beban_usaha)}}</th>
		</tr>
	@endif
	@php
		$labaRugi = $total_pendapatan-$total_hpp-$total_beban_usaha;
	@endphp
	<tr>
		<th colspan='2' style='text-align : left;'>LABA (RUGI)</th>
		<th style='text-align : right;'>{{isNegative($labaRugi)}}</th>
	</tr>
</table>
