<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') {
  $jenis = $_GET['type'];

?>
  <!-- page content -->
  <div class="col" role="main">
    <div class="">
      <div class="clearfix"></div>

      <div class="row">
        <div class="col-md-12">
          <div class="x_panel">
            <div class="x_title" style="text-transform: capitalize; font-size:20px;color:black;" >

              <?php if ($_GET['periode'] == 'y') {
                $bln = $_POST['bln'];
                $thn = $_POST['thn'];
                //echo "Laporan Realisasi Perencanaan Anggaran bulan $bln tahun $thn";
              } else {
                $bln = date('n');
                $thn = date('Y');
                //echo "Laporan Realisasi Perencanaan Anggaran bulan $bln tahun $thn";
              } 
                            
              
              
              ?>



              <div class="clearfix"></div>


              <form action="?module=<?php echo $_GET['module']; ?>&periode=y" enctype="multipart/form-data" method="POST">


                <div class="well" style="overflow: auto">

                  <div class="col-md-12 col-sm-12 col-xs-12" style="padding: 4px;">
                    <label for="nama">Pilih Periode :</label>
                  </div>

                  <div class="col-md-6 col-sm-12 col-xs-12" style="padding: 4px;">

                    <div class="input-group">
                      <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                      <select id="heard" class="form-control" required name="bln">
                        <option value="">- Pilih Bulan -</option>
                            <?php
                            $keri  = mysqli_query($conn, "SELECT * FROM bulan  order by id_bulan asc");
                            while ($data = mysqli_fetch_array($keri)) { ?>
                            <option value="<?php echo $data['id_bulan']; ?>" <?php if ($data['id_bulan'] == $bln) {
                                                                            echo "selected";
                                                                            } ?>><?php echo $data['nama_bulan']; ?></option>

                            <?php }  ?>
                        </select>
                      <div class="input-group-addon" style="background:white;"><small>Tahun</small></i></div>
                      <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                      <select id="heard" class="form-control" required name="thn">
                        <option value="">- Pilih Tahun -</option>
                      <?php
                            $tahun_ini = date('Y');
                            $xthn = $tahun_ini - 2;
                            $ythn = $xthn + 5;
                            for ($tahun = $xthn; $tahun <= $ythn; $tahun++) { ?>
                            <option value="<?php echo $tahun; ?>" <?php if ($tahun == $thn) {
                                                                            echo "selected";
                                                                            } ?>><?php echo $tahun ; ?></option>

                            <?php }  ?>
                            </select>
                    </div>
                  </div>

                  <div class="col-md-6 col-sm-12 col-xs-12" style="padding: 4px;">
                    <button type="submit" class="btn btn-primary btn-sm">Pilih</button>
              </form>


            </div>
            
            
            <div class="x_content">
                
                <table id="" class="table table-bordered" style="border-color:black; color:black">
                  <thead>
                  <tr>
                    <th colspan="6" for="namadivisi" style="font-size:20px;color:black; text-align: center"><?php echo "Laporan Realisasi Perencanaan Anggaran bulan $bln tahun $thn"; ?></th>
                  </tr>
                </thead>
                </table>
                
                <?php
                 if ($_SESSION['leveluser'] == "admin" ) {
                    $adm="";
                 }else{
                    $adm="WHERE id_divisi='$_SESSION[id_divisi]'";
                 }

                 $query_divisi = mysqli_query($conn, "SELECT * FROM jenis_divisi $adm order by id_divisi ASC ");
                 
                 
                 while ($row_divisi = mysqli_fetch_array($query_divisi)) {

                        $id_ang = mysqli_query($conn, "SELECT id_anggaran
                        FROM perencanaan_anggaran_bulan
                        WHERE bln = '$bln' AND thn = '$thn' AND id_divisi = '$row_divisi[id_divisi]'");
                            while ($row_id_ang = mysqli_fetch_array($id_ang)) {
                            $id_anggaran=$row_id_ang['id_anggaran'];
                            }
                
                ?>

              <table id="" class="table table-bordered" style="font-size:20px; border-color:black; color:black">
                  <thead>
                  <tr>
                    <th colspan="8" for="namadivisi" style="color:black; text-align: center"><?php echo $row_divisi['nama_divisi']; ?></th>
                  </tr>
                </thead>
                <thead>
                  <tr>
                    <!--<th>Tanggal</th>-->
                    <!--<th>id transaksi</th> -->
                    <th width="15px">NO.</th>
                    <th width="15px">Kode Kas</th>
                    <th>keterangan</th>
                    <th>Pendapatan</th>
                    <th>Pengeluaran</th>
                    <th>Perencanaan</th>
                    <th>(%) Realisasi</th>
                    <th>Detail</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $count = 1;
                  $total_rekap = 0;
                  $total_debit = 0;
                  $total_kredit = 0;
                  $query = mysqli_query($conn, "SELECT * FROM jenis_transaksi WHERE id_jenis NOT IN (1) AND id_divisi='$row_divisi[id_divisi]' order by jenis,nama_jenis ASC ");


                  $ro = 0;



                  while ($row = mysqli_fetch_array($query)) {

                    
                     if ($_GET['periode'] == 'y') {
                     $query_rekap = mysqli_query($conn, "SELECT SUM(debit) AS jumlah_debit, 
                                                SUM(kredit) AS jumlah_kredit 
                                            FROM transaksi 
                                            WHERE id_jenis = '$row[id_jenis]'
                                            AND MONTH(tanggal) = '$bln'
                                            AND YEAR(tanggal) = '$thn'");

                    } else {
                      $query_rekap = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE id_jenis ='$row[id_jenis]'
                                AND MONTH(tanggal)='$bln' 
                                AND YEAR(tanggal)='$thn'");
                    }

                    $rekap = mysqli_fetch_array($query_rekap);
                    $no++;
                  ?>

                    <tr style="background: <?php 
                    if($row['jenis']=='0' OR $row['id_jenis']=='21'){echo "#eefc6f";}else{
                    
                    if ($no % 2 == 1) { ?>
                          white;
                          <?php } else { ?>
                            white;
                            <?php }} ?>">

                      <!--<td><?php //echo $row['id_transaksi'];
                              ?></td>-->
                      <td><?php echo $no; ?></td>
                      <td><?php echo $row['id_jenis']; ?></td>
                      <td><?php echo $row['nama_jenis']; ?></td>

                      <td><?php echo "Rp." . rupiah($rekap['jumlah_debit']); ?></td>
                      <td><?php echo "Rp." . rupiah($rekap['jumlah_kredit']); ?></td>
                      <td>
                    <?php
                    // untuk total penganggaran per jenis transaksi
                    $v1 = mysqli_query($conn, "SELECT 
                                    id_jenis,
                                    SUM(jml) AS total_per
                                FROM perencanaan_detail_anggaran
                                WHERE id_anggaran = $id_anggaran AND id_jenis='$row[id_jenis]'

                    ");
                        while ($row_v1 = mysqli_fetch_array($v1)) {
                        echo rupiah($row_v1['total_per']);
                        $total_per=$row_v1['total_per'];
                        }
                    ?>


                    </td>
                    <td><?php 
                     if($row['jenis']=='0' OR $row['id_jenis']=='21'){
                        $ang_real=$rekap['jumlah_debit'];
                        }else {$ang_real=$rekap['jumlah_kredit'];}

                    $ang_real_x = $ang_real / $total_per * 100;

                    // CEK NEGATIF AGAR TERTANDA MERAH JIKA YG TDK TERANGGARKAN ADA TRANSAKSI
                    $ceknegatif= $total_per-$ang_real;

                    if ($ang_real_x > 100) {
                        echo '<span style="color:red; font-weight:bold;">' . number_format($ang_real_x, 1) . '</span>';
                    } else {
                      // CEK NEGATIF AGAR TERTANDA MERAH JIKA YG TDK TERANGGARKAN ADA TRANSAKSI
                        if($ceknegatif < 0){
                          echo '<a class="btn btn-danger btn-sm" data-toggle="modal" data-target="#modalinfo">Cek</a>';
                        }else{
                        echo number_format($ang_real_x, 1);
                        }
                    }
                    
                    
                    
                    ?> (%)</td>
                     
                      <td>
                        <?php

                    

                        // Tanggal awal (angka hari saja)
                        $tgl1 = date("Y-m-d", strtotime("$thn-$bln-01"));

                        // Tanggal akhir (angka hari saja)
                        $tgl2 = date("Y-m-d", strtotime(date("Y-m-t", strtotime("$thn-$bln-01"))));



                          echo "<a href='?module=cek-detail-realisasi&periode=y&type=$row[id_jenis]&tgl1=$tgl1&tgl2=$tgl2' class='btn btn-primary btn-sm'><i class='fa fa-print'></i></a>";

                        ?> </td>


                      <?php
                      $count++
                      ?>
                    
                    </tr>

                  <?php
                $total = $rekap['jumlah_debit'] - $rekap['jumlah_kredit'];
                //echo "Rp." . rupiah($total);
                $total_debit = $total_debit + $rekap['jumlah_debit'];
                $total_kredit = $total_kredit + $rekap['jumlah_kredit'];
                $total_rekap = $total_rekap + $total;
                
                } ?>



                  <tr bgcolor="#f6eee0">
             
                    <td colspan="3" align="center"><b>TOTAL</b></td>
                    <td><?php echo "Rp." . rupiah($total_debit); ?></td>
                    <td><?php echo "Rp." . rupiah($total_kredit); ?></td>
                    <td><?php //echo "Rp." . rupiah($total_rekap); ?></td>
                  </tr>



                </tbody>
              </table>
              <br><br>
              <?php
              
                 }
              
              if ($_GET['periode'] == 'y') {
                echo "<a href='component/com_transaksi/export.php?tgl1=$tgl1&tgl2=$tgl2' class='btn btn-primary btn-sm'><i class='fa fa-print'></i> Cetak</a>";
              } else {
                echo "<a href='component/com_transaksi/export.php?bln=$bln&thn=$thn' class='btn btn-primary btn-sm'><i class='fa fa-print'></i> Cetak</a>";
              }
              ?>
            </div>










          </div>

        </div>

      </div>
    </div>
  </div>
  <!-- /page content --><br>










          </div>

        </div>

      </div>
    </div>
  </div>
  <!-- /page content --><br>







<?php } ?>


<!-- modal info -->
<div class="modal fade" id="modalinfo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">Info</h4>
        </div>
        <div class="modal-body">
          <h4 style="color:red;" class="modal-title" id="myModalLabel">Tidak boleh ada data "CEK"</h4><br>
          <h4 class="modal-title" id="myModalLabel">Transaksi ini belum teranggarkan</h4><br>
          <h4 class="modal-title" id="myModalLabel">Hubungi admin Pusat untuk perubahan</h4><br>
          
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Ok</button>
        </div>
  
      </div>
    </div>
  </div>