<?php 
  include '_header.php'; 
?>
<?php  
  if ( $levelLogin === "kurir") {
    echo "
      <script>
        document.location.href = 'bo';
      </script>
    ";
  }  
?>

<?php  
  $tanggal_awal = $_POST['tanggal_awal'];
  $tanggal_akhir = $_POST['tanggal_akhir'];
?>

<?php  
    $toko = query("SELECT * FROM toko WHERE toko_cabang = $sessionCabang");
?>
<?php foreach ( $toko as $row ) : ?>
    <?php 
      $toko_nama = $row['toko_nama'];
      $toko_kota = $row['toko_kota'];
      $toko_tlpn = $row['toko_tlpn'];
      $toko_wa   = $row['toko_wa'];
    ?>
<?php endforeach; ?>

<!-- Total penjualan -->
<?php  
    $totalPenjualan = 0;
    $queryInvoice = $conn->query("SELECT invoice.invoice_id, invoice.invoice_date, invoice.invoice_cabang, invoice.invoice_total_beli, invoice.invoice_sub_total, invoice.penjualan_invoice
        FROM invoice 
        WHERE invoice_piutang = 0 && invoice_piutang_lunas = 0 && invoice_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalPenjualan += $rowProduct['invoice_sub_total'];
    }
?>

<!-- Total Transfer -->
<?php  
    $totaltransfer = 0;
    $queryInvoice = $conn->query("SELECT invoice.invoice_id ,invoice.penjualan_invoice, invoice.invoice_tgl, customer.customer_id, customer.customer_nama, invoice.invoice_total, invoice.invoice_sub_total, invoice.invoice_cabang, invoice.invoice_tipe_transaksi
        FROM invoice 
        JOIN customer ON invoice.invoice_customer = customer.customer_id
        WHERE invoice_piutang < 1 && invoice_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."' && invoice_tipe_transaksi = 1
        ORDER BY invoice_id DESC
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totaltransfer += $rowProduct['invoice_sub_total'];
    }
?>

