<style>
    .table-responsive {
        overflow-x: hidden;
    }

    @media (max-width: 767px) { /* Untuk smartphone */
        .hide-on-mobile {
            display: none;
        }
    }

    @media (min-width: 768px) and (max-width: 1024px) { /* Untuk tablet */
        .hide-on-tablet {
            display: none;
        }
    }

    @media (min-width: 1024px) { /* Untuk laptop */
        .hide-on-laptop {
            display: none;
        }
    }
</style>

<?php 
$invkas1 = mysqli_query($con, "
    SELECT 
        SUM(CASE WHEN metode_pembayaran = 'Tunai' THEN harga ELSE 0 END) AS 'total_inv_tunai',
        SUM(CASE WHEN metode_pembayaran = 'Bank' THEN harga ELSE 0 END) AS 'total_inv_bank',
        SUM(CASE WHEN metode_pembayaran = 'Hutang Jangka Pendek' THEN harga ELSE 0 END) AS 'total_inv_hutang_pendek',
        SUM(CASE WHEN metode_pembayaran = 'Hutang Jangka Panjang' THEN harga ELSE 0 END) AS 'total_inv_hutang_panjang',
        SUM(CASE WHEN kategori = 'Tanah' THEN harga ELSE 0 END) AS 'total_inv_tanah',
        SUM(CASE WHEN kategori = 'Kendaraan' THEN harga ELSE 0 END) AS 'total_inv_kendaraan',
        SUM(CASE WHEN kategori = 'Kendaraan' THEN jumlah_nilai_Penyusutan ELSE 0 END) AS 'total_inv_penyusutan_kendaraan',
        SUM(CASE WHEN kategori = 'Lainnya' THEN harga ELSE 0 END) AS 'total_inv_lainnya',
        SUM(CASE WHEN kategori = 'Lainnya' THEN jumlah_nilai_Penyusutan ELSE 0 END) AS 'total_inv_penyusutan_lainnya',
        SUM(harga) AS 'total_inventaris',
        SUM(jumlah_nilai_Penyusutan) AS 'total_penyusutan',
        SUM(nilai_saat_ini) AS 'total_sisa_inventaris'
    FROM v_akumulasi_inventaris_total $SqlDataWHId
");
$invkas = mysqli_fetch_assoc($invkas1);
$total_inv_tunai = $invkas['total_inv_tunai'];
$total_inv_bank = $invkas['total_inv_bank'];
$total_inv_hutang_pendek = $invkas['total_inv_hutang_pendek'];
$total_inv_hutang_panjang = $invkas['total_inv_hutang_panjang'];
$total_inv_tanah = $invkas['total_inv_tanah'];
$total_inv_kendaraan = $invkas['total_inv_kendaraan'];
$total_inv_penyusutan_kendaraan = '-'.$invkas['total_inv_penyusutan_kendaraan'];
$total_inv_lainnya = $invkas['total_inv_lainnya'];
$total_inv_penyusutan_lainnya = '-'.$invkas['total_inv_penyusutan_lainnya'];
$total_inventaris = $invkas['total_inventaris'];
$total_penyusutan = $invkas['total_penyusutan'];
$total_sisa_inventaris = $invkas['total_sisa_inventaris'];


//NERACA ACTIVA

$vbarang = mysqli_query($con, "
    SELECT 
        SUM(CASE WHEN metode_pembayaran = 'Tunai' THEN total_harga ELSE 0 END) AS 'total_barang_tunai',
        SUM(CASE WHEN metode_pembayaran = 'Bank' THEN total_harga ELSE 0 END) AS 'total_barang_bank',
        SUM(CASE WHEN metode_pembayaran = 'Hutang Barang' THEN total_harga ELSE 0 END) AS 'total_barang_hutang'
    FROM v_jumlah_data_gudang $SqlDataWHId
");
$barang = mysqli_fetch_assoc($vbarang);
$total_barang_tunai = $barang['total_barang_tunai'];
$total_barang_bank = $barang['total_barang_bank'];
$total_barang_hutang = $barang['total_barang_hutang'];


$vactiva = mysqli_query($con,"SELECT * FROM v_neraca_activa $SqlDataWHId");
$activa = mysqli_fetch_assoc($vactiva);

$saldo_kas = $activa['saldo_kas'] - $total_barang_tunai - $total_inv_tunai;
$saldo_bank = $activa['saldo_bank'] - $total_barang_bank - $total_inv_bank;

$piutang_usaha = $activa['piutang_usaha'];
$piutang_barang = 0;
$piutang_karyawan = $activa['piutang_karyawan'];
$piutang_lain = $activa['piutang_lain'];
$persedian_barang = $total_barang_tunai + $total_barang_bank + $total_barang_hutang;

$total_activa_lancar = $saldo_kas + $saldo_bank + $piutang_usaha + $piutang_barang + $piutang_karyawan + $piutang_lain + $persedian_barang;

//ACTIVA TETAP
$total_activa_tetap = $total_sisa_inventaris;

$total_activa = $total_activa_lancar + $total_activa_tetap;



//NERACA PASSIVA
$vpassiva = mysqli_query($con,"SELECT * FROM v_neraca_passiva $SqlDataWHId");
$passiva = mysqli_fetch_assoc($vpassiva);

//$hutang_barang = $passiva['hutang_barang'];
$hutang_barang = $total_barang_hutang + $passiva['hutang_barang'];

$hutang_jangka_pendek = $passiva['hutang_jangka_pendek'] + $total_inv_hutang_pendek;
$hutang_jangka_panjang = $passiva['hutang_jangka_panjang'] + $total_inv_hutang_panjang;
$jumlah_kewajiban = $hutang_barang + $hutang_jangka_pendek + $hutang_jangka_panjang;


$modal_awal = $passiva['modal_awal'];
$modal_tambahan = $passiva['modal_tambahan'];
$laba_ditahan = $passiva['laba_ditahan'];
$laba_rugi_berjalan = $passiva['laba_rugi_berjalan'] - $total_penyusutan;


$pemasukan1 = mysqli_query($con,"SELECT sum(pemasukan) as pemasukan FROM v_akun_laba_rugi WHERE jenis = 1 $SqlDataId and status != 'Modal Tambahan' and status != 'Modal Awal' and status != 'Laba Ditahan'");
$pemasukan = mysqli_fetch_assoc($pemasukan1);

$pengeluaran1 = mysqli_query($con,"SELECT sum(pengeluaran) as pengeluaran FROM v_akun_laba_rugi WHERE jenis = 2 $SqlDataId");
$pengeluaran = mysqli_fetch_assoc($pengeluaran1); 
?>

 <!-- Main content -->
<section class="content">
  <div class="container-fluid">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <div class="row">
              <div class="col-sm-6">
                <h3 class="card-title">Neraca dan Laba Rugi</h3>
              </div>
              <div class="col-sm-6">
                <ol class="float-sm-right mb-0">
                  <!--<a type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#staticBackdrop">Tambah</a>-->
                </ol>
              </div>
            </div>
          </div>
          <div class="card-body">
              <div class="row">
                  <div class="col-md-4">
                      <center>
                          <h5><b><u>Neraca Activa</u></b></h5>
                      </center>
                      <table class="table table-bordered table-striped table-sm">
                          <thead>
                              <tr>
                                  <th>Keterangan</th>
                                  <th>Nominal</th>
                              </tr>
                          </thead>
                          <tbody>
                              <tr>
                                  <th>Activa Lancar</th>
                                  <td></td>
                              </tr>
                              <tr>
                                  <td>Saldo Kas</td>
                                  <td>Rp. <?php echo number_format($saldo_kas); ?></td>
                              </tr>
                              <tr>
                                  <td>Saldo Bank</td>
                                  <td>Rp. <?php echo number_format($saldo_bank); ?></td>
                              </tr>
                              <tr>
                                  <td>Piutang Usaha</td>
                                  <td>Rp. <?php echo number_format($piutang_usaha); ?></td>
                              </tr>
                              <tr>
                                  <td>Piutang Barang</td>
                                  <td>Rp. <?php echo number_format($piutang_barang); ?></td>
                              </tr>
                              <tr>
                                  <td>Piutang Karyawan</td>
                                  <td>Rp. <?php echo number_format($piutang_karyawan); ?></td>
                              </tr>
                              <tr>
                                  <td>Piutang Lain-lain</td>
                                  <td>Rp. <?php echo number_format($piutang_lain); ?></td>
                              </tr>
                              <tr>
                                  <td>Biaya Dibayar Dimuka (BDD)</td>
                                  <td>Rp. -</td>
                              </tr>
                              <tr>
                                  <td>(Akumulasi Penyusutan BDD)</td>
                                  <td>Rp. -</td>
                              </tr>
                              <tr>
                                  <td>Persediaan Barang</td>
                                  <td>Rp. <?php echo number_format($persedian_barang); ?></td>
                              </tr>
                              <tr>
                                  <th>Jumlah Aktiva Lancar</th>
                                  <th>Rp. <?php echo number_format($total_activa_lancar); ?></th>
                              </tr>
                              <tr>
                                  <td></td>
                                  <td></td>
                              </tr>
                              <tr>
                                  <th>Aktiva Tetap</th>
                                  <th></th>
                              </tr>
                              <tr>
                                  <td>Tanah</td>
                                  <td>Rp. <?php echo number_format($total_inv_tanah); ?></td>
                              </tr>
                              <tr>
                                  <td>Kendaraan</td>
                                  <td>Rp. <?php echo number_format($total_inv_kendaraan); ?></td>
                              </tr>
                              <tr>
                                  <td>(Akumulasi Penyusutan Kendaraan)</td>
                                  <td>Rp. <?php echo number_format($total_inv_penyusutan_kendaraan); ?></td>
                              </tr>
                              <tr>
                                  <td>Barang Inventaris</td>
                                  <td>Rp. <?php echo number_format($total_inv_lainnya); ?></td>
                              </tr>
                              <tr>
                                  <td>(Akumulasi Penyusutan Inventaris)</td>
                                  <td>Rp. <?php echo number_format($total_inv_penyusutan_lainnya); ?></td>
                              </tr>
                              <tr>
                                  <th>Jumlah Activa Tetap</th>
                                  <th>Rp. <?php echo number_format($total_activa_tetap); ?></th>
                              </tr>
                              <tr>
                                  <th></th>
                                  <th></th>
                              </tr>
                              <tr>
                                  <th>TOTAL ACTIVA</th>
                                  <th>Rp. <?php echo number_format($total_activa); ?></th>
                              </tr>
                          </tbody>
                      </table>
                  </div>
                  <div class="col-md-4">
                      <center>
                          <h5><b><u>Neraca Passiva</u></b></h5>
                      </center>
                      <table class="table table-bordered table-striped table-sm">
                          <thead>
                              <tr>
                                  <th>Keterangan</th>
                                  <th>Nominal</th>
                              </tr>
                          </thead>
                          <tbody>
                              <tr>
                                  <th>Kewajiban</th>
                                  <td></td>
                              </tr>
                              <tr>
                                  <td>Hutang Barang</td>
                                  <td>Rp. <?php echo number_format($hutang_barang); ?></td>
                              </tr>
                              <tr>
                                  <td>Hutang Jangka Pendek</td>
                                  <td>Rp. <?php echo number_format($hutang_jangka_pendek); ?></td>
                              </tr>
                              <tr>
                                  <td>Hutang Jangka Panjang</td>
                                  <td>Rp. <?php echo number_format($hutang_jangka_panjang); ?></td>
                              </tr>
                              <tr>
                                  <th>Jumlah Kewajiban</th>
                                  <th>Rp. <?php echo number_format($jumlah_kewajiban); ?></th>
                              </tr>
                              <tr>
                                  <td>*</td>
                                  <td></td>
                              </tr>
                              <tr>
                                  <td>*</td>
                                  <td></td>
                              </tr>
                              <tr>
                                  <td>*</td>
                                  <td></td>
                              </tr>
                              <tr>
                                  <td>*</td>
                                  <td></td>
                              </tr>
                              <tr>
                                  <td>*</td>
                                  <td></td>
                              </tr>
                              <tr>
                                  <td>*</td>
                                  <td></td>
                              </tr>
                              <tr>
                                  <td>*</td>
                                  <td></td>
                              </tr>
                              <tr>
                                  <td></td>
                                  <td></td>
                              </tr>
                              <tr>
                                  <th>Modal</th>
                                  <th></th>
                              </tr>
                              <tr>
                                  <td>Modal Awal</td>
                                  <td>Rp. <?php echo number_format($modal_awal); ?></td>
                              </tr>
                              <tr>
                                  <td>Laba Ditahan</td>
                                  <td>Rp. <?php echo number_format($laba_ditahan); ?></td>
                              </tr>
                              <tr>
                                  <td>Modal Tambahan</td>
                                  <td>Rp. <?php echo number_format($modal_tambahan); ?></td>
                              </tr>
                              <tr>
                                  <td>Laba Rugi Berjalan</td>
                                  <td>Rp. <?php echo number_format($laba_rugi_berjalan); ?></td>
                              </tr>
                              <tr>
                                  <th>Jumlah Modal</th>
                                  <th>Rp. <?php echo number_format($modal_awal + $laba_ditahan + $modal_tambahan + $laba_rugi_berjalan); ?></th>
                              </tr>
                              <tr>
                                  <th></th>
                                  <th></th>
                              </tr>
                              <tr>
                                  <th>TOTAL PASSIVA</th>
                                  <th>Rp. <?php echo number_format($modal_awal + $laba_ditahan + $modal_tambahan + $laba_rugi_berjalan + $jumlah_kewajiban); ?></th>
                              </tr>
                          </tbody>
                      </table>
                  </div>
                  <div class="col-md-4">
                      <center>
                          <h5><b><u>Laba Rugi</u></b></h5>
                          <table class="table table-bordered table-striped table-sm">
                              <thead>
                                  <tr>
                                      <th>Keterangan</th>
                                      <th>Nominal</th>
                                  </tr>
                              </thead>
                              <tbody>
                                <?php 
                                $akuntran = mysqli_query($con,"SELECT status as status, sum(pemasukan) as pemasukan FROM v_akun_laba_rugi WHERE jenis = 1 $SqlDataId and status NOT IN ('Modal Tambahan','Modal Awal','Laba Ditahan') group by status order by status DESC");
                                while ($akun = mysqli_fetch_assoc($akuntran)) { ?>
                                  <tr>
                                      <td><?php echo $akun['status']; ?></td>
                                      <td>Rp. <?php echo number_format($akun['pemasukan']); ?></td>
                                  </tr>
                                <?php } ?>
                                  <tr>
                                      <th>Total Pendapatan</th>
                                      <th>Rp. <?php echo number_format($pemasukan['pemasukan']); ?></th>
                                  </tr>
                                  <tr>
                                      <td></td>
                                      <td></td>
                                  </tr>
                                <?php 
                                $akuntran1 = mysqli_query($con,"SELECT status as status, sum(pengeluaran) as pengeluaran FROM v_akun_laba_rugi WHERE jenis = 2 $SqlDataId and status NOT IN ('Piutang Barang','Piutang Usaha','Piutang Lain-lain','Piutang Karyawan') group by status order by status DESC");
                                while ($akun1 = mysqli_fetch_assoc($akuntran1)) { ?>
                                  <tr>
                                      <td><?php echo $akun1['status']; ?></td>
                                      <td>Rp. <?php echo number_format($akun1['pengeluaran']); ?></td>
                                  </tr>
                                <?php } ?>
                                  <tr>
                                      <td>Beban Penyusutan</td>
                                      <td>Rp. <?php echo number_format($total_penyusutan); ?></td>
                                  </tr>
                                  <tr>
                                      <th>Total Pengeluaran</th>
                                      <th>Rp. <?php echo number_format($pengeluaran['pengeluaran'] + $total_penyusutan); ?></th>
                                  </tr>
                                  <tr>
                                      <td></td>
                                      <td></td>
                                  </tr>
                                  <tr>
                                      <th>LABA RUGI</th>
                                      <th>Rp. <?php echo number_format($pemasukan['pemasukan'] - $pengeluaran['pengeluaran'] - $total_penyusutan); ?></th>
                                  </tr>
                              </tbody>
                          </table>
                      </center>                      
                  </div>
              </div>
          </div>
        </div>
        <!-- /.card -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
  </div>
  <!-- /.container-fluid -->
</section>
<!-- /.content -->