<?php
error_reporting(E_ALL^(E_NOTICE|E_WARNING));
ini_set('display_errors',1);
error_reporting(0);
require './kalenderhijriah.php';
?>

<!-- Content Header (Page header) -->
<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-12">
        <!-- <h1 class="m-0 text-dark">Laporan Neraca []</h1> -->
      </div><!-- /.col -->
    </div><!-- /.row -->
  </div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->

 <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            <!-- Default box -->
            <div class="card">
              <div class="card-header bg-info">
                <h3 class="card-title ">Laporan Penerimaan & Pengeluaran</h3>
                <div class="card-tools">
                  <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                    <i class="fas fa-minus"></i>
                  </button>
                  <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
                    <i class="fas fa-times"></i>
                  </button>
                </div>
              </div>
             
        <!-- /.card-header -->
        <div class="card-body">
          <?php
          $kode=$_SESSION['kode'];
          if(isset($_POST['par1'])){
           $par1= $_POST['par1'];
           $par2=$_POST['par2'];

         }else{
          $par1="";
          $par2="";
        }
        ?>
        <form action="home.php?home=<?php echo md5("labarugi");?>" method="post">
          <div class="form-group row">
            <!-- Input tanggal pertama -->
            <div id="datepicker1" class="ml-2 " >
                    <input id="par1" name="par1" type="text" class="form-control d-inline-block" style="width: 100px; display: inline;" value="<?php if(isset($_POST['par1'])){ echo $_POST['par1']; }else{ echo sprintf('%02d', $hijri[1]) . '-' . $calendar->monthName($hijri[0]) . '-' . $hijri[2];} ?>"  />
                    <img id="pick-button1" src="kalender.png" alt="Pick Date" style="cursor: pointer; width: 24px; height: 24px;" onclick="pickADate1();" class="d-inline-block"/>
                    <div id="calendar-popup1" class="datepicker" style="display:none;"></div>
                </div>

                <!-- Input tanggal kedua -->
                <div id="datepicker2" class="ml-2">
                    <input id="par2" name="par2" type="text" class="form-control d-inline-block" style="width: 100px; display: inline;"  value="<?php if(isset($_POST['par2'])){ echo $_POST['par2']; }else{ echo sprintf('%02d', $hijri[1]) . '-' . $calendar->monthName($hijri[0]) . '-' . $hijri[2];} ?>" />
                    <img id="pick-button2" src="kalender.png" alt="Pick Date" style="cursor: pointer; width: 24px; height: 24px;" onclick="pickADate2();" class="d-inline-block"/>
                    <div id="calendar-popup2" class="datepicker" style="display:none;"></div>
                    </div>
                            
            <input type="submit" name="proses" class="btn btn-primary" style="float: right" value="Preview">      
            
            <!-- <div><a href="laporan/lap_neraca_pdf.php" target="_blank" style="float: right;" class="btn btn-success" class="btn btn-success"><i class="fa fa-print"></i>PDF</a></div> -->
          </div>
        </form>

        <hr>

        <?php if(isset($_POST['par1'])){ ?>
          <h5 style="font-weight: bold;">PENERIMAAN</h5>
          <table class="table table-sm">
          <!-- <table class="table table-bordered table-hover"> -->
           
          <tr>
            <th >Kode</th>
            <th >Akun</th>
            <th style="text-align: center;"><?php echo date('d-M-Y', strtotime('-1 days', strtotime($_POST['par1'])));?></th>
            <th style="text-align: center;">Mutasi</th>
            <th style="text-align: center;"><?php echo date('d-M-Y',strtotime($_POST['par2']));?></th>
          </tr>
          <tr>
            <th colspan="4">Pendapatan Operasional</th>
          </tr>
          <?php
          $debetall=0;
          $queryl1      = "SELECT tanggal AS tanggal,kode_akun AS kode_akun,kode_akun.n_akun 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 tanggal <'$par1' and bukubesar.kode='$kode' and kode_akun <'4.410.90' and kode_akun >'3.320.70' 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.tanggal <='$par2' and bukubesar.kode='$kode' and bukubesar.kode_akun <'4.430.90' and bukubesar.kode_akun >'3.320.70' GROUP BY kode_akun having saldoAkhir<>0 ";
          $_SESSION['labarugi']['sql1']=$queryl1;

          $resultz     = $mysqli->query($queryl1);
          $num_resultz = $resultz->num_rows;
          if ($num_resultz >0) {

            while ($datal = mysqli_fetch_assoc($resultz)) {
              extract($datal);
              $tsaldoAwal+=$datal['saldoAwal'];
              $tsaldoAkhir+=$datal['saldoAkhir'];
              $tmutasi+=$datal['mutasi'];
              ?>
              <tr>
               <td width="10%" ><?php echo $datal['kode_akun']; ?></td>
               <td ><?php echo $datal['nama_akun']; ?></td>
               <td width="15%"align="right"><?php echo number_format($datal['saldoAwal'],0); ?></td>
               <td width="15%"align="right"><?php echo number_format($datal['mutasi'],0); ?></td>
               <td width="15%"align="right"><?php echo number_format($datal['saldoAkhir'],0); ?></td>
             </tr>
           <?php }} ?>
           <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>
          <!-- <tr>
            <th colspan="4">Pendapatan Non Operasional</th>
          </tr>
          <?php
          $debetall=0;
          $queryl2      = "SELECT tanggal AS tanggal,kode_akun AS kode_akun,kode_akun.n_akun 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 tanggal <'$par1' and bukubesar.kode='$kode' and kode_akun >='4.410.90' and kode_akun <'5.500.01' 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.tanggal <='$par2' and bukubesar.kode='$kode' and bukubesar.kode_akun >='4.410.90' and bukubesar.kode_akun <'5.500.01' GROUP BY kode_akun having saldoAkhir>0";
          $_SESSION['labarugi']['sql2']=$queryl2;

          $resultz     = $mysqli->query($queryl2);
          $num_resultz = $resultz->num_rows;
          if ($num_resultz > 0) {

            while ($datal2 = mysqli_fetch_assoc($resultz)) {
              $tsaldoAwal2+=$datal2['saldoAwal'];
              $tmutasi2+=$datal2['mutasi'];
              $tsaldoAkhir2+=$datal2['saldoAkhir'];
              

              ?>
              <tr>
               <td width="10%"><?php echo $datal2['kode_akun']; ?></td>
               <td ><?php echo $datal2['nama_akun']; ?></td>
               <td width="15%" align="right"><?php echo number_format($datal2['saldoAwal'],0); ?></td>
               <td width="15%" align="right"><?php echo number_format($datal2['mutasi'],0); ?></td>
               <td width="15%" align="right"><?php echo number_format($datal2['saldoAkhir'],0); ?></td>
             </tr>

           <?php }} ?>
           
           <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
              $taktiva1=$tsaldoAwal+$tsaldoAwal2;
              $taktivam=$tmutasi+$tmutasi2;
              $taktiva2=$tsaldoAkhir+$tsaldoAkhir2;
            ?>
            <tr>
             <th colspan="2">Total </th>
             <th style="text-align: right;"><?=number_format(($taktiva1),0)?></th>
             <th style="text-align: right;"><?=number_format(($taktivam),0)?></th>
             <th style="text-align: right;"><?=number_format(($taktiva2),0)?></th>
           </tr>
         
         </tbody>
       </table>
     </br>
       <h5 style="font-weight: bold;">PENGELUARAN</h5>

       <table class="table table-sm">
        <tr>
            <th >Kode</th>
            <th >Akun</th>
            <th style="text-align: center;"><?php echo date('d-M-Y', strtotime('-1 days', strtotime($_POST['par1'])));?></th>
            <th style="text-align: center;">Mutasi</th>
            <th style="text-align: center;"><?php echo date('d-M-Y',strtotime($_POST['par2']));?></th>
          </tr>
        <?php
        $tsaldoAwal3=0;
        $tmutasi3=0;
        $tsaldoAkhir3=0;
        $queryl3      = "SELECT tanggal AS tanggal,kode_akun AS kode_akun,kode_akun.n_akun AS nama_akun,saldo.saldoAwal AS saldoAwal,SUM(debet-kredit)-saldo.saldoAwal AS mutasi,SUM(debet - kredit) AS saldoAkhir FROM bukubesar LEFT JOIN (SELECT kode_akun AS kode2,SUM(debet - kredit) AS saldoAwal FROM bukubesar WHERE tanggal <'$par1' and bukubesar.kode='$kode' and kode_akun like '%5.5%' 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.tanggal <='$par2' and bukubesar.kode='$kode' and bukubesar.kode_akun like '%5.5%' GROUP BY kode_akun having saldoAkhir<>0 ";
        $_SESSION['labarugi']['sql3']=$queryl3;

        $resultz     = $mysqli->query($queryl3);
        $num_resultz = $resultz->num_rows;
        if ($num_resultz > 0) {

          while ($datal3 = mysqli_fetch_assoc($resultz)) {
              $tsaldoAwal3+=$datal3['saldoAwal'];
              $tmutasi3+=$datal3['mutasi'];
              $tsaldoAkhir3+=$datal3['saldoAkhir'];
            ?>
            <tr>
             <td width="10%"><?php echo $datal3['kode_akun']; ?></td>
             <td ><?php echo $datal3['nama_akun']; ?></td>
             <td width="15%" align="right"><?php echo number_format($datal3['saldoAwal'],0); ?></td>
             <td width="15%" align="right"><?php echo number_format($datal3['mutasi'],0); ?></td>
             <td width="15%" align="right"><?php echo number_format($datal3['saldoAkhir'],0); ?></td>
           </tr>
         <?php }} ?>
         <tr>
           <th colspan="2">Jumlah </th>
           <th style="text-align: right;"><?=number_format(($tsaldoAwal3),0)?></th>
           <th style="text-align: right;"><?=number_format(($tmutasi3),0)?></th>
           <th style="text-align: right;"><?=number_format(($tsaldoAkhir3),0)?></th>
         </tr>

         <tr>
           <th colspan="2">LABA BERSIH </th>
           <th style="text-align: right;"><?=number_format(($taktiva1-$tsaldoAwal3),0)?></th>
           <th style="text-align: right;"><?=number_format(($taktivam-$tmutasi3),0)?></th>
           <th style="text-align: right;"><?=number_format(($taktiva2-$tsaldoAkhir3),0)?></th>
         </tr>


       </tbody>
     </table>
   <?php } ?>

   <?php if(isset($_POST['par1'])){
          
    $_SESSION['neraca']['periode1'] = date('Y-m-d',strtotime($_POST['par1']));
    $_SESSION['neraca']['periode2'] = date('Y-m-d',strtotime($_POST['par2']));
    $_SESSION['neraca']['judul']= "Laporan Laba Rugi";
    ?>
    <a href="pdf/lap_labarugi_pdf.php" target="_blank" style="float: right; margin-top: 10px;" class="btn btn-success" class="btn btn-success"><i class="fa fa-print"></i>PDF</a>
    

  <?php } ?>

