# 04 — Spending, category plans and the month in numbers (v0.5.0) Pure like the rest: `today` and the month are parameters. Money in haléře; every share is whole thousandths. ## One definition of spending (`fiscus-insight::spending`) Spending is money out of your own payments (a confirmed pair once), as positive amounts, by category. Money between your own accounts (`transfers`) is not spending; a payment nobody categorised counts as `uncategorised`. Money in is positive payments that are not transfers. Every sum is checked: when one would not fit, the answer is `None`, never a wrong number. `permille(parts)` gives each part's share in thousandths by the largest remainder, so the shares add up to exactly 1000 whenever there is anything to share (a pie never has a gap or an overlap). A tie goes to the earlier part; callers order the parts first. ## Category plans (`category.planned`, `fiscus-insight::plan`) `category.planned { category, amount, from }`: the most you mean to spend on a category in a month, from the month of `from` (always the 1st) on, until a later plan for that category replaces it. You plan once; zero removes the plan. `State::plan_for(category, month)` is the plan from that month or before, the latest month first, then the latest in the log. A plan is for a spending category only: not `income`, `transfers` or `uncategorised`, and a category the ledger knows (a default one or one of your own). 0 to 100 000 000 CZK. Plans are in your personal log and count **your own payments only**, like the forecast. They are never shared: the sharing boundary (`share.rs`, the app's `outgoing`) builds only `payment.shared` and never copies other events. `month_plan(state, month, today)`: per planned category, what you spent this month up to today, what is left (negative when over), the spent share of the plan (capped at 1000, for a bar), and the pace: | Pace | When | |:--|:--| | Over | spent more than the whole month's plan | | Ahead | more than 110 % of what the plan allows by today (`plan × day / days in month`) | | On pace | 90–110 % | | Under | below 90 %; for a past month, within the plan | Then the categories spent on without a plan (largest first), and the month's totals: planned, spent, and the part of it spent without a plan. A month that has not begun has nothing spent and no pace. `suggest(state, today)`: a plan from your history. For each category you can plan, the average spent over the last three complete months, rounded **up** to whole hundreds of crowns. Only months since your first payment count, so a ledger one month old is not divided by three. ## Categories of your own (`category.created`) `category.created { category, label }`: an id as `categories::is_valid` that is not a default category or one you made before, and a name of 1–40 characters (no spaces around it, no control characters). It can be planned and used like a default one. `State::category_label` and `State::categories()` (the default ones in their order, then yours by id) are what the app lists. ## The month in numbers (`fiscus-insight::stats`) `month_stats(state, insight, month, today)` for a month that has begun (`None` for one to come): - **slices**: spending by category, the largest 7, then the rest as one slice; thousandths that add up to 1000; - **out, in, net** (in less out); - **months**: the last six, oldest first, ending with this one (up to today); - **same day last month**: spent last month up to the same day (all of it for a past month); - **top merchants**: the 5 you paid most, by merchant key (however the bank writes the name), with the latest name and how many payments; a payment without a merchant is not a merchant; - **subscriptions**: what the subscriptions `analyse` found charged this month, and their share of spending. ## Tests `fiscus-ledger/tests/plans.rs` (5): a plan carries forward, the latest for a month wins, zero removes it, every refusal (income, transfers, uncategorised, unknown, not an id, negative, over the limit, not the 1st) changes nothing, categories of your own and their refusals, the events round-trip and replay the same. `fiscus-insight/tests/stats.rs` (5) and `spending.rs` (1): the month against its plan with every pace, the suggested plan, the month in numbers, shares that always add up to 1000, and no numbers when a sum would not fit. Three deliberate mistakes (transfers as spending, a plan before its month, no largest remainder) each fail them.