mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
* Add customizable budget month start day (#253) Allow users to set a custom month-to-date start date (1st-28th) for budgeting and MTD calculations. Useful for users who want budget periods aligned with their pay schedule (e.g., 25th to 24th). Changes: - Add month_start_day column to families table (default: 1) - Add database check constraint for valid range (1-28) - Add Family#uses_custom_month_start?, custom_month_start_for, custom_month_end_for, current_custom_month_period helper methods - Add Period.current_month_for(family), last_month_for(family) methods - Update Budget model for custom month boundaries in find_or_bootstrap, param_to_date, budget_date_valid?, current?, and name methods - Add month_start_day setting to Settings > Preferences UI - Add warning message when custom month start day is configured - Add comprehensive tests with travel_to for date robustness Fixes #253 * Add /api/v1/user endpoint for Flutter mobile app and PWA Expose user preferences including month_start_day via API endpoint following existing pattern for default_period. This allows Flutter mobile app and PWA to read/update user preferences through a consistent API contract. Endpoints: - GET /api/v1/user - Read user preferences including family settings - PATCH /api/v1/user - Update user preferences Response includes: id, email, first_name, last_name, default_period, locale, and family settings (currency, timezone, date_format, country, month_start_day). * Update Periodable to use family-aware MTD periods When users select 'current_month' or 'last_month' period filters on dashboard/reports, now respects the family's custom month_start_day setting instead of using static calendar month boundaries. This ensures MTD filter on dashboard is consistent with how budgets calculate their periods when custom month start day is configured. * Fix param_to_date to correctly map budget params to custom periods When a family uses a custom start day, the previous implementation called custom_month_start_for on the 1st of the month, which incorrectly shifted dates before the start day to the previous month. Now we directly construct the date using family.month_start_day, so 'jan-2026' with month_start_day=25 correctly returns Jan 25, 2026 instead of Dec 25, 2025. * Fix param_to_date and use Current pattern in API controller - Fix param_to_date to directly construct date with family.month_start_day instead of using custom_month_start_for which incorrectly shifted dates - Replace current_user with Current.user/Current.family in API controller to follow project convention used in other API v1 controllers * Add i18n for budget name method Use I18n.t for localizable budget period names to follow project conventions for user-facing strings. * Remove unused budget_end variable in budget_date_valid? * Use Date.current for timezone consistency in Budget#current? * Address PR review feedback - Remove API users endpoint (mobile won't use yet) - Remove user route from config/routes.rb - Remove ai_summary/document_type schema bleed from pdf-import-ai branch * Pass family to param_to_date for custom month logic * Run migration to add month_start_day column to schema * Schema regressions --------- Co-authored-by: mkdev11 <jaysmth689+github@users.noreply.github.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
215 lines
5.7 KiB
Ruby
215 lines
5.7 KiB
Ruby
class Period
|
|
include ActiveModel::Validations, Comparable
|
|
|
|
class InvalidKeyError < StandardError; end
|
|
|
|
attr_reader :key, :start_date, :end_date
|
|
|
|
validates :start_date, :end_date, presence: true, if: -> { PERIODS[key].nil? }
|
|
validates :key, presence: true, if: -> { start_date.nil? || end_date.nil? }
|
|
validate :must_be_valid_date_range
|
|
|
|
PERIODS = {
|
|
"last_day" => {
|
|
date_range: -> { [ 1.day.ago.to_date, Date.current ] },
|
|
label_short: "1D",
|
|
label: "Last Day",
|
|
comparison_label: "vs. yesterday"
|
|
},
|
|
"current_week" => {
|
|
date_range: -> { [ Date.current.beginning_of_week, Date.current ] },
|
|
label_short: "WTD",
|
|
label: "Current Week",
|
|
comparison_label: "vs. start of week"
|
|
},
|
|
"last_7_days" => {
|
|
date_range: -> { [ 7.days.ago.to_date, Date.current ] },
|
|
label_short: "7D",
|
|
label: "Last 7 Days",
|
|
comparison_label: "vs. last week"
|
|
},
|
|
"current_month" => {
|
|
date_range: -> { [ Date.current.beginning_of_month, Date.current ] },
|
|
label_short: "MTD",
|
|
label: "Current Month",
|
|
comparison_label: "vs. start of month"
|
|
},
|
|
"last_month" => {
|
|
date_range: -> { [ 1.month.ago.beginning_of_month.to_date, 1.month.ago.end_of_month.to_date ] },
|
|
label_short: "LM",
|
|
label: "Last Month",
|
|
comparison_label: "vs. last month"
|
|
},
|
|
"last_30_days" => {
|
|
date_range: -> { [ 30.days.ago.to_date, Date.current ] },
|
|
label_short: "30D",
|
|
label: "Last 30 Days",
|
|
comparison_label: "vs. last 30 days"
|
|
},
|
|
"last_90_days" => {
|
|
date_range: -> { [ 90.days.ago.to_date, Date.current ] },
|
|
label_short: "90D",
|
|
label: "Last 90 Days",
|
|
comparison_label: "vs. last quarter"
|
|
},
|
|
"current_year" => {
|
|
date_range: -> { [ Date.current.beginning_of_year, Date.current ] },
|
|
label_short: "YTD",
|
|
label: "Current Year",
|
|
comparison_label: "vs. start of year"
|
|
},
|
|
"last_365_days" => {
|
|
date_range: -> { [ 365.days.ago.to_date, Date.current ] },
|
|
label_short: "365D",
|
|
label: "Last 365 Days",
|
|
comparison_label: "vs. 1 year ago"
|
|
},
|
|
"last_5_years" => {
|
|
date_range: -> { [ 5.years.ago.to_date, Date.current ] },
|
|
label_short: "5Y",
|
|
label: "Last 5 Years",
|
|
comparison_label: "vs. 5 years ago"
|
|
},
|
|
"last_10_years" => {
|
|
date_range: -> { [ 10.years.ago.to_date, Date.current ] },
|
|
label_short: "10Y",
|
|
label: "Last 10 Years",
|
|
comparison_label: "vs. 10 years ago"
|
|
},
|
|
"all_time" => {
|
|
date_range: -> {
|
|
oldest_date = Current.family&.oldest_entry_date
|
|
# If no family or no entries exist, use a reasonable historical fallback
|
|
# to ensure "All Time" represents a meaningful range, not just today
|
|
start_date = if oldest_date && oldest_date < Date.current
|
|
oldest_date
|
|
else
|
|
5.years.ago.to_date
|
|
end
|
|
[ start_date, Date.current ]
|
|
},
|
|
label_short: "All",
|
|
label: "All Time",
|
|
comparison_label: "vs. beginning"
|
|
}
|
|
}
|
|
|
|
class << self
|
|
def from_key(key)
|
|
unless PERIODS.key?(key)
|
|
raise InvalidKeyError, "Invalid period key: #{key}"
|
|
end
|
|
|
|
start_date, end_date = PERIODS[key].fetch(:date_range).call
|
|
|
|
new(key: key, start_date: start_date, end_date: end_date)
|
|
end
|
|
|
|
def custom(start_date:, end_date:)
|
|
new(start_date: start_date, end_date: end_date)
|
|
end
|
|
|
|
def all
|
|
PERIODS.map { |key, period| from_key(key) }
|
|
end
|
|
|
|
def as_options
|
|
all.map { |period| [ period.label_short, period.key ] }
|
|
end
|
|
|
|
def current_month_for(family)
|
|
return from_key("current_month") unless family&.uses_custom_month_start?
|
|
|
|
family.current_custom_month_period
|
|
end
|
|
|
|
def last_month_for(family)
|
|
return from_key("last_month") unless family&.uses_custom_month_start?
|
|
|
|
current_start = family.custom_month_start_for(Date.current)
|
|
last_month_date = current_start - 1.day
|
|
start_date = family.custom_month_start_for(last_month_date)
|
|
end_date = family.custom_month_end_for(last_month_date)
|
|
custom(start_date: start_date, end_date: end_date)
|
|
end
|
|
end
|
|
|
|
PERIODS.each do |key, period|
|
|
define_singleton_method(key) do
|
|
from_key(key)
|
|
end
|
|
end
|
|
|
|
def initialize(start_date: nil, end_date: nil, key: nil, date_format: "%b %d, %Y")
|
|
@key = key
|
|
@start_date = start_date
|
|
@end_date = end_date
|
|
@date_format = date_format
|
|
validate!
|
|
end
|
|
|
|
def <=>(other)
|
|
[ start_date, end_date ] <=> [ other.start_date, other.end_date ]
|
|
end
|
|
|
|
def date_range
|
|
start_date..end_date
|
|
end
|
|
|
|
def days
|
|
(end_date - start_date).to_i + 1
|
|
end
|
|
|
|
def within?(other)
|
|
start_date >= other.start_date && end_date <= other.end_date
|
|
end
|
|
|
|
def interval
|
|
if days > 366
|
|
"1 week"
|
|
else
|
|
"1 day"
|
|
end
|
|
end
|
|
|
|
def label
|
|
if key_metadata
|
|
key_metadata.fetch(:label)
|
|
else
|
|
"Custom Period"
|
|
end
|
|
end
|
|
|
|
def label_short
|
|
if key_metadata
|
|
key_metadata.fetch(:label_short)
|
|
else
|
|
"Custom"
|
|
end
|
|
end
|
|
|
|
def comparison_label
|
|
if key_metadata
|
|
key_metadata.fetch(:comparison_label)
|
|
else
|
|
"#{start_date.strftime(@date_format)} to #{end_date.strftime(@date_format)}"
|
|
end
|
|
end
|
|
|
|
private
|
|
def key_metadata
|
|
@key_metadata ||= PERIODS[key]
|
|
end
|
|
|
|
def must_be_valid_date_range
|
|
return if start_date.nil? || end_date.nil?
|
|
unless start_date.is_a?(Date) && end_date.is_a?(Date)
|
|
errors.add(:start_date, "must be a valid date, got #{start_date.inspect}")
|
|
errors.add(:end_date, "must be a valid date, got #{end_date.inspect}")
|
|
return
|
|
end
|
|
|
|
errors.add(:start_date, "must be before end date") if start_date > end_date
|
|
end
|
|
end
|