Quotes and purchase orders were silently losing their line items. Quote items field was a multi-relation to stock_items, but PocketBase stored only the last id as a plain string no matter how it was sent (JSON string via FormData, raw array, or repeated FormData entries). Every quote created through the UI had `items: ""` or `items: "<id>"`. Recreated the field as JSON so an array of stock_item ids round-trips (companion fields quantities/unitPrices already line up with it). PO snapshot_items had the same class of bug from the other direction: String(obj) coerced the snapshot array to "[object Object]", which PocketBase stored verbatim, rendering the PO as N rows of "Unknown / £0.00". The form now sends JSON-stringified arrays, and the reader only trusts a snapshot whose rows actually carry a name. Also fixes: - Quote item lines are hydrated from stock_items; the parallel quantities/unitPrices arrays are the source of truth, so quotes written before the schema fix still render and total correctly. - Quote form rejects stock rows where no item was actually picked from the dropdown, instead of saving an empty stockItemId. - PO PDF filename is now the plain order number (PO-20261015-001.pdf). Adds the quotes.items schema migration plus three dev scripts for creating a quote, re-applying the field fix and rendering a test PDF.
83 lines
3.3 KiB
JavaScript
83 lines
3.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Creates the user's quote exactly as shown in the screenshot.
|
|
*
|
|
* Customer : JENTRADEPRO
|
|
* Quote # : QT-20260914-001
|
|
* Customer Ref: RFQ;JEI MAGNETIC DRILLING MACHINE PARTS
|
|
* Quote Date : 2026-09-14
|
|
* Valid Until : 2026-10-14
|
|
* Status : Draft
|
|
*
|
|
* Items:
|
|
* 1. PRO132T-B27 - PANEL CONTROL PCB ASSY 110V (BB1) qty 1 R 5518.88
|
|
* 2. PRO132T-C24 - CARBON BRUSH EACH, PRO152T qty 2 R 438.22
|
|
* 3. 35708 - MAGNET SWITCH qty 1 R 485.40
|
|
*/
|
|
import PocketBase from "pocketbase";
|
|
|
|
const PB_URL = "https://pb.jeistk.apphub.co.za";
|
|
const PB_EMAIL = "david@wfsales.co.za";
|
|
const PB_PASSWORD = "1234567890";
|
|
|
|
const pb = new PocketBase(PB_URL);
|
|
pb.autoCancellation(false);
|
|
await pb.admins.authWithPassword(PB_EMAIL, PB_PASSWORD);
|
|
|
|
const CUSTOMER_ID = "dgv6gxgcj2f2o14";
|
|
const QUOTE_NUMBER = "QT-20260914-001";
|
|
|
|
const items = [
|
|
{ id: "oz9zgj5xp72m77s", sku: "PRO132T-B27", qty: 1, price: 5518.88 },
|
|
{ id: "gfcfv9mkveq95co", sku: "PRO132T-C24", qty: 2, price: 438.22 },
|
|
{ id: "9lfcwus2w5w9h84", sku: "35708", qty: 1, price: 485.40 },
|
|
];
|
|
|
|
// Clean any prior attempt
|
|
try {
|
|
const dup = await pb.collection("quotes").getFirstListItem(`quote_number = "${QUOTE_NUMBER}"`);
|
|
await pb.collection("quotes").delete(dup.id);
|
|
console.log(`Removed previous attempt ${dup.id}`);
|
|
} catch {
|
|
/* no duplicate */
|
|
}
|
|
|
|
// Create the quote — `items` is a JSON array (schema fix applied earlier)
|
|
const record = await pb.collection("quotes").create({
|
|
quote_number: QUOTE_NUMBER,
|
|
customerName: CUSTOMER_ID,
|
|
customerRef: "RFQ;JEI MAGNETIC DRILLING MACHINE PARTS",
|
|
quoteDate: "2026-09-14 00:00:00.000Z",
|
|
validUntil: "2026-10-14 00:00:00.000Z",
|
|
notes: "",
|
|
status: "Draft",
|
|
items: items.map(i => i.id),
|
|
quantities: items.map(i => i.qty),
|
|
unitPrices: items.map(i => i.price),
|
|
});
|
|
|
|
// Verify
|
|
const stored = await pb.collection("quotes").getOne(record.id, { expand: "customerName" });
|
|
const customer = stored.expand?.customerName;
|
|
|
|
const subtotal = items.reduce((s, i) => s + i.qty * i.price, 0);
|
|
const vat = subtotal * 0.15;
|
|
const total = subtotal + vat;
|
|
|
|
console.log("\n✅ Quote created on PocketBase");
|
|
console.log("─────────────────────────────────────");
|
|
console.log(`ID : ${stored.id}`);
|
|
console.log(`Quote # : ${stored.quote_number}`);
|
|
console.log(`Customer : ${customer?.comapny_name ?? "(unknown)"}`);
|
|
console.log(`Customer Ref: ${stored.customerRef}`);
|
|
console.log(`Quote Date : ${stored.quoteDate}`);
|
|
console.log(`Valid Until : ${stored.validUntil}`);
|
|
console.log(`Status : ${stored.status}`);
|
|
console.log(`Items stored: ${JSON.stringify(stored.items)}`);
|
|
console.log(`Quantities : ${JSON.stringify(stored.quantities)}`);
|
|
console.log(`Unit Prices : ${JSON.stringify(stored.unitPrices)}`);
|
|
console.log(`Subtotal : R ${subtotal.toFixed(2)}`);
|
|
console.log(`VAT (15%) : R ${vat.toFixed(2)}`);
|
|
console.log(`Total : R ${total.toFixed(2)}`);
|
|
console.log("─────────────────────────────────────");
|
|
console.log(`Admin URL : https://pb.jeistk.apphub.co.za/_/#/collections?collectionId=pbc_134296766&recordId=${stored.id}`);
|