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

  if ($_GET['periode'] == 'y') {
    $tanggal1 = $_POST['tanggal1'];
    $tanggal2 = $_POST['tanggal2'];
    $tgl1 = date('Y-m-d', strtotime($tanggal1));
    $tgl2 = date('Y-m-d', strtotime($tanggal2));
    $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE (id_jenis_sumber = '$_GET[type]' 
                     OR id_jenis = '$_GET[type]') AND tanggal BETWEEN '$tgl1' AND '$tgl2'");
  } else {
    $hariini = date('Y-m-d');
    $query_saldo = mysqli_query($conn, "SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE (id_jenis_sumber = '$_GET[type]' 
                     OR id_jenis = '$_GET[type]') AND tanggal='$hariini'");
  }
  $row_saldo = mysqli_fetch_array($query_saldo);
  $saldo_keseluruhan = $row_saldo['jumlah_debit'] - $row_saldo['jumlah_kredit'];


?>

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

      <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
          <div class="x_panel">
            <div class="x_title" style="text-transform: capitalize; margin:0;">
              <h2 style="color:black">Data <?php echo "$_GET[module] ";

                        $query = mysqli_query($conn, "SELECT * FROM jenis_transaksi WHERE id_jenis='$_GET[type]'");

                        while ($row = mysqli_fetch_array($query)) {
                          echo $row['nama_jenis'];
                          $jentran=$row['jenis']; //untuk mengambil jenis transaksi sebagai pembeda jenis transaksi pengeluaran
                          $idjentran=$row['id_jenis'];
                        }

                        ?> <small></small></h2>

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


             


                <div class="well" style="overflow: auto">
                <form action="?module=transaksi&type=<?php echo $jenis; ?>&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 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="col-md-9 col-sm-12 col-xs-12">
                
                
                
              <?php 
              // jika dari divisi sd dan rt
               if ($_SESSION['id_divisi']=="3") {
     
            }elseif($_SESSION['id_divisi']=="2"){
       
            }else{
      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 { ?>

               
              <?php }
              //khusu donatur

              if ($_GET['type'] == '10') {
                 }

            }
              
              
             
              ?>

            </div>


            <div class="col-md-3 col-sm-12 col-xs-12" style="margin-left: 0px;">

              <h4>
                <table>
                  <tr style="color:green">
                    <td><small><b>TOTAL DEBIT </b></small></td>
                    <td align="right">&nbsp; Rp. <?php echo rupiah($row_saldo['jumlah_debit']); ?></td>
                  </tr>
                  <tr style="color:red">
                    <td id="hit"><small><b>TOTAL KREDIT </b></small></td>
                    <td align="right">&nbsp; Rp. <?php echo rupiah($row_saldo['jumlah_kredit']); ?></td>
                  </tr>
                  <tr>
                    <td colspan="2" height="2px" bgcolor="black"></td>
                  </tr>
                  <tr>
                    <td id="hit"><small><b>TOTAL </b></small></td>
                    <td align="right">&nbsp; Rp. <?php echo rupiah($saldo_keseluruhan); ?></td>
                  </tr>
                </table>
              </h4>
            </div>



            <a class="btn btn-primary btn-sm" href="component/com_transaksi/export_pembukuan.php?type=<?php echo $jenis; ?>&<?php if ($_GET['periode'] == 'y') {
                                                                                                                              echo "periode=y&tanggal1=$tgl1&tanggal2=$tgl2";
                                                                                                                            } else {
                                                                                                                              echo "periode=";
                                                                                                                            } ?>"><i class="fa fa-print"></i>cetak</a>

            <div class="x_content">

              <table style="color:black"  id="datatable" class="table table-striped table-bordered">
                <thead>
                  <tr>
                    <th>Tipe</th>
                    <th>Kode transaksi</th>
                    <th>Tanggal</th>
                    <th>Jenis Sumber Tran.</th>
                    <th>Transaksi</th>
                    <!-- SPP DAN KOPERASI -->
                    <?php if ($_GET['type'] == '5' or $_GET['type'] == '6' or $_GET['type'] == '14' or $_GET['type'] == '17') { ?>
                      <th>Id Santri</th>
                      <th>Nama Santri</th>
                    <?php } ?>
                    <!-- RT PAGI DAN SORE -->
                    <?php if ($_GET['type'] == '11'  or $_GET['type'] == '12') { ?>
                      <th>Id Santri</th>
                      <th>Nama Santri</th>
                      <th>Kelas</th>
                    <?php } ?>
                    <!-- DONATUR -->
                    <?php if ($_GET['type'] == '10') { ?>
                      <th>Id Donatur</th>
                      <th>Nama Donatur</th>
                      <th>Jenis Donasi</th>
                    <?php } ?>
                    <!-- JEnis kas -->
                    <td>
                      Jenis Kas
                    </td>
                    <th>Keterangan</th>
                    <th>Debit</th>
                    <th>Kredit</th>
                    <th>Saldo</th>
                    <?php if ($_GET['type'] == '5' or $_GET['type'] == '10' or $_GET['type'] == '11' or $_GET['type'] == '12' or $_GET['type'] == '13') {
                    } else { ?>
                      <th>aksi</th>
                    <?php } ?>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  if ($_GET['periode'] == 'y') {
                    $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE
                     (id_jenis_sumber = '$_GET[type]' 
                     OR id_jenis = '$_GET[type]') 
                     AND tanggal 
                     BETWEEN '$tgl1' AND '$tgl2' order by tanggal ASC ");
                  } else {
                    $hariini = date('Y-m-d');
                    $query = mysqli_query($conn, "SELECT * 
                                                  FROM transaksi 
                                                  WHERE (id_jenis_sumber = '$_GET[type]' OR id_jenis = '$_GET[type]') 
                                                  AND tanggal = '$hariini' 
                                                  ORDER BY tanggal ASC");
                  }

                  $count = 2;



                  while ($row = mysqli_fetch_array($query)) {
                    $no++;
                  ?>

                    <tr style="background: <?php if ($row['kredit'] == 0) { ?>
                          #defff1;
                          <?php } else { ?>
                            #feeeea;
                            <?php } ?>">
                      <td><?php if ($row['kredit'] == 0) { ?><a class="btn btn-success btn-xs"><i class="glyphicon glyphicon-save-file"></i></a> <?php } else { ?> <a class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-open-file"></i></a><?php } ?></td>
                      <td><?php echo $row['id_transaksi']; ?></td>
                      <td><?php echo $row['tanggal']; ?></td>
                      <td><?php echo namajenis($row['id_jenis_sumber']); ?></td>
                      <td><?php echo namajenis($row['id_jenis']); ?></td>
                      <!-- SPP, DAM DAN KOPERASI -->
                      <?php if ($_GET['type'] == '5' or $_GET['type'] == '6' or $_GET['type'] == '14' or $_GET['type'] == '17') { ?>
                        <td><?php echo $row['id_santri']; ?></td>
                        <td><?php $keri  = mysqli_query($conn, "SELECT * FROM santri where id_santri='$row[id_santri]' order by id_santri asc");
                            while ($r = mysqli_fetch_array($keri)) {
                              echo "$r[nama]";
                            ?></td>
                    <?php }
                          } ?>
                    <!-- RT PAGI DAN SORE -->
                    <?php if ($_GET['type'] == '11'  or $_GET['type'] == '12') { ?>
                      <td><?php echo $row['id_santri']; ?></td>
                      <td><?php $keri  = mysqli_query($conn, "SELECT * FROM santri_rt where id_santri='$row[id_santri]' order by id_santri asc");
                          while ($r = mysqli_fetch_array($keri)) {
                            echo "$r[nama]";
                            $jenrt = $r['jenis_rt'];
                          }
                          ?></td>
                      <td><?php $jen_rt  = mysqli_query($conn, "SELECT * FROM jenis_rt where id='$jenrt' order by id asc");
                          while ($jenrt = mysqli_fetch_array($jen_rt)) {
                            echo "$jenrt[nama_jenis]";
                          }
                          ?></td>
                    <?php
                    } ?>


                    <?php if ($_GET['type'] == '10') { ?>
                      <td><?php

                          if ($row['id_donatur'] == "") {
                            echo "";
                          } else {
                            echo $row['id_donatur'];
                          }

                          ?></td>
                      <td><?php

                          if ($row['id_donatur'] == "") {
                            echo $row['nama_donatur_tidak_tetap'];
                          } else {
                            $keri  = mysqli_query($conn, "SELECT * FROM donatur where id_donatur='$row[id_donatur]' order by id_donatur asc");
                            while ($r = mysqli_fetch_array($keri)) {
                              echo "$r[nama_donatur]";
                            }
                          }
                          ?></td>
                          <td>
                          <?php
                          echo namajenisdonatur($row['jenis_donatur']);
                          ?>
                          </td>
                          
                          
                          
                          
                          <?php } ?>
                    <?php 
                    ?>

                    <!-- Jenis kas -->
                    <td>
                      <?php
                      $jeniskas = mysqli_query($conn, "SELECT * FROM jenis_kas WHERE id='$row[jenis_kas]'");
                      $jekas = mysqli_fetch_array($jeniskas);
                      echo $jekas['nama'];
                      ?>
                    </td>

                    <td><?php
                        $jenrt1 = mysqli_query($conn, "SELECT nama_jenis FROM jenis_rt WHERE id=$row[id_jenis_rt]");
                        $jnrt = mysqli_fetch_array($jenrt1);
                        if ($row['id_jenis_rt'] == "") {
                          $jen = "";
                        } else {
                          $jen = $jnrt['nama_jenis'] . " | ";
                        }
                        if ($_GET['type'] == '5') {
                          echo "SPP $row[spp_bulan]-$row[spp_tahun] | ";
                        }

                        echo $jen . $row['keterangan'];

                        if ($_GET['type'] == '21') {
                          echo nama($row['id_santri'])."-".$row['id_santri'];
                        } ?></td>

                    <?php if ($count == 1) { ?>

                      <td><?php echo "Rp." . rupiah($row['debit']); ?></td>
                      <td><?php echo "Rp." . rupiah($row['kredit']); ?></td>
                      <td>
                        <?php
                        $debit = $row['debit'];
                        $saldo = $row['debit'];
                        echo "Rp." . rupiah($saldo);
                        ?>
                      </td>

                      <?php } else {
                      if ($row['debit'] != 0) {
                      ?>
                        <td><?php echo "Rp." . rupiah($row['debit']); ?></td>
                        <td><?php echo "Rp." . rupiah($row['kredit']); ?></td>
                        <td>
                          <?php
                          $debit = $debit + $row['debit'];
                          $saldo = $saldo + $row['debit'];
                          echo "Rp." . rupiah($saldo);
                          ?>


                        <?php } else { ?>
                        <td><?php echo "Rp." . rupiah($row['debit']); ?></td>
                        <td><?php echo "Rp." . rupiah($row['kredit']); ?></td>
                        <td>
                      <?php
                        $kredit = $kredit + $row['kredit'];
                        $saldo = $saldo - $row['kredit'];
                        echo "Rp." . rupiah($saldo);
                      }
                    }
                    $count++


                      ?>

                      <?php if ($_GET['type'] == '5' or $_GET['type'] == '10'  or $_GET['type'] == '11' or $_GET['type'] == '12' or $_GET['type'] == '13') {
                          
                            if ($_SESSION['leveluser'] == 'admin') {
                          ?>
                          
                           <td>
                          <?php
                          //akses edit bulan lalu
                          $blnakses = date('n', strtotime($row['tanggal']));
                          $thnakses = date('Y', strtotime($row['tanggal']));

                          $cek_akses = "SELECT * FROM akses_edit WHERE bln = '$blnakses' AND thn = '$thnakses'";
                          $cekakses1 = mysqli_query($conn, $cek_akses);
                          $count = mysqli_num_rows($cekakses1);
                          if (($blnakses >= date('n')) && ($thnakses >= date('Y'))) {

                          ?>

                            <a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=form_edit&id_tran=<?php echo $row['id_transaksi']; ?>&type=<?php echo $_GET['type']; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a>
                            <a class="btn  btn-danger btn-xs" onclick="confirm_modal('?module=<?php echo $_GET['module']; ?>&aksi=hapus&type=<?php echo $_GET['type']; ?>&id_tran=<?php echo $row['id_transaksi']; ?>');"><i class="glyphicon glyphicon-trash"></i> Hapus</a>

                          <?php } else if ($count == 1) { ?>
                            <a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=form_edit&id_tran=<?php echo $row['id_transaksi']; ?>&type=<?php echo $_GET['type']; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a>
                            <a class="btn  btn-danger btn-xs" onclick="confirm_modal('?module=<?php echo $_GET['module']; ?>&aksi=hapus&type=<?php echo $_GET['type']; ?>&id_tran=<?php echo $row['id_transaksi']; ?>');"><i class="glyphicon glyphicon-trash"></i> Hapus</a>

                          <?php

                          } else {
                            echo "<a data-toggle='modal' data-target='#akses_edit'><i>Akses dibatasi</i></a>";
                          }

                          ?>

                        </td>
                          
                          
                          
                          <?php
                      }} else {


                      ?>
                        <td>
                          <?php
                          //akses edit bulan lalu
                          $blnakses = date('n', strtotime($row['tanggal']));
                          $thnakses = date('Y', strtotime($row['tanggal']));

                          $cek_akses = "SELECT * FROM akses_edit WHERE bln = '$blnakses' AND thn = '$thnakses'";
                          $cekakses1 = mysqli_query($conn, $cek_akses);
                          $count = mysqli_num_rows($cekakses1);
                          if (($blnakses >= date('n')) && ($thnakses >= date('Y'))) {

                          ?>

                            <a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=form_edit&id_tran=<?php echo $row['id_transaksi']; ?>&type=<?php echo $_GET['type']; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a>
                            <a class="btn  btn-danger btn-xs" onclick="confirm_modal('?module=<?php echo $_GET['module']; ?>&aksi=hapus&type=<?php echo $_GET['type']; ?>&id_tran=<?php echo $row['id_transaksi']; ?>');"><i class="glyphicon glyphicon-trash"></i> Hapus</a>

                          <?php } else if ($count == 1) { ?>
                            <a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=form_edit&id_tran=<?php echo $row['id_transaksi']; ?>&type=<?php echo $_GET['type']; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a>
                            <a class="btn  btn-danger btn-xs" onclick="confirm_modal('?module=<?php echo $_GET['module']; ?>&aksi=hapus&type=<?php echo $_GET['type']; ?>&id_tran=<?php echo $row['id_transaksi']; ?>');"><i class="glyphicon glyphicon-trash"></i> Hapus</a>

                          <?php

                          } else {
                            echo "<a data-toggle='modal' data-target='#akses_edit'><i>Akses dibatasi</i></a>";
                          }

                          ?>

                        </td>

                    </tr>

                <?php
                      }
                    } ?>




                </tbody>
              </table>
            </div>
          </div>
        </div>

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


  <!-- edit data transaksi -->
<?php } elseif ($_GET['aksi'] == 'form_edit') {  ?>
  <div class="col" role="main">
    <div class="">
      <div class="clearfix"></div>
      <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
          <div class="x_panel">

            <form action="?module=<?php echo "transaksi"; ?>&aksi=simpan_edit" enctype="multipart/form-data" method="POST">
              <?php


              $query = "SELECT * FROM transaksi WHERE id_transaksi='$_GET[id_tran]' ";
              $hasil = mysqli_query($conn, $query);
              $data = mysqli_fetch_array($hasil);
              if ($data['kredit'] == 0) {
                $jenis = "1";
              } else {
                $jenis = "0";
              }

              ?>
              <label for="id">ID Transaksi * :</label>
              <input type="text" class="form-control" disabled value="<?php echo $_GET['id_tran'];
                                                                      ?>" />
              <input type="hidden" class="form-control" name="id" value="<?php echo $_GET['id_tran']; ?>" />
              <input type="hidden" class="form-control" name="type" value="<?php echo $_GET['type']; ?>" />

              <label for="id">Jenis * :</label>
              <input type="text" class="form-control" disabled value="<?php if ($jenis == "1") {
                                                                        echo "pemasukan";
                                                                      } else {
                                                                        echo "pengeluaran";
                                                                      }
                                                                      ?>">
              <input type="hidden" class="form-control" name="jenis" value="<?php echo $jenis; ?>" />


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

                                <option value="" selected>- Pilih Jenis -</option>
                                
                            
                                <?php
                                if ($_SESSION['leveluser'] == 'admin' OR 'user1') {
                                $query2  = "SELECT * FROM jenis_transaksi WHERE jenis='0' ORDER BY nama_jenis";
                                $tampil2 = mysqli_query($conn, $query2);
                                while ($w = mysqli_fetch_array($tampil2)) {
                                    if ($data['id_jenis_sumber'] == $w['id_jenis']) {
                                        echo "<option selected value=\"$w[id_jenis]\">$w[nama_jenis]</option>";
                                        }else{
                                        echo "<option value=\"$w[id_jenis]\">$w[nama_jenis]</option>";
                                        }
                                }
                                }else{ 
                                    $query2  = "SELECT * FROM jenis_transaksi  WHERE jenis='0' ORDER BY nama_jenis";
                                $tampil2 = mysqli_query($conn, $query2);
                                while ($w = mysqli_fetch_array($tampil2)) {
                                    if ($data['id_jenis_sumber'] == $w['id_jenis']) {
                                    echo "<option selected value=\"$w[id_jenis]\">$w[nama_jenis]</option>";
                                    }else{
                                    echo "<option value=\"$w[id_jenis]\">$w[nama_jenis]</option>";
                                    }
                                }
                                }
                                ?>
                            </select>

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

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

                                $query2  = "SELECT * FROM jenis_transaksi ORDER BY nama_jenis ASC";
                                $tampil2 = mysqli_query($conn, $query2);
                                while ($w = mysqli_fetch_array($tampil2)) { ?>
                                    <option value="<?php echo $w['id_jenis']; ?>" <?php if ($data['id_jenis'] == $w['id_jenis']) {
                                                                                        echo "selected";
                                                                                    } ?>><?php echo $w['nama_jenis']; ?></option>";
                                <?php }
                                ?>
                            </select>



              <label>Jumlah pembayaran *:</label>
              <input type="number" class="form-control" autocomplete="off" name="jumlah_bayar" value="<?php if ($data['debit'] == "0") {
                                                                                                        echo $data['kredit'];
                                                                                                      } else {
                                                                                                        echo $data['debit'];
                                                                                                      } ?>" required />

              <label f>Jenis Pembayaran * :</label>
              <select id="heard" class="form-control" required name="jenis_kas">

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

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

              <label for="alamat">Keterangan :</label>
              <textarea class="form-control" name="keterangan"><?php echo $data['keterangan']; ?></textarea>


          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default btn-sm" onclick=self.history.back()>Batal</button>
            <button type="submit" class="btn btn-success btn-sm">Simpan</button>
          </div>
          </form>

        </div>
      </div>
    </div>
  </div>
  </div>






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

  $tgl = date('Y-m-d', strtotime($_POST['tanggal']));
  $save = mysqli_query($conn, "INSERT INTO transaksi(id_transaksi,
                                  tanggal,
                                  debit,
                                  kredit,
                                  keterangan,id_jenis,id_pegawai,jenis_kas) VALUES('$_POST[id]',
                                                 '$tgl',
                                                 '$_POST[debit]',
                                                 '0',
                                                 '$_POST[keterangan]','$_POST[jenis_transaksi]','$_SESSION[id]','$_POST[jenis_kas]')");
  // echo "<script language='javascript'>document.location='?module=transaksi&type=$_POST[jenis_transaksi]';</script>";
  if (!$save) {
    echo "<script>nSimpan('?module=transaksi&type=$_POST[jenis_transaksi]');</script>";
  } else {
    historysimpan($_POST['id'], $_SESSION['id'], $_SESSION['mac'], $_POST['jenis_transaksi']);
    echo "<script>ySimpan('?module=transaksi&type=$_POST[jenis_transaksi]');</script>";
  }

  // $query_saldo=mysqli_query($conn,"SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE id_nasabah ='$_POST[id_nasabah]'");
  // $saldo = mysqli_fetch_array($query_saldo);
  // $saldoo= $saldo['jumlah_debit'] - $saldo['jumlah_kredit'];
  // mysqli_query($conn,"UPDATE nasabah SET saldo = '$saldoo'
  //                                 WHERE id_nasabah = '$_POST[id_nasabah]'");




  //echo "<script language='javascript'>document.location='?module=transaksi&hehe=';</script>";


