<?php

/*error_reporting(E_ALL^(E_NOTICE|E_WARNING));
ini_set('display_errors',1);
error_reporting(0);*/
?>

<!-- Content Header (Page header) -->
<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-1">
      <div class="col-sm-12">
        <!-- <h1 class="m-0 text-dark">Laporan Neraca []</h1> -->
      </div><!-- /.col -->
    </div><!-- /.row -->
  </div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->

<!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            <!-- Default box -->
            <div class="card">
              <div class="card-header bg-info">
                <h3 class="card-title ">Laporan Tunggakan</h3>
                <div class="card-tools">
                  <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                    <i class="fas fa-minus"></i>
                  </button>
                  <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
                    <i class="fas fa-times"></i>
                  </button>
                </div>
              </div>

              <!-- /.card-header -->
        <div class="card-body">
          <?php
          
          
          /*$username=$_SESSION['username'];*/
          if(isset($_POST['par2'])){
            /*$par1=date('Y-m-d',strtotime($_POST['par1']));*/
            $par2=date('Y-m-d',strtotime($_POST['par2']));
            /*$id = $_POST['t_rekening'];*/


            if($_POST['par2']=='')
              $where="where  b.tgl<='".$par2."' group by a.rekening having bakidebet<>0 ";
            /*$where="where  (b.tgl between '$par1' and '$par2')  group by a.rekening having bakidebet<>0 ";*/
            else
              $where="where b.tgl<='$par2' group by a.rekening having bakidebet<>0 ";
            /*$where="where (b.tgl between '$par1' and '$par2')  group by a.rekening having bakidebet<>0 ";*/
          }else{
            
            $par2="";

            $where="where b.tgl='$par2' ";
           
          }
          ?>
          <form role="form" id="quickForm" action="home.php?home=<?php echo md5("tunggakan");?>" method="post">
            <div class="row">
              <div class="col-sm-10">
            <div class="form-group row">
              <!-- <label for="nama" class="col-2 m-3">Cari Tunggakan</label>
              <input type="text" name="t_rekening" id="t_rekening"  class="form-control col-sm-2" placeholder="ketik Nama/Alamat/Rekening"> -->
              <label  for="nama" class="col-2 m-2">Periode Tanggal</label>
              <!-- <input type="date" name="par1" id="par1" class="form-control col-2" value="<?=@$par1?>"> -->
              <input type="date" name="par2" id="par2" class="form-control col-2" value="<?=@$par2?>"><!-- required="" -->
              <!-- <div class="form-group row"> -->

                <button type="submit" class="  btn btn-info"  ><i class="fas fa-search"></i></button>
                </div>
              </div>
                <?php if(isset($_POST['par2'])){ ?>

                  <div class="form-group col-2">
                  
                  <!-- <div class="col-2 m-2"> -->
                  <?php
                  $_SESSION['tuggakan']['periode2'] = date('d-m-Y',strtotime($_POST['par2']));
                
                
                $_SESSION['tuggakan']['kantor']=$_SESSION['kantor'];
                
                ?>
                <a href="pdf/nunggak_angsuranpdf.php" target="_blank" style="float: right;margin-top: 10px;" class="btn btn-success"><i class="fa fa-print"></i> PDF</a>
              </div>
            </div>
                <?php } ?>
               
              
                     
          </form>                   
          <hr>

          <table class="table table-bordered table-hover" width="148%">
            <thead>
              <tr align="center">
                <th >No</th>
                <th >TGL</th>
                <th >REKENING</th>
                <th >Nama</th>
                <th >Alamat</th>
                <!-- <th >RT/RW</th> -->
                <th >PLAFOND</th>
                <th >UJR</th>
                <th >LM</th>
                <th >BAKIDEBET</th>
                <th >T POKOK</th>
                <th >T MARGIN</th>
                <th >TOTAL</th>
                <th >KOL</th>
              </tr>
            </thead>
            <tbody>
              <?php
              if(isset($_POST['par2'])){
              include 'koneksi.php';
              $queryt = "SELECT a.plafond*a.margin/100*a.jangkawaktu-SUM(b.margin) as k_margin, SUM(b.pokok) as tpokok,sum(b.margin) as tmargin,c.nama as nama,c.alamat as alamat, a.rekening AS rekening, a.plafond AS plafond,a.plafond-sum(b.pokok)as bakidebet,a.margin AS margin,a.jangkawaktu AS jangkawaktu,a.tgl AS tgl FROM pinjaman a LEFT JOIN angsuran b ON a.rekening=b.rekening LEFT JOIN registrasi c ON a.register=c.id $where";
                $querya       =mysqli_query($koneksi,$queryt);
                if(mysqli_num_rows($querya)){
                  $_SESSION['tuggakan']['kode']['sqlt']=$queryt;
                $no=0;
                
                while ($data = mysqli_fetch_assoc($querya)) {
                  extract($data);
                  //flast
                    $jumlah_pinjaman = $plafond;
                    $lama_pinjaman   = $jangkawaktu;
                    $margin_perbulan  = $margin;
                    $tgl  = $tgl;

    
                    $angsuran_margin=$jumlah_pinjaman*$margin_perbulan/100;
                    $angsuran_pokok = $jumlah_pinjaman/$lama_pinjaman;
                    $jumlahangsuran = $angsuran_pokok+$angsuran_margin;
                  $tpokok2=0;
                  $tmargin2=0;
                  $tot2=0;
                  $tpokok2+=$angsuran_pokok;
                  $tmargin2+=$angsuran_margin;
                  $tot2+=$jumlahangsuran;
                  $date2 = $par2;
                
                $timeStart2 = strtotime("$tgl");
                $timeEnd2 = strtotime("$date2");
                // Menambah bulan ini + semua bulan pada tahun sebelumnya
                $jmlBulan2 = 1 + (date("Y",$timeEnd2)-date("Y",$timeStart2))*12;
                // menghitung selisih bulan
                $jmlBulan2 += date("m",$timeEnd2)-date("m",$timeStart2); 
                 /*Flat*/
                 $flat_t_margin=$angsuran_margin*$jmlBulan2-$tmargin;
                 $flat_t_pokok=$angsuran_pokok*$jmlBulan2-$tpokok;
                 $tot_tunggakan= $flat_t_margin+$flat_t_pokok;
                 $no++;
                  ?>
  
                  <tr>
                    <td align="center"><?php echo $no; ?></td>
                    <!-- <td><?php echo date('d-m-Y',strtotime($tanggal)); ?></td> -->
                    <td><?php echo date('d-m-Y',strtotime($tgl)); ?></td>
                    <td><?php echo $rekening; ?></td>
                    <td><?php echo $nama; ?></td>
                    <td><?php echo $alamat ?></td>
                    <!-- <td><?php echo $rtrw; ?></td> -->
                    <td align="right"><?php echo number_format($plafond); ?></td>
                   <td><?php echo $margin; ?></td>
                    <td><?php echo $jangkawaktu; ?></td>
                    
                     <td align="right"><?php echo number_format($bakidebet,0); ?></td>
                    <td align="right"><?php echo number_format($flat_t_pokok,0); ?></td>
                    <td align="right"><?php echo number_format($flat_t_margin,0); ?></td>
                    <td align="right"><?php echo number_format($tot_tunggakan,0); ?></td>
                      <td align="right"><?php if($tot_tunggakan<=0){ echo "0";}else if($tot_tunggakan>0)
                      if($jmlBulan2<=2) echo "0"; elseif($jmlBulan2<=3)echo "1";else echo "2"; ?></td>                    
                  </tr>
                <?php }}?>
              </tbody>
              </table>

            <?php }?>
             

              
            </div>
            <!-- /.card-body -->
          </div>
          <!-- /.card -->
        </div>
        <!-- /.col -->
      </div>

      <!-- /.row -->
    </section>
    <!-- /.content -->


