<?php 
  $coun = count($detail); 
  
  if($konsumen['no_hp']=='-'){
    $no_hp = '-';
  }else{
    $no_hp = hp_1(hp_2($konsumen['no_hp']));
    $no_hp = phone_number($no_hp);
  }
  $tdetail = $tdetail - $potongan_harga;

  if($tdetail == $total->total AND $pajak==0){
    $stamp  = $lunas;
    $alt    = '';
    $logo   = $logo_lunas;
    $color  = $info['warna_lunas'];
    $_waicon = $waicon['color'];
    $_mail   = $mail['color'];
    $_fbicon = $fbicon['color'];
    $_igicon = $igicon['color'];
  }elseif($tdetail == $total->total AND $pajak>0){
    $stamp = $lunas;
    $alt = '';
    $logo = $logo_lunas;
    $color = $info['warna_lunas'];
    $_waicon = $waicon['color'];
    $_mail   = $mail['color'];
    $_fbicon = $fbicon['color'];
    $_igicon = $igicon['color'];
  }elseif($tdetail != $total->total AND $pajak==0){
    $stamp = $blunas;
    $alt = '';
    $logo = $logo_blunas;
    $color = $info['warna_blunas'];
    $_waicon = $waicon['bw'];
    $_mail   = $mail['bw'];
    $_fbicon = $fbicon['bw'];
    $_igicon = $igicon['bw'];
  }elseif($tdetail != $total->total AND $pajak>0){
    $stamp = $blunas;
    $alt = '';
    $logo = $logo_blunas;
    $color = $info['warna_blunas'];
    $_waicon = $waicon['bw'];
    $_mail   = $mail['bw'];
    $_fbicon = $fbicon['bw'];
    $_igicon = $igicon['bw'];
  }else{
    $stamp = $blunas;
    $alt = '';
    $logo = $logo_blunas;
    $color = $info['warna_blunas'];
    $_waicon = $waicon['bw'];
    $_mail   = $mail['bw'];
    $_fbicon = $fbicon['bw'];
    $_igicon = $igicon['bw'];
  }

  if($cetak->status=='batal'){
    $stamp = '';
    $alt = 'BATAL';
    $color = "#333";
    $_waicon = $waicon['bw'];
    $_mail   = $mail['bw'];
    $_fbicon = $fbicon['bw'];
    $_igicon = $igicon['bw'];
  }

  $invoice_font_size = (isset($font_size) && is_numeric($font_size)) ? (int) $font_size : 11;
  if ($invoice_font_size < 7) {
    $invoice_font_size = 7;
  }
  $invoice_line_height = $invoice_font_size + 5;
  $invoice_heading_font_size = max(7, $invoice_font_size - 1);
  $invoice_heading_line_height = $invoice_heading_font_size + 5;
  $invoice_small_font_size = max(7, $invoice_font_size - 2);
  $invoice_small_line_height = $invoice_small_font_size + 3;
  $invoice_footer_font_size = max(6, $invoice_font_size - 4);
  $invoice_footer_line_height = $invoice_footer_font_size + 3.5;
  $invoice_meta_font_size = max(7, $invoice_font_size - 2);
