Your PO orders 100 units at $12.50 each. The dock receives 60 and sends 10 back. AP has already posted 40 units, so only 10 units are available to bill against receipts. The next invoice splits 15 units across two lines. Each line fits on its own, but together they exceed the net receipt by five units.
Compare the invoice against both quantity limits
Join to a specific PO schedule and check supplier, currency, and unit of measure. Sum the current invoice lines for that schedule before comparing them with the PO or receipts. The example subtracts returns from posted receipts and treats the posted invoice quantity as a net amount supplied by the ledger. The two checks give different follow-up tasks: the buyer reviews the order limit, while receiving reviews the receipt limit. Three-way matching adds the receipt check to the comparison against the PO. Oracle Payables matching guide
This example allows zero quantity variance and a positive unit-price variance of up to two cents. Compare unit prices individually so a cheaper line cannot conceal an expensive one. Route credits through the posted ledger before running this debit-invoice matcher.
Save the block as three-way-match.mts and run node three-way-match.mts on Node.js 24. The fixtures exercise a partial receipt, a receiving return, a later delivery, the price boundary, and an invoice that exceeds the PO. Node.js TypeScript documentation
import assert from "node:assert/strict";
type MatchScope = { schedule: string; supplier: string; currency: string; unit: string };
type PurchaseOrder = MatchScope & { ordered: bigint; unitCents: bigint };
type Receipt = { id: string; quantityChange: bigint };
type Invoice = MatchScope & { number: string; lines: { quantity: bigint; unitCents: bigint }[] };
function positivePart(value: bigint): bigint {
return value > 0n ? value : 0n;
}
function matchInvoice(
po: PurchaseOrder, receipts: Receipt[], postedBilled: bigint, invoice: Invoice, priceToleranceCents: bigint,
) {
assert(invoice.schedule === po.schedule && invoice.supplier === po.supplier
&& invoice.currency === po.currency && invoice.unit === po.unit, "Invoice does not match PO scope");
assert(po.ordered > 0n && po.unitCents >= 0n && postedBilled >= 0n && priceToleranceCents >= 0n);
assert.equal(new Set(receipts.map(({ id }) => id)).size, receipts.length, "Duplicate receipt event");
const received = receipts.reduce((total, { quantityChange }) => total + quantityChange, 0n);
assert(received >= 0n, "Net receipts cannot be negative");
assert(invoice.lines.length > 0 && invoice.lines.every((line) => line.quantity > 0n && line.unitCents >= 0n));
const invoiceQuantity = invoice.lines.reduce((sum, line) => sum + line.quantity, 0n);
const totalBilled = postedBilled + invoiceQuantity;
const overOrdered = positivePart(totalBilled - po.ordered);
const overReceived = positivePart(totalBilled - received);
const priceHolds = invoice.lines.flatMap(({ unitCents }, index) => {
const varianceCents = unitCents - po.unitCents;
return varianceCents > priceToleranceCents ? [{ line: index + 1, varianceCents }] : [];
});
return {
invoice: invoice.number, ordered: po.ordered, received, postedBilled, invoiceQuantity,
overOrdered, overReceived, priceHolds,
status: overOrdered > 0n || overReceived > 0n || priceHolds.length > 0 ? "hold" : "matched",
};
}
const scope = { schedule: "PO-112/03/01", supplier: "SUP-88", currency: "USD", unit: "EA" };
const po: PurchaseOrder = { ...scope, ordered: 100n, unitCents: 1_250n };
const receipts: Receipt[] = [
{ id: "GR-01", quantityChange: 60n }, { id: "RT-01", quantityChange: -10n },
];
const invoice: Invoice = { ...scope, number: "INV-73", lines: [
{ quantity: 8n, unitCents: 1_250n }, { quantity: 7n, unitCents: 1_252n },
] };
const tolerance = 2n;
const hold = matchInvoice(po, receipts, 40n, invoice, tolerance);
assert.deepEqual([hold.received, hold.overReceived, hold.overOrdered, hold.status], [50n, 5n, 0n, "hold"]);
assert.deepEqual(hold.priceHolds, []);
const updated = [...receipts, { id: "GR-02", quantityChange: 6n }];
const cleared = matchInvoice(po, updated, 40n, invoice, tolerance);
assert.equal(cleared.status, "matched");
const priceOnly = matchInvoice(po, updated, 40n, { ...invoice, lines: [
invoice.lines[0], { quantity: 7n, unitCents: 1_253n },
] }, tolerance);
assert.equal(priceOnly.status, "hold");
assert.deepEqual(priceOnly.priceHolds, [{ line: 2, varianceCents: 3n }]);
const overPO = matchInvoice(po, [{ id: "GR-03", quantityChange: 110n }], 90n, invoice, tolerance);
assert.deepEqual([overPO.overOrdered, overPO.overReceived, overPO.status], [5n, 0n, "hold"]);
assert.throws(() => matchInvoice(po, [...receipts, receipts[0]], 40n, invoice, tolerance), /Duplicate receipt event/);
assert.throws(() => matchInvoice(po, receipts, 40n, { ...invoice, unit: "CASE" }, tolerance), /PO scope/);
console.table([hold, cleared, priceOnly, overPO].map(({ priceHolds, ...row }) => ({
...row, priceHolds: priceHolds.map((item) => `line ${item.line}: +${item.varianceCents}ยข`).join(", "),
})));
When you post an invoice, the database transaction must lock the PO line with SELECT ... FOR UPDATE before reading and updating the available quantity, or reserve it with an atomic conditional update using WHERE matched_qty + $delta <= received_qty. This prevents concurrent invoices from consuming the same available receipt balance. If the conditional update affects no rows, put the invoice on hold. Key the posting by the source invoice ID so a retried import cannot post the same invoice twice. Keep the PO schedule and contributing receipt IDs on each exception row so AP can trace a PDF note back to the XLSX detail. ThreeWayMatchPack
Top comments (0)