//! Savings goals and challenges: what you mean to put aside and how, and the challenges you set yourself. Fiscus //! never moves money: a goal holds what you said you put aside (`goal.saved`), so it is never full unless you //! filled it. Personal log only, like plans: a shared budget's members never see them. use crate::event::Category; use crate::money::Haler; use crate::date::Date; use serde::{Deserialize, Serialize}; pub type GoalId = String; pub type ChallengeId = String; /// At most this many goals open at once. pub const MAX_OPEN_GOALS: usize = 20; /// At most this many challenges running (not ended, not given up) at once. pub const MAX_OPEN_CHALLENGES: usize = 20; /// A goal's target, a fixed monthly amount, a challenge's amount: 1 Kč to 100 000 000 Kč. pub const MIN_AMOUNT: Haler = Haler(100); pub const MAX_AMOUNT: Haler = Haler(10_000_000_000); /// A challenge lasts at most 92 days, its first and last day included. pub const MAX_CHALLENGE_DAYS: i32 = 92; /// Round-ups go to the next 10, 50 or 100 Kč. pub const ROUND_UP_TO: [Haler; 3] = [Haler(1_000), Haler(5_000), Haler(10_000)]; /// The tank's colour, from the app's palette. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] pub enum Colour { Teal, Amber, Coral, Blue, Violet, Green, } /// How a goal's monthly saving is found. A goal has at most one of each kind. #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)] pub enum Way { /// The same amount every month. Fixed { amount: Haler }, /// Each card payment of the month rounded up to the next `to` (10, 50 or 100 Kč); the differences add up. RoundUp { to: Haler }, /// This share (in thousandths) of what last month's category plans did not use. Leftover { permille: u16 }, /// This share (in thousandths) of last month's money in. Income { permille: u16 }, } #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum WayKind { Fixed, RoundUp, Leftover, Income, } impl Way { pub fn kind(&self) -> WayKind { match self { Way::Fixed { .. } => WayKind::Fixed, Way::RoundUp { .. } => WayKind::RoundUp, Way::Leftover { .. } => WayKind::Leftover, Way::Income { .. } => WayKind::Income, } } /// The share of the month's leftover or income this way takes, in thousandths (0 for the others). pub fn share_of(&self, kind: WayKind) -> u32 { match (self, kind) { (Way::Leftover { permille }, WayKind::Leftover) | (Way::Income { permille }, WayKind::Income) => u32::from(*permille), _ => 0, } } } /// What a challenge asks. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)] pub enum ChallengeGoal { /// Spend less than `amount` in `category` from the first day to the last. SpendUnder { category: Category, amount: Haler }, /// Spend nothing in `category`. NoSpend { category: Category }, /// Decide to cancel this subscription (its key) and not be charged for it again before the end. Cancel { subscription: String }, /// Put `amount` aside for this goal. Save { goal: GoalId, amount: Haler }, } /// One amount put aside (positive) or taken out (negative). #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct Saving { pub id: String, pub amount: Haler, pub on: Date, pub way: Option, pub seq: u64, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct GoalRecord { pub id: GoalId, pub name: String, pub target: Haler, pub by: Option, pub colour: Colour, pub ways: Vec, /// In log order. pub savings: Vec, pub closed: Option, pub seq: u64, } impl GoalRecord { /// Everything put aside less everything taken out; never negative (the ledger refuses that). pub fn saved(&self) -> Haler { Haler(self.savings.iter().fold(0i64, |s, x| s.saturating_add(x.amount.0))) } pub fn is_open(&self) -> bool { self.closed.is_none() } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct ChallengeRecord { pub id: ChallengeId, pub goal: ChallengeGoal, pub starts: Date, pub ends: Date, pub abandoned: Option, pub seq: u64, }