<?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 ">Kartu Angsuran</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          
          $kode=$_SESSION['kode'];
          if(isset($_POST['rekening'])){

            $rekening=$_POST['rekening'];

            if($_POST['rekening']=='Pilih')
              $where="where rekening='0'";
            else
              $where="where a.rekening='$rekening' ";

          }else{
            $par1="";
            $par2="";
            $rekening="";

            $where="where a.rekening='$rekening'";
           }
          ?>
          <form role="form" id="quickForm" action="" method="POST">
            <div class="form-group row">
              <label for="nama" class="col-1 m-2">Rekening</label>
              <input type="text" id="rekening" name="rekening" maxlength="13" value="<?php echo $_POST['rekening'] ?>" class="form-control col-sm-2" autofocus >
                     
              
                <button type="submit" name="proses" class="btn btn-primary" style="float: right" value="Cari"  onchange="this.form.submit();" class="btn btn-info"><i class="fas fa-search"></i></button>
              </div>
                <?php 
               if(isset($_POST['rekening'])){
                $_SESSION['kangsuran']['judul']="KARTU ANGSURAN";
                $_SESSION['kangsuran']['unit']="BMT UGT NUSANTARA";
                $_SESSION['kangsuran']['rekening']=$_POST['rekening'];
                
                if($_POST['rekening']=='Pilih')
                  $_SESSION['laporan']['rekening']='Pilih';
                else
                  $_SESSION['laporan']['rekening']="select * from pinjaman where rekening='".$_POST['rekening']."'";

                ?>
                <div>
                <a href="pdf/lap_kartu_angsuran_pdf.php" target="_blank" style="float: right;margin-top: 10px;" class="btn btn-success"><i class="fa fa-print"></i> PDF</a>

              <?php } ?>


              </div>          
          </form>
                   
          <hr>

            <?php
            
            function buatrp($angka){
              $jadi =number_format($angka,2,'.',',');
            //$jadi ="Rp. " .number_format($angka,2,'.',',');
              return $jadi;
            }


            include 'koneksi.php';
             $querya = "SELECT a.tgl as tgl,a.rekening as rekening, a.plafond AS plafond, a.margin AS margin, a.jangkawaktu/15 AS jangkawaktu,b.nama AS nama,b.alamat AS alamat ,DATE_ADD(a.tgl, INTERVAL a.jangkawaktu MONTH)as jatuh_tempo from pinjaman a LEFT JOIN registrasi b ON a.register=b.id $where ";
              $query  =mysqli_query($koneksi,$querya);
             
                if(mysqli_num_rows($query)){
                while ($data = mysqli_fetch_assoc($query)) {
                   
                  extract($data);
                                    
                  
          $bunga_rp = $plafond/$margin;

    $angsuran_margin=$plafond*$margin/100;

    $angsuran_pokok = $plafond/$jangkawaktu;

    $jumlahangsuran = $angsuran_pokok+$angsuran_margin;
    
 
}
}
 if(isset($_POST['rekening'])){
    echo "<pre>";
    echo "Nama                     = "."<b>".$nama."</b>"."<br>";
    echo "Alamat                   = "."<b>".$alamat."</b>"."<br>";
    echo "Tgl Realiasasi           = "."<b>".date('d-m-Y',strtotime($tgl))."</b>"."<br>";
    echo "Jumlah Pinjaman          = "."<b>".buatrp($plafond)."</b>"."<br>";
    echo "Lama Pinjaman            = "."<b>".$jangkawaktu. " Hari"."</b>"."<br>";
    echo "Jatuh Tempo              = "."<b>".$jatuh_tempo."</b>"."<br>";
    echo "Ujroh %                  = "."<b>".$margin.' %'."</b>"."<br>";

    echo "Ujroh Rp                 = "."<b>".buatrp($angsuran_margin).'<br>';

/*    echo "<br>";*/

    /*echo "________________________________________ ( + )<br>";

    echo "Total Angsuran Per Bulan = "."<b>".buatrp($jumlahangsuran)."</b>";*/

    echo "</pre>";


  $no=1;

?>
<!-- <hr> -->
           
          <table id="example3" class="table table-bordered table-hover">
            <thead>
    <tr>
      <th align="center"width="5">Ke</th>
      <th align="center"width="8">Tanggal</th>
      <th align="center"width="7">Bukti</th>
      <th align="center"width="18">Pokok</th>
      <th align="center"width="18">Margin</th>
      <th align="center"width="18">Jumlah</th>
      <th align="center"width="20">Baki Debet</th>
      <th align="center">Keterangan</th>
    </tr>
            </thead>
           <tbody>

            <tr>
      <td align="center"></td>
      <td align="center"></td>
      <td align="center"></td>
      <td align="center"></td>
      <td align="center"></td>
      <td align="center"></td>
      <td align="right"><b><?php echo buatrp($plafond);?></b></td>
    </tr>
            <?php 

    $queryp = "SELECT a.tgl as atgl,c.no_bukti as ano_bukti,a.rekening as rekening, a.plafond AS plafond, a.margin AS pmargin, a.jangkawaktu/15 AS jangkawaktu,b.nama AS nama,b.alamat AS alamat,c.pokok as apokok,c.margin as amargin,c.tgl as atgl,c.keterangan as keterangan from pinjaman a LEFT JOIN angsuran c ON a.rekening=c.rekening LEFT JOIN registrasi b ON a.register=b.id  $where order by c.tgl ASC ";
              $query  =mysqli_query($koneksi,$queryp);
             
                if(mysqli_num_rows($query)){
                $jpokok =0;
                $jmargin=0;
                $bakidebet=0;   

                while ($data = mysqli_fetch_assoc($query)) {
                   
                  extract($data); 
                  
      
   
    $jmlangsuran    = $apokok+$amargin;
    $jpokok         +=$apokok;
    $jmargin        +=$amargin;
    $tpm            =$jpokok+$jmargin;
    $bakidebet      +=-$apokok;

                 
      ?>                     
      <tr>
      <td align="center"><?php echo $no++;?></td>
      <td align="center"><?php echo $atgl;?></td>
      <td align="center"><?php echo $ano_bukti;?></td>
      <td align="right"><?php echo buatrp($apokok);?></td>
      <td align="right"><?php echo buatrp($amargin);?></td>
      <td align="right"><?php echo buatrp($jmlangsuran);?></td>
      <td align="right"><?php echo buatrp($bakidebet+$plafond);?></td>
      <td align="left"><?php echo $keterangan;?></td>
    </tr>  

<?php
}}
?>
    <tr>     
      <td align="center" colspan="2"><b>Total</td></b>
      <td align="right"><b><?php echo buatrp($jpokok);?></b></td>
      <td align="right"><b><?php echo buatrp($jmargin);?></b></td>
      <td align="right"><b><?php echo buatrp($tpm);?></b></td>
      <td></td>
      <td></td>
    </tr>
  



  
<div class="container"><br></div>


                <?php 
              
                 }else{
                  echo '<table border="0" style="padding-left: 20%;font-size: 16px;height: 20px; margin-left:20px"
            
              >';
                  echo '<tr>';
                  echo '<td> Mohon Rekeing yang dicari diisi dlu di colum di atas</td>';
                  echo "</tr>";
                  echo "</table>";
                 
               }
                ?>
              </tbody>
              </table>

               <?php 
               if(isset($_POST['rekening'])){
                $_SESSION['kangsuran']['sql2']= $queryp;
                $_SESSION['kangsuran']['sql1']= $querya;
                }
                ?>
              
            </div>
            <!-- /.card-body -->
          </div>
          <!-- /.card -->
        </div>
        <!-- /.col -->
      </div>

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


