Skip to content

Commit

Permalink
Merge PR #1894 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by AaronHForgeFlow
  • Loading branch information
OCA-git-bot committed Feb 3, 2025
2 parents b9a61c5 + 9b84c46 commit 30303ba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
34 changes: 30 additions & 4 deletions account_invoice_show_currency_rate/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AccountMove(models.Model):
"state",
"date",
"line_ids.amount_currency",
"line_ids.balance",
"company_id",
"currency_id",
"show_currency_rate_amount",
Expand All @@ -32,12 +33,14 @@ def _compute_currency_rate_amount(self):
"""
self.currency_rate_amount = 1
for item in self.filtered("show_currency_rate_amount"):
lines = item.line_ids.filtered(lambda x: x.amount_currency > 0)
lines = item.line_ids.filtered(lambda x: abs(x.amount_currency) > 0)
if item.state == "posted" and lines:
amount_currency_positive = sum(lines.mapped("amount_currency"))
total_debit = sum(lines.mapped("debit"))
amount_currency_positive = sum(
[abs(amc) for amc in lines.mapped("amount_currency")]
)
total_balance_positive = sum([abs(b) for b in lines.mapped("balance")])
item.currency_rate_amount = item.currency_id.round(
amount_currency_positive / total_debit
amount_currency_positive / total_balance_positive
)
else:
rates = item.currency_id._get_rates(item.company_id, item.date)
Expand All @@ -49,3 +52,26 @@ def _compute_show_currency_rate_amount(self):
item.show_currency_rate_amount = (
item.currency_id and item.currency_id != item.company_id.currency_id
)


class AccountMoveLine(models.Model):
_inherit = "account.move.line"

@api.depends(
"currency_id",
"move_id.company_id",
"move_id.date",
"move_id.state",
"amount_currency",
"balance",
)
def _compute_currency_rate(self):
# If move is posted, get rate based on line amount
res = super()._compute_currency_rate()
for line in self:
if line.move_id.state != "posted" or not line.amount_currency:
continue
line.currency_rate = line.currency_id.round(
abs(line.amount_currency) / abs(line.balance)
)
return res
4 changes: 4 additions & 0 deletions account_invoice_show_currency_rate/tests/test_account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,19 @@ def test_01_invoice_currency(self):
self.partner.property_product_pricelist = self.pricelist_currency
invoice = self._create_invoice(self.currency)
self.assertAlmostEqual(invoice.currency_rate_amount, 1.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate, 1.0, 2)

def test_02_invoice_currency_extra(self):
self.partner.property_product_pricelist = self.pricelist_currency_extra
invoice = self._create_invoice(self.currency_extra)
self.assertAlmostEqual(invoice.currency_rate_amount, 2.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate, 2.0, 2)
rate_custom = self.currency_extra.rate_ids.filtered(
lambda x: x.name == fields.Date.from_string("2000-01-01")
)
rate_custom.rate = 3.0
self.assertAlmostEqual(invoice.currency_rate_amount, 2.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate, 2.0, 2)
invoice.button_draft()
self.assertAlmostEqual(invoice.currency_rate_amount, 3.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate, 3.0, 2)

0 comments on commit 30303ba

Please sign in to comment.