mega-plant/lib/supplier-server.ts
David cfd54385b3 Add Suppliers list; book purchase orders and petty-cash slips against it
- /expenses/suppliers: terms (cash / 30 / 60 days), PO + petty cash totals per supplier,
  period filters, add/edit/delete, due dates
- POs and slips resolve to a supplier by code then folded name; new PO suppliers start
  with terms unset, new slip suppliers as cash
- Full-user guard on cash-float and job-card write routes

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-22 15:43:57 +02:00

37 lines
1.7 KiB
TypeScript

import "server-only";
import type PocketBase from "pocketbase";
import { supplierKey, type Terms } from "./supplier";
/**
* The supplier a purchase order or slip belongs to — found, or created.
*
* Match order: the supplier's CODE from the PO ("09YTS001") when there is one, then the folded
* NAME. A new supplier takes `terms` (a till slip is "cash"; a PO supplier nobody has classified
* yet is left "" so the list shows it as needing terms). An existing supplier's terms are never
* overwritten here — only a person changes those.
*/
export async function resolveSupplier(
pb: PocketBase,
input: { name: string; code?: string; terms?: Terms; source: string },
): Promise<string> {
const name = (input.name || "").trim();
const key = supplierKey(name);
if (!name || !key) return "";
const code = (input.code || "").trim().toUpperCase();
const all = await pb.collection("suppliers").getFullList({ fields: "id,key,code" });
const hit = (code && all.find((s) => (s.code || "").toUpperCase() === code)) || all.find((s) => s.key === key);
if (hit) {
// Learn a code we did not have, without disturbing anything a person set.
if (code && !(hit.code || "").trim()) await pb.collection("suppliers").update(hit.id, { code }).catch(() => {});
return hit.id;
}
try {
const created = await pb.collection("suppliers").create({ name, key, code, terms: input.terms ?? "", source: input.source });
return created.id;
} catch {
// Lost a race to the unique key: someone created it a moment ago.
const again = (await pb.collection("suppliers").getFullList({ fields: "id,key" })).find((s) => s.key === key);
return again?.id ?? "";
}
}