[ledger] Sharing: budget.shared (never the default budget); shared_payment needs an explicit payment.assigned

This commit is contained in:
2026-11-03 23:36:07 +01:00
parent f21aef9859
commit 64917a9a89
6 changed files with 26 additions and 10 deletions
Generated
+4 -4
View File
@@ -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",
+1 -1
View File
@@ -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"
+4
View File
@@ -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 <merchant key> → <category>".
+10 -2
View File
@@ -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<EventBody> {
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 {
+3 -3
View File
@@ -157,7 +157,7 @@ pub struct State {
pub payments: BTreeMap<PaymentId, PaymentRecord>,
pub line_keys: BTreeMap<String, PaymentId>,
pub budgets: BTreeMap<BudgetId, Budget>,
/// 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<BudgetId>,
/// Merchant key → the latest rule for it.
pub rules: BTreeMap<String, Rule>,
@@ -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());
}
}
+4
View File
@@ -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