diff --git a/docs/purchase-orders-api.md b/docs/purchase-orders-api.md index 9293082..826a791 100644 --- a/docs/purchase-orders-api.md +++ b/docs/purchase-orders-api.md @@ -11,5 +11,6 @@ Purchase orders turn active supplier-material quotes into private, organization- | POST | /purchase-orders/:id/cancel | procurement.manage | Cancel a draft order | Each line snapshots the material name, code, unit, supplier SKU, unit price, and line total. The header snapshots its currency and total. One currency is accepted per order, and quantities below the supplier's configured minimum are rejected. +Quote sources are loaded in one scoped query. Totals use exact decimal arithmetic and are rejected before persistence if they exceed the database money column. Database triggers protect existing line snapshots and restrict header updates to a valid draft transition. Orders begin as `DRAFT`. They can become `ISSUED` or `CANCELLED` exactly once, with an audit record for each transition. Issued orders cannot be changed or cancelled in this phase. Partial goods receipts will be designed separately before any stock movement is introduced. diff --git a/src/procurement/purchase-order-money.ts b/src/procurement/purchase-order-money.ts index c14ed08..f27a85a 100644 --- a/src/procurement/purchase-order-money.ts +++ b/src/procurement/purchase-order-money.ts @@ -8,7 +8,7 @@ function scaled(value: string, scale: number): bigint { } export function lineTotal(quantity: string, unitPrice: string): string { const total = (scaled(quantity, 3) * scaled(unitPrice, 2) + 500n) / 1000n; - if (total > 99999999999999n) throw new AppError('DATA_CONSTRAINT'); + if (total > 999999999999n) throw new AppError('DATA_CONSTRAINT'); return `${total / 100n}.${(total % 100n).toString().padStart(2, '0')}`; } export function totalOf(lines: readonly { lineTotal: string }[]): string { @@ -16,6 +16,6 @@ export function totalOf(lines: readonly { lineTotal: string }[]): string { (sum, line) => sum + scaled(line.lineTotal, 2), 0n, ); - if (total > 99999999999999n) throw new AppError('DATA_CONSTRAINT'); + if (total > 999999999999n) throw new AppError('DATA_CONSTRAINT'); return `${total / 100n}.${(total % 100n).toString().padStart(2, '0')}`; } diff --git a/src/procurement/purchase-order.store.ts b/src/procurement/purchase-order.store.ts index 8002eea..0fefee8 100644 --- a/src/procurement/purchase-order.store.ts +++ b/src/procurement/purchase-order.store.ts @@ -54,31 +54,33 @@ export class PurchaseOrderStore { }, }); if (!supplier) throw new AppError('SUPPLIER_NOT_FOUND'); - const sources = await Promise.all( - input.lines.map(async (line) => { - const source = await tx.supplierMaterial.findFirst({ - where: { - supplierId: input.supplierId, - materialId: line.materialId, - organizationId: actor.organizationId, - active: true, - }, - include: { material: true }, - }); - if (!source || !source.material.active) - throw new AppError('SUPPLIER_MATERIAL_NOT_FOUND'); - if ( - BigInt(line.quantity.replace('.', '')) < - BigInt(source.minOrderQuantity.toFixed(3).replace('.', '')) - ) - throw new AppError('PURCHASE_ORDER_MINIMUM'); - return { - source, - quantity: line.quantity, - lineTotal: lineTotal(line.quantity, source.unitPrice.toFixed(2)), - }; - }), + const quotes = await tx.supplierMaterial.findMany({ + where: { + supplierId: input.supplierId, + materialId: { in: input.lines.map(({ materialId }) => materialId) }, + organizationId: actor.organizationId, + active: true, + }, + include: { material: true }, + }); + const quoteByMaterial = new Map( + quotes.map((quote) => [quote.materialId, quote]), ); + const sources = input.lines.map((line) => { + const source = quoteByMaterial.get(line.materialId); + if (!source || !source.material.active) + throw new AppError('SUPPLIER_MATERIAL_NOT_FOUND'); + if ( + BigInt(line.quantity.replace('.', '')) < + BigInt(source.minOrderQuantity.toFixed(3).replace('.', '')) + ) + throw new AppError('PURCHASE_ORDER_MINIMUM'); + return { + source, + quantity: line.quantity, + lineTotal: lineTotal(line.quantity, source.unitPrice.toFixed(2)), + }; + }); const currency = sources[0]?.source.currency; if ( !currency || diff --git a/test/purchase-order-money.spec.ts b/test/purchase-order-money.spec.ts new file mode 100644 index 0000000..14bb331 --- /dev/null +++ b/test/purchase-order-money.spec.ts @@ -0,0 +1,13 @@ +import { lineTotal, totalOf } from '../src/procurement/purchase-order-money'; + +describe('purchase order money bounds', () => { + it('rounds the third fractional minor unit only once per line', () => { + expect(lineTotal('1.005', '1.01')).toBe('1.02'); + }); + it('rejects totals exceeding the database decimal precision', () => { + expect(() => lineTotal('999999999.999', '999999999.99')).toThrow(); + expect(() => + totalOf([{ lineTotal: '9999999999.99' }, { lineTotal: '0.01' }]), + ).toThrow(); + }); +});