mirror of
https://github.com/we-promise/sure.git
synced 2026-09-05 23:01:26 +00:00
* 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
134 lines
5.2 KiB
Ruby
134 lines
5.2 KiB
Ruby
class RecurringTransaction
|
|
# The one declared-bill build path, shared by the add-bill form and the AI
|
|
# create tool so the two can never drift on the rules that matter here:
|
|
# the account must be one the user can actually write (attaching a bill
|
|
# changes what that account's owners see, so a read-only share is not a
|
|
# destination), and the amount carries the sign convention (income is
|
|
# stored negative).
|
|
class DeclaredBill
|
|
attr_reader :family, :user, :attrs
|
|
|
|
def initialize(family:, user:, attrs:)
|
|
@family = family
|
|
@user = user
|
|
@attrs = attrs
|
|
end
|
|
|
|
def build
|
|
account = Account.writable_by(user).find_by(id: attrs[:account_id])
|
|
due = begin
|
|
Date.parse(attrs[:first_due_on].to_s)
|
|
rescue Date::Error
|
|
nil
|
|
end
|
|
|
|
is_income = ActiveModel::Type::Boolean.new.cast(attrs[:is_income]) || false
|
|
# BigDecimal parses "Infinity" and "NaN" as non-finite numbers; both are
|
|
# invalid here, same as unparseable input.
|
|
amount = begin
|
|
BigDecimal(attrs[:amount].to_s.presence || "0").abs
|
|
rescue ArgumentError
|
|
nil
|
|
end
|
|
# update_bill rejects zero outright, and the schema says minimum 0.01, but
|
|
# strict mode is off so nothing enforced it here: a zero bill was written
|
|
# happily, and its cycles can never close because close_worthy? asks for a
|
|
# positive expected amount, so it sat open and overdue forever.
|
|
amount = nil if amount && (!amount.finite? || amount.zero?)
|
|
amount = -amount if amount && is_income
|
|
|
|
recurring = family.recurring_transactions.new(
|
|
name: attrs[:name],
|
|
amount: amount,
|
|
bill_type: is_income ? "income" : "bill",
|
|
account: account,
|
|
currency: account&.currency || family.currency,
|
|
payment_url: attrs[:payment_url],
|
|
autopay: ActiveModel::Type::Boolean.new.cast(attrs[:autopay]) || false,
|
|
notes: attrs[:notes],
|
|
status: "active",
|
|
manual: true,
|
|
occurrence_count: 0
|
|
)
|
|
recurring.frequency_preset = attrs[:frequency_preset]
|
|
recurring.first_due_on = attrs[:first_due_on]
|
|
|
|
# A chosen account that does not resolve to something writable is said
|
|
# out loud, not silently dropped: a read-only share or a foreign id
|
|
# would otherwise become an accountless bill in the family currency.
|
|
if attrs[:account_id].present? && account.nil?
|
|
recurring.errors.add(:base, I18n.t("recurring_transactions.create.account_invalid"))
|
|
return recurring
|
|
end
|
|
|
|
if amount.nil?
|
|
recurring.errors.add(:base, I18n.t("recurring_transactions.create.amount_invalid"))
|
|
return recurring
|
|
end
|
|
|
|
if due.nil?
|
|
recurring.errors.add(:base, I18n.t("recurring_transactions.create.due_date_required"))
|
|
return recurring
|
|
end
|
|
|
|
recurring.expected_day_of_month = due.day
|
|
recurring.anchor_date = due
|
|
recurring.last_occurrence_date = due
|
|
recurring.next_expected_date = due
|
|
|
|
FrequencyPreset.apply(
|
|
recurring,
|
|
preset: attrs[:frequency_preset],
|
|
day_of_month: due.day,
|
|
weekday: due.wday,
|
|
month_of_year: due.month
|
|
)
|
|
|
|
recurring
|
|
end
|
|
|
|
# A series for this identifier may already exist; a second legitimate one
|
|
# (another tier from the same biller) is distinguished by its amount, so
|
|
# the amount is stamped into dedup_scope before the first insert. The same
|
|
# identity at the same amount then collides immediately and is reported as
|
|
# a validation error rather than an escaping exception.
|
|
def self.save(recurring)
|
|
recurring.dedup_scope = recurring.amount.to_d.to_s("F") if recurring.dedup_scope.blank?
|
|
|
|
# Every dedup index is keyed on account_id, and Postgres treats NULLs as
|
|
# distinct, so a bill created without an account collides with nothing.
|
|
# Declaring one is explicitly supported ("omit it to create the bill
|
|
# without an account"), which meant a retried create silently doubled the
|
|
# family's recurring commitments. No partial index can express this, so
|
|
# the check lives here.
|
|
if recurring.account_id.nil? && account_less_duplicate?(recurring)
|
|
recurring.errors.add(:base, I18n.t("recurring_transactions.create.already_exists"))
|
|
return false
|
|
end
|
|
|
|
recurring.save
|
|
rescue ActiveRecord::RecordNotUnique
|
|
recurring.errors.add(:base, I18n.t("recurring_transactions.create.already_exists"))
|
|
false
|
|
end
|
|
|
|
# Mirrors the identity the unique indexes use, minus the account that is
|
|
# absent by definition: a merchant-keyed row matches on merchant, a
|
|
# name-keyed row matches on name.
|
|
def self.account_less_duplicate?(recurring)
|
|
scope = recurring.family.recurring_transactions
|
|
.where(account_id: nil,
|
|
currency: recurring.currency,
|
|
dedup_scope: recurring.dedup_scope,
|
|
destination_account_id: recurring.destination_account_id)
|
|
scope = scope.where.not(id: recurring.id) if recurring.id
|
|
|
|
if recurring.merchant_id.present?
|
|
scope.exists?(merchant_id: recurring.merchant_id)
|
|
else
|
|
scope.exists?(name: recurring.name, merchant_id: nil)
|
|
end
|
|
end
|
|
end
|
|
end
|