@php
$total_net_sales = ($penjualan->penjualan + $penjualan->biaya_angkut) - $penjualan->nominal_diskon;
                                $total_cogs = ($pembelian->persediaan_barang + $pembelian->biaya_angkut) - $pembelian->nominal_diskon;
                                $laba_kotor = $total_net_sales - $total_cogs;   

                                // hitung total beban
                                $total_expenses = 0;
                                foreach ($header_coa as $header) {
                                    if ($header->tipe_akun == 'Beban' || $header->tipe_akun == 'Beban Lain-Lain') {
                                        foreach ($header->masterCoa as $coa) {
                                            $total_debet = 
                                                $coa->penerimaanDebet->sum('debet') +
                                                $coa->pengeluaranDebet->sum('debet') +
                                                $coa->jurnalUmumDebet->sum('debet') +
                                                $coa->neraca_awal->sum('debet');

                                            $total_kredit = 
                                                $coa->penerimaanKredit->sum('kredit') +
                                                $coa->pengeluaranKredit->sum('kredit') +
                                                $coa->jurnalUmumKredit->sum('kredit') +
                                                $coa->neraca_awal->sum('kredit');

                                            $saldo = $total_debet - $total_kredit;
                                            $total_expenses += $saldo;
                                        }
                                    }
                                }

                                // hitung total bunga (akun tipe Bunga)
                                $total_bunga = 0;
                                foreach ($header_coa as $header) {
                                    if ($header->tipe_akun == 'Bunga') {
                                        foreach ($header->masterCoa as $coa) {
                                            $total_debet = 
                                                $coa->penerimaanDebet->sum('debet') +
                                                $coa->pengeluaranDebet->sum('debet') +
                                                $coa->jurnalUmumDebet->sum('debet') +
                                                $coa->neraca_awal->sum('debet');

                                            $total_kredit = 
                                                $coa->penerimaanKredit->sum('kredit') +
                                                $coa->pengeluaranKredit->sum('kredit') +
                                                $coa->jurnalUmumKredit->sum('kredit') +
                                                $coa->neraca_awal->sum('kredit');

                                            $saldo = $total_debet - $total_kredit;
                                            $total_bunga += $saldo;
                                        }
                                    }
                                }

                                // hitung pendapatan lain-lain
                                $total_other_income = 0;
                                foreach ($header_coa as $header) {
                                    if ($header->tipe_akun == 'Pendapatan Lain') {
                                        foreach ($header->masterCoa as $coa) {
                                            $total_debet = 
                                                $coa->penerimaanDebet->sum('debet') +
                                                $coa->pengeluaranDebet->sum('debet') +
                                                $coa->jurnalUmumDebet->sum('debet') +
                                                $coa->neraca_awal->sum('debet');

                                            $total_kredit = 
                                                $coa->penerimaanKredit->sum('kredit') +
                                                $coa->pengeluaranKredit->sum('kredit') +
                                                $coa->jurnalUmumKredit->sum('kredit') +
                                                $coa->neraca_awal->sum('kredit');

                                            $saldo = $total_kredit - $total_debet; // pendapatan normalnya kredit
                                            $total_other_income += $saldo;
                                        }
                                    }
                                }

                                // Net Profit
                                $net_profit = $laba_kotor - $total_expenses - $total_bunga + $total_other_income;    
@endphp
<table width="100%" style="margin-bottom: 10px;">
    <tr>
        <td width="20%" style="text-align: center;">
            <img src="{{ public_path('img/ptbss-removebg-preview.png') }}" style="height: 70px;">
        </td>
        <td style="text-align: left;">
            <h2 style="margin: 0;">PT. BERKAH SEMANGAT SENTOSA</h2>
            <h4 style="margin: 0;">Data Laba Rugi - {{ $bulanIndonesia }} {{ $tahun }}</h4>
        </td>
    </tr>
