[fiscus-capture] Receipts: the fields every Czech receipt shows, read from an OCR's words; payment.receipt_attached

fiscus_capture::receipt::read takes the OCR's lines (words with boxes and confidence in thousandths) and finds
the total, date and time, IČO (check digit), DIČ and the shop's name by their labels, never a shop's layout;
unsure words mark their field, the shop is always to check; bounded, control characters dropped, a proptest.
The phone's post takes a receipt (source receipt, ico, dic). The ledger gains payment.receipt_attached (the
payment keeps its merchant and key), known_shop and receipt_match (exactly one candidate or none).
docs/07_receipts.md; fixtures/receipts/ waits for redacted samples. v0.9.0.
This commit is contained in:
2026-11-17 19:40:08 +01:00
parent c6805aea8f
commit 507eb62216
17 changed files with 834 additions and 9 deletions
+111
View File
@@ -0,0 +1,111 @@
//! A receipt attached to a payment (`payment.receipt_attached`, docs/07_receipts.md): checked on the way in,
//! visible on the payment (either half of a confirmed pair), the latest one wins; a known shop comes from your
//! latest receipt with its IČO; a receipt fills in exactly one payment or none.
use fiscus_ledger::import::{plan_import, StatementLine};
use fiscus_ledger::{Category, Date, Event, EventBody, Haler, ReceiptInfo, State, Timestamp};
fn d(s: &str) -> Date {
s.parse().unwrap()
}
fn ev(id: &str, body: EventBody) -> Event {
Event::new(id, Timestamp(0), body)
}
fn added(id: &str, amount: i64, date: &str, merchant: &str) -> EventBody {
EventBody::PaymentAdded { payment: id.into(), amount: Haler(amount), date: d(date), merchant: merchant.into(), category: None, note: None }
}
fn captured(id: &str, amount: i64, date: &str) -> EventBody {
EventBody::PaymentCaptured { payment: id.into(), amount: Haler(amount), date: d(date), merchant: "ALBERT HM 4410".into(), card: None }
}
fn info(shop: &str, ico: Option<&str>) -> ReceiptInfo {
ReceiptInfo { shop: shop.into(), ico: ico.map(Into::into), dic: ico.map(|i| format!("CZ{i}")) }
}
fn attach(payment: &str, r: ReceiptInfo) -> EventBody {
EventBody::ReceiptAttached { payment: payment.into(), receipt: r }
}
fn state(bodies: Vec<EventBody>) -> State {
let mut s = State::default();
for (i, b) in bodies.into_iter().enumerate() {
s.apply(&ev(&format!("e{i}"), b));
}
s
}
#[test]
fn a_receipt_is_checked_and_sits_on_its_payment_which_keeps_its_merchant() {
let s = state(vec![captured("p1", -128_490, "2026-11-16"), attach("p1", info("Albert Hypermarket", Some("25672541")))]);
assert!(s.rejected.is_empty(), "{:?}", s.rejected);
let p = s.effective_payment("p1").unwrap();
assert_eq!(p.receipt.unwrap().shop, "Albert Hypermarket");
assert_eq!(p.merchant, "ALBERT HM 4410", "the merchant and its rules stay");
for (bad, why) in [
(attach("nope", info("A", None)), "no such payment"),
(attach("p1", info("", None)), "1-100 characters"),
(attach("p1", info(" Albert", None)), "trimmed"),
(attach("p1", info(&"x".repeat(101), None)), "1-100"),
(attach("p1", info("Albert", Some("25672542"))), "IČO"),
(attach("p1", ReceiptInfo { shop: "Albert".into(), ico: None, dic: Some("SK2020123456".into()) }), "DIČ"),
] {
let mut s = state(vec![captured("p1", -128_490, "2026-11-16")]);
let before = s.effective_payment("p1");
s.apply(&ev("bad", bad));
assert!(s.rejected.last().is_some_and(|r| r.reason.contains(why)), "{why}: {:?}", s.rejected);
assert_eq!(s.effective_payment("p1"), before, "a refusal changes nothing");
}
}
#[test]
fn on_a_confirmed_pair_the_receipt_shows_and_the_later_one_wins() {
let mut s = state(vec![captured("p1", -128_490, "2026-11-16"), attach("p1", info("Albert", Some("25672541")))]);
let line = StatementLine { bank: "test".into(), account: "1/0000".into(), booked: d("2026-11-17"), amount: Haler(-128_490), merchant: "ALBERT HM".into(), counterparty: None, variable_symbol: None, message: None, bank_ref: None };
let mut n = 0;
let plan = plan_import(&s, &[line], &mut || {
n += 1;
format!("i{n}")
}, Timestamp(1));
for e in plan.events.iter().chain(&plan.confirmations) {
s.apply(e);
}
let p = s.effective_payment("p1").unwrap();
assert!(p.line.is_some(), "confirmed by its line");
assert_eq!(p.receipt.as_ref().unwrap().shop, "Albert");
s.apply(&ev("later", attach("p1", info("Albert Hypermarket", Some("25672541")))));
assert_eq!(s.effective_payment("p1").unwrap().receipt.unwrap().shop, "Albert Hypermarket");
}
#[test]
fn a_known_shop_is_your_latest_receipt_with_its_ico() {
let s = state(vec![
added("a", -10_000, "2026-11-01", "x"),
EventBody::PaymentCategorised { payment: "a".into(), category: Category::from("groceries") },
attach("a", info("Albert", Some("25672541"))),
added("b", -20_000, "2026-11-05", "y"),
attach("b", info("Albert Hypermarket", Some("25672541"))),
added("c", -30_000, "2026-11-06", "z"),
attach("c", info("Lékárna", None)),
]);
let k = s.known_shop("25672541").unwrap();
assert_eq!((k.name.as_str(), k.payment.as_str()), ("Albert Hypermarket", "b"));
assert!(s.known_shop("61774031").is_none());
}
#[test]
fn a_receipt_fills_in_exactly_one_payment_or_none() {
let base = vec![captured("p1", -128_490, "2026-11-16"), added("p2", -128_490, "2026-11-30", "x"), added("p3", -9_900, "2026-11-16", "y")];
let s = state(base.clone());
assert_eq!(s.receipt_match(Haler(128_490), d("2026-11-18")).as_deref(), Some("p1"), "same amount, two days later");
assert_eq!(s.receipt_match(Haler(128_490), d("2026-11-20")), None, "four days is too far");
assert_eq!(s.receipt_match(Haler(128_400), d("2026-11-16")), None, "another amount");
let mut two = base.clone();
two.push(added("p4", -128_490, "2026-11-15", "z"));
assert_eq!(state(two).receipt_match(Haler(128_490), d("2026-11-16")), None, "two candidates: nothing guessed");
let mut done = base;
done.push(attach("p1", info("Albert", None)));
assert_eq!(state(done).receipt_match(Haler(128_490), d("2026-11-16")), None, "it has its receipt already");
}