Skip to content

Commit db98d77

Browse files
[IMP] Add currency change when loan/journal and company are not on the same currency
1 parent 44af31a commit db98d77

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

account_loan/models/account_loan_line.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,19 @@ def _add_basic_values(self, vals, account):
283283
"account_id": (account and account.id)
284284
or partner.property_account_payable_id.id,
285285
"partner_id": partner.id,
286-
"credit": self.payment_amount,
287286
"debit": 0,
287+
"currency_id": self.loan_id.currency_id.id,
288+
"credit": self.loan_id.journal_id.currency_id._convert(
289+
from_amount=self.payment_amount,
290+
to_currency=self.loan_id.company_id.currency_id,
291+
company=self.loan_id.company_id,
292+
date=self.date,
293+
round=False,
294+
),
295+
"amount_currency": -self.payment_amount,
288296
}
289297
)
298+
290299
return vals
291300

292301
def _add_interests_values(self, vals):
@@ -295,7 +304,15 @@ def _add_interests_values(self, vals):
295304
{
296305
"account_id": self.loan_id.interest_expenses_account_id.id,
297306
"credit": 0,
298-
"debit": self.interests_amount,
307+
"currency_id": self.loan_id.currency_id.id,
308+
"debit": self.loan_id.journal_id.currency_id._convert(
309+
from_amount=self.interests_amount,
310+
to_currency=self.loan_id.company_id.currency_id,
311+
company=self.loan_id.company_id,
312+
date=self.date,
313+
round=False,
314+
),
315+
"amount_currency": self.interests_amount,
299316
}
300317
)
301318
return vals
@@ -306,7 +323,15 @@ def _add_short_term_account_values(self, vals):
306323
{
307324
"account_id": self.loan_id.short_term_loan_account_id.id,
308325
"credit": 0,
309-
"debit": self.payment_amount - self.interests_amount,
326+
"currency_id": self.loan_id.currency_id.id,
327+
"debit": self.loan_id.journal_id.currency_id._convert(
328+
from_amount=self.payment_amount - self.interests_amount,
329+
to_currency=self.loan_id.company_id.currency_id,
330+
company=self.loan_id.company_id,
331+
date=self.date,
332+
round=False,
333+
),
334+
"amount_currency": self.payment_amount - self.interests_amount,
310335
}
311336
)
312337
return vals
@@ -317,15 +342,31 @@ def _add_long_term_account_values(self, vals):
317342
vals.append(
318343
{
319344
"account_id": self.loan_id.short_term_loan_account_id.id,
320-
"credit": self.long_term_principal_amount,
321345
"debit": 0,
346+
"currency_id": self.loan_id.currency_id.id,
347+
"credit": self.loan_id.journal_id.currency_id._convert(
348+
from_amount=self.long_term_principal_amount,
349+
to_currency=self.loan_id.company_id.currency_id,
350+
company=self.loan_id.company_id,
351+
date=self.date,
352+
round=False,
353+
),
354+
"amount_currency": -self.long_term_principal_amount,
322355
}
323356
)
324357
vals.append(
325358
{
326-
"account_id": self.long_term_loan_account_id.id,
359+
"account_id": self.loan_id.short_term_loan_account_id.id,
327360
"credit": 0,
328-
"debit": self.long_term_principal_amount,
361+
"currency_id": self.loan_id.currency_id.id,
362+
"debit": self.loan_id.journal_id.currency_id._convert(
363+
from_amount=self.long_term_principal_amount,
364+
to_currency=self.loan_id.company_id.currency_id,
365+
company=self.loan_id.company_id,
366+
date=self.date,
367+
round=False,
368+
),
369+
"amount_currency": self.long_term_principal_amount,
329370
}
330371
)
331372
return vals

account_loan/tests/test_loan.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,33 @@ def create_loan(self, type_loan, amount, rate, periods):
617617
)
618618
loan.compute_lines()
619619
return loan
620+
621+
def test_change_currency(self):
622+
loan = self.create_loan("fixed-annuity", 500000, 1, 60)
623+
eur_currency = self.env.ref("base.EUR")
624+
usd_currency = self.env.ref("base.USD")
625+
loan.journal_id.currency_id = eur_currency
626+
loan.currency_id = eur_currency
627+
loan.company_id.currency_id = usd_currency
628+
self.assertEqual(loan.currency_id, loan.journal_id.currency_id)
629+
loan.compute_lines()
630+
for line in loan.line_ids:
631+
self.assertEqual(line.currency_id, loan.currency_id)
632+
633+
line = loan.line_ids.filtered(lambda r: r.sequence == 1)
634+
line.view_process_values()
635+
636+
move_lines = line.mapped("move_ids.line_ids")
637+
# Credit/Debit lines should be in USD, while amount_currency is in EUR
638+
self.assertEqual(
639+
round(move_lines[0].credit, 2),
640+
round(-move_lines[0].amount_currency / eur_currency.rate, 2),
641+
)
642+
self.assertEqual(
643+
round(move_lines[1].debit, 2),
644+
round(move_lines[1].amount_currency / eur_currency.rate, 2),
645+
)
646+
self.assertEqual(
647+
round(move_lines[2].debit, 2),
648+
round(move_lines[2].amount_currency / eur_currency.rate, 2),
649+
)

0 commit comments

Comments
 (0)