</div>
<div class="card-footer bg-info">
                SAK-ETAP
              </div>
              
            <!-- /.card -->
          </div>
        </div>
      </div>
    </section>
    <!-- /.content -->
  <!-- </div> -->
  <!-- /.content-wrapper -->
  <link rel="stylesheet" href="calendar.css"/>
<script type="text/javascript" src="hijri-date.js"></script>
<script type="text/javascript" src="calendar.js"></script>
<!-- https://zulns.github.io/libs/calendar-api-doc.html -->



<script type="text/javascript">
    var pickedTxt1 = document.getElementById('par1'),
        datepicker1 = new Calendar(true, 0, false, true);
    document.getElementById('datepicker1').appendChild(datepicker1.getElement());
    datepicker1.getElement().style.marginTop = '10px';
    
    datepicker1.callback = function() {
        var selectedDate = datepicker1.getDate();
        var day = selectedDate.getDate().toString().padStart(2, '0');
        var month = (selectedDate.getMonth() + 1).toString().padStart(2, '0');
        var year = selectedDate.getFullYear();
        pickedTxt1.value = day + '-' + month + '-' + year;
        pickedTxt1.selectionStart = 0;
        pickedTxt1.selectionEnd = pickedTxt1.value.length;
        pickedTxt1.focus();
        datepicker1.hide();
    };

    function pickADate1() {
        datepicker1.show();
        datepicker1.getElement().style.position = 'absolute'; // Pastikan posisi kalender tidak mengganggu layout
    }

    var pickedTxt2 = document.getElementById('par2'),
        datepicker2 = new Calendar(true, 0, false, true);
    document.getElementById('datepicker2').appendChild(datepicker2.getElement());
    datepicker2.getElement().style.marginTop = '10px';
    
    datepicker2.callback = function() {
        var selectedDate = datepicker2.getDate();
        var day = selectedDate.getDate().toString().padStart(2, '0');
        var month = (selectedDate.getMonth() + 1).toString().padStart(2, '0');
        var year = selectedDate.getFullYear();
        pickedTxt2.value = day + '-' + month + '-' + year;
        pickedTxt2.selectionStart = 0;
        pickedTxt2.selectionEnd = pickedTxt2.value.length;
        pickedTxt2.focus();
        datepicker2.hide();
    };

    function pickADate2() {
        datepicker2.show();
        datepicker2.getElement().style.position = 'absolute'; // Pastikan posisi kalender tidak mengganggu layout
    }
</script>
<style>
    /* Mengatur posisi datepicker agar tidak mempengaruhi layout lain */
    .datepicker {
        position: absolute; /* Mencegah pergeseran elemen lain */
        z-index: 1000; /* Agar kalender muncul di atas elemen lain */
    }
</style>
<script type="text/javascript">
function addClickOutsideListener(datepicker, popupId) {
        function onClick(event) {
            var popup = document.getElementById(popupId);
            if (popup && !popup.contains(event.target) && !datepicker.getElement().contains(event.target)) {
               
                datepicker.hide(); // Menyembunyikan kalender
                document.removeEventListener('click', onClick); // Menghapus event listener setelah digunakan
            }
        }
        
        // Menambahkan event listener untuk mendeteksi klik di luar kalender
        document.addEventListener('click', onClick);
    }
</script>

