1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-25 08:09:38 +02:00

Add loan and credit card views (#1268)

* Add loan and credit card views

* Lint fix

* Clean up overview card markup

* Lint fix

* Test fix
This commit is contained in:
Zach Gollwitzer 2024-10-08 17:16:37 -04:00 committed by GitHub
parent 9263dd3bbe
commit fd941d714d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 564 additions and 102 deletions

View file

@ -82,6 +82,10 @@ class Account < ApplicationRecord
end
end
def original_balance
balances.chronological.first&.balance || balance
end
def owns_ticker?(ticker)
security_id = Security.find_by(ticker: ticker)&.id
entries.account_trades
@ -93,6 +97,15 @@ class Account < ApplicationRecord
classification == "asset" ? "up" : "down"
end
def update_with_sync!(attributes)
transaction do
update!(attributes)
update_balance!(attributes[:balance]) if attributes[:balance]
end
sync_later
end
def update_balance!(balance)
valuation = entries.account_valuations.find_by(date: Date.current)

View file

@ -1,9 +1,6 @@
class Address < ApplicationRecord
belongs_to :addressable, polymorphic: true
validates :line1, :locality, presence: true
validates :postal_code, presence: true, if: :postal_code_required?
def to_s
I18n.t("address.format",
line1: line1,
@ -15,10 +12,4 @@ class Address < ApplicationRecord
postal_code: postal_code
)
end
private
def postal_code_required?
country.in?(%w[US CA GB])
end
end

View file

@ -28,7 +28,7 @@ module Accountable
if balance_series.empty? && period.date_range.end == Date.current
TimeSeries.new([ { date: Date.current, value: account.balance_money.exchange_to(currency) } ])
else
TimeSeries.from_collection(balance_series, :balance_money)
TimeSeries.from_collection(balance_series, :balance_money, favorable_direction: account.asset? ? "up" : "down")
end
rescue Money::ConversionError
TimeSeries.new([])

View file

@ -1,3 +1,15 @@
class Loan < ApplicationRecord
include Accountable
def monthly_payment
return nil if term_months.nil? || interest_rate.nil? || rate_type.nil? || rate_type != "fixed"
return Money.new(0, account.currency) if account.original_balance.zero? || term_months.zero?
annual_rate = interest_rate / 100.0
monthly_rate = annual_rate / 12.0
payment = (account.original_balance * monthly_rate * (1 + monthly_rate)**term_months) / ((1 + monthly_rate)**term_months - 1)
Money.new(payment.round, account.currency)
end
end

View file

@ -3,14 +3,14 @@ class TimeSeries
attr_reader :values, :favorable_direction
def self.from_collection(collection, value_method)
def self.from_collection(collection, value_method, favorable_direction: "up")
collection.map do |obj|
{
date: obj.date,
value: obj.public_send(value_method),
original: obj
}
end.then { |data| new(data) }
end.then { |data| new(data, favorable_direction: favorable_direction) }
end
def initialize(data, favorable_direction: "up")