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

First sketch of budgeting module

This commit is contained in:
Zach Gollwitzer 2024-12-30 17:29:59 -05:00
parent 5d1a2937bb
commit 9a2a7b31d4
45 changed files with 342 additions and 84 deletions

View file

@ -30,7 +30,20 @@ class Account::Entry < ApplicationRecord
)
}
scope :without_transfers, -> { where(marked_as_transfer: false) }
scope :incomes_and_expenses, -> {
joins("INNER JOIN account_transactions ON account_transactions.id = account_entries.entryable_id")
.joins(:account)
.joins("LEFT JOIN categories ON categories.id = account_transactions.category_id")
# All transfers excluded from income/expenses, outflow payments are expenses, inflow payments are NOT income
.where(<<~SQL.squish)
categories.id IS NULL OR
(
categories.classification != 'transfer' AND
(categories.classification != 'payment' OR account_entries.amount > 0)
)
SQL
}
scope :with_converted_amount, ->(currency) {
# Join with exchange rates to convert the amount to the given currency
# If no rate is available, exclude the transaction from the results
@ -98,13 +111,6 @@ class Account::Entry < ApplicationRecord
select("*").from(rolling_totals).where("date >= ?", period.date_range.first)
end
def mark_transfers!
update_all marked_as_transfer: true
# Attempt to "auto match" and save a transfer if 2 transactions selected
Account::Transfer.new(entries: all).save if all.count == 2
end
def bulk_update!(bulk_update_params)
bulk_attributes = {
date: bulk_update_params[:date],
@ -128,8 +134,7 @@ class Account::Entry < ApplicationRecord
end
def income_total(currency = "USD")
total = without_transfers.account_transactions.includes(:entryable)
.where("account_entries.amount <= 0")
total = incomes_and_expenses.where("account_entries.amount <= 0")
.map { |e| e.amount_money.exchange_to(currency, date: e.date, fallback_rate: 0) }
.sum
@ -137,8 +142,7 @@ class Account::Entry < ApplicationRecord
end
def expense_total(currency = "USD")
total = without_transfers.account_transactions.includes(:entryable)
.where("account_entries.amount > 0")
total = incomes_and_expenses.where("account_entries.amount > 0")
.map { |e| e.amount_money.exchange_to(currency, date: e.date, fallback_rate: 0) }
.sum