<?php 
session_start();
ob_start();
error_reporting(0); // Commenting out error reporting
?>
<html>
<head>
  <title>Cetak PDF</title>
  <style>
    table {border-collapse:collapse; table-layout:fixed;width:100%; }
    table td {margin: 0 20px 20px 0;}
    th, td {
        padding: 4px;
        font-size: 12px;
    }
    p{ margin-top: 0px; }
    h5{ margin-top: 0px; }
    h4{ margin-bottom: 1px; }
  </style>
</head>

<body>
     <page_footer align="right">
    Page[[page_cu]]
  </page_footer> 
  <page_header>
  
  </page_header>

    <p align="center" style="margin:0px;font-family:Arial;">MMU A-39 REJOSARI</p>
    <p style="font-size:13px;text-align:center;margin:0px;font-family:Arial;">Ranting Pondok Pesantren Sidogiri </p>
    <p style="font-size:13px;text-align:center;margin:0px;font-family:Arial;">LAPORAN POSISI KEUANGAN </p>
    <p style="font-size:13px;text-align:center;margin:0px;font-family:Arial;"><?php echo date('d-m-Y', strtotime($_SESSION['neracagab']['periode2'])); ?> </p>

  <table border="1" style="padding-top: 19px;padding-bottom: 1px">
    <tr>
      <td style="text-align: center; background: #ddd;"><b>Kode</b></td>
      <td style="text-align: center; background: #ddd;"><b>Nama Akun</b></td>
      <td style="text-align: center; background: #ddd;"><b><?php echo date('d-m-Y', strtotime('-1 days', strtotime($_SESSION['neracagab']['periode1']))); ?></b></td>
      <td style="text-align: center; background: #ddd;"><b>Mutasi</b></td>
      <td style="text-align: center; background: #ddd;"><b><?php echo date('d-m-Y', strtotime($_SESSION['neracagab']['periode2'])); ?></b></td>
    </tr>

    <?php
    include "./../koneksi.php";
     $kode = $_SESSION['kode'];
    $par1 = $_SESSION['neracagab']['periode1'];
    $par2 = $_SESSION['neracagab']['periode2'];

    // Account groups for Aktiva
    $accountGroups1 = [
    ['range' => "1.10", 'label' => "Aktiva Lancar"],
    ['range' => "1.11", 'label' => "Penyertaan"],
    ['range' => "1.12", 'label' => "Piutang Gadai"],
    ['range' => "1.14,1.15,1.16,1.17,1.180,1.190", 'label' => "Aktiva Tetap"], // Combine into one group
    ];

    $totalSaldoAwal = 0;
    $totalMutasi = 0;
    $totalSaldoAkhir = 0;

    foreach ($accountGroups1 as $group) {
        $ranges = explode(',', $group['range']);
        $likeConditions = [];

        foreach ($ranges as $range) {
            $likeConditions[] = "kode_akun LIKE '{$range}%'";
        }
        $likeSQL = implode(' OR ', $likeConditions);

        $query1 = "SELECT bukubesar.kode_akun, kode_akun.n_akun AS nama_akun, saldo.saldoAwal,
                   SUM(bukubesar.debet - bukubesar.kredit) AS saldoAkhir,
                   SUM(bukubesar.debet - bukubesar.kredit) - saldo.saldoAwal AS mutasi 
                   FROM bukubesar 
                   LEFT JOIN (SELECT kode_akun AS kode2, SUM(debet - kredit) AS saldoAwal 
                               FROM bukubesar 
                               WHERE kode ='$kode' AND tanggal < '$par1' 
                               AND ($likeSQL) 
                               GROUP BY kode_akun) AS saldo 
                   ON bukubesar.kode_akun = saldo.kode2 
                   LEFT JOIN kode_akun 
                   ON bukubesar.kode_akun = kode_akun.k_akun 
                   WHERE bukubesar.kode ='$kode' AND bukubesar.tanggal <= '$par2' 
                   AND ($likeSQL) 
                   GROUP BY bukubesar.kode_akun 
                   HAVING saldoAkhir <> 0";
          
        $sql = mysqli_query($koneksi, $query1); 
        $row = mysqli_num_rows($sql);
                $tsaldoAwal = $tmutasi = $tsaldoAkhir = 0;

        if ($row > 0) { 
            while ($datan1 = mysqli_fetch_array($sql)) { 
                $tsaldoAwal += $datan1['saldoAwal'];
                $tmutasi += $datan1['mutasi'];
                $tsaldoAkhir += $datan1['saldoAkhir'];
                ?>   
                <tr>                      
                    <td class="pertama" style="text-align: left; width: 12%;"> <?php echo $datan1['kode_akun'];?> </td>
                    <td class="pertama" style="text-align: left; width: 46%;"> <?php echo $datan1['nama_akun'];?> </td>
                    <td class="pertama" style="text-align: right; width: 12%;"> <?php echo number_format($datan1['saldoAwal'], 0);?> </td>       
                    <td class="pertama" style="text-align: right; width: 12%;"> <?php echo number_format($datan1['mutasi'], 0);?> </td>
                    <td class="pertama" style="text-align: right; width: 12%;"> <?php echo number_format($datan1['saldoAkhir'], 0);?> </td>
                </tr>
                <?php    
            }
            $totalSaldoAwal += $tsaldoAwal;
            $totalMutasi += $tmutasi;
            $totalSaldoAkhir += $tsaldoAkhir;
            ?>
            <tr>
                <th colspan="2">Jumlah</th>
                <th style="text-align: right;"><?= number_format($tsaldoAwal, 0) ?></th>
                <th style="text-align: right;"><?= number_format($tmutasi, 0) ?></th>
                <th style="text-align: right;"><?= number_format($tsaldoAkhir, 0) ?></th>
            </tr>
            <?php
        }
    }
    
    if ($totalSaldoAwal > 0) { // Only show this if there are results
    ?>
    <tr>
        <td class="warna" colspan="2" style="text-align: left;"><b>TOTAL AKTIVA</b></td>
        <td class="warna" style="text-align: right;"><b><?php echo number_format($totalSaldoAwal, 0); ?></b></td>             
        <td class="warna" align="right"><b><?php echo number_format($totalMutasi, 0); ?></b></td>
        <td class="warna" align="right"><b><?php echo number_format($totalSaldoAkhir, 0); ?></b></td>
    </tr>
    <?php } ?>
  </table>
  <!-- Laba Rugi -->
            <?php
              $tsaldoAwals = 0;
              $tmutasis = 0;
              $tsaldoAkhirs = 0;