?>


<?php


} elseif ($_GET['aksi'] == 'pengeluaran') {
  $tgl = date('Y-m-d', strtotime($_POST['tanggal']));
  $save = mysqli_query($conn, "INSERT INTO transaksi(id_transaksi,
                                  tanggal,
                                  debit,
                                  kredit,
                                  keterangan,id_jenis,id_jenis_sumber,id_pegawai,jenis_kas) VALUES('$_POST[id]',
                                                 '$tgl',
                                                 '0',
                                                 '$_POST[kredit]',
                                                 '$_POST[keterangan]','$_POST[jenis_transaksi]','$_POST[jenis_transaksi_sumber]','$_SESSION[id]','$_POST[jenis_kas]')");

  // echo "<script language='javascript'>document.location='?module=transaksi&type=$_POST[jenis_transaksi]';</script>";
  if (!$save) {
    echo "<script>nSimpan('?module=transaksi&type=$_POST[jenis_transaksi]');</script>";
  } else {
    historysimpan($_POST['id'], $_SESSION['id'], $_SESSION['mac'], $_POST['jenis_transaksi']);
    echo "<script>ySimpan('?module=transaksi&type=$_POST[jenis_transaksi]');</script>";
  }
} elseif ($_GET['aksi'] == 'simpan_edit') {

  $module = $_GET['module'];
  $tgl1 = date('Y-m-d', strtotime($_POST['tanggal']));

  //log edit 
  $query = "SELECT * FROM transaksi WHERE id_transaksi='$_POST[id]' ";
  $hasil = mysqli_query($conn, $query);
  $data = mysqli_fetch_array($hasil);


  $logedit = $data['log_edit'] . "[" . $_SESSION['id'] . "~" . date('Y-m-d H:i:s') . "]";


  if ($_POST['jenis'] == "1") {

    $save = mysqli_query($conn, "UPDATE transaksi SET tanggal = '$tgl1',
                                    debit = '$_POST[jumlah_bayar]',
                                    keterangan = '$_POST[keterangan]',
                                    jenis_kas='$_POST[jenis_kas]',
                                    log_edit='$logedit'
                                    WHERE id_transaksi = '$_POST[id]'") or die("GAGAL");
  } else {
    $save = mysqli_query($conn, "UPDATE transaksi SET tanggal = '$tgl1',
    kredit = '$_POST[jumlah_bayar]',
    keterangan = '$_POST[keterangan]',id_jenis_sumber='$_POST[jenis_transaksi_sumber]',
    jenis_kas='$_POST[jenis_kas]',
    log_edit='$logedit',
    id_jenis_sumber='$_POST[jenis_transaksi_sumber]'
    WHERE id_transaksi = '$_POST[id]'") or die("GAGAL");
  }

  // echo "<script language='javascript'>document.location='?module=transaksi&type=$_POST[type]';</script>";
  if (!$save) {
    echo "<script>nSimpan('?module=transaksi&type=$_POST[type]');</script>";
  } else {
    historyedit($_POST['id'], $_SESSION['id'], $_SESSION['mac'], $_POST['jenis_transaksi']);
    echo "<script>ySimpan('?module=transaksi&type=$_POST[type]');</script>";
  }
} elseif ($_GET['aksi'] == 'hapus') {

  $query = mysqli_query($conn, "DELETE FROM transaksi WHERE id_transaksi='$_GET[id_tran]'") or die("GAGAL");
  // echo "<script language='javascript'>document.location='?module=transaksi&type=$_GET[type]';</script>";
  if (!$query) {
    echo "<script>ySimpan('?module=transaksi&type=$_POST[type]');</script>";
  } else {
    historyhapus($_GET['id_tran'], $_SESSION['id'], $_SESSION['mac'], $_POST['jenis_transaksi']);
    echo "<script>ySimpan('?module=transaksi&type=$_POST[type]');</script>";
  }
} ?>

<!-- Modal -->
<div class="modal fade" id="modalAdd" 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">Setoran duit</h4>
      </div>
      <div class="modal-body">

        <form action="?module=transaksi&aksi=setoran_tunai" class="form-horizontal form-label-left" method="POST">

          <div class="form-group">
            <div class="col-sm-12 col-sm-12 col-xs-12 ">
              <div class="input-group">
                <input type="text" class="typeahead form-control" placeholder="Tulis Nomor Rekening" required name="no_rekening" autofocus=”autofocus” autocomplete="off">
                <span class="input-group-btn">
                  <button type="submit" class="btn btn-success"><i class="fa fa-search"></i> Cari</button>
                </span>

              </div>

            </div>
          </div>
        </form>

      </div>

      <!-- end form for validations -->
    </div>
  </div>
</div>
<!-- /modal -->


<!-- Modal -->
<div class="modal fade" id="tarikAdd" 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">Penarikan Tunai</h4>
      </div>
      <div class="modal-body">

        <form action="?module=transaksi&aksi=penarikan_tunai" class="form-horizontal form-label-left" method="POST">

          <div class="form-group">
            <div class="col-sm-12 col-sm-12 col-xs-12 ">
              <div class="input-group">
                <input type="text" class="typeahead form-control" placeholder="Tulis Nomor Rekening" required name="no_rekening" autofocus=”autofocus” autocomplete="off">
                <span class="input-group-btn">
                  <button type="submit" class="btn btn-danger"><i class="fa fa-search"></i> Cari</button>
                </span>

              </div>

            </div>
          </div>
        </form>

      </div>

      <!-- end form for validations -->
    </div>
  </div>
</div>
<!-- /modal -->

<!--  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 pemasukan</h4>
      </div>
      <div class="modal-body">
        <form action="?module=transaksi&aksi=pemasukan" 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 echo date('d-m-Y'); ?>" id="tanggal4" autocomplete="off">
            <input type="hidden" value="<?php echo date('d-m-Y'); ?>" name="tanggal">
          </div>

          <label>Jumlah (Rp) *:</label>
          <input type="hidden" class="form-control" name="kredit" />
          <input 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>
          <input type="hidden" class="form-control" name="jenis_transaksi" value="<?php echo $jenis; ?>">

          <label f>Jenis Pembayaran * :</label>
          <select id="heard" class="form-control" required name="jenis_kas">

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

            $query2  = "SELECT * FROM jenis_kas ORDER BY id";
            $tampil2 = mysqli_query($conn, $query2);
            while ($w = mysqli_fetch_array($tampil2)) {
              echo "<option value=\"$w[id]\">$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>

<!--  input pengeluaran -->
<div class="modal fade" id="pengeluaran" 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 pengeluaran</h4>
      </div>
      <div class="modal-body">
        <form action="?module=transaksi&aksi=pengeluaran" 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);
          $idMax = $data['maxID'];
          $rand = rand(100, 999);

          ?>
          <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" value="<?php echo date('d-m-Y'); ?>" required id="tanggal3" autocomplete="off">
            <input type="hidden" value="<?php echo date('d-m-Y'); ?>" name="tanggal">
          </div>

          <label f>Sumber Kas Transaksi * :</label>
                    <select <?php if($jentran='0') { echo "disabled"; } ?> style="font-weight: bold;color:black;font-size:large" id="heard" class="form-control" required name="jenis_transaksi_sumber">

                        <option value="" selected>- Pilih Jenis -</option>
                        
                      
                        <?php
                        if ($_SESSION['leveluser'] == 'admin' OR 'user1') {
                        $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>Jumlah (Rp) *:</label>

          <input type="hidden" class="form-control" name="debit" />
          <input type="number" id="t4" class="form-control" name="kredit" autofocus=”autofocus” autocomplete="off">
          <input type="hidden" class="form-control" name="jenis_transaksi" value="<?php echo $jenis; ?>">
          <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>

          <label f>Jenis Pembayaran * :</label>
          <select id="heard" class="form-control" required name="jenis_kas">

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

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


          <label>Keterangan *:</label>
          <textarea class="form-control" name="keterangan" required></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>


<!--  data item -->
<div class="modal fade" id="data_item" 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">Data item</h4>
      </div>
      <div class="modal-body">
        <form action="?module=transaksi&aksi=pengeluaran" 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);
          $idMax = $data['maxID'];

          $noUrut = (int) substr($idMax, 1, 9);
          $noUrut++;
          $char = "T";
          $newID = $char . sprintf("%04s", $noUrut);
          ?>
          <label for="id">ID transaksi * :</label>
          <input type="text" class="form-control" disabled value="<?php echo $newID; ?>" />
          <input type="hidden" class="form-control" name="id" value="<?php echo $newID; ?>" />


          <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 id="tanggal2" name="tanggal" autocomplete="off">
          </div>

          <label>Keterangan *:</label>
          <textarea class="form-control" name="keterangan" required></textarea>

          <label>Jumlah (Rp) *:</label>
          <input type="hidden" class="form-control" name="debit" />
          <input type="text" class="form-control" name="kredit" autofocus=”autofocus” autocomplete="off" />
          <input type="hidden" class="form-control" name="jenis_transaksi" value="<?php echo $jenis; ?>">
      </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>

<!-- modal delete-->
<div class="modal fade" id="modal_delete">
  <div class="modal-dialog">
    <div class="modal-content" style="margin-top:100px;">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" style="text-align:center;">Apakah anda yakin menghapus data ini ?</h4>
      </div>
      <div class="modal-footer" style="margin:0px; border-top:0px; text-align:center;">
        <a href="#" class="btn btn-danger btn-sm" id="delete_link">Hapus</a>
        <button type="button" class="btn btn-success btn-sm" data-dismiss="modal">Batal</button>
      </div>
    </div>
  </div>
</div>

<!-- modal akses edit-->
<div class="modal fade" id="akses_edit">
  <div class="modal-dialog">
    <div class="modal-content" style="margin-top:100px;">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" style="text-align:center;">Hubungi Administrator untuk merevisi</h4>
      </div>
      <div class="modal-footer" style="margin:0px; border-top:0px; text-align:center;">
        <button type="button" class="btn btn-success btn-sm" data-dismiss="modal">OK</button>
      </div>
    </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>