<?php 
    session_start();
    include 'header.php'; 
?>

<div class="container">

  <div class="my-2 border-bottom">
    <h5 class="fw-semibold fs-5">Laporan Mutasi Neraca & PHU</h5>
  </div>
    <div class="card-header d-flex justify-content-between">
      <span class="card-title fw-semibold align-self-start mb">Silahkan pilih tanggal dari dan pilih tanggal sampai kemudian klik HITUNG ULANG kemudian klik TAMPILKAN LAPORAN </span>
      <buton>
       <a href="lap_akunt_nrc_tgl_sd_tgl_lihat.php" class="btn btn-primary btn-sm"><i class="bi bi-reply-all"></i> &nbsp Kembali</a> 
      </buton>
    </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="">
              <div class="row">
                <div class="col-md-2">
                  <div class="form-group">
                    <label>Dari</label>
                    <input type="date" class="form-control" name="tanggal_d" required="required" placeholder="Tanggal dari ..">
                  </div>
                </div>

                <div class="col-md-2">
                  <div class="form-group">
                    <label>Sampai</label>
                    <input type="date" class="form-control" name="tanggal_s" required="required" placeholder="Sampai tanggal ..">
                  </div>
                </div>

                <div class="col-md-2">
                  <div class="form-group">
                    <br/>
                    <input type="submit" value="HITUNG ULANG DATA" class="btn btn-sm btn-primary">
                  </div>
                </div>
              </div>
            </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['tanggal_s']) && isset($_GET['tanggal_d'])){
              $tgl_d    = $_GET['tanggal_d'];
              $tgl_s    = $_GET['tanggal_s'];
              
              $tgl_ke_1 =date('2025-01-01');        
              $tgl_p    =strtotime("$tgl_d");
              $tgl_lalu =date('Y-m-d', strtotime("-1 day", $tgl_p));
            //////////////////////////////////////////////////////////////////////////// 
            $oleh   = $_SESSION['id_kar'];
            $tabel_tujuan   = "nawal_".$oleh; 
            $tabel_asal     = "nawal";
            ////////////////////////////////////////////////////////////////////////////
            $query_hapus = "DROP TABLE $tabel_tujuan ";
            mysqli_query($koneksi, $query_hapus);

            $querycc = "CREATE TABLE $tabel_tujuan LIKE $tabel_asal";
            mysqli_query($koneksi, $querycc);
            ////////////////////////////////////////////////////////////////////////////  
$query = "INSERT INTO $tabel_tujuan (perk, noperk, kdc, klp, nosub, ap, ry, awald, awalk, akhird, akhirk)
          SELECT perk, noperk, kdc, klp, nosub, ap, ry, awald, awalk, awald, awalk FROM $tabel_asal";

if (mysqli_query($koneksi, $query)) {
    echo "Data berhasil dikopi";
} else {
    echo "Error: " . $query . "<br>" . mysqli_error($koneksi);
}
            
            $data = mysqli_query($koneksi,"SELECT * FROM $tabel_tujuan ");
            while($d = mysqli_fetch_array($data)){            
                ///////////// NGISI NAWAL_ISI
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////                
                $sql1 = mysqli_query($koneksi, "SELECT noperk AS tnoperk,sum(debet) AS tdeb,sum(kredit) AS tkred FROM jor WHERE noperk='$d[noperk]' and tanggal<='$tgl_lalu' ");
                list($tnoperk,$tdep,$tkred) = mysqli_fetch_array($sql1);
                
                if ($d['klp']<='50') { ;
                    if ($d['klp']<='20') { ;
                        $saldo_a=($d['awald']+$tdep)-$tkred ;
                        if ($saldo_a>=1) {
                            $akhir_d_a=$saldo_a ;
                            $akhir_k_a=0 ;
                        } else {
                            $akhir_d_a=$saldo_a ;
                            $akhir_k_a=0 ;               
                        }                
                    } else {
                        $saldo_a=($d['awalk']-$tdep)+$tkred ;
                        if ($saldo_a>=1) {
                            $akhir_k_a=$saldo_a ;
                            $akhir_d_a=0 ;
                        } else {
                            $akhir_k_a=$saldo_a ;
                            $akhir_d_a=0 ;                  
                        }            
                    }
                } else {
                    if ($d['klp']>='50') { ;
                        $saldo_a=($d['awald']+$tdep)-$tkred ;
                        if ($saldo_a>=1) {
                            $akhir_d_a=$saldo_a ;
                            $akhir_k_a=0 ;
                        } else {
                            $akhir_d_a=$saldo_a ;
                            $akhir_k_a=0 ;               
                        }                
                    } else {
                        $saldo_a=($d['awalk']-$tdep)+$tkred ;
                        if ($saldo_a>=1) {
                            $akhir_k_a=$saldo_a ;
                            $akhir_d_a=0 ;
                        } else {
                            $akhir_k_a=$saldo_a ;
                            $akhir_d_a=0 ;                  
                        }            
                    }                    
                }
                ///////////// MINYIMPAN KE NAWAL
                mysqli_query($koneksi, "UPDATE $tabel_tujuan SET ap='Y',awald='$akhir_d_a',awalk='$akhir_k_a',akhird='$akhir_d_a',akhirk='$akhir_k_a' WHERE noperk='$tnoperk' ")or die(mysqli_errno());  
                ///////////// MINYIMPAN KE NAWAL
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                $sql1 = mysqli_query($koneksi, "SELECT noperk AS tnoperk,sum(debet) AS tdeb_m,sum(kredit) AS tkred_m FROM jor WHERE noperk='$d[noperk]' and tanggal>='$tgl_d' and tanggal<='$tgl_s' ");
                list($tnoperk,$tdep_m,$tkred_m) = mysqli_fetch_array($sql1);
                if ($d['klp']<='50') { ;
                    if ($d['klp']<='20') { ;
                        $saldo_x=($akhir_d_a+$tdep_m)-$tkred_m ;
                        if ($saldo_x>=1) {
                            $akhir_d_x=$saldo_x ;
                            $akhir_k_x=0 ;
                        } else {
                            $akhir_d_x=$saldo_x ;
                            $akhir_k_x=0 ;               
                        }                
                    } else {
                        $saldo_x=($akhir_k_a-$tdep_m)+$tkred_m ;
                        if ($saldo_x>=1) {
                            $akhir_k_x=$saldo_x ;
                            $akhir_d_x=0 ;
                        } else {
                            $akhir_k_x=$saldo_x ;
                            $akhir_d_x=0 ;                  
                        }            
                    }  
                } else {
                    if ($d['klp']>='50') { ;
                        $saldo_x=($akhir_d_a+$tdep_m)-$tkred_m ;
                        if ($saldo_x>=1) {
                            $akhir_d_x=$saldo_x ;
                            $akhir_k_x=0 ;
                        } else {
                            $akhir_d_x=$saldo_x ;
                            $akhir_k_x=0 ;               
                        }                
                    } else {
                        $saldo_x=($akhir_k_a-$tdep_m)+$tkred_m ;
                        if ($saldo_x>=1) {
                            $akhir_k_x=$saldo_x ;
                            $akhir_d_x=0 ;
                        } else {
                            $akhir_k_x=$saldo_x ;
                            $akhir_d_x=0 ;                  
                        }            
                    }                      
                }    
                ///////////// MINYIMPAN KE NAWAL
                mysqli_query($koneksi, "UPDATE $tabel_tujuan SET ap='Y',mutasid='$tdep_m',mutasik='$tkred_m',akhird='$akhir_d_x',akhirk='$akhir_k_x',tgl_htg='$tgl_d',tgl_sd='$tgl_s' WHERE noperk='$tnoperk' ")or die(mysqli_errno());  
                ///////////// MINYIMPAN KE NAWAL
            }
              ?>

              
              <div class="row">
                <div class="col-lg-8">
                  <table class="table table-bordered">
                    <tr>
                      <th width="40%">DATA SUDAH DIHITUNG ULANG : </th>
                      <th width="1%">:</th>
                      <td>Dari Tanggal s/d Tanggal</td>
                    </tr                      
                    <tr>
                      <th width="30%">DARI TANGGAL</th>
                      <th width="1%">:</th>
                      <td><?php echo date('d-m-Y', strtotime($tgl_d)); ?></td>
                    </tr>
                    <tr>
                      <th>SAMPAI TANGGAL</th>
                      <th>:</th>
                      <td><?php echo date('d-m-Y', strtotime($tgl_s)); ?></td>
                    </tr>
                    <tr>
                      <th>Cetak Ke Printer</th>
                      <th>:</th>
                      <td><a href="lap_akunt_nrc_tgl_sd_tgl_lihat.php" class="btn btn-warning btn-sm"><i class="bi bi-printer"></i> &nbsp TAMPILKAN LAPORAN</a></td>
                    </tr>                    
                  </table>
                </div>
              </div>

              <div class="table-responsive">
                <table class="table table-bordered table-striped" id="table-datatable1">
                  <thead>
          <tbody>

        </table>
              </div>

              <?php 
              
            }else{
              ?>

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

              <?php
            }
            ?>

          </div>
        </div>
      </section>
    </div>
  </section>
  
<?php
// menu1.php
if (isset($_POST['submit'])) {
  // Proses data menu pertama
  echo '<button onclick="prosesMenu2()">KLIK</button>';
  echo '<script>
          function prosesMenu2() {
            window.location.href = "lap_akunt_nrc_tgl_sd_tgl_lihat.php";
          }
        </script>';
  exit;
}
?>
</div>
<?php include 'footer.php'; ?>

