Implement purchase order to receipt to invoice three-way matching with variance analysis and exception handling
harbor run -t snowflake-labs/dbt-three-way-matchingharbor download snowflake-labs/dbt-three-way-matchingBuild a dbt model that implements three-way matching for purchase orders: matching PO lines to receipts to supplier invoices. The system must identify discrepancies in quantities, prices, and timing, apply tolerance rules, flag exceptions for review, and calculate a composite risk score for prioritizing invoice reviews.
Your procurement team manually reviews hundreds of invoices each week. They need an automated system that compares what was ordered (PO), what was received (receipt), and what was billed (invoice) to catch errors and prevent overpayment. Additionally, they want a risk-based prioritization system to focus on the highest-risk invoices first.
The staging models are pre-built and ready to use:
Purchase Orders (models/staging/procurement/):
stg_procurement__purchase_orders - PO header with supplier, dates, status
po_id, po_number, supplier_id, warehouse_id, status, total_amount, currency_code, expected_date, ordered_at, created_by, created_at, updated_atstg_procurement__purchase_order_lines - line items on each PO
po_line_id, po_id, line_number, variant_id, sku, quantity_ordered, quantity_received, unit_price, line_total, created_atstg_procurement__purchase_order_receipts - receipt headers
receipt_id, receipt_number, po_id, received_at, received_by, status, created_atstg_procurement__purchase_order_receipt_lines - individual receipt line items
receipt_line_id, receipt_id, po_line_id, quantity_received, quantity_accepted, quantity_rejected, reject_reason, created_atSupplier Invoices (models/staging/procurement/):
stg_procurement__supplier_invoices - invoice headers from suppliers
invoice_id, invoice_number, supplier_id, po_id, invoice_date, due_date, total_amount, currency_code, status, created_at, updated_atstg_procurement__supplier_invoice_lines - invoice line items
invoice_line_id, invoice_id, po_line_id, description, quantity, unit_price, line_total, created_atSuppliers (models/staging/procurement/):
stg_procurement__suppliers - supplier master data
supplier_id, supplier_code, supplier_name, supplier_type, payment_terms, currency_code, lead_time_days, min_order_value, rating, status, created_at, updated_atNote: The staging models read from source tables in the PROCUREMENT schema. Your solution should reference the staging models using {{ ref('stg_procurement__table_name') }} syntax, not the raw schema tables.
This task supports both DuckDB and Snowflake. Run echo $DB_TYPE in a shell BEFORE you write any code or spawn any subagents — the live env determines which backend the verifier runs against. Do NOT assume a default from this prose. Both /app/dbt_models_duckdb/ and /app/dbt_models_snowflake/ exist on disk; the verifier only checks the project matching the live $DB_TYPE.
DB_TYPE=duckdb$DUCKDB_PATH (default: /app/database/retail.duckdb)DB_TYPE=snowflakeSNOWFLAKE_ACCOUNTSNOWFLAKE_USERSNOWFLAKE_PASSWORDSNOWFLAKE_DATABASE - The clone database to useSNOWFLAKE_SCHEMASNOWFLAKE_WAREHOUSESNOWFLAKE_ROLE (optional)Note: For Snowflake, the entrypoint automatically creates a clone database and sets SNOWFLAKE_DATABASE. The clone is destroyed when the task completes.
You must configure dbt to connect to the database:
profiles.yml in the dbt project directory with profile name retail_dw_masterDBT_PROFILES_DIR environment variable to the directory containing profiles.ymlConfigure with type: duckdb and the database path from $DUCKDB_PATH.
Configure with type: snowflake using password authentication:
schema: to $SNOWFLAKE_SCHEMA — do NOT leave it blank. A blank or omitted schema makes Snowflake silently default to PUBLIC, so your models get built in the wrong schema and the verifier cannot find them./app/dbt_models_duckdb/app/dbt_models_snowflakeBuild in the marts layer under models/marts/. Do not modify the staging layer.
Create a single model called three_way_match.sql in models/marts/ with these columns:
| Column | Type | Description |
|---|---|---|
match_id |
VARCHAR | Unique identifier for each match record (generate using po_line_id) |
po_id |
VARCHAR | Purchase order ID |
po_number |
VARCHAR | PO number for reference |
po_line_id |
VARCHAR | PO line being matched |
supplier_id |
VARCHAR | Supplier ID |
supplier_name |
VARCHAR | Supplier name |
supplier_rating |
DECIMAL | Current supplier rating (1-5 scale) |
variant_id |
VARCHAR | Product variant being purchased |
sku |
VARCHAR | SKU code |
po_quantity |
DECIMAL | Quantity ordered on PO |
po_unit_price |
DECIMAL | Unit price on PO |
po_line_total |
DECIMAL | Total amount on PO line |
receipt_quantity |
DECIMAL | Total quantity received across all receipts |
accepted_quantity |
DECIMAL | Quantity accepted (not rejected) |
rejected_quantity |
DECIMAL | Quantity rejected with issues |
rejection_rate |
DECIMAL | Percentage of received quantity that was rejected |
invoice_quantity |
DECIMAL | Quantity billed on invoice |
invoice_unit_price |
DECIMAL | Unit price on invoice |
invoice_line_total |
DECIMAL | Total amount invoiced |
quantity_variance |
DECIMAL | Difference: invoice_quantity - accepted_quantity |
quantity_variance_pct |
DECIMAL | Percentage variance: (invoice_qty - accepted_qty) / accepted_qty * 100 |
price_variance |
DECIMAL | Difference: invoice_unit_price - po_unit_price |
price_variance_pct |
DECIMAL | Percentage variance: (inv_price - po_price) / po_price * 100 |
total_variance |
DECIMAL | Difference: invoice_line_total - (accepted_quantity * po_unit_price) |
expected_amount |
DECIMAL | Expected invoice amount: accepted_quantity * po_unit_price |
quantity_match_status |
VARCHAR | Result of quantity matching logic |
price_match_status |
VARCHAR | Result of price matching logic |
overall_match_status |
VARCHAR | Final match determination |
exception_reasons |
VARCHAR | Concatenated list of all exception reasons |
exception_count |
INTEGER | Number of distinct exceptions flagged |
risk_score |
DECIMAL | Composite risk score (0-100) for prioritization |
risk_category |
VARCHAR | Risk category: LOW, MEDIUM, HIGH, CRITICAL |
recommended_action |
VARCHAR | What should be done with this match |
days_to_invoice |
INTEGER | Days between PO date and invoice date |
days_receipt_to_invoice |
INTEGER | Days between first receipt and invoice |
receipt_count |
INTEGER | Number of separate receipts for this PO line |
invoice_count |
INTEGER | Number of separate invoices for this PO line |
Compare the quantity invoiced to the quantity accepted (not just received). Apply rules strictly in this precedence order - once a rule matches, stop checking further rules:
Important: A record may satisfy multiple conditions (e.g., EXACT_MATCH and PARTIAL_RECEIPT). Always apply the first matching rule in the precedence order above. For example, if invoice_quantity = accepted_quantity = 71 AND receipt_quantity < po_quantity, flag as EXACT_MATCH (not PARTIAL_RECEIPT).
Compare the invoice unit price to the PO unit price. Apply rules strictly in this precedence order:
Combine quantity and price status into final determination:
For BLOCKED status, apply when ANY of these critical conditions exist:
Important precedence rule: A line meeting any BLOCKED criterion is BLOCKED regardless of receipt status; BLOCKED takes precedence over PENDING_RECEIPT.
Calculate a composite risk score from 0-100 based on multiple weighted factors:
| Factor | Max Points | Description |
|---|---|---|
| Quantity Variance | 25 | Higher variance = more points. Scale from no variance (0) to severe (max) |
| Price Variance | 25 | Higher variance = more points. Scale from no variance (0) to severe (max) |
| Dollar Amount | 15 | Larger absolute dollar variance = more points |
| Supplier Rating | 15 | Lower supplier rating = more points. NULL rating = max points |
| Invoice Timing | 10 | More days between receipt and invoice = more points |
| Rejection Rate | 10 | Higher rejection rate = more points |
Risk Categories (based on total score):
Build a pipe-delimited list of all issues found. Exception codes should appear in the order they are evaluated (not necessarily alphabetical). Include reason codes:
QTY_OVER - quantity over-billed (when quantity_match_status = 'OVER_BILLED')QTY_UNDER - quantity under-billed (when quantity_match_status = 'UNDER_BILLED')QTY_PARTIAL - partial receipt (when quantity_match_status = 'PARTIAL_RECEIPT')QTY_NONE - no receipt (when quantity_match_status = 'NO_RECEIPT')PRC_HIGH - price higher than PO (when price_match_status = 'PRICE_INCREASE')PRC_LOW - price lower than PO (when price_match_status = 'PRICE_DECREASE')PRC_MISSING - no PO price to compare (when price_match_status = 'NO_PO_PRICE')AMT_LARGE - large dollar variance (significant total_variance amount)TIMING_LATE - invoice received long after receiptHIGH_REJECT - high rejection rateMULTI_INVOICE - multiple invoices for same PO line (when invoice_count > 1)LOW_SUPPLIER_RATING - supplier has poor rating or no rating on fileIf no exceptions exist, set to 'NONE'.
Exception Logic - Apply these codes based on the corresponding conditions:
QTY_OVER: Apply when quantity_match_status = 'OVER_BILLED'QTY_UNDER: Apply when quantity_match_status = 'UNDER_BILLED'QTY_PARTIAL: Apply when quantity_match_status = 'PARTIAL_RECEIPT'QTY_NONE: Apply when quantity_match_status = 'NO_RECEIPT'PRC_HIGH: Apply when price_match_status = 'PRICE_INCREASE'PRC_LOW: Apply when price_match_status = 'PRICE_DECREASE'PRC_MISSING: Apply when price_match_status = 'NO_PO_PRICE'AMT_LARGE: Apply when total_variance exceeds the large amount thresholdTIMING_LATE: Apply when days_receipt_to_invoice exceeds the late thresholdHIGH_REJECT: Apply when rejection_rate exceeds the high rejection thresholdMULTI_INVOICE: Apply when invoice_count > 1LOW_SUPPLIER_RATING: Apply when supplier_rating is below acceptable level OR is NULLBased on overall status, risk score, and exceptions, recommend one of:
PAY_IMMEDIATELY - approved for payment, no issuesPAY_WITH_ADJUSTMENT - pay but apply credit/debit for variancesHOLD_FOR_REVIEW - requires manual review before paymentREQUEST_CREDIT_MEMO - request credit from supplier for overbillingCONTACT_SUPPLIER - pricing or quantity issues need supplier clarificationWAIT_FOR_RECEIPT - cannot process until goods receivedESCALATE_TO_MANAGEMENT - critical risk items requiring management approval (when risk_category = 'CRITICAL')Action Decision Logic (apply first matching rule):
When performing matches:
days_to_invoice = invoice_date - po ordered_at date (extract date from timestamp)days_receipt_to_invoice = invoice_date - earliest receipt date for this PO line (not entire PO)rejection_rate = (rejected_quantity / receipt_quantity) * 100 (return 0 if no receipts)expected_amount = accepted_quantity * po_unit_priceexception_count = number of pipe-delimited exception codes (0 if 'NONE')receipt_count = count of distinct receipt_id values for the PO lineinvoice_count = count of distinct invoice_id values for the PO lineRun these commands to test:
cd /app/dbt_models_duckdb # or /app/dbt_models_snowflake for Snowflake
dbt run --select three_way_match
dbt test --select three_way_match
The system uses the following tolerance thresholds (implement these values):
These thresholds represent industry-standard tolerances for three-way matching systems.
Use these breakpoints for the risk score component calculations:
| Factor | 0 pts | Low pts | Med pts | Max pts |
|---|---|---|---|---|
| Quantity Variance | ≤2% | 2-5% (5pts) | 5-10% (15pts) | >10% (25pts) |
| Price Variance | ≤1% | 1-3% (5pts) | 3-5% (15pts) | >5% (25pts) |
| Dollar Amount | <$100 | $100-500 (5pts) | $500-1000 (10pts) | >$1000 (15pts) |
| Supplier Rating | ≥4.0 | 3.0-3.99 (5pts) | 2.0-2.99 (10pts) | <2.0/NULL (15pts) |
| Invoice Timing | ≤14 days | 15-30 days (3pts) | 31-60 days (7pts) | >60 days (10pts) |
| Rejection Rate | 0% | 1-10% (3pts) | 11-25% (7pts) | >25% (10pts) |
Risk Categories: