<!DOCTYPE html>
<html lang="id"><head><meta charset="utf-8"><title>Invoice <?= e($inv['invoice_no']) ?></title>
<style>
body{font-family:Arial,Helvetica,sans-serif;font-size:12px;color:#222;margin:22px}
h1{font-size:17px;margin:0 0 2px;color:#1f3864}h2{font-size:14px;margin:14px 0 6px}
.meta{font-size:11px;color:#666}table{width:100%;border-collapse:collapse;margin-top:8px}
th{background:#1f3864;color:#fff;padding:6px;text-align:left;font-size:11px;border:1px solid #1f3864}
td{padding:5px 6px;border:1px solid #d7d7d7;font-size:11px}.num{text-align:right}
.tot td{background:#dce6f1;font-weight:700}.box{display:flex;gap:26px;margin-top:10px}
.box>div{flex:1}.lbl{color:#666;width:130px;display:inline-block}
.ttd{margin-top:40px;display:flex;gap:60px}.ttd div{flex:1;font-size:11px}
.line{margin-top:52px;border-top:1px solid #888;padding-top:4px}
@media print{.noprint{display:none}body{margin:10mm}}
button{padding:7px 14px;margin-bottom:12px;border:1px solid #1f3864;background:#1f3864;color:#fff;border-radius:5px;cursor:pointer}
</style></head><body>
<div class="noprint"><button onclick="window.print()">Cetak</button> <button onclick="window.close()" style="background:#fff;color:#1f3864">Tutup</button></div>
<h1><?= e(setting('umum.nama_perusahaan','')) ?></h1>
<div class="meta"><?= e(setting('umum.alamat_perusahaan','')) ?><?= setting('umum.npwp_perusahaan') ? ' · NPWP ' . e(setting('umum.npwp_perusahaan')) : '' ?></div>
<h2>RINCIAN INVOICE &amp; PIUTANG</h2>
<div class="box">
  <div>
    <div><span class="lbl">Nomor Invoice</span>: <strong><?= e($inv['invoice_no']) ?></strong></div>
    <div><span class="lbl">Nomor Faktur Pajak</span>: <?= e($inv['tax_invoice_no'] ?: '-') ?></div>
    <div><span class="lbl">Nomor SO</span>: <?= e($inv['so_no'] ?: '-') ?></div>
    <div><span class="lbl">Tanggal Invoice</span>: <?= tgl($inv['invoice_date']) ?></div>
    <div><span class="lbl">Tanggal Kirim</span>: <?= tgl($inv['sent_date']) ?></div>
    <div><span class="lbl">Jatuh Tempo</span>: <strong><?= tgl($inv['due_date']) ?></strong></div>
  </div>
  <div>
    <div><span class="lbl">Customer</span>: <strong><?= e($inv['customer_name']) ?></strong></div>
    <div><span class="lbl">Outlet</span>: <?= e($inv['outlet_name'] ?: '-') ?></div>
    <div><span class="lbl">NPWP</span>: <?= e($inv['customer_npwp'] ?: '-') ?></div>
    <div><span class="lbl">Area / Unit</span>: <?= e($inv['area_name'] ?: '-') ?> / <?= e($inv['business_unit_name'] ?: '-') ?></div>
    <div><span class="lbl">Jenis Pekerjaan</span>: <?= e($inv['job_type_name'] ?: '-') ?></div>
    <div><span class="lbl">Status</span>: <?= e($inv['payment_status_name'] ?: $inv['payment_status_code']) ?></div>
  </div>
</div>
<table><thead><tr><th>Uraian</th><th class="num" style="width:180px">Nilai</th></tr></thead><tbody>
  <tr><td>Nominal Bruto (termasuk PPN <?= nf(setting('pajak.tarif_ppn',11),0) ?>%)</td><td class="num"><?= e(rp($inv['gross_amount'])) ?></td></tr>
  <tr><td style="padding-left:20px;color:#666">Dasar Pengenaan Pajak (DPP)</td><td class="num" style="color:#666"><?= e(rp($inv['dpp_amount'])) ?></td></tr>
  <tr><td style="padding-left:20px;color:#666">PPN</td><td class="num" style="color:#666"><?= e(rp($inv['vat_amount'])) ?></td></tr>
  <?php foreach ($deductions as $d): ?>
    <tr><td style="padding-left:20px">(−) <?= e($d['name']) ?></td><td class="num">(<?= e(rp((float)$d['amount'])) ?>)</td></tr>
  <?php endforeach; ?>
  <tr class="tot"><td>TOTAL TAGIHAN</td><td class="num"><?= e(rp($inv['net_amount'])) ?></td></tr>
  <tr><td>(−) Pembayaran Diterima</td><td class="num"><?= e(rp($inv['paid_amount'])) ?></td></tr>
  <?php if ((float)$inv['credit_note_amount'] != 0): ?><tr><td>(−) Nota Kredit</td><td class="num"><?= e(rp($inv['credit_note_amount'])) ?></td></tr><?php endif; ?>
  <tr class="tot"><td>SALDO PIUTANG</td><td class="num"><?= e(rp($inv['outstanding_amount'])) ?></td></tr>
</tbody></table>
<?php if ($payments): ?>
<h2 style="margin-top:16px">Riwayat Pembayaran</h2>
<table><thead><tr><th>No. Pembayaran</th><th>Tanggal</th><th>Metode</th><th>Referensi</th><th class="num">Nilai</th></tr></thead><tbody>
<?php foreach ($payments as $pm): ?>
<tr><td><?= e($pm['payment_no']) ?></td><td><?= tgl($pm['payment_date']) ?></td><td><?= e($pm['method']) ?></td>
    <td><?= e($pm['reference_no'] ?: '-') ?></td><td class="num"><?= e(rp($pm['allocated_amount'])) ?></td></tr>
<?php endforeach; ?></tbody></table>
<?php endif; ?>
<div class="ttd">
  <div>Dibuat oleh,<div class="line"><?= e(userName()) ?></div></div>
  <div>Diketahui oleh,<div class="line">(...................................)</div></div>
</div>
<div class="meta" style="margin-top:20px">Dicetak <?= tglWaktu(App\Core\Dates::now()) ?> melalui SIPIUTANG</div>
<script>window.addEventListener('load',()=>setTimeout(()=>window.print(),400))</script>
</body></html>
