Scan receipts into transactions: models, AI and API
Problem
There is no way to turn a paper receipt, bill or invoice into a transaction. You type it in by hand, and you can't record what was bought or split it by category. Receipts photographed before the bank reports the payment also end up as duplicates once it does.
This is the first of three PRs, split for review as requested: models, AI and API here; the web UI and the mobile app follow in their own PRs, built on this one.
Change
Receipt scanning, behind preview features. It is available only when the AI route extract_receipt allows it and the preferred LLM provider can read images (Anthropic for now). Receipts are never sent to another configured provider.
- Upload.
ReceiptScan.create_from_upload!takes up to 5 photos, or one PDF on its own, of 10 MB each. Images are normalized with libvips (HEIC to JPEG, EXIF stripped, downscaled); contents and size are checked before decoding. - Processing. A placeholder "Processing receipt…" transaction is created on the chosen account right away.
ReceiptScan::ProcessJobthen:- reads the receipt into a
ReceiptExtraction(merchant, NIP, date, total, currency, payment method, VAT breakdown, line items); - validates the total, the date and the NIP checksum;
- allocates the line items to categories and splits the transaction when there is more than one group (a category the user chose is kept);
- attaches the receipt to the bank's transaction when exactly one matches: same amount and currency, within 3 days, not excluded, not a transfer. A refund is a negative total.
- reads the receipt into a
- Bank sync. A receipt entered before the bank booked the payment is claimed by the provider import within 3 days, also while pending, and keeps its date, receipt and split. Unread, cash, excluded and still-processing receipts are left alone, and a booking linked to its pending entry keeps that link. A booking can claim its pending receipt even after the receipt was split (a split parent is always excluded, which #4215 made unclaimable).
- Files. Receipt files are attached to the transaction; blob access follows the account's sharing.
- API.
POST /api/v1/receipts,GET /api/v1/receipts/:id; transactions carryreceiptandattachments;GET /api/v1/transactions/:id/attachments/:idserves attachments to API clients. Minitest coverage; rswag docs regenerated. - Evaluation. A
receipts:evaluatetask scores the extraction against labelled receipts.
Related: #4425 proposes splitting a transaction from an attached receipt. This reads the receipt when it is uploaded instead, and could feed that flow.
Commits
- Receipt scans, AI reading and processing (models, job, migration).
- Bank imports claiming receipt transactions (
Account::ProviderImportAdapter). - The receipts API.
Migration
20261123100000_create_receipt_scans adds receipt_scans.
Validation
bin/rails test: 18745 runs, 0 failures. rubocop, erb_lint and brakeman are clean.rswag:specs:swaggerizeleavesdocs/api/openapi.yamlunchanged.
Summary by CodeRabbit
-
New Features
- Added receipt and invoice scanning that extracts merchant, payment, totals, and line-item details, then links eligible receipts to matching transactions.
- Added API support for uploading receipts, checking scan status, and viewing transaction attachments, including thumbnails.
- Transaction responses now include receipt details and attachment links.
-
Improvements
- Scans flag items needing review, such as missing or inconsistent receipt information.