<?php 
    session_start();
    include 'header.php'; 
            //////////////////////////////////////////////////////////////////////////// 
        $oleh   = $_SESSION['id_kar'];
        $kdc    = $_SESSION['kdc'];
        $tabel_tujuan   = "cmob_trans_".$oleh."_".$kdc;
        $tabel_asal     = "cmob_trans";
            ////////////////////////////////////////////////////////////////////////////  
?>

<div class="container">

  <div class="my-2 border-bottom">
    <h5 class="fw-semibold fs-5">TRANSAKSI PENYUSUTAN ASET TETAP</h5>
  </div>

  <section class="content">
    <div class="row">
      <section class="col-lg-12">
        <div class="box box-info">
          <div class="box-header">
          <div class="box-body">
            <form method="get" action="">

            </form>
          </div>
        </div>
        
        <div class="box box-info">
          <div class="box-header">
            <h6 class="box-title"></h6>
          </div>
          <div class="box-body">
            <?php 
            if(isset($_GET['xbulan']) && isset($_GET['xtahun']) && isset($_GET['xkas'])){
              $bulane    = $_GET['xbulan'];
              $tahune    = $_GET['xtahun'];
              $kase      = $_GET['xkas'];
              //$tahune    = $_SESSION['tahun'] ;
                
                    $sql11  = mysqli_query($koneksi, "SELECT kd AS tkd,nama,akunt_ak,akunt_ph AS tnama FROM kdjenis_at WHERE kd='$kase' ");
                    list($tkd,$tnama,$akunt_ak,$akunt_ph) = mysqli_fetch_array($sql11);
                    
                    $sql12  = mysqli_query($koneksi, "SELECT bulan FROM aa_bulan WHERE ke=$bulane ");
                    list($bulan) = mysqli_fetch_array($sql12); 
                    
                     mysqli_query($koneksi, "delete from $tabel_tujuan where kd<>'$tkd' ");
            ?>
              <div class="row">
                <div class="col-lg-6">
                  <table class="table table-bordered">
                    <tr>
                      <th width="30%">Bulan </th>
                      <th width="1%">:</th>
                      <td><?php echo $bulan ; ?> - <?php echo $tahune ; ?></td>
                    </tr>
                    <tr>
                      <th>Jenis </th>
                      <th>:</th>
                      <td><?php echo $tkd ; ?> ( <?php echo $tnama ?> )</td>
                    </tr>
                  </table>
                </div>
              </div>

    <div class="card-body">
      <div class="table-responsive">
        <table class="table table-bordered table-striped" id="table-datatable">
          <thead>
            <tr>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">No</th>
              <th width="6%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Kode</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Beli</th>
              <th width="20%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Nama</th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">HBeli</th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Akum Peny</th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Peny/bln</th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;"><font color='#ff0000'>Peny Skrg</font></th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Iput Penyusutan</th>
              <th width="2%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Opsi</th>
            </tr>
          </thead>
          <tbody>
            <?php 
              $no   = 1;
              $awal = $awale;
              $debet  = 0;
              $kredit  = 0;
              //WHERE kd='$kase' and month(tanggal)='$bulane' and year(tanggal)='$tahune'
            $jjj = mysqli_query($koneksi,"SELECT * FROM $tabel_tujuan WHERE kd='$kase' order by id ASC ");
            while($p = mysqli_fetch_array($jjj)){
                $kode   =$p['kode'];
                $peny   =$p['be_ph'];
                $dft = mysqli_query($koneksi,"select * from cmob_brg where kode='$kode' ");
                    $n = mysqli_fetch_assoc($dft);  
                $sql10 = mysqli_query($koneksi, "SELECT akunt_ph FROM kdjenis_at WHERE kd='$kase' ");
                    list($akunt_ph) = mysqli_fetch_array($sql10);
                $sql11 = mysqli_query($koneksi, "SELECT sum(be_ph) AS tbe_ph FROM cmob_trans WHERE kode='$kode' ");
                    list($tbe_ph) = mysqli_fetch_array($sql11);    
              ?>
              <tr>
                <td><?php echo $no++; ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $kode ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo date('d/m/Y', strtotime($n['tanggal'])); ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $n['nama'] ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$n[n_beli]", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$tbe_ph", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$n[ph_bln]", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><font color='#ff0000'><?php echo "" . number_format("$peny", 0, ",", "."); ?></font></td>
<td>
  <form action="at_simpan_act.php" method="post" style="display: flex; align-items: center;">
    <input type="hidden" name="aksi" value="isi_ph_bln">
    <input type="hidden" name="tid" value="<?php echo $p['id']; ?>">
    <input type="hidden" name="tkd" value="<?php echo $p['kd'] ?>">
    <input type="hidden" name="tbln" value="<?php echo $bulane; ?>">
    <input type="hidden" name="tthn" value="<?php echo $tahune; ?>">
    <input type="text" class="form-control nilai rupiah" id="nilai" name="tn_ph_bln" value="<?php echo $peny; ?>" style="width: 180px; margin-right: 5px;">
    <button class="btn btn-primary btn-sm" type="submit">
      <i class="bi bi-save"></i>
    </button>
  </form>
</td>
                <td>
                    <a data-toggle='tooltip' data-placement='top' title='Buka Kartu Inventory <?php echo $n['nama'] ?>' class="btn btn-warning btn-sm" href="at_kartu_per_kode.php?kode=<?php echo $kode ?>"><i class="bi bi-layer-backward"></i></a>                                
                </td>           
                </tr>    
                        </div>
                      </div>
                    </div>
                  </form>                      
              <?php } ?>
          </tbody>
                        <?php 
                $sql3a = mysqli_query($koneksi, "SELECT sum(be_ph) AS tbe_ph FROM $tabel_tujuan ");
                list($tbe_ph) = mysqli_fetch_array($sql3a);                
            	        ?>						
							<tr>
						    	<td colspan="6" style="font-weight: bold;text-align: center;">Total </td>																																									
								<td align='right'><style="font-weight: bold;">
								    <?php echo ""; ?>
								</td>    
								<td align='right'><style="font-weight: bold;">
								    <font color='#ff0000'><?php echo "" . number_format("$tbe_ph", 0, ",", "."); ?></font>
								</td>
								<td align='right'><style="font-weight: bold;">
								    <?php echo ""; ?>
								</td>
							</tr>                   
        </table>
       </div> 
    </div>    
            <?php if($tbe_ph>=1) {; ?>
<div class="row">
    <div class="col-lg-6">
        <table class="table table-bordered">
            <tr>
                <th width="60%">Total Penyusutan yg telah diinput </th>
                <th width="1%">:</th>
                <td><font color='#ff0000'><?php echo "" . number_format("$tbe_ph", 0, ",", "."); ?></font></td>
            </tr>
            <tr>
                <th>Simpan sebagai Transaksi Akuntansi </th>
                <th>:</th>
                <td>
                    <a class="btn btn-success btn-sm me-2" title="Simpan Sebagai Jornal " data-bs-toggle="modal" data-bs-target="#jornal_penyusutan_<?php echo $tbe_ph ?>">
                        <i class="bi bi-download"></i>
                    </a>
                </td>
            </tr>
        </table>
    </div>
</div>
     
<!-- Modal -->
<div class="modal fade" id="jornal_penyusutan_<?php echo $tbe_ph ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form action="at_simpan_act.php" method="post">
                <div class="modal-header">
                    <h1 class="modal-title fs-5" id="exampleModalLabel">Simpan Jornal <?php echo $tnama ?> Bulan <?php echo $bulan ; ?> - <?php echo $tahune ; ?></h1>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                
                <div class="modal-body">
<div style="background-color: <?php echo$_SESSION['warna_bag']; ?>; padding: 15px; border-radius: 15px; box-shadow: 0 0 15px <?php echo$_SESSION['warna_bag']; ?>;">                                      
                    <div class="form-group" style="width:100%">
                        <input type="hidden" name="aksi" value="simpan_jornal_penyusutan">
                        <input type="hidden" name="tkd" required="required"  class="form-control" value="<?php echo $kase ?>">
                        <input type="hidden" name="tbln" required="required" class="form-control" value="<?php echo $bulan ; ?>">
                        <input type="hidden" name="tthn" required="required" class="form-control" value="<?php echo $tahune ; ?>">
                        <input type="hidden" name="tkdc" required="required" class="form-control" value="<?php echo $kdc ; ?>">
                        <input type="hidden" name="toleh" required="required" class="form-control" value="<?php echo $oleh ; ?>">
                    </div>
                    <div class="form-group" style="width:100%">
                        <label class="form-label"><font color="#000fff">Jumlah Penyusutan </font></label>
                        <input type="text" class="form-control nilai rupiah" id="nilai" name="tnilai" value="<?php echo "" . number_format("$tbe_ph", 0, ",", "."); ?>">
                    </div>
                    <div class="form-group" style="width:100%">
                        <label class="form-label"><font color="#000fff">Tanggal Penyusutan</font></label>
                        <input type="date" class="form-control" name="ttanggal" required="required" placeholder="Masukkan Tanggal ..">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
                    <button type="submit" class="btn btn-primary">Simpan</button>
                </div>
</div>                
            </form>
        </div>
    </div>
</div>

                  
                      </td>
                    </tr>
                  </table>
                </div>
              </div>
              
              
            <?php }; ?>
              <?php 
            }else{
              ?>

              <div class="alert alert-info text-center">
                Silahkan Filter Laporan Terlebih Dulu.
              </div>

              <?php
            }
            ?>

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

</div>
<?php include 'footer.php'; ?>