2025-04-14 11:40:34 -04:00
|
|
|
module EntriesTestHelper
|
2024-07-10 11:22:59 -04:00
|
|
|
def create_transaction(attributes = {})
|
2025-06-20 13:31:58 -04:00
|
|
|
entry_attributes = attributes.except(:category, :tags, :merchant, :kind)
|
|
|
|
transaction_attributes = attributes.slice(:category, :tags, :merchant, :kind)
|
2024-07-10 11:22:59 -04:00
|
|
|
|
|
|
|
entry_defaults = {
|
|
|
|
account: accounts(:depository),
|
|
|
|
name: "Transaction",
|
|
|
|
date: Date.current,
|
|
|
|
currency: "USD",
|
|
|
|
amount: 100,
|
2025-04-14 11:40:34 -04:00
|
|
|
entryable: Transaction.new(transaction_attributes)
|
2024-07-10 11:22:59 -04:00
|
|
|
}
|
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
Entry.create! entry_defaults.merge(entry_attributes)
|
2024-07-10 11:22:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_valuation(attributes = {})
|
2025-07-15 11:42:41 -04:00
|
|
|
entry_attributes = attributes.except(:kind)
|
|
|
|
valuation_attributes = attributes.slice(:kind)
|
|
|
|
|
|
|
|
account = attributes[:account] || accounts(:depository)
|
|
|
|
amount = attributes[:amount] || 5000
|
|
|
|
|
2024-07-10 11:22:59 -04:00
|
|
|
entry_defaults = {
|
2025-07-15 11:42:41 -04:00
|
|
|
account: account,
|
2024-07-10 11:22:59 -04:00
|
|
|
name: "Valuation",
|
|
|
|
date: 1.day.ago.to_date,
|
|
|
|
currency: "USD",
|
2025-07-15 11:42:41 -04:00
|
|
|
amount: amount,
|
|
|
|
entryable: Valuation.new({ kind: "reconciliation" }.merge(valuation_attributes))
|
2024-07-10 11:22:59 -04:00
|
|
|
}
|
|
|
|
|
2025-07-15 11:42:41 -04:00
|
|
|
Entry.create! entry_defaults.merge(entry_attributes)
|
2024-07-10 11:22:59 -04:00
|
|
|
end
|
2024-07-16 09:26:49 -04:00
|
|
|
|
2025-05-06 12:12:44 -04:00
|
|
|
def create_trade(security, account:, qty:, date:, price: nil, currency: "USD")
|
2024-10-29 15:37:59 -04:00
|
|
|
trade_price = price || Security::Price.find_by!(security: security, date: date).price
|
2024-07-25 16:46:04 -04:00
|
|
|
|
2025-04-14 11:40:34 -04:00
|
|
|
trade = Trade.new \
|
2024-07-25 16:46:04 -04:00
|
|
|
qty: qty,
|
|
|
|
security: security,
|
2025-05-06 12:12:44 -04:00
|
|
|
price: trade_price,
|
|
|
|
currency: currency
|
2024-07-25 16:46:04 -04:00
|
|
|
|
2024-07-16 09:26:49 -04:00
|
|
|
account.entries.create! \
|
2024-07-25 16:46:04 -04:00
|
|
|
name: "Trade",
|
2024-07-16 09:26:49 -04:00
|
|
|
date: date,
|
2024-07-25 16:46:04 -04:00
|
|
|
amount: qty * trade_price,
|
2025-05-06 12:12:44 -04:00
|
|
|
currency: currency,
|
2024-07-25 16:46:04 -04:00
|
|
|
entryable: trade
|
2024-07-16 09:26:49 -04:00
|
|
|
end
|
2025-07-18 17:56:25 -04:00
|
|
|
|
|
|
|
def create_transfer(from_account:, to_account:, amount:, date: Date.current, currency: "USD")
|
|
|
|
outflow_transaction = Transaction.create!(kind: "funds_movement")
|
|
|
|
inflow_transaction = Transaction.create!(kind: "funds_movement")
|
|
|
|
|
|
|
|
transfer = Transfer.create!(
|
|
|
|
outflow_transaction: outflow_transaction,
|
|
|
|
inflow_transaction: inflow_transaction
|
|
|
|
)
|
|
|
|
|
|
|
|
# Create entries for both accounts
|
|
|
|
from_account.entries.create!(
|
|
|
|
name: "Transfer to #{to_account.name}",
|
|
|
|
date: date,
|
|
|
|
amount: -amount.abs,
|
|
|
|
currency: currency,
|
|
|
|
entryable: outflow_transaction
|
|
|
|
)
|
|
|
|
|
|
|
|
to_account.entries.create!(
|
|
|
|
name: "Transfer from #{from_account.name}",
|
|
|
|
date: date,
|
|
|
|
amount: amount.abs,
|
|
|
|
currency: currency,
|
|
|
|
entryable: inflow_transaction
|
|
|
|
)
|
|
|
|
|
|
|
|
transfer
|
|
|
|
end
|
2024-07-10 11:22:59 -04:00
|
|
|
end
|