Skip to content

Commit

Permalink
Merge PR #1838 into 13.0
Browse files Browse the repository at this point in the history
Signed-off-by AaronHForgeFlow
  • Loading branch information
OCA-git-bot committed Jan 27, 2025
2 parents b7ad0cc + 39d0a39 commit 19a6ba4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
58 changes: 54 additions & 4 deletions account_invoice_show_currency_rate/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class AccountMove(models.Model):
"state",
"date",
"line_ids.amount_currency",
"line_ids.balance",
"company_id",
"currency_id",
"show_currency_rate_amount",
Expand All @@ -30,12 +31,16 @@ 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 and x.date
)
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 @@ -47,3 +52,48 @@ 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"

# TODO: This field should be removed in V16 as it is already
# added by Odoo in the account module
currency_rate_amount = fields.Float(
string="Rate amount", compute="_compute_currency_rate_amount", digits=0,
)

@api.depends(
"move_id.state",
"move_id.date",
"amount_currency",
"balance",
"move_id.company_id",
"currency_id",
)
def _compute_currency_rate_amount(self):
""" It's necessary to define value according to some cases:
- Case A: Currency is equal to company currency (Value = 1)
- Case B: Move exist previously (posted) and get real rate according to lines
- Case C: Get expected rate (according to date) to show some value in creation.
"""
# TODO: This method should be removed in V16 as the logic is
# already added by Odoo in the account module
self.currency_rate_amount = 1
for item in self:
if (
not item.currency_id
or not item.move_id.date
or item.currency_id == item.move_id.company_id.currency_id
):
continue
amount_currency = abs(item.amount_currency)
if item.move_id.state == "posted" and amount_currency > 0:
item.currency_rate_amount = item.currency_id.round(
amount_currency / abs(item.balance)
)
else:
rates = item.currency_id._get_rates(
item.move_id.company_id, item.move_id.date
)
item.currency_rate_amount = rates.get(item.currency_id.id)
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 @@ -78,15 +78,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_amount, 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_amount, 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_amount, 2.0, 2)
invoice.button_draft()
self.assertAlmostEqual(invoice.currency_rate_amount, 3.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate_amount, 3.0, 2)
12 changes: 12 additions & 0 deletions account_invoice_show_currency_rate/views/account_move_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
attrs="{'invisible':[('show_currency_rate_amount', '=', False)]}"
/>
</field>
<xpath
expr="//field[@name='line_ids']/tree/field[@name='currency_id']"
position="after"
>
<field
name="currency_rate_amount"
string="Rate"
digits="[12,12]"
groups="base.group_multi_currency"
optional="hide"
/>
</xpath>
</field>
</record>
</odoo>

0 comments on commit 19a6ba4

Please sign in to comment.