1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-22 14:49:38 +02:00
Maybe/app/models/assistant/function/get_balance_sheet.rb
Zach Gollwitzer 5da4bb6dc3
Subscription tests and domain (#2209)
* Save work

* Subscriptions and trials domain

* Store family ID on customer

* Remove indirection of stripe calls

* Test simplifications

* Update brakeman

* Fix stripe tests in CI

* Update billing page to show subscription details

* Remove legacy columns

* Complete billing settings page

* Fix hardcoded plan name

* Handle subscriptions for self hosting mode

* Lint fixes
2025-05-06 14:05:21 -04:00

73 lines
2.1 KiB
Ruby

class Assistant::Function::GetBalanceSheet < Assistant::Function
include ActiveSupport::NumberHelper
class << self
def name
"get_balance_sheet"
end
def description
<<~INSTRUCTIONS
Use this to get the user's balance sheet with varying amounts of historical data.
This is great for answering questions like:
- What is the user's net worth? What is it composed of?
- How has the user's wealth changed over time?
INSTRUCTIONS
end
end
def call(params = {})
observation_start_date = [ 5.years.ago.to_date, family.oldest_entry_date ].max
period = Period.custom(start_date: observation_start_date, end_date: Date.current)
{
as_of_date: Date.current,
oldest_account_start_date: family.oldest_entry_date,
currency: family.currency,
net_worth: {
current: family.balance_sheet.net_worth_money.format,
monthly_history: historical_data(period)
},
assets: {
current: family.balance_sheet.total_assets_money.format,
monthly_history: historical_data(period, classification: "asset")
},
liabilities: {
current: family.balance_sheet.total_liabilities_money.format,
monthly_history: historical_data(period, classification: "liability")
},
insights: insights_data
}
end
private
def historical_data(period, classification: nil)
scope = family.accounts.active
scope = scope.where(classification: classification) if classification.present?
if period.start_date == Date.current
[]
else
balance_series = scope.balance_series(
currency: family.currency,
period: period,
interval: "1 month",
favorable_direction: "up"
)
to_ai_time_series(balance_series)
end
end
def insights_data
assets = family.balance_sheet.total_assets
liabilities = family.balance_sheet.total_liabilities
ratio = liabilities.zero? ? 0 : (liabilities / assets.to_f)
{
debt_to_asset_ratio: number_to_percentage(ratio * 100, precision: 0)
}
end
end