Skip to content

Commit 22c8c44

Browse files
[IMP] Currency not readonly + convert in company currency
1. We allow to change the currency on the loan if the user needs it 2. If the currency on the loan (or on the journal) is different from the one on the company, we do the conversion for credit/debit + add amount in currency on the move line
1 parent 69debbf commit 22c8c44

File tree

3 files changed

+129
-13
lines changed

3 files changed

+129
-13
lines changed

account_loan/models/account_loan_line.py

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,41 @@ def _check_move_amount(self):
249249
long_term_moves = self.move_ids.mapped("line_ids").filtered(
250250
lambda r: r.account_id == self.loan_id.long_term_loan_account_id
251251
)
252-
self.interests_amount = sum(interests_moves.mapped("debit")) - sum(
252+
interests_in_company_currency = sum(interests_moves.mapped("debit")) - sum(
253253
interests_moves.mapped("credit")
254254
)
255-
self.long_term_principal_amount = sum(long_term_moves.mapped("debit")) - sum(
256-
long_term_moves.mapped("credit")
255+
self.interests_amount = self.loan_id.company_id.currency_id._convert(
256+
from_amount=interests_in_company_currency,
257+
to_currency=self.loan_id.currency_id,
258+
company=self.loan_id.company_id,
259+
date=self.date,
260+
round=False,
257261
)
258-
self.payment_amount = (
262+
263+
long_term_principal_amount_in_company_currency = sum(
264+
long_term_moves.mapped("debit")
265+
) - sum(long_term_moves.mapped("credit"))
266+
self.long_term_principal_amount = self.loan_id.company_id.currency_id._convert(
267+
from_amount=long_term_principal_amount_in_company_currency,
268+
to_currency=self.loan_id.currency_id,
269+
company=self.loan_id.company_id,
270+
date=self.date,
271+
round=False,
272+
)
273+
274+
payment_amount_in_company_currency = (
259275
sum(short_term_moves.mapped("debit"))
260276
- sum(short_term_moves.mapped("credit"))
261277
+ self.long_term_principal_amount
262278
+ self.interests_amount
263279
)
280+
self.payment_amount = self.loan_id.company_id.currency_id._convert(
281+
from_amount=payment_amount_in_company_currency,
282+
to_currency=self.loan_id.currency_id,
283+
company=self.loan_id.company_id,
284+
date=self.date,
285+
round=False,
286+
)
264287

265288
def _move_vals(self, journal=False, account=False):
266289
self.ensure_one()
@@ -283,10 +306,19 @@ def _add_basic_values(self, vals, account):
283306
"account_id": (account and account.id)
284307
or partner.property_account_payable_id.id,
285308
"partner_id": partner.id,
286-
"credit": self.payment_amount,
287309
"debit": 0,
310+
"currency_id": self.loan_id.currency_id.id,
311+
"credit": self.loan_id.currency_id._convert(
312+
from_amount=self.payment_amount,
313+
to_currency=self.loan_id.company_id.currency_id,
314+
company=self.loan_id.company_id,
315+
date=self.date,
316+
round=False,
317+
),
318+
"amount_currency": -self.payment_amount,
288319
}
289320
)
321+
290322
return vals
291323

292324
def _add_interests_values(self, vals):
@@ -295,7 +327,15 @@ def _add_interests_values(self, vals):
295327
{
296328
"account_id": self.loan_id.interest_expenses_account_id.id,
297329
"credit": 0,
298-
"debit": self.interests_amount,
330+
"currency_id": self.loan_id.currency_id.id,
331+
"debit": self.loan_id.currency_id._convert(
332+
from_amount=self.interests_amount,
333+
to_currency=self.loan_id.company_id.currency_id,
334+
company=self.loan_id.company_id,
335+
date=self.date,
336+
round=False,
337+
),
338+
"amount_currency": self.interests_amount,
299339
}
300340
)
301341
return vals
@@ -306,7 +346,15 @@ def _add_short_term_account_values(self, vals):
306346
{
307347
"account_id": self.loan_id.short_term_loan_account_id.id,
308348
"credit": 0,
309-
"debit": self.payment_amount - self.interests_amount,
349+
"currency_id": self.loan_id.currency_id.id,
350+
"debit": self.loan_id.currency_id._convert(
351+
from_amount=self.payment_amount - self.interests_amount,
352+
to_currency=self.loan_id.company_id.currency_id,
353+
company=self.loan_id.company_id,
354+
date=self.date,
355+
round=False,
356+
),
357+
"amount_currency": self.payment_amount - self.interests_amount,
310358
}
311359
)
312360
return vals
@@ -317,15 +365,31 @@ def _add_long_term_account_values(self, vals):
317365
vals.append(
318366
{
319367
"account_id": self.loan_id.short_term_loan_account_id.id,
320-
"credit": self.long_term_principal_amount,
321368
"debit": 0,
369+
"currency_id": self.loan_id.currency_id.id,
370+
"credit": self.loan_id.currency_id._convert(
371+
from_amount=self.long_term_principal_amount,
372+
to_currency=self.loan_id.company_id.currency_id,
373+
company=self.loan_id.company_id,
374+
date=self.date,
375+
round=False,
376+
),
377+
"amount_currency": -self.long_term_principal_amount,
322378
}
323379
)
324380
vals.append(
325381
{
326382
"account_id": self.long_term_loan_account_id.id,
327383
"credit": 0,
328-
"debit": self.long_term_principal_amount,
384+
"currency_id": self.loan_id.currency_id.id,
385+
"debit": self.loan_id.currency_id._convert(
386+
from_amount=self.long_term_principal_amount,
387+
to_currency=self.loan_id.company_id.currency_id,
388+
company=self.loan_id.company_id,
389+
date=self.date,
390+
round=False,
391+
),
392+
"amount_currency": self.long_term_principal_amount,
329393
}
330394
)
331395
return vals

