2024-02-02 09:05:04 -06:00
|
|
|
class Family < ApplicationRecord
|
2025-03-17 11:54:53 -04:00
|
|
|
include Syncable, AutoTransferMatchable
|
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 :plaid_items, dependent: :destroy
|
|
|
|
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
|
|
|
|
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
|
2024-06-20 08:38:59 -04:00
|
|
|
has_many :merchants, dependent: :destroy
|
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-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
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
def sync_data(start_date: nil)
|
|
|
|
update!(last_synced_at: Time.current)
|
|
|
|
|
|
|
|
accounts.manual.each do |account|
|
2025-02-04 14:22:44 -05:00
|
|
|
account.sync_later(start_date: start_date)
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
plaid_items.each do |plaid_item|
|
2025-02-04 14:22:44 -05:00
|
|
|
plaid_item.sync_later(start_date: start_date)
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-20 11:01:52 -05:00
|
|
|
def post_sync
|
|
|
|
broadcast_refresh
|
|
|
|
end
|
|
|
|
|
2024-11-15 13:49:37 -05:00
|
|
|
def syncing?
|
2025-01-31 19:08:21 -05:00
|
|
|
Sync.where(
|
|
|
|
"(syncable_type = 'Family' AND syncable_id = ?) OR
|
|
|
|
(syncable_type = 'Account' AND syncable_id IN (SELECT id FROM accounts WHERE family_id = ? AND plaid_account_id IS NULL)) OR
|
|
|
|
(syncable_type = 'PlaidItem' AND syncable_id IN (SELECT id FROM plaid_items WHERE family_id = ?))",
|
|
|
|
id, id, id
|
|
|
|
).where(status: [ "pending", "syncing" ]).exists?
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
|
2025-01-31 17:04:26 -05:00
|
|
|
def eu?
|
|
|
|
country != "US" && country != "CA"
|
|
|
|
end
|
|
|
|
|
2025-02-12 12:59:35 -06:00
|
|
|
def get_link_token(webhooks_url:, redirect_url:, accountable_type: nil, region: :us, access_token: nil)
|
2025-01-31 17:04:26 -05:00
|
|
|
provider = if region.to_sym == :eu
|
2025-03-17 11:54:53 -04:00
|
|
|
Providers.plaid_eu
|
2025-01-31 17:04:26 -05:00
|
|
|
else
|
2025-03-17 11:54:53 -04:00
|
|
|
Providers.plaid_us
|
2025-01-31 12:13:58 -06:00
|
|
|
end
|
|
|
|
|
2025-02-03 17:18:49 +01:00
|
|
|
# early return when no provider
|
|
|
|
return nil unless provider
|
|
|
|
|
2025-01-31 12:13:58 -06:00
|
|
|
provider.get_link_token(
|
2024-11-15 13:49:37 -05:00
|
|
|
user_id: id,
|
|
|
|
webhooks_url: webhooks_url,
|
|
|
|
redirect_url: redirect_url,
|
2025-01-31 12:13:58 -06:00
|
|
|
accountable_type: accountable_type,
|
2025-02-12 12:59:35 -06:00
|
|
|
access_token: access_token
|
2024-11-15 13:49:37 -05:00
|
|
|
).link_token
|
|
|
|
end
|
|
|
|
|
2025-02-28 11:35:10 -05:00
|
|
|
def subscribed?
|
|
|
|
stripe_subscription_status == "active"
|
2024-10-02 12:07:56 -04:00
|
|
|
end
|
2024-10-08 14:37:47 -05:00
|
|
|
|
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
|
|
|
|
|
|
|
|
def primary_user
|
|
|
|
users.order(:created_at).first
|
|
|
|
end
|
2025-01-16 14:36:37 -05:00
|
|
|
|
|
|
|
def oldest_entry_date
|
|
|
|
entries.order(:date).first&.date || Date.current
|
|
|
|
end
|
|
|
|
|
2025-02-03 09:04:39 -06:00
|
|
|
def active_accounts_count
|
|
|
|
accounts.active.count
|
|
|
|
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
|
2024-02-02 09:05:04 -06:00
|
|
|
end
|