2025-04-14 11:40:34 -04:00
|
|
|
class Entry < ApplicationRecord
|
2025-03-17 11:54:53 -04:00
|
|
|
include Monetizable
|
2024-07-01 10:49:43 -04:00
|
|
|
|
|
|
|
monetize :amount
|
|
|
|
|
|
|
|
belongs_to :account
|
|
|
|
belongs_to :transfer, optional: true
|
2024-10-01 10:47:59 -04:00
|
|
|
belongs_to :import, optional: true
|
2024-07-01 10:49:43 -04:00
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
delegated_type :entryable, types: Entryable::TYPES, dependent: :destroy
|
2024-07-01 10:49:43 -04:00
|
|
|
accepts_nested_attributes_for :entryable
|
|
|
|
|
2024-12-19 10:16:09 -05:00
|
|
|
validates :date, :name, :amount, :currency, presence: true
|
2025-04-14 11:40:34 -04:00
|
|
|
validates :date, uniqueness: { scope: [ :account_id, :entryable_type ] }, if: -> { valuation? }
|
2024-07-26 10:47:27 -04:00
|
|
|
validates :date, comparison: { greater_than: -> { min_supported_date } }
|
2024-07-01 10:49:43 -04:00
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
scope :active, -> {
|
|
|
|
joins(:account).where(accounts: { is_active: true })
|
|
|
|
}
|
|
|
|
|
2024-12-10 17:41:20 -05:00
|
|
|
scope :chronological, -> {
|
|
|
|
order(
|
|
|
|
date: :asc,
|
2025-04-14 11:40:34 -04:00
|
|
|
Arel.sql("CASE WHEN entries.entryable_type = 'Valuation' THEN 1 ELSE 0 END") => :asc,
|
2024-12-10 17:41:20 -05:00
|
|
|
created_at: :asc
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
scope :reverse_chronological, -> {
|
|
|
|
order(
|
|
|
|
date: :desc,
|
2025-04-14 11:40:34 -04:00
|
|
|
Arel.sql("CASE WHEN entries.entryable_type = 'Valuation' THEN 1 ELSE 0 END") => :desc,
|
2024-12-10 17:41:20 -05:00
|
|
|
created_at: :desc
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
def sync_account_later
|
2024-12-10 17:41:20 -05:00
|
|
|
sync_start_date = [ date_previously_was, date ].compact.min unless destroyed?
|
2024-07-10 11:22:59 -04:00
|
|
|
account.sync_later(start_date: sync_start_date)
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def entryable_name_short
|
|
|
|
entryable_type.demodulize.underscore
|
|
|
|
end
|
|
|
|
|
2024-12-10 17:41:20 -05:00
|
|
|
def balance_trend(entries, balances)
|
2025-04-14 11:40:34 -04:00
|
|
|
Balance::TrendCalculator.new(self, entries, balances).trend
|
2024-07-01 10:49:43 -04:00
|
|
|
end
|
|
|
|
|
2024-12-19 10:16:09 -05:00
|
|
|
def display_name
|
|
|
|
enriched_name.presence || name
|
|
|
|
end
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
class << self
|
2024-12-20 11:37:26 -05:00
|
|
|
def search(params)
|
2025-04-14 11:40:34 -04:00
|
|
|
EntrySearch.new(params).build_query(all)
|
2024-12-20 11:37:26 -05:00
|
|
|
end
|
|
|
|
|
2024-07-26 10:47:27 -04:00
|
|
|
# arbitrary cutoff date to avoid expensive sync operations
|
|
|
|
def min_supported_date
|
2024-11-15 13:49:37 -05:00
|
|
|
30.years.ago.to_date
|
2024-07-26 10:47:27 -04:00
|
|
|
end
|
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
def bulk_update!(bulk_update_params)
|
|
|
|
bulk_attributes = {
|
|
|
|
date: bulk_update_params[:date],
|
2024-10-09 14:59:18 -04:00
|
|
|
notes: bulk_update_params[:notes],
|
2024-07-01 10:49:43 -04:00
|
|
|
entryable_attributes: {
|
|
|
|
category_id: bulk_update_params[:category_id],
|
2025-04-11 18:14:21 +02:00
|
|
|
merchant_id: bulk_update_params[:merchant_id],
|
|
|
|
tag_ids: bulk_update_params[:tag_ids]
|
2024-07-01 10:49:43 -04:00
|
|
|
}.compact_blank
|
|
|
|
}.compact_blank
|
|
|
|
|
|
|
|
return 0 if bulk_attributes.blank?
|
|
|
|
|
|
|
|
transaction do
|
|
|
|
all.each do |entry|
|
|
|
|
bulk_attributes[:entryable_attributes][:id] = entry.entryable_id if bulk_attributes[:entryable_attributes].present?
|
|
|
|
entry.update! bulk_attributes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
all.size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|