[ledger] Invoices and the tax reserve (v1.1, v0.4.0) #5

Manually merged
mnovak merged 1 commits from feature/invoices into main 2026-11-05 14:44:47 +01:00
Owner

v1.1's first piece in the core: invoices (faktury) and the tax reserve. Design: docs/03_invoices.md.

What

  • invoice module: Party, Supplier, Invoice, InvoiceLine, Totals, IČO and DIČ checks, line_amount, totals, invoice_number, same_vs, plan_invoice_payments.
  • Events: supplier.set, invoice.issued, invoice.paid, invoice.cancelled, reserve.withdrawn.
  • State: supplier, invoices, invoice_numbers, invoice_payments, withdrawals; reserve_balance(), next_invoice_number(year), invoice_spayd(id).
  • Version 0.4.0.

Decisions (proposals, recorded in the doc)

  • The number is YYYYNNNN and is also the variable symbol: one string, so it cannot drift from the symbol.
  • Quantities are in thousandths and money in haléře. Rounding is half away from zero: per line, then VAT once per rate from the base (§ 37 ZDPH).
  • The reserve is a per-supplier share of the amount without VAT, 0-60 %. A VAT payer's VAT is always set aside in full on top. The share is the person's to set. The core has no tax tables.
  • A paid invoice is matched only by exact total and variable symbol, never before its issue date. One line pays one invoice. The planner emits explicit events (paid, then income, then assigned to the invoice's budget), so sharing into a shared budget stays on the explicit-assignment path.
  • An invoice keeps the supplier it was issued under.

Tests

cargo test --workspace: 103 (92 before, plus 10 in tests/invoices.rs and 1 rounding unit test). All pass.
Mutations, each caught by a failing test: the variable-symbol guard removed, the issue-date guard removed, the reserve set to zero.

Self-review

  • No floats: every multiplication is i128 then try_from; overflow becomes InvoiceError::Total, never a wrap.
  • Hostile input: control characters, empty or oversized texts, 0 or 51 lines, rates other than 21/12/0, a rate on a non-VAT invoice and a missing one on a VAT invoice, a DUZP mismatch, a due date before the issue date, a wrong year in the number, a bad IČO, an unknown budget. All are rejected and change nothing (tested).
  • Determinism: the state is all BTreeMap/Vec in log order; the planner sorts by log order and takes the earliest line in date then log order. Replaying the log twice gives the same state (tested).
  • Not built yet: a credit note for a paid invoice, partial payments (a line of another amount stays for the person), foreign currency.
v1.1's first piece in the core: invoices (faktury) and the tax reserve. Design: `docs/03_invoices.md`. ## What - `invoice` module: `Party`, `Supplier`, `Invoice`, `InvoiceLine`, `Totals`, IČO and DIČ checks, `line_amount`, `totals`, `invoice_number`, `same_vs`, `plan_invoice_payments`. - Events: `supplier.set`, `invoice.issued`, `invoice.paid`, `invoice.cancelled`, `reserve.withdrawn`. - State: `supplier`, `invoices`, `invoice_numbers`, `invoice_payments`, `withdrawals`; `reserve_balance()`, `next_invoice_number(year)`, `invoice_spayd(id)`. - Version 0.4.0. ## Decisions (proposals, recorded in the doc) - The number is `YYYYNNNN` and is also the variable symbol: one string, so it cannot drift from the symbol. - Quantities are in thousandths and money in haléře. Rounding is half away from zero: per line, then VAT once per rate from the base (§ 37 ZDPH). - The reserve is a per-supplier share of the amount without VAT, 0-60 %. A VAT payer's VAT is always set aside in full on top. The share is the person's to set. The core has no tax tables. - A paid invoice is matched only by exact total and variable symbol, never before its issue date. One line pays one invoice. The planner emits explicit events (`paid`, then `income`, then `assigned` to the invoice's budget), so sharing into a shared budget stays on the explicit-assignment path. - An invoice keeps the supplier it was issued under. ## Tests `cargo test --workspace`: 103 (92 before, plus 10 in `tests/invoices.rs` and 1 rounding unit test). All pass. Mutations, each caught by a failing test: the variable-symbol guard removed, the issue-date guard removed, the reserve set to zero. ## Self-review - **No floats:** every multiplication is `i128` then `try_from`; overflow becomes `InvoiceError::Total`, never a wrap. - **Hostile input:** control characters, empty or oversized texts, 0 or 51 lines, rates other than 21/12/0, a rate on a non-VAT invoice and a missing one on a VAT invoice, a DUZP mismatch, a due date before the issue date, a wrong year in the number, a bad IČO, an unknown budget. All are rejected and change nothing (tested). - **Determinism:** the state is all `BTreeMap`/`Vec` in log order; the planner sorts by log order and takes the earliest line in date then log order. Replaying the log twice gives the same state (tested). - **Not built yet:** a credit note for a paid invoice, partial payments (a line of another amount stays for the person), foreign currency.
mnovak added 1 commit 2026-11-05 14:44:36 +01:00
mnovak manually merged commit 95a8a8965b into main 2026-11-05 14:44:47 +01:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mnovak/FiscusPublic#5