


<?php 
    $kdc    = $_SESSION['kdc'];
    $oleh   = $_SESSION['id_kar'];
    
    $angg_id    = $_GET['id'];

    $tgl_ini=date('d-m-Y') ;
    $tgl_ini2=date('Y-m-d') ;  
    $tahun_ini = date('Y');
    
    $tanggal_awal_tahun = '01-01-' . $tahun_ini;
    $tanggal_akhir_tahun = $tanggal_awal_tahun-1 ;

    $sql1 = mysqli_query($koneksi, "SELECT kelurahan,kecamatan,kota FROM kdkanca WHERE kd='01' ");
                list($kelurahan,$kecamatan,$kota) = mysqli_fetch_array($sql1);
                
    $angg = mysqli_query($koneksi, "SELECT angg_no AS no FROM anggota WHERE angg_id='$angg_id' ");
                list($no) = mysqli_fetch_array($angg);     
    $angg_no    = $no;            
?>              

<div class="container">
  <div class="row justify-content-center">
    <div class="col-lg-12">
       <div class="card">
    <div class="card-header d-flex justify-content-between">
      <span class="card-title fw-semibold align-self-start mb">DATA JUAL/BELI TOKO</span>
      <buton>
        <a href="index.php" class="btn btn-primary btn-sm"><i class="bi bi-house-fill"></i> &nbsp Home</a> 
        <?php if ($_SESSION['level']<>'ANGGOTA') { ?>
            <a href="javascript:history.go(-1)" class="btn btn-info btn-sm"><i class="bi bi-reply-all"></i> &nbsp Kembali</a>
        <?php } ?>
      </buton>
    </div>
      <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: 10pt; font-weight:normal;">No</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Nota</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Tanggal</th>
              <th width="10%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Keterangan</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Belanja</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Suplai</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Pembayaran</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Hutang</th>
              <th width="1%" class="text-center" style="text-align:left; font-size: 10pt; font-weight:normal;">Piutang</th>
              <th width="1%" class="text-center">NOTA</th>
            </tr>
            </tr>
          </thead>
          <tbody>
            <?php 
            $no=1;
            $trans = mysqli_query($koneksi,"SELECT * FROM brg_mts_r WHERE kd=$d[angg_no] ");
            while($p = mysqli_fetch_array($trans)){
                $noperknya = $p['noperk'] ;
                $sql1 = mysqli_query($koneksi, "SELECT perk FROM nawal WHERE noperk='$p[noperk]' ");
                list($perk) = mysqli_fetch_array($sql1);
                
                $sql2 = mysqli_query($koneksi, "SELECT SUM(bayar+retur) AS tbayar FROM brg_angs WHERE kdr='$p[kdr]' ");
                list($tbayar) = mysqli_fetch_array($sql2);   
                $belanja=$p['jual']+$p['ongkir']-$p['disc_j'];
                $suplayer=$p['beli']-$p['disc'];
                
                if ($p['jual']>1) {
                    $ket='Belanja dengan '.$perk ;
                    $dua_digit_depan = substr($noperknya, 0, 3);
                    if ($dua_digit_depan='114') {
                        $hutang=$belanja-$tbayar ;
                        $piutang=0;
                    } else {
                        $hutang=0 ;
                        $piutang=0;                        
                    }
                    
                } else { ;
                    $ket='Suplay brg dengan '.$perk ;
                    $dua_digit_depan = substr($noperknya, 0, 2);
                    if ($dua_digit_depan='22') {
                        $piutang=$belanja-$tbayar ;
                        $hutang=0;
                    } else {
                        $hutang=0 ;
                        $piutang=0;                        
                    }                 
                } ;
                

                
                $hutang=$belanja-$tbayar


              ?>
              <tr>
                <td><?php echo $no++; ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $p['kdr'] ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo date('d/m/Y', strtotime($p['tanggal'])); ?></td>
                <td style="text-align:left; font-size: 10pt; font-weight:normal;"><?php echo $ket ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$belanja", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$suplayer", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$tbayar", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$hutang", 0, ",", "."); ?></td>
                <td style="text-align:right; font-size: 10pt; font-weight:normal;"><?php echo "" . number_format("$piutang", 0, ",", "."); ?></td>
                <td>
    <a class="btn btn-warning btn-sm me-2" title="Buka Nota <?php echo $d['kdr'] ?>" href="toko_trans_jual_per_nota_angg.php?id=<?php echo $p['kdr'] ?>">
      <i class="bi bi-file-ruled-fill"></i>
    </a>             
                </td>    
              </tr>
              <?php } ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
