<?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'];
                
                $tglpilih = date('Y-m-d', strtotime($tanggal1));
                
                $day = date('d', strtotime($tanggal1));
                $bln = date('m', strtotime($tanggal1));
                $thn = date('Y', strtotime($tanggal1));
                $namabulan=namabulan($bln);
              
                echo "<h2>Penyesuaian Harian Tanggal $day $namabulan tahun $thn</h2>";
              } else {
                $day = date('d');
                $bln = date('m');
                $thn = date('Y');
                $tglnow = date('Y-m-d');
                $namabulan=namabulan(date('m'));
                echo "<h2>Penyesuaian Harian Tanggal $day $namabulan tahun $thn</h2>";
              } ?>



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

<div class="well" style="overflow: auto">
              <form action="?module=<?php echo $_GET['module'];?>&periode=y" enctype="multipart/form-data" method="POST">


                

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

                  <div class="col-md-3 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>
                  </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>
                    </div>
                    
                    
              </form>
               <div class="col-md-9 col-sm-12 col-xs-12">
                            <?php if ($_GET['type'] == '5' or $_GET['type'] == '10' or $_GET['type'] == '11' or $_GET['type'] == '12' or $_GET['type'] == '13' or $_GET['type'] == '14') {
                            } else { ?>

                                <a class="btn btn-success btn-sm" data-toggle="modal" data-target="#pemasukan"><i class="glyphicon glyphicon-save-file"></i> Penyesuaian</a>
                       

                            <?php }
                            //khusu donatur

                            if ($_GET['type'] == '10') {
                                echo " <a class='btn btn-danger btn-sm' data-toggle='modal' data-target='#pengeluaran'><i class='glyphicon glyphicon-open-file'></i> Pengeluaran</a>";
                            }

                            ?>

                        </div>
</div>

            

            <div class="x_content">
                
                <?php
                
                 $query_divisi = mysqli_query($conn, "SELECT * FROM jenis_divisi order by id_divisi ASC ");
                 
                 
                 while ($row_divisi = mysqli_fetch_array($query_divisi)) {
                
                ?>

            <h2 for="namadivisi" style="color:black; text-align: center"><?php echo $row_divisi['nama_divisi']; ?></h2>
              <table id="" class="table table-bordered" style="border-color:black; color:black">
                <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 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='$tglpilih'");
                    } 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 tanggal='$tglnow'");
                    }

                    $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><h2><?php echo $row['nama_jenis']; ?></h2></td>

                      <td><h2><?php echo "Rp." . rupiah($rekap['jumlah_debit']); ?></h2></td>
                      <td><h2><?php echo "Rp." . rupiah($rekap['jumlah_kredit']); ?></h2></td>
                      <td><h2>
                        <?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;
                        ?></h2>
                      </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><h2><?php echo "Rp." . rupiah($total_debit); ?></h2></td>
                    <td><h2><?php echo "Rp." . rupiah($total_kredit); ?></h2></td>
                    <td><h2><?php echo "Rp." . rupiah($total_rekap); ?></h2></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>

<?php } elseif ($_GET['aksi'] == 'penyesuaian') {

    $tgl = date('Y-m-d', strtotime($_POST['tanggal']));
    //dari sumber
    
    $buatid1 = mysqli_query($conn, "SELECT MAX(id_transaksi) AS idbaru FROM transaksi");
                 $buatid = mysqli_fetch_array($buatid1);
                 $idbaru=$buatid[idbaru]+1;
                 $j2=namajenis($_POST[jenis_transaksi2]);
    
    $ket1="kode#$_POST[jenis_transaksi2] untuk kas $j2 # $_POST[keterangan]";
    $save1 = mysqli_query($conn, "INSERT INTO transaksi(id_transaksi,
                                  tanggal,
                                  debit,
                                  kredit,
                                  keterangan,id_jenis,id_pegawai,jenis_kas) VALUES('$idbaru',
                                                 '$tgl',
                                                 '0',
                                                 '$_POST[debit]',
                                                 '$ket1','$_POST[jenis_transaksi1]','$_SESSION[id]','$_POST[jenis_kas]')");

    //simpan transaksi tujuan
     if ($save1) {
          $buatid1 = mysqli_query($conn, "SELECT MAX(id_transaksi) AS idbaru FROM transaksi");
                 $buatid = mysqli_fetch_array($buatid1);
                $idbaru=$buatid[idbaru]+1;
                $j1=namajenis($_POST[jenis_transaksi1]);
         $ket2="kode#$_POST[jenis_transaksi1] dari kas $j1 # $_POST[keterangan]";
            $save = mysqli_query($conn, "INSERT INTO transaksi(id_transaksi,
                                  tanggal,
                                  debit,
                                  kredit,
                                  keterangan,id_jenis,id_pegawai,jenis_kas) VALUES('$idbaru',
                                                 '$tgl',
                                                 '$_POST[debit]',
                                                 '0',
                                                 '$ket2','$_POST[jenis_transaksi2]','$_SESSION[id]','$_POST[jenis_kas]')");
    }
    
    if (!$save) {
        echo "<script>nSimpan('?module=penyesuaianharian');</script>";
    } else {
        historysimpan($_POST['id'], $_SESSION['id'], $_SESSION['mac'], $_POST['jenis_transaksi']);
        echo "<script>ySimpan('?module=penyesuaianharian');</script>";
    }
}
?>



