diff --git a/Cargo.lock b/Cargo.lock index 7d4dde2..3870751 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -139,7 +139,7 @@ checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" [[package]] name = "fiscus-capture" -version = "0.2.0" +version = "0.2.1" dependencies = [ "fiscus-ledger", "proptest", @@ -150,7 +150,7 @@ dependencies = [ [[package]] name = "fiscus-insight" -version = "0.2.0" +version = "0.2.1" dependencies = [ "fiscus-ledger", "serde", @@ -159,7 +159,7 @@ dependencies = [ [[package]] name = "fiscus-ledger" -version = "0.2.0" +version = "0.2.1" dependencies = [ "proptest", "serde", @@ -169,7 +169,7 @@ dependencies = [ [[package]] name = "fiscus-statement" -version = "0.2.0" +version = "0.2.1" dependencies = [ "encoding_rs", "fiscus-ledger", diff --git a/Cargo.toml b/Cargo.toml index 5fd174f..4583b5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "2" members = ["crates/fiscus-ledger", "crates/fiscus-statement", "crates/fiscus-insight", "crates/fiscus-capture"] [workspace.package] -version = "0.2.0" +version = "0.2.1" edition = "2021" license = "MPL-2.0" repository = "https://gitea.marek-novak.cz/mnovak/FiscusPublic" diff --git a/crates/fiscus-ledger/src/event.rs b/crates/fiscus-ledger/src/event.rs index b38a777..96f4bd7 100644 --- a/crates/fiscus-ledger/src/event.rs +++ b/crates/fiscus-ledger/src/event.rs @@ -86,6 +86,10 @@ pub enum EventBody { 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 → ". diff --git a/crates/fiscus-ledger/src/share.rs b/crates/fiscus-ledger/src/share.rs index cf89db3..08724ad 100644 --- a/crates/fiscus-ledger/src/share.rs +++ b/crates/fiscus-ledger/src/share.rs @@ -4,10 +4,18 @@ use crate::event::EventBody; use crate::state::State; -/// `payment.shared` for `payment` into `budget`, or `None` if that payment is not in that budget. +/// `payment.shared` for `payment` into `budget`, or `None` unless that payment was **explicitly** assigned to +/// that budget (`payment.assigned`, the latest on either half of a confirmed pair). Falling into a budget as +/// the default never shares a payment. pub fn shared_payment(state: &State, payment: &str, budget: &str) -> Option { let p = state.effective_payment(payment)?; - if p.budget.as_deref() != Some(budget) { + let explicit = [p.captured.as_deref(), p.line.as_deref()] + .into_iter() + .flatten() + .filter_map(|id| state.payments.get(id).and_then(|r| r.budget.as_ref())) + .max_by_key(|stamp| stamp.seq) + .map(|stamp| stamp.value.as_str()); + if explicit != Some(budget) { return None; } Some(EventBody::PaymentShared { diff --git a/crates/fiscus-ledger/src/state.rs b/crates/fiscus-ledger/src/state.rs index bdc1d52..edd2438 100644 --- a/crates/fiscus-ledger/src/state.rs +++ b/crates/fiscus-ledger/src/state.rs @@ -157,7 +157,7 @@ pub struct State { pub payments: BTreeMap, pub line_keys: BTreeMap, pub budgets: BTreeMap, - /// The first budget created: where a payment goes when nobody assigned it. + /// The first budget created (never a shared one): where a payment goes when nobody assigned it. pub default_budget: Option, /// Merchant key → the latest rule for it. pub rules: BTreeMap, @@ -397,7 +397,7 @@ impl State { EventBody::PaymentTrialMarked { payment, ends } => { self.payment_mut(payment)?.trial_ends = Some(Stamped { seq, value: *ends }); } - EventBody::BudgetCreated { budget, name, limit } => { + EventBody::BudgetCreated { budget, name, limit } | EventBody::BudgetShared { budget, name, limit } => { if budget.is_empty() || self.budgets.contains_key(budget) { return Err("a budget with this id exists"); } @@ -409,7 +409,7 @@ impl State { return Err("a limit cannot be negative"); } self.budgets.insert(budget.clone(), Budget { id: budget.clone(), name: name.into(), limit: *limit, seq }); - if self.default_budget.is_none() { + if self.default_budget.is_none() && matches!(body, EventBody::BudgetCreated { .. }) { self.default_budget = Some(budget.clone()); } } diff --git a/docs/02_capture_sharing.md b/docs/02_capture_sharing.md index e12e8d0..845e107 100644 --- a/docs/02_capture_sharing.md +++ b/docs/02_capture_sharing.md @@ -13,6 +13,7 @@ is a log of its own (one MLS group): its members see exactly what was put into i |:--|:--|:--| | `payment.shared` | shared budget | a member put one payment into this budget: `payment, budget, amount, date, merchant, category, origin`. Creates the payment in this log, or reassigns it after an unshare | | `payment.unshared` | shared budget | takes it out again (it stays in the log, unassigned) | +| `budget.shared` | personal | a shared budget (created here or joined): like `budget.created`, but **never the default budget** | | `split.created` | either | `split, payment, payer, shares: [{person, name, amount}]`. Rejected unless the shares sum **exactly** to the payment's amount, every amount has the payment's sign, people are unique, and the payer is one of them | | `split.settled` | either | `split, person, how` (`qr`, `marked`). Rejected for an unknown split or person, the payer, or a second settlement | | `capture.token_added` / `capture.token_revoked` | personal | a phone's capture token: only its SHA-256 (`token`), a `name` and the `kind` (`iphone`, `android`). The token itself lives in Custos | @@ -21,6 +22,9 @@ is a log of its own (one MLS group): its members see exactly what was put into i `apply()` stays total: anything that does not fit is recorded in `state.rejected` with the reason. +**Only an explicit `payment.assigned` shares a payment** (`share::shared_payment`). A payment that falls into +a budget as the default is never sent anywhere. + ### The split rule `split_equal(total, people)`: the people are **sorted by id and deduplicated first**, then split with equal weights by the day-1 largest-remainder `split()`. Every device computes the same shares in whatever