mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 15:49:39 +02:00
Basic Account Balance Sync Algorithm (#501)
* Sketch out sync interface * Add basic account sync algorithm * Update logic for final balance in series * Remove start_date concept * Clean up tests * Improve clarity of test * Update app/models/account.rb Co-authored-by: Rob Zolkos <rob@zolkos.com> Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> * Update app/models/transaction.rb Co-authored-by: Rob Zolkos <rob@zolkos.com> Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> * Update app/models/valuation.rb Co-authored-by: Rob Zolkos <rob@zolkos.com> Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> * Re-organize code, simplify job interface * Consolidate balance calculations * More cleanup --------- Signed-off-by: Zach Gollwitzer <zach.gollwitzer@gmail.com> Co-authored-by: Rob Zolkos <rob@zolkos.com>
This commit is contained in:
parent
fb657856a5
commit
dbf575c02a
14 changed files with 207 additions and 175 deletions
103
db/seeds.rb
103
db/seeds.rb
|
@ -26,92 +26,35 @@ puts "User created: #{user.email} for family: #{family.name}"
|
|||
# Create default currency
|
||||
Currency.find_or_create_by(iso_code: "USD", name: "United States Dollar")
|
||||
|
||||
current_balance = 350000
|
||||
|
||||
account = Account.create_or_find_by(name: "Seed Property Account", accountable: Account::Property.new, family: family, balance: current_balance, currency: "USD")
|
||||
checking_account = Account::Depository.new
|
||||
account = Account.create_or_find_by(
|
||||
name: "Seed Checking Account",
|
||||
accountable: checking_account,
|
||||
family: family,
|
||||
balance: 5000
|
||||
)
|
||||
puts "Account created: #{account.name}"
|
||||
|
||||
# Represent user-defined "Valuations" at various dates
|
||||
valuations = [
|
||||
{ date: Date.today - 30, value: 300000 },
|
||||
{ date: Date.today - 22, value: 300700 },
|
||||
{ date: Date.today - 17, value: 301400 },
|
||||
{ date: Date.today - 10, value: 300000 },
|
||||
{ date: Date.today - 3, value: 301900 }
|
||||
{ date: 1.year.ago.to_date, value: 4200 },
|
||||
{ date: 250.days.ago.to_date, value: 4500 },
|
||||
{ date: 200.days.ago.to_date, value: 4444.96 }
|
||||
]
|
||||
|
||||
account.valuations.upsert_all(valuations, unique_by: :index_valuations_on_account_id_and_date)
|
||||
|
||||
puts "Valuations created: #{valuations.count}"
|
||||
|
||||
transactions = [
|
||||
{ date: Date.today - 27, amount: 7.56, currency: "USD", name: "Starbucks" },
|
||||
{ date: Date.today - 18, amount: -2000, currency: "USD", name: "Paycheck" },
|
||||
{ date: Date.today - 18, amount: 18.20, currency: "USD", name: "Walgreens" },
|
||||
{ date: Date.today - 13, amount: 34.20, currency: "USD", name: "Chipotle" },
|
||||
{ date: Date.today - 9, amount: -200, currency: "USD", name: "Birthday check" },
|
||||
{ date: Date.today - 5, amount: 85.00, currency: "USD", name: "Amazon stuff" }
|
||||
{ date: Date.today - 27, amount: 7.56, name: "Starbucks" },
|
||||
{ date: Date.today - 18, amount: -500, name: "Paycheck" },
|
||||
{ date: Date.today - 18, amount: 18.20, name: "Walgreens" },
|
||||
{ date: Date.today - 13, amount: 34.20, name: "Chipotle" },
|
||||
{ date: Date.today - 9, amount: -200, name: "Birthday check" },
|
||||
{ date: Date.today - 5, amount: 85.00, name: "Amazon stuff" }
|
||||
]
|
||||
|
||||
# Represent system-generated "Balances" at various dates, based on valuations
|
||||
balances = [
|
||||
{ date: Date.today - 30, balance: 300000 },
|
||||
{ date: Date.today - 29, balance: 300000 },
|
||||
{ date: Date.today - 28, balance: 300000 },
|
||||
{ date: Date.today - 27, balance: 300000 },
|
||||
{ date: Date.today - 26, balance: 300000 },
|
||||
{ date: Date.today - 25, balance: 300000 },
|
||||
{ date: Date.today - 24, balance: 300000 },
|
||||
{ date: Date.today - 23, balance: 300000 },
|
||||
{ date: Date.today - 22, balance: 300700 },
|
||||
{ date: Date.today - 21, balance: 300700 },
|
||||
{ date: Date.today - 20, balance: 300700 },
|
||||
{ date: Date.today - 19, balance: 300700 },
|
||||
{ date: Date.today - 18, balance: 300700 },
|
||||
{ date: Date.today - 17, balance: 301400 },
|
||||
{ date: Date.today - 16, balance: 301400 },
|
||||
{ date: Date.today - 15, balance: 301400 },
|
||||
{ date: Date.today - 14, balance: 301400 },
|
||||
{ date: Date.today - 13, balance: 301400 },
|
||||
{ date: Date.today - 12, balance: 301400 },
|
||||
{ date: Date.today - 11, balance: 301400 },
|
||||
{ date: Date.today - 10, balance: 300000 },
|
||||
{ date: Date.today - 9, balance: 300000 },
|
||||
{ date: Date.today - 8, balance: 300000 },
|
||||
{ date: Date.today - 7, balance: 300000 },
|
||||
{ date: Date.today - 6, balance: 300000 },
|
||||
{ date: Date.today - 5, balance: 300000 },
|
||||
{ date: Date.today - 4, balance: 300000 },
|
||||
{ date: Date.today - 3, balance: 301900 },
|
||||
{ date: Date.today - 2, balance: 301900 },
|
||||
{ date: Date.today - 1, balance: 301900 },
|
||||
{ date: Date.today, balance: current_balance }
|
||||
]
|
||||
|
||||
|
||||
|
||||
valuations.each do |valuation|
|
||||
Valuation.find_or_create_by(
|
||||
account_id: account.id,
|
||||
date: valuation[:date]
|
||||
) do |valuation_record|
|
||||
valuation_record.value = valuation[:value]
|
||||
valuation_record.currency = "USD"
|
||||
end
|
||||
transactions.each do |t|
|
||||
account.transactions.find_or_create_by(t)
|
||||
end
|
||||
|
||||
balances.each do |balance|
|
||||
AccountBalance.find_or_create_by(
|
||||
account_id: account.id,
|
||||
date: balance[:date]
|
||||
) do |balance_record|
|
||||
balance_record.balance = balance[:balance]
|
||||
end
|
||||
end
|
||||
|
||||
transactions.each do |transaction|
|
||||
Transaction.find_or_create_by(
|
||||
account_id: account.id,
|
||||
date: transaction[:date],
|
||||
amount: transaction[:amount]
|
||||
) do |transaction_record|
|
||||
transaction_record.currency = transaction[:currency]
|
||||
transaction_record.name = transaction[:name]
|
||||
end
|
||||
end
|
||||
puts "Transactions created: #{transactions.count}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue