Skip to content

Commit

Permalink
[MIG] account_invoice_show_currency_rate: Migration to V18
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiMForgeFlow committed Jan 3, 2025
1 parent 2aefe7d commit ac1d0a9
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 100 deletions.
16 changes: 13 additions & 3 deletions account_invoice_show_currency_rate/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ Account Invoice Show Currency Rate

|badge1| |badge2| |badge3| |badge4| |badge5|

This module shows the currency rate applied in invoices, so you can
visually verify what is going to be applied for the exchange, or which
one was applied once converted to company currency.
Since V18, Odoo shows the currency rate applied to invoices. Also, it
computes the currency rate on journal items by simply looking at the
rates configured in the system, but it does not display it.

This module shows the currency rate on journal items, useful for other
entries that are not invoices, as well as entries containing lines with
multiple currencies. Furthermore, it ensures that the currency rate on
journal items is based on the actual amount in the journal item's
currency, as this can be manually modified by the user.

**Table of contents**

Expand Down Expand Up @@ -96,6 +102,10 @@ Contributors
- Pedro M. Baeza
- Víctor Martínez

- `ForgeFlow <https://www.forgeflow.com>`__:

- Jordi Masvidal

Maintainers
-----------

Expand Down
2 changes: 1 addition & 1 deletion account_invoice_show_currency_rate/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Account Invoice Show Currency Rate",
"summary": "Show currency rate in invoices.",
"version": "17.0.1.0.0",
"version": "18.0.1.0.0",
"category": "Accounting & Finance",
"website": "https://github.com/OCA/account-invoicing",
"author": "Tecnativa, Odoo Community Association (OCA)",
Expand Down
103 changes: 32 additions & 71 deletions account_invoice_show_currency_rate/models/account_move.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,57 @@
# Copyright 2021 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models
from odoo import api, models


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

currency_rate_amount = fields.Float(
string="Rate amount",
compute="_compute_currency_rate_amount",
digits=0,
)
show_currency_rate_amount = fields.Boolean(
compute="_compute_show_currency_rate_amount", readonly=True
)

