2024-02-02 09:05:04 -06:00
|
|
|
class Family < ApplicationRecord
|
2025-05-15 10:19:56 -04:00
|
|
|
include PlaidConnectable, Syncable, AutoTransferMatchable, Subscribeable
|
2024-11-15 13:49:37 -05:00
|
|
|
|
2025-02-03 11:19:56 -05:00
|
|
|
DATE_FORMATS = [
|
|
|
|
[ "MM-DD-YYYY", "%m-%d-%Y" ],
|
|
|
|
[ "DD.MM.YYYY", "%d.%m.%Y" ],
|
|
|
|
[ "DD-MM-YYYY", "%d-%m-%Y" ],
|
|
|
|
[ "YYYY-MM-DD", "%Y-%m-%d" ],
|
|
|
|
[ "DD/MM/YYYY", "%d/%m/%Y" ],
|
|
|
|
[ "YYYY/MM/DD", "%Y/%m/%d" ],
|
|
|
|
[ "MM/DD/YYYY", "%m/%d/%Y" ],
|
|
|
|
[ "D/MM/YYYY", "%e/%m/%Y" ],
|
|
|
|
[ "YYYY.MM.DD", "%Y.%m.%d" ]
|
|
|
|
].freeze
|
2024-10-23 11:20:55 -04:00
|
|
|
|
2024-02-02 09:05:04 -06:00
|
|
|
has_many :users, dependent: :destroy
|
|
|
|
has_many :accounts, dependent: :destroy
|
2025-02-21 11:57:59 -05:00
|
|
|
has_many :invitations, dependent: :destroy
|
|
|
|
|
2024-10-01 10:47:59 -04:00
|
|
|
has_many :imports, dependent: :destroy
|
2025-02-21 11:57:59 -05:00
|
|
|
|
2024-07-01 10:49:43 -04:00
|
|
|
has_many :entries, through: :accounts
|
2025-02-21 11:57:59 -05:00
|
|
|
has_many :transactions, through: :accounts
|
2025-04-18 11:39:58 -04:00
|
|
|
has_many :rules, dependent: :destroy
|
2025-02-21 11:57:59 -05:00
|
|
|
has_many :trades, through: :accounts
|
|
|
|
has_many :holdings, through: :accounts
|
|
|
|
|
|
|
|
has_many :tags, dependent: :destroy
|
2024-06-20 08:15:09 -04:00
|
|
|
has_many :categories, dependent: :destroy
|
2025-04-18 11:39:58 -04:00
|
|
|
has_many :merchants, dependent: :destroy, class_name: "FamilyMerchant"
|
2025-02-21 11:57:59 -05:00
|
|
|
|
2025-01-16 14:36:37 -05:00
|
|
|
has_many :budgets, dependent: :destroy
|
|
|
|
has_many :budget_categories, through: :budgets
|
2024-03-04 08:31:22 -05:00
|
|
|
|
2024-10-02 14:02:17 -04:00
|
|
|
validates :locale, inclusion: { in: I18n.available_locales.map(&:to_s) }
|
2025-02-03 11:19:56 -05:00
|
|
|
validates :date_format, inclusion: { in: DATE_FORMATS.map(&:last) }
|
2024-10-02 14:02:17 -04:00
|
|
|
|
2025-05-15 10:19:56 -04:00
|
|
|
# If any accounts or plaid items are syncing, the family is also syncing, even if a formal "Family Sync" is not running.
|
|
|
|
def syncing?
|
2025-06-01 06:30:38 -05:00
|
|
|
# Check for any in-progress syncs that belong directly to the family, to one of the
|
|
|
|
# family's accounts, or to one of the family's Plaid items. By moving the `visible`
|
|
|
|
# scope to the beginning we narrow down the candidate rows **before** performing the
|
|
|
|
# joins and by explicitly constraining the `syncable_type` for the direct Family
|
|
|
|
# match we allow Postgres to use the composite index on `(syncable_type, syncable_id)`.
|
|
|
|
Sync.visible
|
2025-05-15 10:19:56 -04:00
|
|
|
.joins("LEFT JOIN accounts ON accounts.id = syncs.syncable_id AND syncs.syncable_type = 'Account'")
|
2025-06-01 06:30:38 -05:00
|
|
|
.joins("LEFT JOIN plaid_items ON plaid_items.id = syncs.syncable_id AND syncs.syncable_type = 'PlaidItem'")
|
|
|
|
.where(
|
|
|
|
"(syncs.syncable_type = 'Family' AND syncs.syncable_id = :family_id) OR " \
|
|
|
|
"accounts.family_id = :family_id OR " \
|
|
|
|
"plaid_items.family_id = :family_id",
|
|
|
|
family_id: id
|
|
|
|
)
|
2025-05-15 10:19:56 -04:00
|
|
|
.exists?
|
|
|
|
end
|
|
|
|
|
2025-04-18 11:39:58 -04:00
|
|
|
def assigned_merchants
|
|
|
|
merchant_ids = transactions.where.not(merchant_id: nil).pluck(:merchant_id).uniq
|
|
|
|
Merchant.where(id: merchant_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_categorize_transactions_later(transactions)
|
|
|
|
AutoCategorizeJob.perform_later(self, transaction_ids: transactions.pluck(:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_categorize_transactions(transaction_ids)
|
|
|
|
AutoCategorizer.new(self, transaction_ids: transaction_ids).auto_categorize
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_detect_transaction_merchants_later(transactions)
|
|
|
|
AutoDetectMerchantsJob.perform_later(self, transaction_ids: transactions.pluck(:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_detect_transaction_merchants(transaction_ids)
|
|
|
|
AutoMerchantDetector.new(self, transaction_ids: transaction_ids).auto_detect
|
|
|
|
end
|
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
def balance_sheet
|
|
|
|
@balance_sheet ||= BalanceSheet.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def income_statement
|
|
|
|
@income_statement ||= IncomeStatement.new(self)
|
|
|
|
end
|
|
|
|
|
2025-01-31 17:04:26 -05:00
|
|
|
def eu?
|
|
|
|
country != "US" && country != "CA"
|
|
|
|
end
|
|
|
|
|
2025-02-28 11:35:10 -05:00
|
|
|
def requires_data_provider?
|
|
|
|
# If family has any trades, they need a provider for historical prices
|
|
|
|
return true if trades.any?
|
2024-12-04 00:36:59 +05:30
|
|
|
|
2025-02-28 11:35:10 -05:00
|
|
|
# If family has any accounts not denominated in the family's currency, they need a provider for historical exchange rates
|
|
|
|
return true if accounts.where.not(currency: self.currency).any?
|
2024-11-26 07:45:00 -06:00
|
|
|
|
2025-02-28 11:35:10 -05:00
|
|
|
# If family has any entries in different currencies, they need a provider for historical exchange rates
|
|
|
|
uniq_currencies = entries.pluck(:currency).uniq
|
|
|
|
return true if uniq_currencies.count > 1
|
2025-03-03 12:47:30 -05:00
|
|
|
return true if uniq_currencies.count > 0 && uniq_currencies.first != self.currency
|
2025-02-28 11:35:10 -05:00
|
|
|
|
|
|
|
false
|
2024-10-08 14:37:47 -05:00
|
|
|
end
|
|
|
|
|
2025-04-30 18:14:22 -04:00
|
|
|
def missing_data_provider?
|
|
|
|
requires_data_provider? && Provider::Registry.get_provider(:synth).nil?
|
|
|
|
end
|
|
|
|
|
2025-01-16 14:36:37 -05:00
|
|
|
def oldest_entry_date
|
|
|
|
entries.order(:date).first&.date || Date.current
|
|
|
|
end
|
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
# Cache key that is invalidated when any of the family's entries are updated (which affect rollups and other calculations)
|
|
|
|
def build_cache_key(key)
|
|
|
|
[
|
|
|
|
"family",
|
|
|
|
id,
|
|
|
|
key,
|
|
|
|
entries.maximum(:updated_at)
|
|
|
|
].compact.join("_")
|
|
|
|
end
|
2025-04-18 11:39:58 -04:00
|
|
|
|
|
|
|
def self_hoster?
|
|
|
|
Rails.application.config.app_mode.self_hosted?
|
|
|
|
end
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|