<!--  pemasukan -->
<div class="modal fade" id="pemasukan" 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">Tambah Data Penyesuaian</h4>
            </div>
            <div class="modal-body">
                <form action="?module=penyesuaianharian&aksi=penyesuaian" enctype="multipart/form-data" method="POST">
                    <?php
                    $query = "SELECT max(id_transaksi) as maxID FROM transaksi ";
                    $hasil = mysqli_query($conn, $query);
                    $data = @mysqli_fetch_array($hasil);
                    $rand = rand(100, 999);
                    include 'id_transaksi.php';
                    ?>
                    <label for="id">ID transaksi * :</label>
                    <input type="text" class="form-control" disabled value="<?php echo id_transaksi(); ?>" />
                    <input type="hidden" class="form-control" name="id" value="<?php echo id_transaksi(); ?>" />


                    <label f>Tanggal * :</label>
                    <div class="input-group">
                        <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                        <input type="text" class="form-control" required value="<?php if ($_GET['periode'] == 'y') {
                                                                                                                  echo $tanggal1;
                                                                                                                } else {
                                                                                                                  echo date('d-m-Y');;
                                                                                                                } ?>" id="tanggal4" name="tanggal" autocomplete="off">
                    </div>
                    <label f>Sumber Kas* :</label>
                    <select style="font-weight: bold;color:black;font-size:large" id="heard" class="form-control" required name="jenis_transaksi1">

                        <option value="" selected>- Pilih Jenis -</option>
                        
                      
                        <?php
                        if ($_SESSION['leveluser'] == 'admin') {
                        $query2  = "SELECT * FROM jenis_transaksi  WHERE jenis='0' ORDER BY nama_jenis";
                        $tampil2 = mysqli_query($conn, $query2);
                        while ($w = mysqli_fetch_array($tampil2)) {
                            echo "<option value=\"$w[id_jenis]\">$w[nama_jenis]</option>";
                        }
                        }else{ 
                            $query2  = "SELECT * FROM jenis_transaksi  WHERE jenis='1' OR jenis='3' OR id_jenis='7' ORDER BY nama_jenis";
                        $tampil2 = mysqli_query($conn, $query2);
                        while ($w = mysqli_fetch_array($tampil2)) {
                            echo "<option value=\"$w[id_jenis]\">$w[nama_jenis]</option>";
                        }
                        }
                        ?>
                    </select>
                    
                    <label f>Menuju Kas * :</label>
                    <select style="font-weight: bold;color:black;font-size:large" id="heard" class="form-control" required name="jenis_transaksi2">

                        <option value="" selected>- Pilih Jenis -</option>
                        
                      
                        <?php
                        if ($_SESSION['leveluser'] == 'admin') {
                        $query2  = "SELECT * FROM jenis_transaksi ORDER BY nama_jenis";
                        $tampil2 = mysqli_query($conn, $query2);
                        while ($w = mysqli_fetch_array($tampil2)) {
                            echo "<option value=\"$w[id_jenis]\">$w[nama_jenis]</option>";
                        }
                        }else{ 
                            $query2  = "SELECT * FROM jenis_transaksi  WHERE jenis='1' OR jenis='3' OR id_jenis='7' ORDER BY nama_jenis";
                        $tampil2 = mysqli_query($conn, $query2);
                        while ($w = mysqli_fetch_array($tampil2)) {
                            echo "<option value=\"$w[id_jenis]\">$w[nama_jenis]</option>";
                        }
                        }
                        ?>
                    </select>
                      
                    <label>Jumlah (Rp) *:</label>
                    <input type="hidden" class="form-control" name="kredit" />
                    <input style="font-weight: bold;color:black;font-size:large" type="number" class="form-control" id="t3" name="debit" autofocus=”autofocus” autocomplete="off" />
                    <div class="input-group">

                        <button type="button" class="btn btn-danger" onclick="resetcash();"><i class="fa fa-refresh" aria-hidden="true"></i></button>
                        <button type="button" class="btn btn-info" onclick="cashsepuluh();">+ 10.000</button>
                        <button type="button" class="btn btn-info" onclick="cashlima();">+ 5.000</button>
                        <button type="button" class="btn btn-info" onclick="cashdua();">+ 2.000</button>
                        <button type="button" class="btn btn-info" onclick="cashsatu();">+ 1.000</button>
                    </div>
                    <div class="input-group">

                        <!-- <button type="button" class="btn btn-danger" onclick="resetcash();"><i class="fa fa-refresh" aria-hidden="true"></i></button> -->
                        <button type="button" class="btn btn-info" onclick="cashlimaratus();">+ 500.000</button>
                        <button type="button" class="btn btn-info" onclick="cashseratus();">+ 100.000</button>
                        <button type="button" class="btn btn-info" onclick="cashlimapuluh();">+ 50.000</button>
                    </div>

                    <label f>Jenis Pembayaran * :</label>
                    <select style="font-weight: bold;color:black;font-size:large" id="heard" class="form-control" required name="jenis_kas">

                        <option value="">- Pilih Jenis -</option>
                        <?php

                        $query2  = "SELECT * FROM jenis_kas ORDER BY id";
                        $tampil2 = mysqli_query($conn, $query2);
                        while ($w = mysqli_fetch_array($tampil2)) {
                            if($w[id]=='6'){
                                $pilih="selected";
                            }else{
                              $pilih="";  
                            }
                            
                            echo "<option value=\"$w[id]\" $pilih>$w[nama]</option>";
                        }
                        ?>
                    </select>


                    <label>Keterangan *:</label>

                    <script type="text/javascript">
                        function changetextbox() {
                            if (document.getElementById("barang").value != 0) {
                                document.getElementById("keterangan").disabled = true;
                                document.getElementById("keterangan").value = '';
                            } else {
                                document.getElementById("keterangan").disabled = false;
                            }
                        }
                    </script>
                    <?php if ($_GET['type'] == 100) { ?>
                        <select onchange="changetextbox();" id="barang" class="form-control" name="level">
                            <option value=0>Lainnya</option>
                            <option value=2>yes</option>
                            <option value=3>semen</option>
                            <option value=4>semongko</option>
                            <option value=5>setia</option>
                            <option value=6>senama</option>

                        </select>
                    <?php } ?>
                    <!--<a data-dismiss="modal"  data-toggle="modal" data-target="#data_item"><i class="glyphicon glyphicon-open-file"></i> Data item</a>-->
                    <textarea class="form-control" name="keterangan" id="keterangan"></textarea>


            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Batal</button>
                <button type="submit" class="btn btn-success btn-sm">Simpan</button>
            </div>
            </form>
        </div>
    </div>
