
<?php 
    include 'header.php'; 
?>            
<div class="container">

  <div class="my-4 border-bottom">
    <h4 class="fw-semibold fs-4">Laporan Jornal
    </h4>  
  </div>
  <section class="content">
    <div class="row">
      <section class="col-lg-14">
        <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="TAMPILKAN LAPORAN" 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_pertama    =strtotime("$tgl_d");
              $tgl_kemarin    =date('Y-m-d', strtotime("-1 day", $tgl_pertama));       
            } else { ;
                $tgl_d    = '2025-01-01';
                $tgl_s    = $_SESSION['tgl_sd'];
            }
            $nosub='1101';
                $sql1 = mysqli_query($koneksi, "SELECT sum(awald) as tn_awal FROM nawal WHERE left(noperk,4)='$nosub'");
                    list($tn_awal) = mysqli_fetch_array($sql1);
                    
                $sql2 = mysqli_query($koneksi, "SELECT sum(mutasid-mutasik) as tj_awal FROM jor WHERE left(noperk,4)='$nosub' and tgljornal<='$tgl_d' ");
                    list($tj_awal) = mysqli_fetch_array($sql2);                    
                
                $tsaldo_awal=$tn_awal+$tj_awal;
                
              ?>

              <div class="row">
                <div class="col-lg-6">
                  <table class="table table-bordered">
                    <tr>
                      <th width="40%">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>
                  </table>
                </div>
              </div>
<!--
      <button type="button" class="btn btn-success btn-sm align-self-end" data-bs-toggle="modal" data-bs-target="#exampleModal">
       <a href="lap_a_kas_tgl_sd_tgl_lihat_unit.php?xunit=01" class="btn btn-success btn-sm"><i class="bi bi-eyeglasses"></i> &nbsp JORNAL USP</a> 
      </button>
      <button type="button" class="btn btn-primary btn-sm align-self-end" data-bs-toggle="modal" data-bs-target="#exampleModal">
       <a href="lap_a_kas_tgl_sd_tgl_lihat_unit.php?xunit=02" class="btn btn-primary btn-sm"><i class="bi bi-eyeglasses"></i> &nbsp JORNAL INDUK</a> 
      </button>      
-->
      <button type="button" class="btn btn-info btn-sm align-self-end" data-bs-toggle="modal" data-bs-target="#exampleModal">
       <a href="index.php" class="btn btn-info btn-sm pull-right"><i class="bi bi-reply"></i> &nbsp; Kembali</a> 
      </button>
              <div class="table-responsive">
                <table class="table table-bordered table-striped" id="table-datatable1">
          <thead>
            <tr>
              <th width="1%" class="text-center" style="text-align:left; font-size: 9pt; font-weight:normal;">No</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 9pt; font-weight:normal;">Notrans</th>
              <th width="4%" class="text-center">Tanggal</th>
              <th width="35%" class="text-center">Uraian</th>
              <th width="25%" class="text-center">Noperk</th>
              <th width="8%" class="text-center">Debet</th>
              <th width="8%" class="text-center">Kredit</th>
              
            </tr>
          </thead>
          <tbody>
            <?php 
              $no   = 1;
              $awal = $tsaldo_awal ;
              $debet  = 0;
              $kredit  = 0;

$data = mysqli_query($koneksi,"SELECT * FROM jor WHERE tgljornal>='$tgl_d' and tgljornal<='$tgl_s' ORDER BY notrans, tgljornal ,id ASC ");

$notrans_prev = '';
$tgljornal_prev = '';
$rowspan = 0;
$no = 1;

while($d = mysqli_fetch_array($data)){
    $notrans =$d['notrans'] ;
    $noperk  =$d['noperk'] ;
    $sql3 = mysqli_query($koneksi, "SELECT uraiankas FROM kas WHERE notrans='$notrans' ");
    list($uraiankas) = mysqli_fetch_array($sql3);
    
    $sql4 = mysqli_query($koneksi, "SELECT perk FROM nawal WHERE noperk='$noperk' ");
    list($perk) = mysqli_fetch_array($sql4);    

    if ($notrans_prev != $d['notrans']) {
        if ($notrans_prev != '') {
            // tutup baris sebelumnya
        }
        $notrans_prev = $d['notrans'];
        $tgljornal_prev = $d['tgljornal'];

        // hitung rowspan
        $query_rowspan = mysqli_query($koneksi, "SELECT COUNT(*) as jumlah FROM jor WHERE notrans = '$notrans'");
        $rowspan_data = mysqli_fetch_array($query_rowspan);
        $rowspan = $rowspan_data['jumlah'];
        ?>
        <tr>
            <td rowspan="<?php echo $rowspan ?>"><?php echo $no++; ?></td>
            <td rowspan="<?php echo $rowspan ?>" style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $d['notrans']; ?></td>
            <td rowspan="<?php echo $rowspan ?>" style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo date('d/m/Y', strtotime($d['tgljornal'])); ?></td>
            <td rowspan="<?php echo $rowspan ?>" style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $uraiankas; ?></td>
            <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $d[noperk]; ?> - <?php echo $perk; ?></td>
            <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$d[mutasid]", 0, ",", "."); ?></td>
            <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$d[mutasik]", 0, ",", "."); ?></td>
        </tr>
    <?php } else { ?>
        <tr>

            <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $d[noperk]; ?> - <?php echo $perk; ?></td>
            <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$d[mutasid]", 0, ",", "."); ?></td>
            <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$d[mutasik]", 0, ",", "."); ?></td>
        </tr>
    <?php }
} ?>

          </tbody>
                        <?php 
                $sql21 = mysqli_query($koneksi, "SELECT sum(mutasid) AS tmutd,sum(mutasik) AS tmutk FROM jor WHERE tgljornal>='$tgl_d' and tgljornal<='$tgl_s' ");
                list($tmutd,$tmutk) = mysqli_fetch_array($sql21);
            	        ?>						
     
        <tr>
                    <td colspan="5" style="font-weight: bold;text-align: center;background-color: <?php echo$_SESSION['warna_bag']; ?>;">Total BALANCE</td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo "" . number_format("$tmutd", 0, ",", "."); ?></td>
                    <td align='right' style="font-weight: bold;background-color: <?php echo$_SESSION['warna_bag']; ?>;"><?php echo "" . number_format("$tmutk", 0, ",", "."); ?></td>
                </tr>							
        </table>        
        </table>
        
        
              </div>

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

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