fix(procurement): batch quote lookup and enforce money bounds
This commit is contained in:
parent
a9dc62167f
commit
35d352434d
|
|
@ -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 |
|
| 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.
|
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.
|
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.
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ function scaled(value: string, scale: number): bigint {
|
||||||
}
|
}
|
||||||
export function lineTotal(quantity: string, unitPrice: string): string {
|
export function lineTotal(quantity: string, unitPrice: string): string {
|
||||||
const total = (scaled(quantity, 3) * scaled(unitPrice, 2) + 500n) / 1000n;
|
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')}`;
|
return `${total / 100n}.${(total % 100n).toString().padStart(2, '0')}`;
|
||||||
}
|
}
|
||||||
export function totalOf(lines: readonly { lineTotal: string }[]): string {
|
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),
|
(sum, line) => sum + scaled(line.lineTotal, 2),
|
||||||
0n,
|
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')}`;
|
return `${total / 100n}.${(total % 100n).toString().padStart(2, '0')}`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,31 +54,33 @@ export class PurchaseOrderStore {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!supplier) throw new AppError('SUPPLIER_NOT_FOUND');
|
if (!supplier) throw new AppError('SUPPLIER_NOT_FOUND');
|
||||||
const sources = await Promise.all(
|
const quotes = await tx.supplierMaterial.findMany({
|
||||||
input.lines.map(async (line) => {
|
where: {
|
||||||
const source = await tx.supplierMaterial.findFirst({
|
supplierId: input.supplierId,
|
||||||
where: {
|
materialId: { in: input.lines.map(({ materialId }) => materialId) },
|
||||||
supplierId: input.supplierId,
|
organizationId: actor.organizationId,
|
||||||
materialId: line.materialId,
|
active: true,
|
||||||
organizationId: actor.organizationId,
|
},
|
||||||
active: true,
|
include: { material: true },
|
||||||
},
|
});
|
||||||
include: { material: true },
|
const quoteByMaterial = new Map(
|
||||||
});
|
quotes.map((quote) => [quote.materialId, quote]),
|
||||||
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 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;
|
const currency = sources[0]?.source.currency;
|
||||||
if (
|
if (
|
||||||
!currency ||
|
!currency ||
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue