76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
|
|
import Link from "next/link";
|
||
|
|
import { getPb, fmtGBP, fmtDate } from "../lib/pb";
|
||
|
|
import RemittanceClient from "./RemittanceClient";
|
||
|
|
|
||
|
|
export const dynamic = "force-dynamic";
|
||
|
|
|
||
|
|
const PB_URL = process.env.NEXT_PUBLIC_PB_URL || process.env.PB_URL || "https://pb.agent.apphub.co.za";
|
||
|
|
|
||
|
|
async function fetchBatchWithInvoices(pb: any, batchId: string) {
|
||
|
|
const inv = await pb.collection("JEI_invoices").getList(1, 500, {
|
||
|
|
filter: `payment_batch="${batchId}"`,
|
||
|
|
sort: "-invoice_date",
|
||
|
|
fields: "id,invoice_no,invoice_date,buyer_name_variant,invoice_total",
|
||
|
|
});
|
||
|
|
return inv.items as any[];
|
||
|
|
}
|
||
|
|
|
||
|
|
export default async function RemittancePage() {
|
||
|
|
const pb = getPb();
|
||
|
|
let batches: any[] = [];
|
||
|
|
let errorMsg: string | null = null;
|
||
|
|
|
||
|
|
try {
|
||
|
|
const r = await pb.collection("JEI_payment_batches").getList(1, 500, {
|
||
|
|
sort: "-payment_date,-created",
|
||
|
|
});
|
||
|
|
batches = r.items;
|
||
|
|
} catch (e: any) {
|
||
|
|
errorMsg = String(e?.message || e);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Fetch invoices per batch in parallel
|
||
|
|
let batchWithInvoices: { batch: any; invoices: any[]; popUrl: string | null }[] = [];
|
||
|
|
if (!errorMsg) {
|
||
|
|
batchWithInvoices = await Promise.all(
|
||
|
|
batches.map(async (b) => {
|
||
|
|
try {
|
||
|
|
const invoices = await fetchBatchWithInvoices(pb, b.id);
|
||
|
|
const popUrl = b.pop_file ? `${PB_URL}/api/files/JEI_payment_batches/${b.id}/${b.pop_file}` : null;
|
||
|
|
return { batch: b, invoices, popUrl };
|
||
|
|
} catch (_) {
|
||
|
|
return { batch: b, invoices: [], popUrl: null };
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div className="space-y-6">
|
||
|
|
<div className="flex items-center justify-between">
|
||
|
|
<h1 className="text-2xl font-bold text-brand-dark">Remittance</h1>
|
||
|
|
<div className="text-sm text-slate-500">{batches.length} batch(es)</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
{errorMsg && (
|
||
|
|
<div className="bg-red-50 border border-red-200 text-red-800 rounded p-4">
|
||
|
|
<div className="font-semibold">Cannot reach PocketBase</div>
|
||
|
|
<div className="text-sm mt-1">{errorMsg}</div>
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
|
||
|
|
{!errorMsg && batches.length === 0 && (
|
||
|
|
<div className="bg-white rounded-lg shadow p-8 text-center text-slate-500">
|
||
|
|
No payment batches yet. Tick invoices on the{" "}
|
||
|
|
<Link href="/unpaid" className="text-brand hover:underline">Unpaid tab</Link>{" "}
|
||
|
|
and click <strong>Create Payment</strong>.
|
||
|
|
</div>
|
||
|
|
)}
|
||
|
|
|
||
|
|
{!errorMsg && batches.length > 0 && (
|
||
|
|
<RemittanceClient batches={batchWithInvoices} />
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|