Files
FiscusPublic/crates/fiscus-ledger/src/event.rs
T

230 lines
12 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::goal::{ChallengeGoal, ChallengeId, Colour, GoalId, Way, WayKind};
use crate::project::{EntryId, Kind, LineId, PhaseId, ProjectId, Status, TaskFields, TaskId, TaskStatus};
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 },
/// A savings goal (see [`crate::goal`]): a name, a target, an optional date to reach it by, the tank's colour.
#[serde(rename = "goal.created")]
GoalCreated { goal: GoalId, name: String, target: Haler, by: Option<Date>, colour: Colour },
/// A goal's name, target, date or colour changed; the latest counts.
#[serde(rename = "goal.changed")]
GoalChanged { goal: GoalId, name: String, target: Haler, by: Option<Date>, colour: Colour },
/// How a goal is saved for: up to one way of each kind. Replaces the goal's earlier ways.
#[serde(rename = "goal.ways_set")]
GoalWaysSet { goal: GoalId, ways: Vec<Way> },
/// Money put aside for a goal (positive) or taken out (negative), by one of its ways or by hand (`None`).
#[serde(rename = "goal.saved")]
GoalSaved { goal: GoalId, saving: String, amount: Haler, on: Date, way: Option<WayKind> },
/// Reached or given up: no more saving into it. Its history stays.
#[serde(rename = "goal.closed")]
GoalClosed { goal: GoalId, on: Date },
/// A challenge you set yourself, from `starts` to `ends` (both included, at most 92 days).
#[serde(rename = "challenge.started")]
ChallengeStarted { challenge: ChallengeId, goal: ChallengeGoal, starts: Date, ends: Date },
/// Given up; a challenge is given up once.
#[serde(rename = "challenge.abandoned")]
ChallengeAbandoned { challenge: ChallengeId, on: Date },
/// A project (see [`crate::project`]): simple or business, from a template or empty.
#[serde(rename = "project.created")]
ProjectCreated { project: ProjectId, name: String, kind: Kind, template: Option<String>, starts: Option<Date>, ends: Option<Date>, colour: Colour, rate: Option<Haler> },
/// Its name, kind, dates, colour or rate changed; the latest counts.
#[serde(rename = "project.changed")]
ProjectChanged { project: ProjectId, name: String, kind: Kind, starts: Option<Date>, ends: Option<Date>, colour: Colour, rate: Option<Haler> },
#[serde(rename = "project.status_set")]
ProjectStatusSet { project: ProjectId, status: Status, on: Date },
/// A budget line: its name and planned amount. Zero planned removes it.
#[serde(rename = "project.line_set")]
ProjectLineSet { project: ProjectId, line: LineId, name: String, planned: Haler },
/// A phase of a project, from `starts` to `ends`.
#[serde(rename = "project.phase_set")]
ProjectPhaseSet { project: ProjectId, phase: PhaseId, name: String, starts: Date, ends: Date },
#[serde(rename = "project.phase_removed")]
ProjectPhaseRemoved { project: ProjectId, phase: PhaseId },
/// A payment put into a project (and one of its lines). One project per payment; it stays in its budget.
#[serde(rename = "payment.projected")]
PaymentProjected { payment: PaymentId, project: ProjectId, line: Option<LineId> },
#[serde(rename = "payment.unprojected")]
PaymentUnprojected { payment: PaymentId },
/// "Always <merchant key> → <project>"; `None` removes the rule. It stops when the project is done.
#[serde(rename = "project.rule_set")]
ProjectRuleSet { merchant_key: String, project: Option<ProjectId> },
#[serde(rename = "task.created")]
TaskCreated { task: TaskId, project: ProjectId, fields: TaskFields },
#[serde(rename = "task.changed")]
TaskChanged { task: TaskId, fields: TaskFields },
#[serde(rename = "task.status_set")]
TaskStatusSet { task: TaskId, status: TaskStatus, on: Date },
/// Time worked on a business project (a task of it, or the project as a whole), at an hourly rate.
#[serde(rename = "time.logged")]
TimeLogged { entry: EntryId, project: ProjectId, task: Option<TaskId>, minutes: u32, on: Date, rate: Haler, note: String },
#[serde(rename = "time.removed")]
TimeRemoved { entry: EntryId },
/// An invoice issued for a business project, and the time entries it bills (none: not from time).
#[serde(rename = "invoice.projected")]
InvoiceProjected { invoice: InvoiceId, project: ProjectId, entries: Vec<EntryId> },
}