Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 36 ยท Projects and Practice

Project: Three-Way Match

Build a runnable three-way match checker like SAP invoice verification: compare purchase order, goods receipt and invoice, apply tolerances and block exceptions.

In this project you build a small program that does what SAP's invoice verification (MIRO) does at its core: compare a vendor invoice with the purchase order and the goods receipt, then decide whether to post, block or reject it. The match is called a three-way match: the invoice must agree with what was ordered and with what was actually received. Background for the business process is in Procure-to-Pay in SAP MM.

Everything here is a stand-in in JavaScript. SAP's real logic (tolerance keys, OMR6, GR-based invoice verification, delivery cost handling) is far richer, and the real system cannot run on this site. The aim is to understand the rules by building them. All names and numbers are invented.

The rules we implement

  1. Every invoice item must reference a PO item.
  2. The invoice quantity must not exceed the received but not yet invoiced quantity (goods receipt quantity minus earlier invoice quantity), within a quantity tolerance.
  3. The invoice price must equal the PO price within a price tolerance.
  4. If a rule is violated beyond tolerance the item is blocked for payment; if within tolerance it posts with a variance.
  5. Items without a PO reference are rejected.

In SAP, tolerance limits are configured per tolerance key (for example PP price variance, DQ quantity variance) and company code, with absolute and percentage limits.

flowchart TD
  I["Invoice item"] --> P{"PO item found?"}
  P -- no --> R["Reject"]
  P -- yes --> Q{"Quantity within tolerance?"}
  Q -- no --> B["Block"]
  Q -- yes --> V{"Price within tolerance?"}
  V -- no --> B
  V -- yes --> OK["Post invoice"]

The decision tree for one invoice item.

Step 1: data

JS Step 1: the documents

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Step 2: the matching function

Now the heart of the project. It looks up the PO item, sums receipts, subtracts quantities already invoiced and applies the tolerances. Run the whole thing, then change the tolerances and data to see how decisions change.

JS Step 2: the full three-way match

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Reading the result: item 10 posts because 100 received equals 100 invoiced and the price matches. Item 20 is blocked, because only 150 pieces were received but 200 are invoiced, and the price is 5 percent above the PO. The third line has no PO and is rejected.

Step 3: extend it

Try these extensions yourself, in order of difficulty:

  1. Blocking reasons list: collect all violations of an item instead of returning at the first one (SAP shows every blocking reason on the invoice: R for quantity variance, P for price variance).
  2. Absolute limits: add qtyAbsolute and priceAbsolute and block when the percentage OR the absolute amount is exceeded.
  3. Partial invoicing: update alreadyInvoiced after each posted item and invoice the rest of an order in a second run.
  4. Unplanned delivery costs: allow an extra invoice line that is not matched to a PO item but is split over items proportionally.
  5. Accounting entries: for every posted item, create the journal lines you saw earlier: debit GR/IR clearing at the PO price, debit or credit the price difference account, credit the vendor.
JS Step 3: the accounting lines of a posted item

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What this project teaches

  • Why SAP blocks invoices: the system protects the company from paying for goods it did not order or receive.
  • Why master data and receipts must be accurate: a missing goods receipt blocks an otherwise correct invoice.
  • How tolerances balance control and workload: too tight and the AP team drowns in blocked invoices, too loose and errors slip through.

For the next project you will build the engine that creates prices rather than checking them: Project: A Pricing Engine.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three documents in a three-way match?

    Purchase order, goods receipt and invoice must agree.
  2. 2. An invoice quantity exceeds the received quantity by more than the tolerance. What should happen?

    Quantity variance beyond tolerance blocks the invoice for payment.
  3. 3. In the project code, which line has no PO reference?

    Items without a PO reference cannot be matched and are rejected.
  4. 4. Why are tolerances configured at all?

    Small differences can be accepted automatically, so only real exceptions need a person.

Summary

  • A three-way match compares invoice, PO and goods receipt.
  • Decisions: post, post with variance, block or reject.
  • Tolerances (quantity and price) separate noise from real exceptions.
  • Posted items create GR/IR, price difference and vendor lines.
  • You can extend the model with absolute limits, partial invoicing and delivery costs.

Next: Project: A Pricing Engine