-
-
Notifications
You must be signed in to change notification settings - Fork 698
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIG] account_invoice_show_currency_rate: Migration to V18
- Loading branch information
1 parent
2aefe7d
commit ac1d0a9
Showing
8 changed files
with
79 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 32 additions & 71 deletions
103
account_invoice_show_currency_rate/models/account_move.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters