<?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;">

              <?php if ($_GET['periode'] == 'y') {
                $tanggal1 = $_POST['tanggal1'];
                $tanggal2 = $_POST['tanggal2'];
                $tgl1 = date('Y-m-d', strtotime($tanggal1));
                $tgl2 = date('Y-m-d', strtotime($tanggal2));
                echo "<h4>Rekap buku besar/Kas periode $tanggal1 sampai $tanggal2</h4>";
              } else {
                $bln = date('m');
                $thn = date('Y');
                echo "<h4>Rekap buku besar/Kas bulan $bln tahun $thn</h4>";
              } ?>



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


              <form action="?module=buku_besar&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>
                      <input type="text" autocomplete="off" class="form-control " id="tanggal" required value="<?php if ($_GET['periode'] == 'y') {
                                                                                                                  echo $tanggal1;
                                                                                                                } else {
                                                                                                                  echo "";
                                                                                                                } ?>" name="tanggal1">
                      <div class="input-group-addon" style="background:white;"><small>Sampai</small></i></div>
                      <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                      <input type="text" autocomplete="off" class="form-control " required value="<?php if ($_GET['periode'] == 'y') {
                                                                                                    echo $tanggal2;
                                                                                                  } else {
                                                                                                    echo "";
                                                                                                  } ?>" id="tanggal2" name="tanggal2">
                    </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">
                <thead>
                  <tr>
                    <!--<th>Tanggal</th>-->
                    <!--<th>id transaksi</th> -->
                    <th width="15px">NO.</th>
                    <th width="15px">Kode</th>
                    <th width="15px">Divisi</th>
                    <th>keterangan</th>
                    <th>Debit</th>
                    <th>Kredit</th>
                    <th width="110">TOTAL (debit-kredit)</th>
                    <th width="100">Cetak laporan per bulan</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) order by id_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 tanggal BETWEEN '$tgl1' AND '$tgl2'");
                    } 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 ($no % 2 == 1) { ?>
                          #defff1;
                          <?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 namadivisi($row['id_divisi']); ?></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
                        $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;
                        ?>
                      </td>
                      <td>
                        <?php
                        if ($_GET['periode'] == 'y') {
                          echo "<a href='component/com_transaksi/export_pembukuan.php?periode=y&type=$row[id_jenis]&tanggal1=$tgl1&tanggal2=$tgl2' class='btn btn-primary btn-sm'><i class='fa fa-print'></i></a>";
                        } else {
                          echo "<a href='component/com_transaksi/export_pembukuan.php?periode=bln&type=$row[id_jenis]&bln=$bln&thn=$thn' class='btn btn-primary btn-sm'><i class='fa fa-print'></i></a>";
                        }
                        ?> </td>


                      <?php
                      $count++
                      ?>

                    </tr>

                  <?php } ?>



                  <tr bgcolor="#f6eee0">
                    <td colspan="4" 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>
              <?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 class="x_content">
                
                <table id="" class="table table-bordered" style="border-color:black; color:black">
                  <thead>
                  <tr>
                    <th colspan="6" for="namadivisi" style="color:black; text-align: center"><?php echo "periode $tanggal1 sampai $tanggal2"; ?></th>
                  </tr>
                </thead>
                </table>
                
                <?php
                
                 $query_divisi = mysqli_query($conn, "SELECT * FROM jenis_divisi order by id_divisi ASC ");
                 
                 
                 while ($row_divisi = mysqli_fetch_array($query_divisi)) {
                
                ?>

              <table id="" class="table table-bordered" style="border-color:black; color:black">
                  <thead>
                  <tr>
                    <th colspan="6" 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</th>
                    <th>keterangan</th>
                    <th>Debit</th>
                    <th>Kredit</th>
                    <th width="110">TOTAL<br>(debit-kredit)</th>
                    <th width="100">Cetak laporan<br>per bulan</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 tanggal BETWEEN '$tgl1' AND '$tgl2'");
                    } 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><h4><?php echo $row['nama_jenis']; ?></h4></td>

                      <td><h4><?php echo "Rp." . rupiah($rekap['jumlah_debit']); ?></h4></td>
                      <td><h4><?php echo "Rp." . rupiah($rekap['jumlah_kredit']); ?></h4></td>
                      <td><h4>
                        <?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;
                        ?></h4>
                      </td>
                      <td>
                        <?php
                        if ($_GET['periode'] == 'y') {
                          echo "<a href='component/com_transaksi/export_pembukuan.php?periode=y&type=$row[id_jenis]&tanggal1=$tgl1&tanggal2=$tgl2' class='btn btn-primary btn-sm'><i class='fa fa-print'></i></a>";
                        } else {
                          echo "<a href='component/com_transaksi/export_pembukuan.php?periode=bln&type=$row[id_jenis]&bln=$bln&thn=$thn' class='btn btn-primary btn-sm'><i class='fa fa-print'></i></a>";
                        }
                        ?> </td>


                      <?php
                      $count++
                      ?>

                    </tr>

                  <?php } ?>



                  <tr bgcolor="#f6eee0">
                    <td colspan="3" align="center"><b>TOTAL</b></td>
                    <td><h4><?php echo "Rp." . rupiah($total_debit); ?></h4></td>
                    <td><h4><?php echo "Rp." . rupiah($total_kredit); ?></h4></td>
                    <td><h4><?php echo "Rp." . rupiah($total_rekap); ?></h4></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 } ?>