Files
FiscusPublic/crates/fiscus-ledger/src/event.rs
T
mnovak 03dc2061f9 [insight] Plans and statistics: category plans, categories of your own, one definition of spending, the month in numbers; v0.5.0
- category.planned (carries forward, zero removes it; spending categories only) and category.created
- fiscus-insight: spending (outflows, inflow, largest-remainder thousandths that add up to 1000), plan (the
  month against its plan with the pace, a suggested plan from the last three months), stats (pie slices, in,
  out, net, six months, same day last month, top merchants, subscriptions' share)
- docs/04_plans_stats.md; 11 new tests
2026-11-06 13:02:39 +01:00

169 lines
7.6 KiB
Rust

//! The event log. Append only: a correction is a new event, nothing is ever edited or removed.
use crate::date::{Date, Timestamp};
use crate::import::StatementLine;
use crate::invoice::{Invoice, InvoiceId, Supplier};
use crate::money::Haler;
use crate::split::Share;
use crate::state::Glyph;
use serde::{Deserialize, Serialize};
/// Ids are opaque strings the app generates (random, unguessable); the core never makes one up.
pub type EventId = String;
pub type PaymentId = String;
pub type BudgetId = String;
pub type RuleId = String;
pub type SplitId = String;
/// A category id, e.g. `groceries` (see [`crate::categories`]).
pub type Category = String;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Event {
pub id: EventId,
/// When the event was written (the app's clock), for display and sync; never for ordering.
pub at: Timestamp,
#[serde(flatten)]
pub body: EventBody,
}
impl Event {
pub fn new(id: impl Into<EventId>, at: Timestamp, body: EventBody) -> Event {
Event { id: id.into(), at, body }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Decision {
Keep,
Cancel,
}
/// How a share of a split was settled.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Settlement {
/// Paid by QR Platba.
Qr,
/// Marked paid by hand (cash, another way).
Marked,
}
/// Which phone a capture token is for.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CaptureKind {
Iphone,
Android,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum EventBody {
/// A phone saw a card payment (○).
#[serde(rename = "payment.captured")]
PaymentCaptured { payment: PaymentId, amount: Haler, date: Date, merchant: String, card: Option<String> },
/// One statement line (●). `line_key` identifies the line across imports.
#[serde(rename = "payment.imported")]
PaymentImported { payment: PaymentId, line: StatementLine, line_key: String },
/// Added by hand (◆). The merchant may be empty (cash, "groceries").
#[serde(rename = "payment.added")]
PaymentAdded {
payment: PaymentId,
amount: Haler,
date: Date,
merchant: String,
category: Option<Category>,
note: Option<String>,
},
/// A captured or hand-added payment and the statement line that confirms it are one payment.
#[serde(rename = "payment.confirmed")]
PaymentConfirmed { payment: PaymentId, line: PaymentId },
#[serde(rename = "payment.categorised")]
PaymentCategorised { payment: PaymentId, category: Category },
#[serde(rename = "payment.assigned")]
PaymentAssigned { payment: PaymentId, budget: BudgetId },
#[serde(rename = "payment.trial_marked")]
PaymentTrialMarked { payment: PaymentId, ends: Date },
#[serde(rename = "budget.created")]
BudgetCreated { budget: BudgetId, name: String, limit: Haler },
/// A shared budget in a personal log (created here or joined): like `budget.created`, but never the
/// default budget, so unassigned payments never fall into a budget other people see.
#[serde(rename = "budget.shared")]
BudgetShared { budget: BudgetId, name: String, limit: Haler },
#[serde(rename = "budget.limit_set")]
BudgetLimitSet { budget: BudgetId, limit: Haler },
/// "Always <merchant key> → <category>".
#[serde(rename = "rule.created")]
RuleCreated { rule: RuleId, merchant_key: String, category: Category },
/// Keep or cancel a subscription (by its key), seen at `amount` (a positive cost) on `on`.
#[serde(rename = "subscription.decided")]
SubscriptionDecided { subscription: String, decision: Decision, amount: Haler, on: Date },
/// A shared budget's log: a member put this payment (as they see it) into the budget.
#[serde(rename = "payment.shared")]
PaymentShared {
payment: PaymentId,
budget: BudgetId,
amount: Haler,
date: Date,
merchant: String,
category: Option<Category>,
glyph: Glyph,
},
/// A shared budget's log: the payment is taken out of the budget again.
#[serde(rename = "payment.unshared")]
PaymentUnshared { payment: PaymentId, budget: BudgetId },
/// The payment's amount, split among people (see [`crate::split::split_equal`]).
#[serde(rename = "split.created")]
SplitCreated { split: SplitId, payment: PaymentId, payer: String, shares: Vec<Share> },
#[serde(rename = "split.settled")]
SplitSettled { split: SplitId, person: String, how: Settlement },
/// A phone's capture token: only its SHA-256 in lowercase hex. The token itself lives in Custos.
#[serde(rename = "capture.token_added")]
CaptureTokenAdded { token: String, name: String, kind: CaptureKind },
#[serde(rename = "capture.token_revoked")]
CaptureTokenRevoked { token: String },
/// A share link for one person of a split: only the SHA-256 of the link's token, in lowercase hex.
#[serde(rename = "share.link_created")]
ShareLinkCreated { link: String, split: SplitId, person: String, expires: Date },
/// The account people pay back to by QR Platba.
#[serde(rename = "account.set")]
AccountSet { iban: String, name: String },
/// The name this person shows to the members of their shared budgets (the personal log).
#[serde(rename = "profile.named")]
ProfileNamed { name: String },
/// Share my subscription names with this shared budget's members, or stop (the personal log).
#[serde(rename = "household.sharing_set")]
HouseholdSharingSet { budget: BudgetId, on: bool },
/// A shared budget's log: the name its author shows there. The latest per author counts.
#[serde(rename = "member.named")]
MemberNamed { budget: BudgetId, name: String },
/// A shared budget's log: every subscription its author pays, as service keys (`spotify`). The latest per
/// author replaces the earlier ones; an empty list withdraws them.
#[serde(rename = "subscriptions.shared")]
SubscriptionsShared { budget: BudgetId, names: Vec<String> },
/// Who issues invoices from this ledger, and what share of each paid one is set aside for tax. The latest
/// counts from then on; an invoice keeps the supplier it was issued under.
#[serde(rename = "supplier.set")]
SupplierSet { supplier: Supplier },
#[serde(rename = "invoice.issued")]
InvoiceIssued { invoice: Invoice },
/// Paid: by `payment` (a statement line whose amount and variable symbol match), or marked paid by hand
/// (`None`: cash, another account). The reserve is set aside then.
#[serde(rename = "invoice.paid")]
InvoicePaid { invoice: InvoiceId, payment: Option<PaymentId>, on: Date },
/// Cancelled before it was paid.
#[serde(rename = "invoice.cancelled")]
InvoiceCancelled { invoice: InvoiceId },
/// Money taken out of the tax reserve (the tax or insurance paid from it).
#[serde(rename = "reserve.withdrawn")]
ReserveWithdrawn { withdrawal: String, amount: Haler, on: Date, note: String },
/// The most you mean to spend on one category in a month, from the month of `from` (its 1st) on, until a
/// later plan for that category replaces it. Zero removes the plan. Personal log only.
#[serde(rename = "category.planned")]
CategoryPlanned { category: Category, amount: Haler, from: Date },
/// A category of your own (an id as [`crate::categories::is_valid`]) and the name the app shows for it.
#[serde(rename = "category.created")]
CategoryCreated { category: Category, label: String },
}