$queryzs = "SELECT tanggal AS tanggal, 
    '3.320.60' AS kode_akun, 
    'LABA BERJALAN' AS nama_akun,
    saldo.saldoAwal AS saldoAwal,
    SUM(kredit - debet) - saldo.saldoAwal AS mutasi,
    SUM(kredit - debet) AS saldoAkhir 
FROM bukubesar 
LEFT JOIN (
SELECT kode_akun AS kode2, 
       SUM(kredit - debet) AS saldoAwal 
FROM bukubesar 
WHERE kode='$kode' AND tanggal < '$par1' AND kode_akun >= '4.400' 
GROUP BY kode_akun
) AS saldo ON bukubesar.kode_akun = saldo.kode2 
LEFT JOIN (SELECT n_akun, k_akun FROM kode_akun) AS kode_akun 
ON bukubesar.kode_akun = kode_akun.k_akun 
WHERE bukubesar.kode='$kode' AND bukubesar.tanggal <= '$par2' AND bukubesar.kode_akun >= '4.400'";
                           
             
              $resultz = mysqli_query($koneksi, $queryzs);
              $num_resultz = mysqli_num_rows($resultz); // Memperbaiki variabel yang digunakan di sini
              //$resultz = $mysqli->query($resultz);
              //$num_resultz = $resultz->num_rows;

              if ($num_resultz > 0) {
                while ($datazs = mysqli_fetch_assoc($resultz)) {
                  $tsaldoAwals += $datazs['saldoAwal'];
                  $tmutasis += $datazs['mutasi'];
                  $tsaldoAkhirs += $datazs['saldoAkhir'];
                }
              }
              ?>
              <!-- Laba RUgi -->

  <table border="1" style="padding-top: 0px;padding-bottom: 16px">
    <tr>
      <td style="text-align: center; background: #ddd;"><b>Kode</b></td>
      <td style="text-align: center; background: #ddd;"><b>Nama Akun</b></td>
      <td style="text-align: center; background: #ddd;"><b><?php echo date('d-m-Y', strtotime('-1 days', strtotime($_SESSION['neracagab']['periode1']))); ?></b></td>
      <td style="text-align: center; background: #ddd;"><b>Mutasi</b></td>
      <td style="text-align: center; background: #ddd;"><b><?php echo date('d-m-Y', strtotime($_SESSION['neracagab']['periode2'])); ?></b></td>
    </tr>

    <?php
    // Account groups for Pasiva
    $accountGroups2 = [
        ['range' => "2.2", 'label' => "Pasiva Lancar"],
        ['range' => "2.310", 'label' => "Modal"],
        ['range' => "2.320", 'label' => "Kenaikan Aset"],
    ];

    $totalsaldoAwal22 = 0;
    $totalmutasi2 = 0;
    $totalsaldoAkhir22 = 0;

    foreach ($accountGroups2 as $group2) {
        $query2 = "SELECT bukubesar.kode_akun, kode_akun.n_akun AS nama_akun, 
                    saldo.saldoAwal2, 
                    SUM(bukubesar.kredit - bukubesar.debet) AS saldoAkhir2,
                    SUM(bukubesar.kredit - bukubesar.debet) - saldo.saldoAwal2 AS mutasi2 
                    FROM bukubesar 
                    LEFT JOIN (SELECT kode_akun AS kode2, SUM(kredit - debet) AS saldoAwal2 
                               FROM bukubesar
                               WHERE kode ='$kode' AND tanggal < '$par1' 
                               AND kode_akun LIKE '{$group2['range']}%' 
                               GROUP BY kode_akun) AS saldo 
                    ON bukubesar.kode_akun = saldo.kode2 
                    LEFT JOIN kode_akun ON bukubesar.kode_akun = kode_akun.k_akun
                    WHERE bukubesar.kode ='$kode' AND bukubesar.tanggal <= '$par2' 
                    AND bukubesar.kode_akun LIKE '{$group2['range']}%' 
                    GROUP BY bukubesar.kode_akun 
                    HAVING saldoAkhir2 <> 0";
        
        $sql = mysqli_query($koneksi, $query2);
        $row = mysqli_num_rows($sql); 
        $tsaldoAwal2 = $tmutasi2 = $tsaldoAkhir2 = 0;

        if ($row > 0) { 
            while ($datan = mysqli_fetch_array($sql)) { 
              $tsaldoAwal2 += $datan['saldoAwal2'] ?? 0; 
              $tmutasi2 += $datan['mutasi2'] ?? 0; 
              $tsaldoAkhir2 += $datan['saldoAkhir2'] ?? 0; 
              
                ?>   
                <tr>                      
                    <td class="pertama" style="text-align: left; width: 12%;"> <?php echo $datan['kode_akun'];?> </td>
                    <td class="pertama" style="text-align: left; width: 46%;"> <?php echo $datan['nama_akun'];?> </td>
                    <td class="pertama" style="text-align: right; width: 12%;"> <?php echo number_format($datan['saldoAwal2'], 0);?> </td>       
                    <td class="pertama" style="text-align: right; width: 12%;"> <?php echo number_format($datan['mutasi2'], 0);?> </td>
                    <td class="pertama" style="text-align: right; width: 12%;"> <?php echo number_format($datan['saldoAkhir2'], 0);?> </td>
                </tr>
                <?php    
            }
            $totalsaldoAwal22 += $tsaldoAwal2;
            $totalmutasi2 += $tmutasi2;
            $totalsaldoAkhir22 += $tsaldoAkhir2;
            ?>
            <tr>
                <th colspan="2">Jumlah</th>
                <th style="text-align: right;"><?= number_format($tsaldoAwal2, 0) ?></th>
                <th style="text-align: right;"><?= number_format($tmutasi2, 0) ?></th>
                <th style="text-align: right;"><?= number_format($tsaldoAkhir2, 0) ?></th>
            </tr>
            <?php
        }
    }
    ?>
    <tr>
                  <td >2.320.60</td>
                  <td >KENAIKAN ASET BERSIH</td>
                  <td style="text-align: right;"><?= number_format($tsaldoAwals, 0) ?></td>
                  <td style="text-align: right;"><?= number_format($tmutasis, 0) ?></td>
                  <td style="text-align: right;"><?= number_format($tsaldoAkhirs, 0) ?></td>
              </tr>
    <?PHP
    if ($totalsaldoAwal22 > 0) { // Only show this if there are results
    ?>
    <tr>
        <td class="warna" colspan="2" style="text-align: left;"><b>TOTAL PASIVA</b></td>
        <td class="warna" style="text-align: right;"><b><?php echo number_format($totalsaldoAwal22+$tsaldoAwals, 0); ?></b></td>             
        <td class="warna" align="right"><b><?php echo number_format($totalmutasi2+$tmutasis, 0); ?></b></td>
        <td class="warna" align="right"><b><?php echo number_format($totalsaldoAkhir22+$tsaldoAkhirs, 0); ?></b></td>
    </tr>
    <?php } ?>
  </table>

</body>
</html>

<?php
$html = ob_get_contents();
ob_end_clean();
        
require '../html2pdf/autoload.php';
$pdf = new Spipu\Html2Pdf\Html2Pdf('P', 'A4', 'en');
$pdf->WriteHTML($html);
$pdf->Output('Laporan Neraca Harian.pdf', 'D');
?>

