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
|
# File 'lib/acts_as_account/journal.rb', line 26
def transfer(amount, from_account, to_account, reference = nil, valuta = Time.now)
transaction do
if (amount < 0)
amount, from_account, to_account = -amount, to_account, from_account
end
if logger
logger.debug(
[
"ActsAsAccount::Journal.transfer",
"amount: #{amount}",
"from: #{from_account.id}",
"to: #{to_account.id}",
"reference: #{reference.class.name}(#{reference&.id})",
"valuta: #{valuta}",
].join(' ')
)
end
if ActsAsAccount.configuration.persist_attributes_on_account
[from_account, to_account].sort_by(&:id).each(&:lock!)
end
posting1 = build_posting(-amount, from_account, to_account, reference, valuta)
posting2 = build_posting( amount, to_account, from_account, reference, valuta)
result = postings.model.insert_all([ posting1.attributes.compact, posting2.attributes.compact ])
update_attributes_on(from_account, -amount)
update_attributes_on(to_account, amount)
!!result
end
end
|