</div>


<script>
    //fungsi hitung tambah uang saku
    function sum() {
        var txtFirstNumberValue = document.getElementById('txt1').value;
        var txtSecondNumberValue = document.getElementById('txt3').value;
        var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
        if (!isNaN(result)) {
            document.getElementById('txt2').value = result;
            document.getElementById('txt4').value = result;
        } else {
            document.getElementById('txt2').value = txtFirstNumberValue;
        }

    }


    function cashsatu() {
        var txttambah = document.getElementById('t3').value;

        if (txttambah === "") {
            txttambah = 0;
        }
        var txttambah = parseInt(txttambah) + 1000;
        document.getElementById('t3').value = txttambah;
        document.getElementById('t4').value = txttambah;

    }

    function cashdua() {
        var txttambah = document.getElementById('t3').value;

        if (txttambah === "") {
            txttambah = 0;
        }
        var txttambah = parseInt(txttambah) + 2000;
        document.getElementById('t3').value = txttambah;
        document.getElementById('t4').value = txttambah;

    }

    function cashlima() {
        var txttambah = document.getElementById('t3').value;

        if (txttambah === "") {
            txttambah = 0;
        }
        var txttambah = parseInt(txttambah) + 5000;
        document.getElementById('t3').value = txttambah;
        document.getElementById('t4').value = txttambah;

    }

    function cashsepuluh() {
        var txttambah = document.getElementById('t3').value;

        if (txttambah === "") {
            txttambah = 0;
        }
        var txttambah = parseInt(txttambah) + 10000;
        document.getElementById('t3').value = txttambah;
        document.getElementById('t4').value = txttambah;

    }

    function cashlimapuluh() {
        var txttambah = document.getElementById('t3').value;

        if (txttambah === "") {
            txttambah = 0;
        }
        var txttambah = parseInt(txttambah) + 50000;
        document.getElementById('t3').value = txttambah;
        document.getElementById('t4').value = txttambah;

    }

    function cashseratus() {
        var txttambah = document.getElementById('t3').value;

        if (txttambah === "") {
            txttambah = 0;
        }
        var txttambah = parseInt(txttambah) + 100000;
        document.getElementById('t3').value = txttambah;
        document.getElementById('t4').value = txttambah;

    }

    function cashlimaratus() {
        var txttambah = document.getElementById('t3').value;

        if (txttambah === "") {
            txttambah = 0;
        }
        var txttambah = parseInt(txttambah) + 500000;
        document.getElementById('t3').value = txttambah;
        document.getElementById('t4').value = txttambah;

    }

    function resetcash() {

        var txttambah = "";
        document.getElementById('t3').value = txttambah;
        document.getElementById('t4').value = txttambah;


    }

    function isNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))

            return false;
        return true;
    }
</script>