Update Fix
This commit is contained in:
513
resources/js/admin/tailadmin/pages/Pos/Piutang.tsx
Normal file
513
resources/js/admin/tailadmin/pages/Pos/Piutang.tsx
Normal file
@@ -0,0 +1,513 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import PageMeta from "../../components/common/PageMeta";
|
||||
import { apiGet, apiPost } from "../../lib/api";
|
||||
import { useToastOnError } from "../../hooks/useToastOnError";
|
||||
import { Modal } from "../../components/ui/modal";
|
||||
import { useModal } from "../../hooks/useModal";
|
||||
|
||||
type PiutangRow = {
|
||||
id: number;
|
||||
nomor_penjualan: string;
|
||||
waktu: string;
|
||||
total: number;
|
||||
jumlah_bayar: number;
|
||||
sisa_piutang: number;
|
||||
status_pembayaran: string;
|
||||
unit_piutang?: string | null;
|
||||
kasir?: string | null;
|
||||
};
|
||||
|
||||
type PiutangHistoryRow = {
|
||||
id: number;
|
||||
penjualan_id: number;
|
||||
nomor_penjualan: string | null;
|
||||
unit_piutang?: string | null;
|
||||
jumlah: number;
|
||||
metode: string;
|
||||
catatan: string | null;
|
||||
sisa_before: number;
|
||||
sisa_after: number;
|
||||
created_at: string;
|
||||
kasir?: string | null;
|
||||
};
|
||||
|
||||
export default function Piutang() {
|
||||
const [rows, setRows] = useState<PiutangRow[]>([]);
|
||||
const [history, setHistory] = useState<PiutangHistoryRow[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [loadingHistory, setLoadingHistory] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [msg, setMsg] = useState<string | null>(null);
|
||||
const [q, setQ] = useState("");
|
||||
const [from, setFrom] = useState("");
|
||||
const [to, setTo] = useState("");
|
||||
const [historyQ, setHistoryQ] = useState("");
|
||||
const [historyFrom, setHistoryFrom] = useState("");
|
||||
const [historyTo, setHistoryTo] = useState("");
|
||||
|
||||
const [pay, setPay] = useState({
|
||||
id: 0,
|
||||
nomor: "",
|
||||
sisa: 0,
|
||||
jumlah: "",
|
||||
metode: "CASH",
|
||||
catatan: "",
|
||||
});
|
||||
const [paying, setPaying] = useState(false);
|
||||
const { isOpen, openModal, closeModal } = useModal();
|
||||
|
||||
useToastOnError(error);
|
||||
|
||||
const metodeLabel = (value?: string) => {
|
||||
if (!value) return "-";
|
||||
if (value === "CASH") return "TUNAI";
|
||||
if (value === "VOUCHER_CASH") return "VOUCHER + TUNAI";
|
||||
return value;
|
||||
};
|
||||
|
||||
const load = () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const params = new URLSearchParams();
|
||||
params.set("limit", "200");
|
||||
if (q.trim()) params.set("q", q.trim());
|
||||
if (from) params.set("from", from);
|
||||
if (to) params.set("to", to);
|
||||
apiGet<{ data: PiutangRow[] }>(`/admin-api/piutang?${params.toString()}`)
|
||||
.then((res) => setRows(res.data))
|
||||
.catch((e) => setError(e?.message || "Gagal memuat piutang"))
|
||||
.finally(() => setLoading(false));
|
||||
};
|
||||
|
||||
const loadHistory = () => {
|
||||
setLoadingHistory(true);
|
||||
const params = new URLSearchParams();
|
||||
params.set("limit", "200");
|
||||
if (historyQ.trim()) params.set("q", historyQ.trim());
|
||||
if (historyFrom) params.set("from", historyFrom);
|
||||
if (historyTo) params.set("to", historyTo);
|
||||
apiGet<{ data: PiutangHistoryRow[] }>(`/admin-api/piutang/history?${params.toString()}`)
|
||||
.then((res) => setHistory(res.data))
|
||||
.catch((e) => setError(e?.message || "Gagal memuat riwayat"))
|
||||
.finally(() => setLoadingHistory(false));
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
load();
|
||||
loadHistory();
|
||||
}, []);
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
const s = q.trim().toLowerCase();
|
||||
if (!s) return rows;
|
||||
return rows.filter((r) => r.nomor_penjualan.toLowerCase().includes(s));
|
||||
}, [rows, q]);
|
||||
|
||||
const openPay = (r: PiutangRow) => {
|
||||
setPay({
|
||||
id: r.id,
|
||||
nomor: r.nomor_penjualan,
|
||||
sisa: r.sisa_piutang,
|
||||
jumlah: "",
|
||||
metode: "CASH",
|
||||
catatan: "",
|
||||
});
|
||||
openModal();
|
||||
};
|
||||
|
||||
const submitPay = async () => {
|
||||
setError(null);
|
||||
setMsg(null);
|
||||
const jumlah = Number(pay.jumlah || 0);
|
||||
if (!jumlah || jumlah <= 0) {
|
||||
return setError("Jumlah bayar wajib diisi.");
|
||||
}
|
||||
setPaying(true);
|
||||
try {
|
||||
const res = await apiPost<{ data?: { penjualan?: PiutangRow; payment?: any } }>(
|
||||
`/admin-api/piutang/${pay.id}/pay`,
|
||||
{
|
||||
jumlah,
|
||||
metode: pay.metode,
|
||||
catatan: pay.catatan || null,
|
||||
}
|
||||
);
|
||||
setMsg(`Pembayaran piutang berhasil untuk ${pay.nomor}`);
|
||||
closeModal();
|
||||
load();
|
||||
loadHistory();
|
||||
const payment = res?.data?.payment;
|
||||
if (payment) {
|
||||
printPelunasan({
|
||||
nomor: pay.nomor,
|
||||
jumlah,
|
||||
metode: pay.metode,
|
||||
sisa_after: payment.sisa_after ?? Math.max(0, pay.sisa - jumlah),
|
||||
waktu: payment.created_at,
|
||||
});
|
||||
}
|
||||
} catch (e: any) {
|
||||
setError(e?.message || "Gagal menyimpan pembayaran");
|
||||
} finally {
|
||||
setPaying(false);
|
||||
}
|
||||
};
|
||||
|
||||
const exportPiutang = () => {
|
||||
const params = new URLSearchParams();
|
||||
if (q.trim()) params.set("q", q.trim());
|
||||
if (from) params.set("from", from);
|
||||
if (to) params.set("to", to);
|
||||
const url = `/admin-api/piutang/export?${params.toString()}`;
|
||||
window.open(url, "_blank");
|
||||
};
|
||||
|
||||
const exportHistory = () => {
|
||||
const params = new URLSearchParams();
|
||||
if (historyQ.trim()) params.set("q", historyQ.trim());
|
||||
if (historyFrom) params.set("from", historyFrom);
|
||||
if (historyTo) params.set("to", historyTo);
|
||||
const url = `/admin-api/piutang/history/export?${params.toString()}`;
|
||||
window.open(url, "_blank");
|
||||
};
|
||||
|
||||
const printPelunasan = (payload: {
|
||||
nomor: string;
|
||||
jumlah: number;
|
||||
metode: string;
|
||||
sisa_after: number;
|
||||
waktu?: string;
|
||||
}) => {
|
||||
const win = window.open("", "_blank", "width=520,height=640");
|
||||
if (!win) {
|
||||
setError("Popup diblokir. Izinkan popup untuk mencetak.");
|
||||
return;
|
||||
}
|
||||
const timeText = payload.waktu
|
||||
? new Date(payload.waktu).toLocaleString("id-ID")
|
||||
: new Date().toLocaleString("id-ID");
|
||||
const html = `
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Bukti Pelunasan</title>
|
||||
<style>
|
||||
@page { margin: 6mm; }
|
||||
body { font-family: "Courier New", monospace; padding: 0; color: #111; }
|
||||
.receipt { width: 58mm; }
|
||||
h1 { font-size: 13px; margin: 0 0 4px; text-align: center; }
|
||||
.muted { color: #666; font-size: 10px; text-align: center; }
|
||||
.divider { border-top: 1px dashed #666; margin: 6px 0; }
|
||||
.row { display: flex; justify-content: space-between; font-size: 11px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="receipt">
|
||||
<h1>Bukti Pelunasan Piutang</h1>
|
||||
<div class="muted">No: ${payload.nomor}</div>
|
||||
<div class="muted">${timeText}</div>
|
||||
<div class="divider"></div>
|
||||
<div class="row"><div>Metode</div><div>${metodeLabel(payload.metode)}</div></div>
|
||||
<div class="row"><div>Bayar</div><div>Rp ${payload.jumlah.toLocaleString("id-ID")}</div></div>
|
||||
<div class="row"><div>Sisa</div><div>Rp ${payload.sisa_after.toLocaleString("id-ID")}</div></div>
|
||||
<div class="divider"></div>
|
||||
<div class="muted">Terima kasih</div>
|
||||
</div>
|
||||
<script>window.print();</script>
|
||||
</body>
|
||||
</html>`;
|
||||
win.document.open();
|
||||
win.document.write(html);
|
||||
win.document.close();
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageMeta title="Piutang" description="Daftar dan pembayaran piutang" />
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-semibold text-gray-800 dark:text-white/90">
|
||||
Piutang
|
||||
</h1>
|
||||
<button
|
||||
type="button"
|
||||
onClick={exportPiutang}
|
||||
className="h-10 rounded-xl border border-gray-200 bg-white px-4 text-sm font-semibold hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-950"
|
||||
>
|
||||
Export Excel
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{msg && (
|
||||
<div className="rounded-xl bg-emerald-50 px-4 py-3 text-sm text-emerald-700 dark:bg-emerald-500/10 dark:text-emerald-300">
|
||||
{msg}
|
||||
</div>
|
||||
)}
|
||||
{error && (
|
||||
<div className="rounded-xl bg-red-50 px-4 py-3 text-sm text-red-700 dark:bg-red-500/10 dark:text-red-300">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
||||
Daftar Piutang
|
||||
</div>
|
||||
<div className="flex w-full flex-col gap-2 sm:w-auto sm:flex-row">
|
||||
<input
|
||||
type="date"
|
||||
value={from}
|
||||
onChange={(e) => setFrom(e.target.value)}
|
||||
className="h-10 w-full sm:w-[160px] rounded-xl border border-gray-200 bg-white px-3 text-sm dark:border-gray-800 dark:bg-gray-950"
|
||||
/>
|
||||
<input
|
||||
type="date"
|
||||
value={to}
|
||||
onChange={(e) => setTo(e.target.value)}
|
||||
className="h-10 w-full sm:w-[160px] rounded-xl border border-gray-200 bg-white px-3 text-sm dark:border-gray-800 dark:bg-gray-950"
|
||||
/>
|
||||
<input
|
||||
value={q}
|
||||
onChange={(e) => setQ(e.target.value)}
|
||||
placeholder="Cari no transaksi..."
|
||||
className="h-10 w-full sm:w-[240px] rounded-xl border border-gray-200 bg-white px-4 text-sm outline-none focus:ring-2 focus:ring-blue-500/30 dark:border-gray-800 dark:bg-gray-950"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={load}
|
||||
className="h-10 rounded-xl border border-gray-200 bg-white px-4 text-sm font-semibold hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-950"
|
||||
>
|
||||
Cari
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="mt-4 text-sm text-gray-600 dark:text-gray-400">Memuat...</div>
|
||||
) : (
|
||||
<div className="mt-4 overflow-x-auto">
|
||||
<table className="min-w-full text-sm">
|
||||
<thead>
|
||||
<tr className="text-left text-gray-500 dark:text-gray-400">
|
||||
<th className="py-2 pr-4">Waktu</th>
|
||||
<th className="py-2 pr-4">No</th>
|
||||
<th className="py-2 pr-4">Unit</th>
|
||||
<th className="py-2 pr-4">Kasir</th>
|
||||
<th className="py-2 pr-4">Total</th>
|
||||
<th className="py-2 pr-4">Bayar</th>
|
||||
<th className="py-2 pr-4">Sisa</th>
|
||||
<th className="py-2 pr-4">Status</th>
|
||||
<th className="py-2 pr-4">Aksi</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{filtered.map((r) => (
|
||||
<tr key={r.id} className="border-t border-gray-100 dark:border-gray-800">
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{new Date(r.waktu).toLocaleString("id-ID")}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{r.nomor_penjualan}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{r.unit_piutang || "-"}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{r.kasir || "-"}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
Rp {Number(r.total || 0).toLocaleString("id-ID")}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
Rp {Number(r.jumlah_bayar || 0).toLocaleString("id-ID")}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
Rp {Number(r.sisa_piutang || 0).toLocaleString("id-ID")}
|
||||
</td>
|
||||
<td className="py-3 pr-4">
|
||||
<span className="inline-flex rounded-full bg-amber-50 px-3 py-1 text-xs font-semibold text-amber-700 dark:bg-amber-500/10 dark:text-amber-300">
|
||||
{r.status_pembayaran || "PIUTANG"}
|
||||
</span>
|
||||
</td>
|
||||
<td className="py-3 pr-4">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => openPay(r)}
|
||||
className="h-8 rounded-lg border border-gray-200 bg-white px-3 text-xs font-semibold hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-950"
|
||||
>
|
||||
Bayar
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{filtered.length === 0 && (
|
||||
<tr>
|
||||
<td colSpan={9} className="py-6 text-center text-gray-500 dark:text-gray-400">
|
||||
Tidak ada piutang.
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
||||
Riwayat Pembayaran Piutang
|
||||
</div>
|
||||
<div className="flex w-full flex-col gap-2 sm:w-auto sm:flex-row">
|
||||
<input
|
||||
type="date"
|
||||
value={historyFrom}
|
||||
onChange={(e) => setHistoryFrom(e.target.value)}
|
||||
className="h-10 w-full sm:w-[160px] rounded-xl border border-gray-200 bg-white px-3 text-sm dark:border-gray-800 dark:bg-gray-950"
|
||||
/>
|
||||
<input
|
||||
type="date"
|
||||
value={historyTo}
|
||||
onChange={(e) => setHistoryTo(e.target.value)}
|
||||
className="h-10 w-full sm:w-[160px] rounded-xl border border-gray-200 bg-white px-3 text-sm dark:border-gray-800 dark:bg-gray-950"
|
||||
/>
|
||||
<input
|
||||
value={historyQ}
|
||||
onChange={(e) => setHistoryQ(e.target.value)}
|
||||
placeholder="Cari no transaksi..."
|
||||
className="h-10 w-full sm:w-[240px] rounded-xl border border-gray-200 bg-white px-4 text-sm outline-none focus:ring-2 focus:ring-blue-500/30 dark:border-gray-800 dark:bg-gray-950"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={loadHistory}
|
||||
className="h-10 rounded-xl border border-gray-200 bg-white px-4 text-sm font-semibold hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-950"
|
||||
>
|
||||
Cari
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={exportHistory}
|
||||
className="h-10 rounded-xl border border-gray-200 bg-white px-4 text-sm font-semibold hover:bg-gray-50 dark:border-gray-800 dark:bg-gray-950"
|
||||
>
|
||||
Export Excel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{loadingHistory ? (
|
||||
<div className="mt-4 text-sm text-gray-600 dark:text-gray-400">Memuat...</div>
|
||||
) : history.length === 0 ? (
|
||||
<div className="mt-4 text-sm text-gray-600 dark:text-gray-400">
|
||||
Belum ada riwayat pembayaran.
|
||||
</div>
|
||||
) : (
|
||||
<div className="mt-4 overflow-x-auto">
|
||||
<table className="min-w-full text-sm">
|
||||
<thead>
|
||||
<tr className="text-left text-gray-500 dark:text-gray-400">
|
||||
<th className="py-2 pr-4">Waktu</th>
|
||||
<th className="py-2 pr-4">No</th>
|
||||
<th className="py-2 pr-4">Unit</th>
|
||||
<th className="py-2 pr-4">Kasir</th>
|
||||
<th className="py-2 pr-4">Metode</th>
|
||||
<th className="py-2 pr-4">Jumlah</th>
|
||||
<th className="py-2 pr-4">Sisa Sebelum</th>
|
||||
<th className="py-2 pr-4">Sisa Sesudah</th>
|
||||
<th className="py-2 pr-4">Catatan</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{history.map((h) => (
|
||||
<tr key={h.id} className="border-t border-gray-100 dark:border-gray-800">
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{new Date(h.created_at).toLocaleString("id-ID")}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{h.nomor_penjualan || "-"}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{h.unit_piutang || "-"}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{h.kasir || "-"}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{metodeLabel(h.metode)}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
Rp {Number(h.jumlah || 0).toLocaleString("id-ID")}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
Rp {Number(h.sisa_before || 0).toLocaleString("id-ID")}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
Rp {Number(h.sisa_after || 0).toLocaleString("id-ID")}
|
||||
</td>
|
||||
<td className="py-3 pr-4 text-gray-700 dark:text-gray-200">
|
||||
{h.catatan || "-"}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Modal
|
||||
isOpen={isOpen}
|
||||
onClose={closeModal}
|
||||
className="max-w-[520px] p-6 lg:p-8"
|
||||
>
|
||||
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm dark:border-gray-800 dark:bg-gray-900">
|
||||
<div className="text-sm font-semibold text-gray-800 dark:text-white/90">
|
||||
Pembayaran Piutang
|
||||
</div>
|
||||
<div className="mt-2 text-xs text-gray-500">
|
||||
No: <span className="font-semibold">{pay.nomor}</span>
|
||||
</div>
|
||||
<div className="mt-2 text-xs text-gray-500">
|
||||
Sisa: <span className="font-semibold">Rp {pay.sisa.toLocaleString("id-ID")}</span>
|
||||
</div>
|
||||
<div className="mt-4 grid gap-3">
|
||||
<input
|
||||
value={pay.jumlah}
|
||||
onChange={(e) => setPay((p) => ({ ...p, jumlah: e.target.value }))}
|
||||
placeholder="Jumlah bayar"
|
||||
type="number"
|
||||
min={1}
|
||||
className="h-11 rounded-xl border border-gray-200 bg-white px-4 text-sm dark:border-gray-800 dark:bg-gray-950"
|
||||
/>
|
||||
<select
|
||||
value={pay.metode}
|
||||
onChange={(e) => setPay((p) => ({ ...p, metode: e.target.value }))}
|
||||
className="h-11 rounded-xl border border-gray-200 bg-white px-4 text-sm dark:border-gray-800 dark:bg-gray-950"
|
||||
>
|
||||
<option value="CASH">TUNAI</option>
|
||||
<option value="TRANSFER">TRANSFER</option>
|
||||
<option value="QRIS">QRIS</option>
|
||||
<option value="KARTU">KARTU</option>
|
||||
</select>
|
||||
<input
|
||||
value={pay.catatan}
|
||||
onChange={(e) => setPay((p) => ({ ...p, catatan: e.target.value }))}
|
||||
placeholder="Catatan (opsional)"
|
||||
className="h-11 rounded-xl border border-gray-200 bg-white px-4 text-sm dark:border-gray-800 dark:bg-gray-950"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={submitPay}
|
||||
disabled={paying}
|
||||
className="h-11 rounded-xl bg-blue-600 px-5 text-sm font-semibold text-white hover:bg-blue-700 disabled:opacity-60"
|
||||
>
|
||||
{paying ? "Menyimpan..." : "Simpan Pembayaran"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user