mirror of
https://github.com/we-promise/sure.git
synced 2026-04-08 14:54:49 +00:00
* Support manual recurring * Automatic variance calc * Automatic variance update * Tooltip for manual * Review * Fix variance calculations Manual recurring updates collapse occurrence tracking when amounts repeat * Proper Bigdecimal calcs * Fix n+1 query * Nicer UI errors. * Style --------- Signed-off-by: Juan José Mata <juanjo.mata@gmail.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
45 lines
1.3 KiB
Ruby
45 lines
1.3 KiB
Ruby
class RecurringTransaction
|
|
class Cleaner
|
|
attr_reader :family
|
|
|
|
def initialize(family)
|
|
@family = family
|
|
end
|
|
|
|
# Mark recurring transactions as inactive if they haven't occurred recently
|
|
# Uses 2 months for automatic recurring, 6 months for manual recurring
|
|
def cleanup_stale_transactions
|
|
stale_count = 0
|
|
|
|
family.recurring_transactions.active.find_each do |recurring_transaction|
|
|
next unless recurring_transaction.should_be_inactive?
|
|
|
|
# Determine threshold based on manual flag
|
|
threshold = recurring_transaction.manual? ? 6.months.ago.to_date : 2.months.ago.to_date
|
|
|
|
# Double-check if there are any recent matching transactions
|
|
recent_matches = recurring_transaction.matching_transactions.select { |entry| entry.date >= threshold }
|
|
|
|
if recent_matches.empty?
|
|
recurring_transaction.mark_inactive!
|
|
stale_count += 1
|
|
end
|
|
end
|
|
|
|
stale_count
|
|
end
|
|
|
|
# Remove inactive recurring transactions that have been inactive for 6+ months
|
|
# Manual recurring transactions are never automatically deleted
|
|
def remove_old_inactive_transactions
|
|
six_months_ago = 6.months.ago
|
|
|
|
family.recurring_transactions
|
|
.inactive
|
|
.where(manual: false)
|
|
.where("updated_at < ?", six_months_ago)
|
|
.destroy_all
|
|
end
|
|
end
|
|
end
|