<?php 
    include '../koneksi.php';
    $id = $_GET['id'];
    $rand = rand();

    $sql = mysqli_query($koneksi, "SELECT noperk,perk,kdc,awald,awalk FROM nawal WHERE noperk='$id'");
    list($noperk,$perk,$kdc,$awald,$awalk) = mysqli_fetch_array($sql);

    $sql2 = mysqli_query($koneksi, "SELECT sum(mutasid) as tmutasid, sum(mutasik) as tmutasik FROM jor WHERE noperk='$id' and kdc='$kdc' ");
    list($tmutasid,$tmutasik) = mysqli_fetch_array($sql2);    
    $akhird   = ($awald+$tmutasid-$tmutasik);
    
    $query1 = mysqli_query($mysqli, "UPDATE nawal SET mutasid    = '$tmutasid',
                                                      mutasik    = '$tmutasik',
                                                      akhird      = '$akhird'
                                                     WHERE noperk   = '$id'")
                                                or die('Ada kesalahan pada query update : '.mysqli_error($mysqli));    
?>

<script type="text/javascript">


</script>

<?php  
// fungsi untuk pengecekan tampilan form
// jika form add data yang dipilih
if ($_GET['form']=='add') { ?> 
  <!-- tampilan form add data -->
	<!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      <i class="fa fa-edit icon-title"></i><?php echo $_SESSION['hak_akses']; ?>-<?php echo $_SESSION['id_user']; ?> - Dana Masuk <?php echo $noperk ; ?> (<?php echo $perk ; ?>)
    </h1>
    <ol class="breadcrumb">
      <li><a href="?module=beranda"><i class="fa fa-home"></i> Beranda </a></li>
      <li><a href="?module=kas-keluar"> Kas Masuk </a></li>
      <li class="active"> Tambah </li>
    </ol>
  </section>

  <!-- Main content -->
  <section class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-primary">
          <!-- form start -->
          <form role="form" class="form-horizontal" action="modules/kas/proses_masuk.php?act=insert" method="POST" >
            <div class="box-body">
              <?php  
              // fungsi untuk membuat kode transaksi
              $query_id = mysqli_query($mysqli, "SELECT right(notrans,7) as kode FROM kas WHERE kasir=$_SESSION[id_user] ORDER BY notrans DESC
                                                LIMIT 1")
                                                or die('Ada kesalahan pada query tampil kode_transaksi : '.mysqli_error($mysqli));

              $count = mysqli_num_rows($query_id);

              if ($count <> 0) {
                  // mengambil data kode transaksi
                  $data_id = mysqli_fetch_assoc($query_id);
                  $kode    = $data_id['kode']+1;
              } else {
                  $kode = 1;
              }

              // buat kode_transaksi
              $buat_id        = str_pad($kode, 7, "0", STR_PAD_LEFT);
              $id_user        = $_SESSION['id_user'];
              $kode_transaksi2 = "$id_user$buat_id";
              ?>
              <div class="form-group">
                <label class="col-sm-2 control-label">Kode Toko</label>
                <div class="col-sm-1">
                  <input type="text" class="form-control" name="tunit" value="<?php echo $kdc; ?>" readonly required>
                </div>
                
                <div class="col-sm-2">
                  <input type="text" class="form-control" name="tnoperk" value="<?php echo $noperk; ?>" readonly required>
                </div>
                <label class="col-sm-1 control-label">Kd Trans</label>
                <div class="col-sm-2">
                  <input type="text" class="form-control" name="kode_transaksi" value="<?php echo $kode_transaksi2; ?>" readonly required>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Tanggal</label>
                <div class="col-sm-2">
                  <input type="text" class="form-control date-picker" data-date-format="dd-mm-yyyy" name="tanggal" autocomplete="off" value="<?php echo date("d-m-Y"); ?>" required>
                </div>
              </div>
              
              <div class="form-group">
                <label class="col-sm-2 control-label">Akunt Lawan </label>
                <div class="col-sm-4">
                  <select class="chosen-select" name="kode_akunt" data-placeholder="-- Pilih Akunt Lawan--" onchange="tampil_akunt(this)" autocomplete="off" required>
                    <option value=""></option>
                    <?php
                      $query_akunt = mysqli_query($mysqli, "SELECT noperk,perk,unit FROM nawal WHERE noperk>'110111' and kdc=$kdc and hapus<>'HAPUS' ORDER BY perk ASC")
                                                            or die('Ada kesalahan pada query tampil akunt : '.mysqli_error($mysqli));
                      while ($data_akunt = mysqli_fetch_assoc($query_akunt)) {
                        echo"<option value=\"$data_akunt[noperk]\"> $data_akunt[noperk] | $data_akunt[perk] </option>";
                      }
                    ?>
                  </select>
                </div>
              </div>

              <!-- <hr> -->
              
              <div class="form-group">
                <label class="col-sm-2 control-label">Nama Yg Membayar</label>
                <div class="col-sm-6">
                  <textarea cols="60" rows="1" input type="text" class="form-control" id="nama" name="nama" autocomplete="off" required></textarea>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">Alamat</label>
                <div class="col-sm-6">
                  <textarea cols="60" rows="1" input type="text" class="form-control" id="alamat" name="alamat" autocomplete="off" required></textarea>
                </div>
              </div>              
              <div class="form-group">
                <label class="col-sm-2 control-label">Uraian Kas</label>
                <div class="col-sm-6">
                  <textarea cols="60" rows="2" input type="text" class="form-control" id="uraian" name="uraian" autocomplete="off" required></textarea>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">Jumlah</label>
                <div class="col-sm-3">
                  <div class="input-group">
                    <span class="input-group-addon">Rp.</span>
                    <input type="text" class="form-control" id="jumlah" name="jumlah" autocomplete="off" onKeyPress="return goodchars(event,'0123456789',this)" required>
                  </div>
                </div>
              </div>

              
            </div><!-- /.box body -->

            <div class="box-footer">
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <input type="submit" class="btn btn-primary btn-submit" name="simpan" value="Simpan">
                  <a href="javascript:window.history.go(-1);" class="btn btn-default btn-reset">Batal</a>
                </div>
              </div>
              <!--
 					<center>
                        <a href="https://php.id/ref/31620">Untuk transaksi PENARIKAN BANK untuk Kas maka harus dibukukan 2 kali yaitu BANK KELUAR dan KAS MASUK. Demikian pula jika sebaliknya</a>
					</center>              
			  -->
            </div><!-- /.box footer -->

          </form>
          </div>

        </form>          
        </div><!-- /.box -->
      </div><!--/.col -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->
<?php
}
?>