?>
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Cetak Invoice</title>
    <link href="<?= $favicon;?>" rel="icon">
    <style>
      <?php if($html=="Y"){ ?>
      @page {
        size: <?=htmlspecialchars($paper_size, ENT_QUOTES, 'UTF-8');?> <?=$paper_orientation;?>;
        margin: 10mm;
      }
      <?php }else{ ?>
      @page { margin: 0.5cm 0 0 0; }
      <?php } ?>
      body {
        margin: 0 1cm 0 0.5cm;
        font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;
        font-size: <?=$invoice_font_size;?>px;
        line-height: <?=$invoice_line_height;?>px;
        color:#555;
      }
	  .gbr{width:100%;position:relative;top:24px}
	  .qr-tracking-label{position:absolute;bottom:104px;left:0;width:170px;text-align:center;font-size:8pt;font-weight:600;line-height:10pt;}
      <?php if($html=="Y"){ ?>
      .invoice-box {
        width: 100%;
        box-sizing: border-box;
        margin: 0 auto;
        padding: 0;
        font-size: <?=$invoice_font_size;?>px;
        line-height: <?=$invoice_line_height;?>px;
        font-weight: <?=$font_wight;?>;
        font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;
        color: #555;
        background:#fff;
      }
      <?php }else{ ?>
      .invoice-box {
        width:100%;
        margin: 0 auto;
        padding: 8px;
        font-size: <?=$invoice_font_size;?>px;
        line-height: <?=$invoice_line_height;?>px;
        font-weight: <?=$font_wight;?>;
        font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;
        color: #555;
      }
      .qris{position:absolute;bottom:40px;left:20px;z-index:100}
      <?php } ?>
      footer {
        position: fixed; bottom: 0; left: 0; right: 0;
        height: 0.5cm; padding:2.3px;
        background-color: <?=$color;?>; color: white; text-align: center;
        font-size: <?=$invoice_heading_font_size;?>px;
      }

      /* ====== Tabel umum ====== */
      .invoice-box table { width: 100%; line-height: inherit; text-align: left; border-collapse: collapse; }
      .invoice-box table td { padding: 0 4px; vertical-align: top; }

      .invoice-box table tr.top table td { padding-bottom: 0; }
      .invoice-box table tr.top table td.title { font-size: 30px; line-height: 34px; color: #333; }

      .invoice-box table tr.heading td {
        background: <?=$color;?>; color: #fff; font-weight: normal; padding: 4px; text-transform: uppercase;
        font-size: <?=$invoice_heading_font_size;?>px;
      }

      /* ====== GARIS: dotted utk detail/catatan, solid utk pemisah item ====== */
      /* default: baris item tanpa border bawah */
      .row-item td { border-bottom: none; }
      /* kalau ada catatan, baris item diberi class "with-note" sehingga muncul dotted */
      .row-item.with-note td { border-bottom: 1px dotted <?=$color;?>; }
      /* baris catatan sendiri tanpa border bawah */
      .row-note td { border-bottom: 0; font-style: italic; }
      .row-sep  td { border-bottom: 1px solid  <?=$color;?>; height:2px; padding:0; }
      .invoice-size-col {
        white-space: nowrap;
        word-break: keep-all;
        overflow-wrap: normal;
      }

      .invoice-box table tr.kepada td{ font-weight:bold; color: #fff; }
      .invoice-box table tr.total{ font-weight: bold; text-align:right; }
      .invoice-box table tr.hormat{ font-weight: bold; text-align:left; }
      .invoice-box table tr.pelanggan{ font-weight: bold; text-align:center; }
      .invoice-box table td.total { text-align:right; }
      .invoice-box table td.tgl { font-weight:bold; border-bottom:1px dotted #000; }
      .invoice-box table td.tkepada { background:<?=$color;?>; width:13%!important }
      .invoice-box table tr.kepada td.talamat { background:<?=$color;?>; width:12%!important border-bottom:2px solid <?=$color;?>; }
      .invoice-box table tr.kepada td.bawah { color:#000; border-bottom:1px dotted #000; font-weight:normal; width:30%!important }
      .invoice-box table tr.alamat td.nomor { font-weight:bold; vertical-align: text-bottom!important; }

      .invoice-box table td.total1 { border-left:1px solid <?=$color;?>; border-top:1px solid <?=$color;?>; border-bottom:1px dotted <?=$color;?>; }
      .invoice-box table td.total2 { border-top:1px solid <?=$color;?>; border-right:1px solid <?=$color;?>; border-bottom:1px dotted <?=$color;?>; text-align:right; font-weight:bold; }
      .invoice-box table td.lunas1 { border-left:1px solid <?=$color;?>; border-bottom:1px solid <?=$color;?>; background:<?=$color;?>; color:#fff; }
      .invoice-box table td.lunas2 { border-right:1px solid <?=$color;?>; border-bottom:1px solid <?=$color;?>; text-align:right; font-weight:bold; background:<?=$color;?>; color:#fff; }
      .invoice-box table td.umuka1 { border-left:1px solid <?=$color;?>; border-bottom:1px dotted <?=$color;?>; }
      .invoice-box table td.umuka2 { border-right:1px solid <?=$color;?>; border-bottom:1px dotted <?=$color;?>; text-align:right; font-weight:bold; }
      .invoice-box table td.sisa1 { border-left:1px solid <?=$color;?>; border-bottom:1px solid <?=$color;?>; }
      .invoice-box table td.sisa2 { border-right:1px solid <?=$color;?>; border-bottom:1px solid <?=$color;?>; text-align:right; font-weight:bold; }
      .invoice-box .currency-line { display:inline-table; width:auto; border-collapse:collapse; white-space:nowrap; }
      .invoice-box .currency-prefix,
      .invoice-box .currency-amount { display:table-cell; vertical-align:middle; line-height:inherit; }
      .invoice-box .currency-prefix { width:auto; padding-right:6px; text-align:left; }
      .invoice-box .currency-amount { text-align:right; }
      .invoice-box table td.ttd { border-bottom:1px dotted <?=$color;?>; text-align:center; font-weight:bold; }
	  .invoice-box table tr.signature-spacer td { height:<?=$invoice_line_height;?>px; line-height:<?=$invoice_line_height;?>px; padding:0 4px; }
      .invoice-box table td.border { border-right:1px dotted <?=$color;?>; }

      .invoice-box .table img{ position: fixed; z-index:-1000; width:200px; }
      .watermark{ top:-150px; left:40%; margin:0 auto; width: 4.5cm; height:auto; opacity:0.2; position:relative; z-index:10; }

      .alamat{font-size:<?=$invoice_font_size;?>px;}
      .sosmed{font-size:<?=$invoice_small_font_size;?>px;}

      /* === Footer invoice: rapatkan spasi === */
      .footer-invoice{
        font-size:<?=$invoice_footer_font_size;?>px;
        line-height:<?=$invoice_footer_line_height;?>px;
        text-align:left;
        margin:2px 0 0;          /* rapat ke atas */
      }
      .footer-invoice *{
        margin-top:2px !important;
        margin-bottom:2px !important;
      }


    </style>

    <?php if($html=="Y"){ ?>
    <style>
      @media print {
        html, body { width:100%; margin:0; padding:0; }
        .invoice-box { width:100%; margin:0; padding:0; box-sizing:border-box; font-size:<?=$invoice_font_size;?>px; line-height:<?=$invoice_line_height;?>px; color:#555; }
        .invoice-box table { width:100%; line-height:inherit; text-align:left; border-collapse:collapse; }
        .invoice-box table td { padding:0 4px; vertical-align:top; }
        .invoice-box table tr.top table td.title { font-size:30px; line-height:34px; }
        .invoice-box table tr.heading td { background: <?=$color;?>; color:#fff; padding:4px; text-transform:uppercase; font-size:<?=$invoice_heading_font_size;?>px; }

        /* aturan yang sama saat print */
        .row-item td { border-bottom:none; }
        .row-item.with-note td { border-bottom:1px dotted <?=$color;?>; }
        .row-note td { border-bottom:0; }
        .row-sep  td { border-bottom:1px solid  <?=$color;?>; height:2px; padding:0; }
        .invoice-size-col {
          white-space: nowrap;
          word-break: keep-all;
          overflow-wrap: normal;
        }

        .invoice-box table td.tgl { font-weight:bold; border-bottom:1px dotted #000; }
        .invoice-box table td.border { border-right:1px dotted #000; }
        .watermark{ width: 4.5cm; height:auto; opacity:0.2; position:absolute; z-index:10; }
        .sosmed{font-size:<?=$invoice_small_font_size;?>px;}
        .footer-invoice{ line-height:<?=$invoice_footer_line_height;?>px; } /* rapat juga saat print */
      }
    </style>
    <script type="text/javascript">
      window.print();
      window.onfocus=function(){ window.close(); }
    </script>
    <?php }

      $deskripsi = base64_decode($info['deskripsi']);
      $deskripsi = cleanString($deskripsi);
      $wa = $email = $_fb = $fb ='';
      if($info['phone']!='-'){
        $_wa = '<img src="'.$_waicon.'" alt="" height="10" />';
        $wa = $info['phone'];
      }
      if($info['email']!='-'){
        $_email = '<img src="'.$_mail.'" alt="" height="10" />';
        $email = $info['email'];
      }
      if($info['fb']!='-'){
        $_fb = '<img src="'.$_fbicon.'" alt="" height="10" />';
        $fb = $info['fb'];
      }
      $_ig ='';$ig ='';
      if($info['ig']!='-'){
        $_ig = '<img src="'.$_igicon.'" alt="" height="10" />';
        $ig = $info['ig'];
      }

      if($konsumen['tampil']==1){
        $nama = $konsumen['perusahaan'];
        $perusahaan = cek_jenis_lembaga($konsumen['jenis']);
        $telp = phone_number($konsumen['no_telp']);
        $alamat = $konsumen['alamat_lembaga'];
      }else{
        $nama = $konsumen['nama'];
        $perusahaan = cek_jenis_lembaga($konsumen['jenis']);
        $telp = phone_number($konsumen['no_hp']);
        $alamat = $konsumen['alamat'];
      }
    ?>
    <style> img.logo{max-width:380px;max-height:90px} </style>
  </head>
  <body>
    <?php if($show_footer==0){ ?>
    <footer><?= $info['title'];?> © <?php echo date("Y");?></footer>
    <?php } ?>
    <div class="invoice-box">
      <table width="100%" cellpadding="0" cellspacing="0">
        <tr class="alamat">
          <td colspan="4" rowspan="6" valign="top" style="width:60%!important">
            <img class="logo" src="<?=$logo;?>" alt="" /><br>
            <span class="alamat"><?=$deskripsi;?></span><br>
            <?=$_wa;?>&nbsp;<span class="sosmed"><?=$wa;?></span>&nbsp;<?=$_email;?>&nbsp;<span class="sosmed"><?=$email;?></span>&nbsp;<?=$_fb;?>&nbsp;<span class="sosmed"><?=$fb;?></span>&nbsp;<?=$_ig;?>&nbsp;<span class="sosmed"><?=$ig;?></span>
          </td>
          <td colspan="2" class="tgl"><?=$info['keywords'];?>, <?=date_time($cetak->tgl_trx,false);?></td>
        </tr>
        <tr class="kepada">
          <td width="244" class="tkepada">Kepada Yth.</td>
          <td width="72" class="bawah"><?=$nama;?></td>
        </tr>
        <tr class="kepada">
          <td class="tkepada">Telp</td>
          <td class="bawah"><?=$telp;?></td>
        </tr>
        <tr class="kepada">
          <td class="talamat">Alamat</td>
          <td rowspan="2" class="bawah"><?=$alamat;?></td>
        </tr>
        <tr class="kepada"><td class="tkepada">&nbsp;</td></tr>
        <tr class="alamat"><td colspan="2" class="nomor">NO. ORDER #<?=$cetak->id_transaksi;?></td></tr>
      </table>

      <table style="width:100%;margin-top:5px" cellpadding="0" cellspacing="0">
        <tr class="heading">
          <td align="center" style="width:4%!important">No</td>
          <td style="width:16%!important">Nama Barang</td>
          <td style="width:23%!important">Keterangan</td>
          <td style="width:12%!important">Finishing</td>
          <td class="invoice-size-col" align="left" style="width:10%!important">Ukuran</td>
          <td align="center" style="width:7%!important">Qty</td>
          <td style="width:11%!important;text-align:right">Harga</td>
          <?php if($cdiskon>0){ ?>
            <td style="width:5%!important;text-align:right">Disc%</td>
          <?php } ?>
          <td class="total" style="width:17%!important">Total harga</td>
        </tr>
        <?php 
          $no=1; 
          $totalb=0; 
          $subtotal=0; 
          $sisa=0; 
          $diskon=0; 
          foreach($detail  AS $val){

            $diskon  = $val['jumlah'] * $val['harga'] * $val['diskon']/100;
            $totalb  = $val['jumlah'] * $val['harga'] - $diskon;
            $subtotal += $totalb;

            $satuan = '';
            if($val['status_hitung']==0){
              $satuan = get_satuan($val['satuan']);
            }

            $ukuran_text = trim((string)$val['ukuran']);
            $ukuran_text = preg_replace('/\s+/', ' ', $ukuran_text);
            $ukuran_text = ($ukuran_text !== '') ? $ukuran_text : '-';
            $ukuran_html = str_replace(' ', '&nbsp;', htmlspecialchars($ukuran_text, ENT_QUOTES, 'UTF-8'));

            // === CEK CATATAN / DETAIL FINISHING SEKALI DI SINI ===
            $has_note  = false;
            $finishing = null;
            $finishing_text = '-';
            if (!empty($val['detail'])) {
              $finishing = json_decode($val['detail']);
              // pastikan ada properti data dan isinya tidak kosong
              if (!empty($finishing) && !empty($finishing->data)) {
                $has_note = true;
                $finishing_items = [];
                foreach($finishing->data AS $key=>$vals){
                  if (isset($vals->isi) && trim((string)$vals->isi) !== '') {
                    $finishing_items[] = trim((string)$vals->isi);
                  }
                }
                if (!empty($finishing_items)) {
                  $finishing_text = implode(', ', $finishing_items);
                }
              }
            }
        ?>
        <!-- Baris item utama -->
        <tr class="row-item">
          <td align="center" class="border"><?=$no;?></td>
          <td class="border"><?=$val['nbahan'];?></td>
          <td class="border"><?=$val['keterangan'];?></td>
          <td class="border"><?=$finishing_text;?></td>
          <td class="border invoice-size-col" align="left"><?=$ukuran_html;?></td>
          <td align="center" class="border"><?=$val['jumlah'];?> <?=$satuan;?></td>
          <td class="border" align="right"><?=rp($val['harga']);?></td>
          <?php if($cdiskon>0){ ?>
            <td class="border" align="right"><?=rp($val['diskon']);?></td>
          <?php } ?>
          <td class="total"><?=rp($totalb);?></td>
        </tr>

        <?php /*
              // Legacy finishing note row moved to the Finishing column.
              $__first = true;
              foreach($finishing->data  AS $key=>$vals){
                echo ($__first ? '' : ' • ').$vals->isi;
                $__first = false;
              }
            ?>
          </td>
        </tr>
        <?php } ?>

        */ ?>
        <!-- Garis pemisah antar item: SOLID (selalu tampil) -->
        <tr class="row-sep">
          <td colspan="<?= ($cdiskon>0) ? 9 : 8; ?>"></td>
        </tr>

        <?php $no++; } 

        $pajak = ($subtotal * $cetak->pajak) / 100;
        $sisa  = $pajak + $subtotal - $total->total - $potongan_harga;
        $cek_bayar = $pajak + $subtotal;

        if (!is_array($bdetail)) { $bdetail = []; }
        $cnt_bdetail = count($bdetail);

        $jumlah_bayar = isset($jumlah_bayar) ? $jumlah_bayar : 0;
        $kembalian    = isset($kembalian) ? $kembalian : 0;

        $rows_pay = 0; $i = 0; $ni = 1; $_jumlah_bayar = 0; $jml_bayar = 0; $lunas_flag = false;

        foreach ($bdetail as $val) {
          $_jumlah_bayar = $val['jml_bayar'];
          if ($sisa == 0 && $cdetail == 1 && $val['jml_bayar'] == $cek_bayar) {
            $rows_pay += 2;
          } elseif ($sisa > 0 && $cdetail >= 1 && $val['jml_bayar'] != $cek_bayar) {
            $rows_pay += 1;
          } else {
            $rows_pay += 1;
            if (++$i === $cnt_bdetail) {
              $lunas_flag = true;
              $jml_bayar = $val['jml_bayar'];
              if ($jumlah_bayar > $val['jml_bayar']) {
                $rows_pay += 1;
              }
            }
          }
          $ni++;
        }

        $rows_tax  = ($cetak->pajak > 0) ? 1 : 0;
        $rows_disc = ($potongan_harga > 0) ? 1 : 0;

		/*
		 * Area tanda tangan harus tetap memiliki ruang yang sama. Baris pembayaran
		 * lunas biasanya sudah memberi ruang tersebut, sedangkan transaksi piutang
		 * dapat tidak mempunyai baris pembayaran sama sekali.
		 */
		$rows_balance = ($sisa > 0) ? 1 : 0;
		$signature_spacer_rows = max(0, 2 - ($rows_pay + $rows_balance));
		$rowspan = 1 + $rows_tax + $rows_disc + $rows_pay + $rows_balance + $signature_spacer_rows + 1;
        ?>
      </table>

      <table class="table" style="width:100%;margin-top:10px" cellpadding="0" cellspacing="0">
        <tr>
          <td width="11%" rowspan="<?= $rowspan; ?>" align="left" style="width:35%!important">
            <div style="font-size:<?=$invoice_heading_font_size;?>px; line-height:<?=$invoice_heading_line_height;?>px;">
              <?php
                if ($alt == 'BATAL') {
                  echo "<span style='color:red'>ORDER DI BATALKAN</span>";
                } else {
                  echo "<strong>Bayar :</strong> ";
                  if ($total->total != 0) {
                    if (isset($cara) && is_array($cara) && !empty($cara['nama_bayar'])) {
                      echo strtoupper($cara['nama_bayar']);
                    } elseif (isset($cara) && is_object($cara) && !empty($cara->nama_bayar)) {
                      echo strtoupper($cara->nama_bayar);
                    } else {
                      echo "-";
                    }
                  } else {
                    echo "PIUTANG";
                  }
                }
                if ($cashback > 0) {
                  echo "<br><strong>Cashback :</strong> " . rp($cashback);
                }
              ?>
              <?php if (!empty($bank)) { ?>
                <br><strong>No. Rekening</strong><br>
                <span style="font-size:<?=$invoice_small_font_size;?>px; line-height:<?=$invoice_small_line_height;?>px; display:inline-block;">
                  <?php foreach ($bank as $val) {
                    echo $val->inisial . ' a.n ' . $val->pemilik . ' <b>' . $val->nomor_rekening . '</b><br>';
                  } ?>
                </span>
              <?php } ?>
            </div>
          </td>

          <td width="17%" align="center" style="width:20%!important; text-align:center;"><strong>HORMAT KAMI</strong></td>
          <td width="0%">&nbsp;</td>
          <td width="21%" align="center" style="width:20%!important; text-align:center;"><strong>PEMESAN</strong></td>
          <td width="1%">&nbsp;</td>
          <td width="9%" class="total1" style="width:12%">Total Order</td>
          <td width="16%" class="total2" style="width:19%"><span class="currency-line"><span class="currency-prefix">Rp</span><span class="currency-amount"><?=rp($subtotal);?></span></span></td>
        </tr>

        <?php if($cetak->pajak >0){?>
        <tr>
          <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
          <td class="umuka1">Pajak <?=$cetak->pajak;?>%</td>
          <td class="umuka2"><span class="currency-line"><span class="currency-prefix">Rp</span><span class="currency-amount"><?=rp($pajak);?></span></span></td>
        </tr>
        <?php } ?>

        <?php if($potongan_harga >0){?>
        <tr>
          <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
          <td class="umuka1">Diskon </td>
          <td class="umuka2"><span class="currency-line"><span class="currency-prefix">Rp</span><span class="currency-amount"><?=rp($potongan_harga);?></span></span></td>
        </tr>
        <?php } ?>

        <?php 
          $i = 0; $ni = 1; $_jumlah_bayar = 0; $jml_bayar = 0; $lunas = false;
          foreach($bdetail AS $val){
            $_jumlah_bayar = $val['jml_bayar'];

            if($sisa==0 AND $cdetail==1 AND $val['jml_bayar']==$cek_bayar){
              if($jumlah_bayar > $total->total){ $jumlah_bayar = $jumlah_bayar; } else { $jumlah_bayar = $total->total; }
              echo '<tr class="lunas">
              <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
              <td class="umuka1">Jml. Bayar</td>
              <td class="umuka2"><span class="currency-line"><span class="currency-prefix">Rp</span><span class="currency-amount">'.rp($jumlah_bayar).'</span></span></td>
              </tr>';
              echo '<tr class="lunas">
              <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
              <td class="lunas1">Kembalian</td>
              <td class="lunas2"><span class="currency-line"><span class="currency-prefix">Rp</span><span class="currency-amount">'.rp($kembalian).'</span></span></td>
              </tr>';

            }elseif($sisa >0 AND $cdetail >=1 AND $val['jml_bayar']!=$cek_bayar){
              echo '<tr>
              <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
              <td class="umuka1">Bayar ke-'.$ni.'</td>
              <td class="umuka2"><span class="currency-line"><span class="currency-prefix">Rp</span><span class="currency-amount">'.rp($val['jml_bayar']).'</span></span></td>
              </tr>';

            }else{
              echo '<tr>
              <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
              
              if($cdetail >1 AND $total->total!=$cek_bayar){
                echo '<td class="umuka1">Bayar ke-'.$ni.'</td>';
              }elseif($cdetail >1 AND $total->total==$cek_bayar){
                if(++$i === $cnt_bdetail) {
                  $lunas = true;
                  $jml_bayar = $val['jml_bayar'];
                  if($jumlah_bayar > $val['jml_bayar']){
                    $_jumlah_bayar = $jumlah_bayar;
                  }
                  echo '<td class="umuka1">Pelunasan</td>';
                }else{
                  echo '<td class="umuka1">Bayar ke-'.$ni.'</td>';
                }
              }else{
                echo '<td class="umuka1">Pelunasan</td>';
              }
              echo '<td class="umuka2"><span class="currency-line"><span class="currency-prefix">Rp</span><span class="currency-amount">'.rp($_jumlah_bayar).'</span></span></td></tr>';
            }
            $ni++;
          }

          if($jumlah_bayar > $jml_bayar AND $lunas==true ){
            $jumlah_bayar = $jumlah_bayar;
        ?>
        <tr>
          <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
          <td class="lunas1">Kembalian</td>
          <td class="lunas2"><span class="currency-line"><span class="currency-prefix">Rp</span><span class="currency-amount"><?=rp($kembalian);?></span></span></td>
        </tr>
        <?php } ?>

		<?php if ($sisa > 0) { ?>
		<tr>
		  <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
		  <td class="sisa1">Piutang</td>
		  <td class="sisa2"><span class="currency-line"><span class="currency-prefix">Rp</span><span class="currency-amount"><?=rp($sisa);?></span></span></td>
		</tr>
		<?php } ?>

		<?php for ($signature_space = 0; $signature_space < $signature_spacer_rows; $signature_space++) { ?>
		<tr class="signature-spacer">
		  <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
		</tr>
		<?php } ?>

        <tr>
          <?php if($jumlah_bayar > $jml_bayar AND $lunas==true AND $ni > 3){ ?>
            <td>&nbsp;</td>
          <?php } ?>
          <td class="ttd"><?=$marketing['nama_lengkap'];?></td>
          <td>&nbsp;</td>
          <td class="ttd"><?=$konsumen['nama'];?></td>
          <td>&nbsp;</td>
		  <td>&nbsp;</td><td>&nbsp;</td>
        </tr>

        <tr>
          <td colspan="8" align="left" style="font-size:<?=$invoice_meta_font_size;?>px">
            dicetak pada <?=dtimes(date('Y-m-d H:i:s'));?> | 
            <?php if(isset($cara)){ echo $cetak->cetak.' x cetak'; } ?>
          </td>
        </tr>

        <tr>
          <?php if($alt=='BATAL'){ ?>
            <td colspan="8" align="left" style="font-size:<?=$invoice_font_size;?>px;width:30%!important;line-height:<?=$invoice_line_height;?>px;color:red">
              Keterangan : <?=$cetak->history;?>
            </td>
          <?php }else{ ?>
            <td colspan="8" align="left" style="width:30%!important;padding-top:2px;padding-bottom:2px">
              <div class="footer-invoice">
                <?=base64_decode($info['footer_invoice']);?>
              </div>
            </td>
          <?php } ?>
        </tr>

      </table>

      <img class="watermark" src="<?=$stamp;?>" width="100px" alt="" />
	  <?php if($html!="Y" && !empty($qr_available)){ ?>
      <div class="gbr">
		<?php if(!empty($qr_is_tracking)){ ?><div class="qr-tracking-label"><?=htmlspecialchars($qr_label, ENT_QUOTES, 'UTF-8');?></div><?php } ?>
        <img src="<?=$kode_qris;?>" class="qris" alt="" style="width:100px; height:100px;" />
      </div>
      <?php } ?>
    </div>
  </body>
</html>