account_loan/tests/test_loan.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,30 @@ 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 = fields.first(loan.line_ids)
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.assertAlmostEqual(
639+
move_lines[0].credit, -move_lines[0].amount_currency / eur_currency.rate, 2
640+
)
641+
self.assertAlmostEqual(
642+
move_lines[1].debit, move_lines[1].amount_currency / eur_currency.rate, 2
643+
)
644+
self.assertAlmostEqual(
645+
move_lines[2].debit, move_lines[2].amount_currency / eur_currency.rate, 2
646+
)

account_loan/wizards/account_loan_post.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,35 @@ def move_line_vals(self):
4848
"name": self.loan_id.name,
4949
"partner_id": partner.id,
5050
"credit": 0,
51-
"debit": line.pending_principal_amount,
51+
"currency_id": self.loan_id.currency_id.id,
52+
"amount_currency": line.pending_principal_amount,
53+
"debit": self.loan_id.currency_id._convert(
54+
from_amount=line.pending_principal_amount,
55+
to_currency=self.loan_id.company_id.currency_id,
56+
company=self.loan_id.company_id,
57+
date=fields.Date.today(),
58+
round=False,
59+
),
5260
}
5361
)
5462
if line.pending_principal_amount - line.long_term_pending_principal_amount > 0:
5563
res.append(
5664
{
5765
"account_id": self.loan_id.short_term_loan_account_id.id,
58-
"credit": (
66+
"debit": 0,
67+
"currency_id": self.loan_id.currency_id.id,
68+
"amount_currency": -(
5969
line.pending_principal_amount
6070
- line.long_term_pending_principal_amount
6171
),
62-
"debit": 0,
72+
"credit": self.loan_id.currency_id._convert(
73+
from_amount=line.pending_principal_amount
74+
- line.long_term_pending_principal_amount,
75+
to_currency=self.loan_id.company_id.currency_id,
76+
company=self.loan_id.company_id,
77+
date=fields.Date.today(),
78+
round=False,
79+
),
6380
}
6481
)
6582
if (
@@ -69,8 +86,16 @@ def move_line_vals(self):
6986
res.append(
7087
{
7188
"account_id": self.loan_id.long_term_loan_account_id.id,
72-
"credit": line.long_term_pending_principal_amount,
7389
"debit": 0,
90+
"currency_id": self.loan_id.currency_id.id,
91+
"amount_currency": -line.long_term_pending_principal_amount,
92+
"credit": self.loan_id.currency_id._convert(
93+
from_amount=line.long_term_pending_principal_amount,
94+
to_currency=self.loan_id.company_id.currency_id,
95+
company=self.loan_id.company_id,
96+
date=fields.Date.today(),
97+
round=False,
98+
),
7499
}
75100
)
76101

0 commit comments

Comments
 (0)