@api.depends(
"currency_id",
"company_currency_id",
"company_id",
"invoice_date",
"state",
"date",
"line_ids.amount_currency",
"line_ids.balance",
"company_id",
"currency_id",
"show_currency_rate_amount",
)
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.
"""
self.currency_rate_amount = 1
for item in self.filtered("show_currency_rate_amount"):
lines = item.line_ids.filtered(lambda x: abs(x.amount_currency) > 0)
if item.state == "posted" and lines:
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_balance_positive
)
else:
rates = item.currency_id._get_rates(item.company_id, item.date)
item.currency_rate_amount = rates.get(item.currency_id.id)

@api.depends("currency_id", "currency_id.rate_ids", "company_id")
def _compute_show_currency_rate_amount(self):
for item in self:
item.show_currency_rate_amount = (
item.currency_id and item.currency_id != item.company_id.currency_id
def _compute_invoice_currency_rate(self):
# If move is posted, get rate based on line amount
res = super()._compute_invoice_currency_rate()
for move in self:
lines = move.line_ids.filtered(lambda x: abs(x.amount_currency) > 0)
if move.state != "posted" or not lines or not move.currency_id:
continue
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")])
move.invoice_currency_rate = move.currency_id.round(
amount_currency_positive / total_balance_positive
)
return res


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

currency_rate_amount = fields.Float(
string="Rate amount",
compute="_compute_currency_rate_amount",
digits=0,
)

@api.depends(
"move_id.state",
"currency_id",
"company_id",
"move_id.invoice_currency_rate",
"move_id.date",
"move_id.state",
"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.
"""
self.currency_rate_amount = 1
for item in self:
if (
not item.currency_id
or item.currency_id == item.move_id.company_id.currency_id
):
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
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)
line.currency_rate = line.currency_id.round(
abs(line.amount_currency) / abs(line.balance)
)
return res
2 changes: 2 additions & 0 deletions account_invoice_show_currency_rate/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- [Tecnativa](https://www.tecnativa.com):
- Pedro M. Baeza
- Víctor Martínez
- [ForgeFlow](https://www.forgeflow.com):
- Jordi Masvidal
12 changes: 9 additions & 3 deletions account_invoice_show_currency_rate/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
This module shows the currency rate applied in invoices, so you can
visually verify what is going to be applied for the exchange, or which
one was applied once converted to company currency.
Since V18, Odoo shows the currency rate applied to invoices. Also,
it computes the currency rate on journal items by simply looking at the rates
configured in the system, but it does not display it.

This module shows the currency rate on journal items, useful for other
entries that are not invoices, as well as entries containing lines with multiple
currencies. Furthermore, it ensures that the currency rate on journal items is
based on the actual amount in the journal item's currency, as this can be manually
modified by the user.
15 changes: 12 additions & 3 deletions account_invoice_show_currency_rate/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,14 @@ <h1 class="title">Account Invoice Show Currency Rate</h1>
!! source digest: sha256:9f77ee0ab5b39a670d3a6f6162c64cf595c51ae2dd2782fa01f188cd0fa97dc2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/account-invoicing/tree/18.0/account_invoice_show_currency_rate"><img alt="OCA/account-invoicing" src="https://img.shields.io/badge/github-OCA%2Faccount--invoicing-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/account-invoicing-18-0/account-invoicing-18-0-account_invoice_show_currency_rate"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/account-invoicing&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module shows the currency rate applied in invoices, so you can
visually verify what is going to be applied for the exchange, or which
one was applied once converted to company currency.</p>
<p>Since V18, Odoo shows the currency rate applied to invoices. Also, it
computes the currency rate on journal items by simply looking at the
rates configured in the system, but it does not display it.</p>
<p>This module shows the currency rate on journal items, useful for other
entries that are not invoices, as well as entries containing lines with
multiple currencies. Furthermore, it ensures that the currency rate on
journal items is based on the actual amount in the journal item’s
currency, as this can be manually modified by the user.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand Down Expand Up @@ -444,6 +449,10 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>Víctor Martínez</li>
</ul>
</li>
<li><a class="reference external" href="https://www.forgeflow.com">ForgeFlow</a>:<ul>
<li>Jordi Masvidal</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
16 changes: 8 additions & 8 deletions account_invoice_show_currency_rate/tests/test_account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,20 @@ def _create_invoice(self, currency_id):
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)
self.assertAlmostEqual(invoice.invoice_currency_rate, 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_amount, 2.0, 2)
self.assertAlmostEqual(invoice.invoice_currency_rate, 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_amount, 2.0, 2)
self.assertAlmostEqual(invoice.invoice_currency_rate, 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_amount, 3.0, 2)
self.assertAlmostEqual(invoice.invoice_currency_rate, 3.0, 2)
self.assertAlmostEqual(invoice.line_ids[0].currency_rate, 3.0, 2)
13 changes: 2 additions & 11 deletions account_invoice_show_currency_rate/views/account_move_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,12 @@ License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form" />
<field name="arch" type="xml">
<xpath expr="//div[@name='journal_div']" position="after">
<field name="show_currency_rate_amount" invisible="1" />
<field
name="currency_rate_amount"
groups="base.group_multi_currency"
digits="[12,12]"
invisible="not show_currency_rate_amount"
/>
</xpath>
<xpath
expr="//field[@name='line_ids']/tree/field[@name='currency_id']"
expr="//field[@name='line_ids']/list/field[@name='currency_id']"
position="after"
>
<field
name="currency_rate_amount"
name="currency_rate"
string="Rate"
digits="[12,12]"
groups="base.group_multi_currency"
Expand Down

0 comments on commit ac1d0a9

Please sign in to comment.