</table>
<table border="1" width="100%" cellspacing="0" cellpadding="4">
    <tr>
        <th colspan="2" align="center"><strong>PT. BERKAH SEMANGAT SENTOSA</strong></th>
    </tr>
    <tr>
        <th colspan="2" align="center">Neraca Bulan {{ $bulanIndonesia.' '.$tahun }}</th>
    </tr>
    <tr>
        <th colspan="2" style="text-align: left">Revenues :</th>
    </tr>
    <tr>
        <td style="width: 50%">Penjualan barang dagang/Sales</td>
        <td style="width: 50%" class="">
            {{ number_format($penjualan->penjualan, 0, ',', '.') }}
        </td>
    </tr>
    <tr>
        <td style="width: 50%">Beban Angkut Penjualan/Freight Out</td>
        <td style="width: 50%" class="">
            {{ number_format($penjualan->biaya_angkut, 0, ',', '.') }}
        </td>
    </tr>
   
    <tr>
        <td style="width: 50%">Potongan penjualan barang dagangang/Sales Discount</td>
        <td style="width: 50%" class="">
            {{ number_format($penjualan->nominal_diskon, 0, ',', '.') }}
        </td>
    </tr>

    <tr>
        <td style="width: 50%" class="text-right">Net Sales : </td>
        <td style="width: 50%" class="">
            {{ number_format($total_net_sales, 0, ',', '.') }}
        </td>
    </tr>
    <tr>
        <th colspan="2" style="text-align: left">Cost of goods sold :</th>
    </tr>
    @foreach ($header_coa as $header)
        @if ($header->tipe_akun == 'Harga Pokok')
            @foreach ($header->masterCoa as $coa)
                @php
                    $total_debet = 
                        $coa->penerimaanDebet->sum('debet') +
                        $coa->pengeluaranDebet->sum('debet') +
                        $coa->jurnalUmumDebet->sum('debet') +
                        $coa->neraca_awal->sum('debet');
                    
                    $total_kredit = 
                        $coa->penerimaanKredit->sum('kredit') +
                        $coa->pengeluaranKredit->sum('kredit') +
                        $coa->jurnalUmumKredit->sum('kredit')+
                        $coa->neraca_awal->sum('kredit');
                   
                    $saldo = $total_debet - $total_kredit;
                    
                @endphp
                @if ($saldo != 0)
                    <tr>
                        <td>
                            {{ $coa->nama_akun }}
                        </td>
                        <td>{{ number_format(abs($saldo), 0, ',', '.') }}</td>
                    </tr>
                @endif
            @endforeach
        @endif
    @endforeach

    <tr>
        <td style="width: 50%">Pembelian/Purchasing</td>
        <td style="width: 50%" class="">
            {{ number_format($pembelian->persediaan_barang, 0, ',', '.') }}
        </td>
    </tr>
    <tr>
        <td style="width: 50%">Beban transportasi pembelian/Freight Paid</td>
        <td style="width: 50%" class="">
            {{ number_format($pembelian->biaya_angkut, 0, ',', '.') }}
        </td>
    </tr>
    {{-- <tr>
        <td style="width: 50%">PPN Masukan/Input VAT</td>
        <td style="width: 50%" class="">
            {{ number_format($pembelian->ppn_masukan, 0, ',', '.') }}
        </td>
    </tr> --}}
    <tr>
        <td style="width: 50%">Potongan pembelian barang dagangan/Purchase Discount</td>
        <td style="width: 50%" class="">
            {{ number_format($pembelian->nominal_diskon, 0, ',', '.') }}
        </td>
    </tr>
    <tr>
        <td style="width: 50%" class="text-right">Total COGS : </td>
        <td style="width: 50%" class="">
            {{ number_format($total_cogs, 0, ',', '.') }}
        </td>
    </tr>
    <tr>
        <td style="width: 50%" class="text-right">Laba Kotor : </td>
        <td style="width: 50%" class="">
            {{ number_format($laba_kotor, 0, ',', '.') }}
        </td>
    </tr>
    <tr>
        <th colspan="2" style="text-align: left">Expenses :</th>
    </tr>
    @foreach ($header_coa as $header)
        @if ($header->tipe_akun == 'Beban' || $header->tipe_akun == 'Beban Lain-Lain')
            
        
        
            @foreach ($header->masterCoa as $coa)
                @php
                    $total_debet = 
                        $coa->penerimaanDebet->sum('debet') +
                        $coa->pengeluaranDebet->sum('debet') +
                        $coa->jurnalUmumDebet->sum('debet') +
                        $coa->neraca_awal->sum('debet');
                    //var_dump($coa->neraca_awal->sum('debet'));
                    $total_kredit = 
                        $coa->penerimaanKredit->sum('kredit') +
                        $coa->pengeluaranKredit->sum('kredit') +
                        $coa->jurnalUmumKredit->sum('kredit')+
                        $coa->neraca_awal->sum('kredit');
                    //var_dump($coa->neraca_awal->sum('kredit'));
                    $saldo = $total_debet - $total_kredit;
                    
                @endphp
                @if ($saldo != 0)
                    <tr>
                        <td>
                            {{ $coa->nama_akun }}
                        </td>
                        <td>{{ number_format(abs($saldo), 0, ',', '.') }}</td>
                    </tr>
                @endif
            @endforeach
        @endif
    @endforeach
    <tr>
        <th colspan="2" style="text-align: left">Other revenues and gains :</th>
    </tr>
    @foreach ($header_coa as $header)
        @if ($header->tipe_akun == 'Pendapatan Lain')
            
        
       
            @foreach ($header->masterCoa as $coa)
                @php
                    $total_debet = 
                        $coa->penerimaanDebet->sum('debet') +
                        $coa->pengeluaranDebet->sum('debet') +
                        $coa->jurnalUmumDebet->sum('debet') +
                        $coa->neraca_awal->sum('debet');
                    
                    $total_kredit = 
                        $coa->penerimaanKredit->sum('kredit') +
                        $coa->pengeluaranKredit->sum('kredit') +
                        $coa->jurnalUmumKredit->sum('kredit')+
                        $coa->neraca_awal->sum('kredit');
                    
                    $saldo = $total_debet - $total_kredit;
                    
                @endphp
                @if ($saldo != 0)
                    <tr>
                        <td>
                            {{ $coa->nama_akun }}
                        </td>
                        <td>{{ number_format(abs($saldo), 0, ',', '.') }}</td>
                    </tr>
                @endif
            @endforeach
        @endif
    @endforeach
    <tr>
        <th>Total Bunga :</th>
        <td>{{ number_format($total_bunga, 0, ',', '.') }}</td>
    </tr>
    <tr>
        <th>Net Profit :</th>
        <td>{{ number_format($net_profit, 0, ',', '.') }}</td>
    </tr>
</table>
