ArthaFlow - AI-Powered Procure-to-Pay Management
Author : CA. Siddharth Shah
ArthaFlow is an AI-powered Procure-to-Pay (P2P) management platform that automates the entire procurement lifecycle — from raising a Purchase Inquiry to recording vendor payments — while enforcing India-specific statutory and compliance requirements end-to-end.
The platform combines a modern Next.js 14 application with Google Gemini 2.5 Flash (vision) for invoice OCR, a multi-provider fallback chain, a background job worker for asynchronous extraction, and four parallel intelligence passes that triage every invoice: confidence triage, math reconciliation, PO auto-matching, and a vendor-historical anomaly detector. A new line-level 3-way match module compares each invoice line directly against the linked Purchase Order and Goods Receipt Note, flagging rate-over-PO, quantity-over-PO, and quantity-over-GRN violations as critical anomalies.
Mid-market Indian enterprises spend a disproportionate share of their finance bandwidth on procurement paperwork. A typical 3-way match requires a clerk to physically open the Purchase Order, the Goods Receipt Note, and the supplier invoice, then compare line items, rates, quantities, and totals manually. The process is slow, error-prone, and creates friction with MSME suppliers who depend on the statutory 45-day payment cycle.
| Primary user | Mid-market manufacturing & infrastructure firms (₹50 Cr – ₹500 Cr revenue) with 50–500 active vendors and multiple project sites. |
| Direct beneficiaries | Procurement managers, site engineers, finance / AP team, vendors (via portal), internal & statutory auditors. |
| Indirect beneficiaries | MSME suppliers who get paid on time; statutory auditors who get a complete audit trail. |
ArthaFlow models the entire P2P process as a typed state machine. Each artefact — Purchase Inquiry, RFQ, Bid, Purchase Order, GRN, Invoice, Payment — has well-defined statuses and transitions enforced at the database, server-action, and UI layer. Every state change is written to an immutable AuditLog with the acting user, timestamp, before-and-after snapshot, and any override reason.
Invoice intake is the AI-heavy module. When a PDF lands (drag-drop, bulk upload, or vendor-portal upload), the system computes a SHA-256 hash, checks the InvoiceExtractionCache, and either returns the cached result instantly or hands the file to a background worker which calls Google Gemini 2.5 Flash with a strict Zod-validated schema. The worker then runs four parallel intelligence passes whose outputs are merged into a single anomaly stream surfaced to the reviewer.
To make the platform tangible, the demo seed wires a single end-to-end purchase: a leak-repair Purchase Inquiry at the Mumbai Central pipeline becomes an RFQ, three vendors bid, Acme Steel Industries wins on price, a PO is issued, materials are received, an invoice is uploaded, and a payment is recorded. The same Acme thread is also visible on the vendor portal so judges can compare the buyer-side and vendor-side views in parallel.
| Layer | Technology | Version / Notes |
| Framework | Next.js (App Router) | 14.2.x — Server Components first |
| UI library | React | 18.3 — Concurrent rendering |
| Language | TypeScript | 5.x — Strict mode |
| Styling | Tailwind CSS | 3.4 + tailwind-merge + tailwindcss-animate |
| Component primitives | Radix UI | Dialog, Dropdown, Select, Tabs, Toast, Popover, Tooltip, Switch, Checkbox |
| Icons | Lucide React | 0.460 — Tree-shakeable SVG icon set |
| Forms | React Hook Form + Zod | Shared validation client + server |
| Data fetching (client) | TanStack Query (React Query) | v5 — used for live polling on bulk extraction |
| Charts | Recharts | 2.x — Dashboard analytics |
| Layer | Technology | Version / Notes |
| Server runtime | Next.js Server Actions + API Routes | 14.2 — runs in same Node process as the UI |
| Authentication | NextAuth | 4.24 — Credentials provider with Prisma adapter |
| Password hashing | bcryptjs | 2.4 — Salted hash, slow by design |
| Nodemailer | 7.0 — Transactional vendor invites & notifications | |
| Rate limiting | Custom Postgres token bucket | RateLimitBucket table — survives restarts, no Redis needed |
| Audit logging | AuditLog table | Immutable, append-only, before/after snapshots in JSONB |
| Layer | Technology | Version / Notes |
| Database | PostgreSQL | 16 — JSONB-heavy schema for extraction results |
| ORM | Prisma | 5.22 — Type-safe queries, transactions, migrations |
| Schema models | 30 models, 13 enums | User, Vendor, Site, InventoryItem, PurchaseInquiry, Rfq, Bid, PurchaseOrder, Grn, Invoice, Payment, Contract, Budget, RecurringPI, ExtractionJob, InvoiceExtractionCache, ExtractionMetric, ExtractionFeedback, ApprovalRule, AuditLog, etc. |
| Seeding | tsx prisma/seed.ts | Deterministic seed with 10 demo invoices + coherent Acme thread |
| Layer | Technology | Version / Notes |
| Primary model | Google Gemini 2.5 Flash (Vision) | via REST — generativelanguage.googleapis.com/v1beta |
| Fallback chain | Azure Document AI → Anthropic Claude → Stub | Graceful degradation; the router picks the first healthy provider |
| Result caching | InvoiceExtractionCache table | SHA-256 file hash + provider key; re-uploads are free |
| Async pipeline | Background worker process | Polls ExtractionJob rows; FIFO claim with atomic updateMany; horizontally scalable |
| Validation | Zod schema | Strict line-item schema with HSN/SAC, CGST/SGST/IGST/UTGST splits |
| Confidence scoring | Per-field + aggregate | Triaged into HIGH / MEDIUM / LOW tiers for routing |
| Math reconciliation | Pure-TS reconciler | Re-computes subtotal + tax = grandTotal; flags mismatch |
| PO auto-matching | Jaccard token similarity | Scores top-N candidate POs from the last 90 days |
| Anomaly detection | Vendor-history comparator | PRICE_SPIKE / QUANTITY_SPIKE / FIRST_INVOICE / GSTIN_FIRST_SEEN |
| Line-level 3-way match | po-line-match module | RATE_OVER_PO / QTY_OVER_PO / QTY_OVER_GRN / ITEM_NOT_IN_PO |
| Layer | Technology | Version / Notes |
| PDF generation | jsPDF + jsPDF-AutoTable | 4.2 / 5.0 — PO, GRN, Invoice, Demo-invoice PDFs |
| Word generation | docx | 9.6 — RFQ documents, this submission doc |
| Charts in reports | Recharts SVG → PDF | Server-rendered for executive reports |
| Layer | Technology | Version / Notes |
| Unit & integration tests | Vitest + Testing Library + jsdom | Reconciler, confidence, matcher, anomaly suites |
| Runtime scripts | tsx | 4.19 — Direct TS execution for seed, demo, backfill scripts |
| Linting | ESLint | next/core-web-vitals config |
| Production process manager | PM2 | Two processes: web + ocr-worker on the VPS |
| Version control | Git + GitHub | Conventional commits |
The intake pipeline is the most complex flow in the system. The numbered steps below describe what happens when a single PDF is uploaded: