Late fees on overdue invoices
Ruby · Rails · beginner · modification
Adds per-account late fees, so an overdue invoice shows the fee in its amount due. The migration adds `late_fee_percent` to `accounts` (integer, not null, default 0, with a check constraint keeping it between 0 and 100). `Invoice` gains `late_fee_cents`, which is 0 unless the invoice is late and otherwise takes the account's percentage of the invoice amount, and `amount_due_cents` now adds it for unpaid invoices. `test/models/invoice_test.rb` adds model tests for the new methods.
Ruby 4.0, Rails 8.1. SQLite in development and test, PostgreSQL 16 in production; nothing here depends on the database beyond the column and its check constraint. `amount_cents` is a positive Integer (validated, and a DB check constraint), `due_on` is a required date, and `paid_at` is set when the invoice is paid. `Invoice#late?` and `Invoice#paid?` are existing and unchanged; `Date.current` uses the app's configured time zone. `Account has_many :invoices` and `Invoice belongs_to :account` (existing). The invoice page and the invoice JSON (`InvoicesController#show`, existing, unchanged, scoped to `Current.account`) already render `amount_due_cents`. The setting is edited from the account settings form, which ships separately and validates the same 0 to 100 range; until then support sets it in the console.
Requirements
- Each account gets a `late_fee_percent` setting: a whole number from 0 to 100, stored on `accounts`, defaulting to 0 (no late fee). Existing accounts get 0.
- `Invoice#late_fee_cents` is the fee owed on a late invoice: `late_fee_percent` percent of `amount_cents`, rounded down to the whole cent. For example a 5% fee on an invoice of 12,345 cents is 617. An invoice that is not late owes no fee (0). The fee is charged once, it does not grow with the number of days late.
- `Invoice#amount_due_cents` (existing) becomes `amount_cents + late_fee_cents` for an unpaid invoice. A paid invoice still owes 0, whenever it was paid.
- Unchanged: an invoice is late when it is unpaid and its `due_on` date is before today (`Invoice#late?`); an invoice due today is not late yet.
Files touched
- db/migrate/20261102090000_add_late_fee_percent_to_accounts.rb
- app/models/invoice.rb
- test/models/invoice_test.rb