-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathentries_test_helper.rb
More file actions
85 lines (70 loc) · 2.41 KB
/
Copy pathentries_test_helper.rb
File metadata and controls
85 lines (70 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module EntriesTestHelper
def create_transaction(attributes = {})
entry_attributes = attributes.except(:category, :tags, :merchant, :kind)
transaction_attributes = attributes.slice(:category, :tags, :merchant, :kind)
entry_defaults = {
account: accounts(:depository),
name: "Transaction",
date: Date.current,
currency: "USD",
amount: 100,
entryable: Transaction.new(transaction_attributes)
}
Entry.create! entry_defaults.merge(entry_attributes)
end
def create_valuation(attributes = {})
entry_attributes = attributes.except(:kind)
valuation_attributes = attributes.slice(:kind)
account = attributes[:account] || accounts(:depository)
amount = attributes[:amount] || 5000
entry_defaults = {
account: account,
name: "Valuation",
date: 1.day.ago.to_date,
currency: "USD",
amount: amount,
entryable: Valuation.new({ kind: "reconciliation" }.merge(valuation_attributes))
}
Entry.create! entry_defaults.merge(entry_attributes)
end
def create_trade(security, account:, qty:, date:, price: nil, currency: "USD", exchange_rate: nil)
trade_price = price || Security::Price.find_by!(security: security, date: date).price
trade = Trade.new \
qty: qty,
security: security,
price: trade_price,
currency: currency,
investment_activity_label: qty > 0 ? "Buy" : "Sell"
trade.exchange_rate = exchange_rate if exchange_rate
account.entries.create! \
name: "Trade",
date: date,
amount: qty * trade_price,
currency: currency,
entryable: trade
end
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
end