The month is a domain object#
Coconut does not simply filter every screen by the wall-clock calendar month. The provider exposes activeMonthKey, pendingMonthKey and currentMonth. The working period advances through the lifecycle; starting a month explicitly creates its plan.
active-month-service.ts owns resolveActiveMonth, the active date window, clamping and validation. A transaction whose date falls outside that working period must follow those rules; changing a date widget alone is not sufficient.
Four kinds of financial input#
- Allowance: the month's baseline spending provision.
- Fixed expenses: templates that become month-specific instances. An instance has an expected value and may later have an actual value.
- Budgets and transactions: a budget sets a planned envelope; transactions are spending against a budget or unbudgeted spending. A plan allocation is not itself a purchase.
- Subscriptions and income: subscriptions contribute month-specific allocated charges; income is additional one-off money received in the month.
Money fields are integers ending in Cents in TypeScript and _cents in SQL. 1299 means 12.99 currency units. Currency symbols are presentation/settings, not permission to mix unrelated currencies into one total.
Forecast arithmetic#
forecastMonth aggregates the same month inputs used by dashboard/report flows. The distinction between expected and actual is essential:
available = allowance + one-off income
fixed charged = actual fixed amount when known, otherwise expected amount
actual spending = fixed charged + discretionary transactions + subscriptions
planned spending = expected fixed expenses + budget allocations + subscriptions
remaining allowance = available - actual spending
remaining vs plan = planned spending - actual spending
expected adjustment = available - actual spending
allowance vs plan = allowance - planned spending
“Actual spending” here includes a fallback expected amount for an unresolved fixed expense. It is not a bank-reconciled cash ledger. Likewise, the implemented forecast is an aggregation of recorded/committed inputs; do not assume a statistical extrapolation of daily discretionary spending.
For example, with allowance 100000, income 10000, charged fixed expenses 30000, subscriptions 5000 and transactions 20000, available money is 110000, spending is 55000 and the reserve adjustment is 55000.
budgetStatus and related budget functions derive used/remaining amounts. A budget's allocation and its actual transactions must not both be counted as realized spending.
Allowance and reserve signs#
allowance-service.ts gives these concepts explicit functions:
| Function | Meaning |
|---|---|
remainingAllowance(allowance, actual) |
Allowance minus spending; positive means headroom |
spendingDifference(actual, planned) |
Spending minus plan; positive means over plan |
reserveAdjustmentCents(actual, allowance, income) |
Allowance plus income minus spending; positive saves to reserve |
unplannedAllowance(allowance, committed) |
Planning space after commitments |
remainingForBudget(input) |
Allowance less expected fixed costs, subscriptions and all other budgets |
reserveDrawBehindPlan(planned, actual) |
Detects a negative actual movement at least as large a draw as planned |
remainingForBudget deliberately excludes one-off income and excludes the budget currently being edited. Otherwise the edit form would subtract its own tentative amount twice.
reserve-service.ts owns projectReserve, buildAutomaticReserveTransfer and effectiveEndingReserve. A closing transfer records the month's movement rather than a second independent spending event.
Planning and replanning#
planMonth evaluates a preview using allowanceCents + drawCents. A positive draw increases available planning funds; a negative draw reserves more money instead. clampDrawCents caps a positive draw at the starting reserve.
This preview does not mean the saved base allowance has been increased by a reserve withdrawal. Keep the base allowance, planned draw and realized reserve adjustment conceptually separate.
diffMonthPlan compares the current budget allocations/draw against the proposed values. It emits only real changes. fundedBudgetIdForDraw associates a raised draw with a budget whose increase matches that draw increase; budgetHeadroomCents computes how much that particular budget can receive.
database/monthPlan.ts persists the audit trail, including budget plan events. Re-saving an unchanged plan should not fabricate a new financial decision. Historical reports can then distinguish the starting plan from later replanning.
Variable fixed expenses#
estimateAmount accepts a fixed-expense configuration, historical actuals and an optional target month. When a target is supplied, it filters to strictly earlier months before estimating.
| Strategy | Implemented result |
|---|---|
| Fixed kind | Configured expected amount |
manual |
Configured expected amount |
last-month |
Latest available earlier actual, not necessarily an immediately adjacent calendar month |
average |
Rounded average of the configured recent window, or all available history if no positive window is set |
history-average |
Rounded average of all relevant actuals |
| No usable history | Configured expected amount |
Do not accidentally include the target month's just-entered actual in its own estimate. That would alter the meaning of historical plan-versus-result comparisons.
Subscriptions#
The repository retains the historical table name yearly_subscriptions, but the current implementation supports period-based allocation with total amount, start month and end month. Do not infer a mandatory annual billing cycle from that old name.
subscription-service.ts implements monthlyChargeCents, subscriptionChargesForMonth, monthSubscriptionTotal, subscriptionMonths and monthlyFromTotal. database/monthSubscriptions.ts owns per-month subscription data. These instances let the month retain its financial shape rather than relying only on a mutable template.
Changing a subscription therefore requires considering both the template and historical/month-specific rows, and their sync representation.
Closing and automatic settlement#
month-closing-service.ts defines the closing window relative to the start of the next month:
- Opens 24 hours before that boundary.
- Ends 36 hours after it.
- Start is inclusive; end is exclusive.
findClosableMonthchooses the oldest qualifying open month.monthsRequiringAutoClosereturns overdue open months, oldest first.
For a 31-day month, the window runs from midnight beginning the 31st until noon on the 2nd of the following month, using the service's local dayjs date semantics.
In the provider, settleMonth builds the dashboard, persists closure with the projected ending reserve, and records an automatic reserve transfer. If persistence says the month was already closed, it returns without recording a second transfer. This idempotency is financially important.
Reports and statistics#
monthly-report-service.ts builds report data with buildMonthlyReport, including fixed expense, subscription and budget breakdowns and plan summaries. statistics-service.ts computes monthly totals, averages, medians, category spending, budget performance, rating shares and reserve development.
Before changing a number in the UI, identify whether it is a live forecast, closed-month result, plan allocation or statistical aggregate. Similar-looking values can legitimately differ because they answer different questions.