import { getPb } from "../lib/pb"; import OrderClient from "./OrderClient"; export const dynamic = "force-dynamic"; type StockWithHighestPrice = { id: string; code: string; description: string; highestUnitPrice: number | null; refInvoiceNo: string | null; // invoice_no where the highest price was paid refInvoiceDate: string | null; // invoice_date of that invoice }; export default async function OrderPage() { const pb = getPb(); let stockCodes: StockWithHighestPrice[] = []; let errorMsg: string | null = null; try { // 1) All stock codes (just the fields we need) const scRes = await pb.collection("JEI_stock_codes").getList(1, 5000, { sort: "code", fields: "id,code,description_goods", }); // 2) All invoice lines + their parent invoice (so we can grab the // invoice_no + invoice_date of whichever line carries the MAX // unit_price for each stock code). // PB only returns the expanded record when the field selector // includes `expand.invoice.`; `invoice` alone just returns // the relation id and drops the expansion. const linesRes = await pb.collection("JEI_invoice_lines").getList(1, 5000, { expand: "invoice", fields: "id,stock_code,unit_price,expand.invoice.invoice_no,expand.invoice.invoice_date", }); // 3) For each stock_code, keep the line with the highest unit_price // and remember which invoice (no + date) it came from. Ties broken // by most-recent invoice_date so the price source reflects the // current market rather than an old historical peak. type Highest = { price: number; date: string; invNo: string }; const highestByStock = new Map(); for (const line of linesRes.items) { const scId = line.stock_code as string; if (!scId) continue; const inv = line.expand?.invoice as any; const invNo: string = inv?.invoice_no || ""; const invDate: string = inv?.invoice_date || ""; const price = Number(line.unit_price) || 0; const cur = highestByStock.get(scId); const isHigher = !cur || price > cur.price; const isTieNewer = cur && price === cur.price && invDate > cur.date; if (isHigher || isTieNewer) { highestByStock.set(scId, { price, date: invDate, invNo }); } } stockCodes = scRes.items.map((s: any) => { const h = highestByStock.get(s.id); return { id: s.id, code: s.code, description: s.description_goods || "", highestUnitPrice: h?.price ?? null, refInvoiceNo: h?.invNo || null, refInvoiceDate: h?.date ? h.date.slice(0, 10) : null, }; }); } catch (e: any) { errorMsg = String(e?.message || e); } return (
{errorMsg ? (
Cannot reach PocketBase
{errorMsg}
) : ( )}
); }