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

<table>
	@php
		$total_pendapatan = 0;
		$total_hpp = 0;
		$total_beban = 0;
	@endphp
	@if(count($pendapatan) > 0)
		<tr><th colspan="2" style="font-weight: bold;">Pendapatan</th></tr>
		@foreach($pendapatan as $key => $value)
			<tr>
				<td style="text-align: left;">({{ $value['kode_perkiraan'] }}) {{ $value['nama_perkiraan'] }}</td>
				<td style="text-align: right;">{{ isNegative($value['total_saldo']) }}</td>
			</tr>
			@if($value['id'] == 91 || $value['id'] == 92)
				@php($total_pendapatan -= $value['total_saldo'])
			@else
				@php($total_pendapatan += $value['total_saldo'])
			@endif
		@endforeach
		<tr>
			<td style="text-align: center; font-weight: bold;">Total Pendapatan</td>
			<td style="text-align: right; font-weight: bold;">{{ isNegative($total_pendapatan) }}</td>
		</tr>
	@endif

	@if(count($hpp) > 0)
		<tr><th colspan="2" style="font-weight: bold;">HPP</th></tr>
		@foreach($hpp as $key => $value)
			<tr>
				<td style="text-align: left;">({{ $value['kode_perkiraan'] }}) {{ $value['nama_perkiraan'] }}</td>
				<td style="text-align: right;">{{ isNegative($value['total_saldo']) }}</td>
			</tr>
			@if($value['id'] == 98 || $value['id'] == 99)
				@php($total_hpp -= $value['total_saldo'])
			@else
				@php($total_hpp += $value['total_saldo'])
			@endif
		@endforeach
		<tr>
			<td style="text-align: center; font-weight: bold;">Total HPP</td>
			<td style="text-align: right; font-weight: bold;">{{ isNegative($total_hpp) }}</td>
		</tr>
	@endif

	@if(count($beban) > 0)
		<tr><th colspan="2" style="font-weight: bold;">Beban</th></tr>
		@foreach($beban as $key => $value)
			<tr>
				<td style="text-align: left;">({{ $value['kode_perkiraan'] }}) {{ $value['nama_perkiraan'] }}</td>
				<td style="text-align: right;">{{ isNegative($value['total_saldo']) }}</td>
			</tr>
			@php($total_beban += $value['total_saldo'])
		@endforeach
		<tr>
			<td style="text-align: center; font-weight: bold;">Total Beban</td>
			<td style="text-align: right; font-weight: bold;">{{ isNegative($total_beban) }}</td>
		</tr>
	@endif
</table>

@php
	$laba_rugi = $total_pendapatan - $total_hpp - $total_beban;
@endphp

<table>
	<tr>
		<td style="text-align: center; font-weight: bold;">LABA (RUGI)</td>
		<td style="text-align: right; font-weight: bold;">{{ isNegative($laba_rugi) }}</td>
	</tr>
</table>
