Schedule icon
Webhook icon
Script icon
Process icon
Query icon
If icon
Log icon
SlackIncomingWebhook icon

Invoice Math Discrepancy Gate with In-Memory DuckDB Reconciliation

Reconcile vendor invoices with in-memory DuckDB math verification. Detect tax and subtotal variances to halt erroneous ERP syncs before payment.

Categories
BusinessCoreData

Automated invoice processing systems often ingest vendor PDF extracts or OCR scans where rounding errors, omitted discounts, or mismatched tax brackets introduce subtle mathematical discrepancies. When automated pipelines push these mismatched invoices directly to Enterprise Resource Planning (ERP) databases, accounting teams must spend days untangling balance sheets and issuing credit adjustments during monthly financial closing.

This blueprint provides an automated mathematical discrepancy gate. Ingested invoice line items are loaded into an embedded, in-memory DuckDB engine. A single-statement common table expression (CTE) verifies line item sums (quantity * unit_price), calculates expected sales tax based on jurisdiction rates, and verifies stated totals against calculated amounts down to the penny. Any variance exceeding $0.01 immediately halts downstream ERP synchronization and routes an audit report to Slack for accounts payable review.

How it works

  1. prepare_invoice_tables (scripts.python.Script): Ingests the raw JSON invoice payload, normalizes header details and line items, and writes clean CSV files to Kestra internal storage.
  2. audit_invoice_math (jdbc.duckdb.Query): Executes an embedded DuckDB query to compute line item subtotals, calculate tax amounts, and determine absolute variance between stated and calculated figures.
  3. discrepancy_gate (core.flow.If):
    • Clean State (Variance <= $0.01): Logs validation pass-through and sends an approval notification to #accounts-payable for ERP posting.
    • Discrepancy State (Variance > $0.01): Generates an invoice-discrepancy-report.md audit artifact and sends an urgent discrepancy escalation alert to #finance-discrepancies.
  4. alert_on_failure (errors block): Catches unexpected pipeline failures and alerts the finance operations team in Slack.
  5. Triggers: Daily scheduled reconciliation sweep (disabled: true by default) plus an authenticated Webhook trigger for accounts payable ingestion systems.

What you get

  • In-memory relational math verification without spinning up external database infrastructure.
  • Zero rounding leakage: halts payments on variances as small as $0.01.
  • Automated audit reporting with markdown artifact generation in Kestra execution storage.

Who it's for

  • Financial Operations (FinOps) and Accounts Payable teams.
  • Data and Analytics Engineers maintaining accounting ETL/ELT pipelines.
  • ERP Administrators (NetSuite, SAP, QuickBooks, Odoo).

Why orchestrate this with Kestra

Hardcoding invoice validation into OCR glue code or custom Python cron scripts lacks execution lineage, fails silently on format changes, and provides no built-in audit trail. Kestra provides declarative DAG orchestration, built-in secret management, native DuckDB execution, and automatic error handling with human-in-the-loop Slack escalations.

Prerequisites

  • Kestra instance with network access to Slack incoming webhooks.
  • Configured secret for Slack notifications.

Secrets

  • SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint for financial alerts.
  • WEBHOOK_KEY: Secret authentication key for the event webhook trigger.

Quick start

  1. Configure SLACK_WEBHOOK_URL and WEBHOOK_KEY in your Kestra namespace secrets.
  2. Click Execute in the Kestra UI to run the blueprint with the default sample invoice.
  3. Modify stated_total in the input to test the discrepancy branch and observe the Slack alert.

How to extend

  • Connect io.kestra.plugin.core.http.Request tasks inside the approved branch to post valid invoices directly to ERP REST APIs (NetSuite, SAP, QuickBooks).
  • Add an OCR extraction task (e.g. AWS Textract or Google Cloud Document AI) before prepare_invoice_tables to process scanned PDF invoices directly from cloud storage buckets.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.