Files
sure/app/controllers/recurring_allocations_controller.rb
T
Brandon fe0d27471d feat(bills): the bills pages, calendar feed and in-page AI helpers (#3202)
* feat(bills): the bills pages, calendar feed and in-page AI helpers

Second of three chunks carved out of #3083, stacked on the schema and domain
core. This is everything a user sees and clicks. The whole surface sits behind
the preview flag, so it is unreachable until someone opts in.

Pages, all under one nav entry:

- the pay run, a month calendar, the full bills table, and the paycheck planner
- a detail drawer per bill, with payment history, price changes and cost
  analytics
- create and edit flows for bills, subscriptions, installment plans and income

The overview marks pay periods inside the month, so a weekly paycheck no longer
reads as one undifferentiated month of bills. Markers appear only when income
actually subdivides the month, which means monthly and undeclared income render
exactly as before and there is no new setting to configure.

Navigation and design system:

- one preview-gated nav item shared by the desktop rail and the mobile bar
- DS::Sparkline for payment-history charts, replacing raw SVG in views
- status badges render through DS::Pill rather than hand-rolled spans
- the suggestions panel is a disclosure that remembers being collapsed, per
  device, the way privacy mode and the sidebar width already do
- every surface reflows to phone widths without horizontal scroll

Calendar feed: a signed ICS feed per family, served sessionless by token, with a
reset that revokes previously shared URLs.

In-page AI helpers: smart fill on the bill form and a smart configuration
proposal on an existing bill, each reading a bounded slice of charge history.
Provider-side prompt assembly sits behind the existing LlmConcept interface,
with an implementation for each of the two providers. These belong here rather
than with the assistant tools because they are buttons on these pages and lean
on the provider suggester, not on the tool registry.

Suite 7,776 runs green apart from the pre-existing passkey-session flake, which passes standalone. Rubocop clean, eager loading verified. The hosting guide for the feature ships here rather than with the schema, since its instructions walk pages this PR introduces.

* Render the suggested strip through DS::Disclosure

The hand-rolled details pair predates the component. The card_inset
variant is the same shape, so the strip now inherits the design system
chrome, and the persisted-disclosure controller rides along unchanged.

* Route the remaining hand-rolled chips through the design system

The subscription-state chips, rule-match chips and match-reason chips
become DS::Pill, with the state chips extracted to one shared partial so
the drawer and the summary tab stop carrying copy-pasted markup. The AI
prompt chips become DS::Button and the bills-index filter becomes
DS::SearchInput, both of which this PR already uses elsewhere for the
same shapes.

* Fix erb_lint whitespace offenses in bills views

* Address the post-ready review round

* Require a writable destination account and gate the feed on preview

* Reject an unresolvable declared account out loud
2026-09-02 02:08:58 +02:00

139 lines
4.8 KiB
Ruby

class RecurringAllocationsController < ApplicationController
include RecurringFeatureGuardable
before_action :ensure_recurring_enabled
def create
occurrence = find_occurrence(params[:recurring_occurrence_id])
ensure_series_writable(occurrence)
entry = find_entry(occurrence, params[:entry_id])
RecurringTransaction::Allocator.new(occurrence).allocate!(
entry: entry,
amount: params[:amount].presence,
# Defaults to today via RecurringAllocation's callback; accepting a date
# lets someone record last Tuesday's payment as last Tuesday.
paid_on: parse_paid_on(params[:paid_on])
)
redirect_with notice: t(".success")
rescue RecurringTransaction::Allocator::OverAllocationError,
RecurringTransaction::Allocator::MissingRateError,
ActiveRecord::RecordInvalid,
ActiveRecord::RecordNotUnique,
ArgumentError => e
redirect_with alert: allocation_error_message(e)
end
def destroy
allocation = find_allocation
occurrence = allocation.recurring_occurrence
ensure_series_writable(occurrence)
RecurringTransaction::Allocator.new(occurrence).unallocate!(allocation)
redirect_with notice: t(".success")
end
def confirm
allocation = find_allocation
occurrence = allocation.recurring_occurrence
ensure_series_writable(occurrence)
RecurringTransaction::Allocator.new(occurrence).confirm_suggestion!(allocation)
redirect_with_return notice: t(".success")
end
def reject
allocation = find_allocation
occurrence = allocation.recurring_occurrence
ensure_series_writable(occurrence)
RecurringTransaction::Allocator.new(occurrence).reject_suggestion!(allocation)
redirect_with_return notice: t(".success")
end
private
# Active Record casts an unparseable date to nil, and a nil paid_on records
# the payment as today. Parsing here raises Date::Error (an ArgumentError),
# which the create rescue turns into the invalid-allocation message.
def parse_paid_on(raw)
return nil if raw.blank?
Date.iso8601(raw.to_s)
end
# Reading a shared bill is fine; changing its payment state is not. Sharing
# is per account, so a read-only account share must not mutate. Accountless
# series carry no account gate.
def ensure_series_writable(occurrence)
series = occurrence.recurring_transaction
return if series.account_id.nil?
return if Account.writable_by(Current.user).where(id: series.account_id).exists?
raise ActiveRecord::RecordNotFound
end
def find_allocation
RecurringAllocation
.joins(recurring_occurrence: :recurring_transaction)
.where(recurring_occurrences: { family_id: Current.family.id })
.merge(RecurringTransaction.accessible_by(Current.user))
.find(params[:id])
end
# Queue actions come from the Bills page and should land back there. The
# same-host referer check lives in RecurringFeatureGuardable#safe_return_path.
def redirect_with_return(notice:)
flash[:notice] = notice
target = safe_return_path(fallback: bills_path)
respond_to do |format|
format.html { redirect_to target }
format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, target) }
end
end
def find_occurrence(id)
Current.family.recurring_occurrences
.joins(:recurring_transaction)
.merge(RecurringTransaction.accessible_by(Current.user))
.find(id)
end
# Scoped to what this user can see: sharing is per account, so a family
# scope alone would let a member pay with another account's transaction.
def find_entry(occurrence, entry_id)
return nil if entry_id.blank?
Current.accessible_entries.find(entry_id)
end
def allocation_error_message(error)
case error
when RecurringTransaction::Allocator::OverAllocationError then t("recurring_allocations.over_allocation")
when RecurringTransaction::Allocator::MissingRateError then t("recurring_allocations.missing_rate")
when ActiveRecord::RecordNotUnique then t("recurring_allocations.already_allocated")
else t("recurring_allocations.invalid")
end
end
# Back to the worklist, not the occurrence: a plain GET of
# recurring_occurrence_path renders the settings layout, which already emits
# an empty <turbo-frame id="drawer">, so the page would carry two frames
# sharing one id. See the two-frames trap in
# RecurringTransactionsController#edit.
def redirect_with(notice: nil, alert: nil)
flash[:notice] = notice if notice
flash[:alert] = alert if alert
target = bills_path
respond_to do |format|
format.html { redirect_to target }
format.turbo_stream { render turbo_stream: turbo_stream.action(:redirect, target) }
end
end
end