<!-- cashdrawer -->
<?php  
    $cashdrawer = query("SELECT * FROM cashdrawer WHERE cashdrawer_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."' ORDER BY cashdrawer_id DESC");
?>

<!-- Total HPP -->
<?php  
    $totalHpp = 0;
    $queryInvoice = $conn->query("SELECT invoice.invoice_id, invoice.invoice_date, invoice.invoice_cabang, invoice.invoice_total_beli, invoice.invoice_sub_total, invoice.penjualan_invoice
        FROM invoice 
        WHERE invoice_piutang = 0 && invoice_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalHpp += $rowProduct['invoice_total_beli'];
    }
?>

<!-- Total Piutang Cicilan -->
<?php  
    $totalPiutang = 0;
    $queryInvoice = $conn->query("SELECT piutang.piutang_id, piutang.piutang_date, piutang.piutang_nominal, piutang.piutang_cabang
        FROM piutang 
        WHERE piutang_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalPiutang += $rowProduct['piutang_nominal'];
    }
?>

<!-- Total Piutang Kembalian -->
<?php  
    $totalPiutangKembalian = 0;
    $queryInvoice = $conn->query("SELECT piutang_kembalian.pl_id, piutang_kembalian.pl_date, piutang_kembalian.pl_nominal, piutang_kembalian.pl_cabang
        FROM piutang_kembalian 
        WHERE pl_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalPiutangKembalian += $rowProduct['pl_nominal'];
    }
?>

<!-- Piutang = Total Piutang - Total Piutang Kembalian -->
<?php  
    $piutang = $totalPiutang - $totalPiutangKembalian;
?>

<!-- Total Hutang Cicilan -->
<?php  
    $totalHutang = 0;
    $queryInvoice = $conn->query("SELECT hutang.hutang_id, hutang.hutang_date, hutang.hutang_nominal, hutang.hutang_cabang
        FROM hutang 
        WHERE hutang_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalHutang += $rowProduct['hutang_nominal'];
    }
?>

<!-- Total Hutang Kembalian -->
<?php  
    $totalHutangKembalian = 0;
    $queryInvoice = $conn->query("SELECT hutang_kembalian.hl_id, hutang_kembalian.hl_date, hutang_kembalian.hl_nominal, hutang_kembalian.hl_cabang
        FROM hutang_kembalian 
        WHERE hl_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalHutangKembalian += $rowProduct['hl_nominal'];
    }
?>

<!-- Hutang = Total Hutang - Total Hutang Kembalian -->
<?php  
    $hutang = $totalHutang - $totalHutangKembalian;
?>

<!-- Total DP Piutang-->
<?php  
    $totalDpPiutang = 0;
    $queryInvoice = $conn->query("SELECT invoice.invoice_id, invoice.invoice_date, invoice.invoice_cabang, invoice.piutang_dp, invoice.penjualan_invoice
        FROM invoice 
        WHERE invoice_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalDpPiutang += $rowProduct['piutang_dp'];
    }
?>

<!-- Total DP Hutang-->
<?php  
    $totalDpHutang = 0;
    $queryInvoice = $conn->query("SELECT invoice_pembelian.invoice_pembelian_id, invoice_pembelian.invoice_date, invoice_pembelian.invoice_pembelian_cabang, invoice_pembelian.hutang_dp, invoice_pembelian.pembelian_invoice_parent
        FROM invoice_pembelian 
        WHERE invoice_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalDpHutang += $rowProduct['hutang_dp'];
    }
?>

<!-- Total Pembelian -->
<?php  
    $totalPembelian = 0;
    $queryInvoice = $conn->query("SELECT invoice_pembelian.invoice_pembelian_id, invoice_pembelian.invoice_date, invoice_pembelian.invoice_pembelian_cabang, invoice_pembelian.invoice_total, invoice_pembelian.pembelian_invoice_parent
        FROM invoice_pembelian 
        WHERE invoice_hutang = 0 && invoice_hutang_lunas = 0 && invoice_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalPembelian += $rowProduct['invoice_total'];
    }
?>

<!-- Total Servis DP -->
<?php  
    $totalDpServis = 0;
    $queryInvoice = $conn->query("SELECT data_servis.ds_id, data_servis.ds_dp, data_servis.ds_terima_date, data_servis.ds_cabang
        FROM data_servis 
        WHERE ds_terima_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalDpServis += $rowProduct['ds_dp'];
    }
?>

<!-- Total Servis Ambil -->
<?php  
    $totalSisaBayarServis = 0;
    $totalBiayaSparepartBeli = 0;
    $queryInvoice = $conn->query("SELECT data_servis.ds_id, data_servis.ds_total_biaya_jasa, data_servis.ds_total_biaya_sparepart, data_servis.ds_total_biaya_sparepart_beli, data_servis.ds_total, data_servis.ds_total_sisa_bayar, data_servis.ds_ambil_date, data_servis.ds_cabang
        FROM data_servis 
        WHERE ds_ambil_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."'
    ");
    while ($rowProduct = mysqli_fetch_array($queryInvoice)) {
        $totalSisaBayarServis += $rowProduct['ds_total_sisa_bayar'];
        $totalBiayaSparepartBeli += $rowProduct['ds_total_biaya_sparepart_beli'];
    }
?>

<!-- Pendapatan Servis Teknis -->
<?php  
    $biayaJasaKeseluruhan = query("SELECT * FROM data_servis_teknisi WHERE dst_pengambilan_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."' ");
?>

<?php 
    $ds_biaya_jasa_teknisiKeseluruhan = 0;
    $ds_biaya_profitKeseluruhan = 0; 
?>
<?php foreach ( $biayaJasaKeseluruhan as $row ) : ?>
    <?php 
      $ds_biaya_jasa_teknisiKeseluruhan += $row['ds_biaya_jasa_teknisi']; 
      $ds_biaya_profitKeseluruhan += $row['ds_biaya_profit']; 
    ?>
<?php endforeach; ?>

<!-- Pengeluaran Gaji & Bonus -->
<?php  
    $gajiBonus = query("SELECT * FROM user ");
?>

<?php 
    $user_gaji_pokok = 0;
    $user_bonus = 0; 
?>
<?php foreach ( $gajiBonus as $row ) : ?>
    <?php 
      $user_gaji_pokok += $row['user_gaji_pokok']; 
      $user_bonus += $row['user_bonus']; 
    ?>
<?php endforeach; ?>

<!-- Laba Bersih -->
<?php  
    $labaBersih = query("SELECT * FROM laba_bersih ");
?>
<?php foreach ( $labaBersih as $row ) : ?>
    <?php 
      $lb_pendapatan_lain = $row['lb_pendapatan_lain'];
      $lb_pengeluaran_listrik = $row['lb_pengeluaran_listrik'];
      $lb_pengeluaran_tlpn_internet = $row['lb_pengeluaran_tlpn_internet'];
      $lb_pengeluaran_perlengkapan_toko = $row['lb_pengeluaran_perlengkapan_toko']; 
      $lb_pengeluaran_biaya_penyusutan = $row['lb_pengeluaran_biaya_penyusutan'];
      $lb_pengeluaran_bensin = $row['lb_pengeluaran_bensin'];
      $lb_pengeluaran_tak_terduga = $row['lb_pengeluaran_tak_terduga'];
      $lb_pengeluaran_lain = $row['lb_pengeluaran_lain']; 
    ?>
<?php endforeach; ?>

<!-- pengeluaran -->
<?php  
    $pengeluaran = query("
        SELECT 
            k.kategori_pengeluaran_nama, 
            SUM(p.pengeluaran_total_dibayar) AS total_per_kategori
        FROM 
            pengeluaran p 
        JOIN 
            kategori_pengeluaran k 
        ON 
            p.kategori_pengeluaran_id = k.kategori_pengeluaran_id 
            AND p.pengeluaran_date BETWEEN '".$tanggal_awal."' AND '".$tanggal_akhir."' 
        GROUP BY 
            k.kategori_pengeluaran_nama
        ORDER BY 
            k.kategori_pengeluaran_nama
    ");
?>
<!-- End pengeluaran -->

    <section class="laporan-laba-bersih">
        <div class="container">
            <div class="llb-header">
            <div class="llb-header-title">
              <h3><strong>LAPORAN KOMULATIF EYD COMPUTER</strong></h3>
            </div>
            <div class="llb-header-contact">
            <h5><small>GABUNGAN SEMUA CABANG</small></h5>
            <h5><small><b>No.tlpn:</b> 07243292019   <b>Wa:</b> 081274278718</small></h5>
            </div>
              </div>

              <div class="laporan-laba-bersih-detail">
                  <div class="llbd-title">
                      Laporan LABA BERSIH Periode <?= tanggal_indo($tanggal_awal); ?> - <?= tanggal_indo($tanggal_akhir); ?>
                  </div>
                    <table class="table">
                      <thead>
                        <tr>
                          <th colspan="2">PENDAPATAN</th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr>
                          <td>a. Sub Total Penjualan</td>
                          <td>Rp <?= number_format($totalPenjualan, 0, ',', '.'); ?></td>
                        </tr>
                        <tr>
                          <td>b. DP Piutang</td>
                          <td>Rp <?= number_format($totalDpPiutang, 0, ',', '.'); ?></td>
                        </tr>
                        <tr>
                          <td>c. Piutang (Cicilan)</td>
                          <td>Rp <?= number_format($piutang, 0, ',', '.'); ?></td>
                        </tr>
                        <tr>
                          <td>d. Total DP Servis</td>
                          <td>Rp <?= number_format($totalDpServis, 0, ',', '.'); ?></td>
                        </tr>
                        <tr>
                          <td>e. Total Servis - Total DP Servis (Sisa Bayar)</td>
                          <td>Rp <?= number_format($totalSisaBayarServis, 0, ',', '.'); ?></td>
                        </tr>
                        <tr>
                          <td><b>Total Pendapatan</b></td>
                          <td>
                            <?php  
                              $totalPendapatan = $totalPenjualan + $piutang + $totalDpPiutang + $totalDpServis + $totalSisaBayarServis + $lb_pendapatan_lain;
                              echo "<b>Rp ".number_format($totalPendapatan, 0, ',', '.')."</b>";
                            ?> 
                          </td>
                        </tr>
                    
                        <?php if ( $levelLogin !== "kasir" && $levelLogin !== "kurir" ) { ?>
                        <tr>
                          <th colspan="2">HPP</th>
                        </tr>
                        <tr>
                          <td>a. HPP (Harga Pokok Penjualan & Sparepart)</td>
                          <?php $totalHpp += $totalBiayaSparepartBeli; ?>
                          <td>Rp <?= number_format($totalHpp, 0, ',', '.'); ?></td>
                        </tr>
                        <tr>
                          <td><b>Laba / Rugi Kotor</b></td>
                          <td>
                            <?php  
                              $labaRugiKotor = $totalPendapatan - $totalHpp;
                              echo "<b>Rp ".number_format($labaRugiKotor, 0, ',', '.')."</b>";
                            ?>
                          </td>
                        </tr>
                        <?php } ?>
                    
                        <tr>
                          <th colspan="2">BIAYA PENGELUARAN</th>
                        </tr>
                        <?php 
                          $grandTotal = 0;
                        ?>
                        <?php foreach ($pengeluaran as $row) : ?>
                        <tr>
                          <td><?= $row['kategori_pengeluaran_nama']; ?></td>
                          <td>Rp <?= number_format($row['total_per_kategori'], 0, ',', '.'); ?></td>
                        </tr>
                        <?php 
                          $grandTotal += $row['total_per_kategori']; // Hitung grand total
                        ?>
                        <?php endforeach; ?>
                    
                        <!-- Grand Total -->
                        <tr>
                          <td><b>Total Semua Pengeluaran</b></td>
                          <td><b>Rp <?= number_format($grandTotal, 0, ',', '.'); ?></b></td>
                        </tr>
                    
                        <!-- Laba Bersih (jika bukan kasir atau kurir) -->
                        <?php if ($levelLogin !== "kasir" && $levelLogin !== "kurir") { ?>
                        <tr>
                          <td><b>Laba Bersih</b></td>
                          <td><b>
                            <?php  
                              $labaBersih = $labaRugiKotor - $grandTotal;
                              echo "Rp " . number_format($labaBersih, 0, ',', '.');
                            ?>
                          </b></td>
                        </tr>
                        <?php } ?>
                    
                        <tr>
                          <th colspan="2">LAPORAN CASHDRAWER</th>
                        </tr>
                        <tr>
                          <td>Total Pendapatan</td>
                          <td>
                            <?php  
                              $totalPendapatan = $totalPenjualan + $piutang + $totalDpPiutang + $totalDpServis + $totalSisaBayarServis;
                              echo "Rp ".number_format($totalPendapatan, 0, ',', '.');
                            ?> 
                          </td>
                        </tr>
                        <?php 
                          $i = ""; 
                          $totalcashdrawer = 0;
                        ?>
                        <?php foreach ( $cashdrawer as $row ) : ?>
                          <?php $totalcashdrawer += $row['cashdrawer_total_dibayar']; ?>
                          <?php $i++; ?>
                        <?php endforeach; ?>
                        
                        <tr>
                          <td>Total Cashdrawer</td>
                          <td>
                            <?php  
                              $totalcashdrawer = $totalcashdrawer;
                              echo "Rp ".number_format($totalcashdrawer, 0, ',', '.' );
                            ?>
                          </td>
                        </tr>
                    
                        <tr>
                          <td>Total Bayar Transfer</td>
                          <td>
                            <?php  
                              $totaltransfer = $totaltransfer;
                              echo "Rp ".number_format($totaltransfer, 0, ',', '.' );
                            ?>
                          </td>
                        </tr>
                    
                        <tr>
                          <td><b>TOTAL SELISIH</b></td>
                          <td>
                            <?php  
                              $totalselisih = $totalcashdrawer - ($totalPendapatan - $totaltransfer);
                              echo "<b>Rp ".number_format($totalselisih, 0, ',', '.')."</b>";
                            ?> 
                          </td>
                        </tr>
                        <tr>
                          <td>Catatan Selisih</td>
                          <td></td>
                        </tr>
                      </tbody>

                  <?php if ( $levelLogin !== "kasir" && $levelLogin !== "kurir" ) { ?>
                    <thead>
                      <tr>
                        <th colspan="2">Laporan Pembelian ke Supplier</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr>
                      <tr>
                        <td>a. Total Pembelian ke Supplier</td>
                        <td>Rp <?= number_format($totalPembelian, 0, ',', '.'); ?></td>
                      </tr>
                      <tr>
                        <td>b. DP Hutang</td>
                        <td>Rp <?= number_format($totalDpHutang, 0, ',', '.'); ?></td>
                      </tr>
                      <tr>
                        <td>c. Hutang (Cicilan)</td>
                        <td>Rp <?= number_format($hutang, 0, ',', '.'); ?></td>
                      </tr>
                      <tr>
                      <?php } ?>
                      
                    </tbody>
                  </table>
              </div>
              <div class="text-center">
                <a href='http://www.eydcom.com'/> TOKO KAMI <a href='http://www.eydcom.com'/> - ONLINE DAN OFFLINE <a href='http://www.eydcom.com'/><br/>
 &#169; <strong>2012-<?= date("Y"); ?><a href='http://www.eydcom.com'/>. EYD COMPUTER - <a href='http://www.eydcom.com'/>www.eydcom.com
</div>
        </div>
    </section>


</body>
</html>
<script>
  window.print();
</script>