-
-
Notifications
You must be signed in to change notification settings - Fork 692
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_change_currency: Migration to 16.0
- Loading branch information
1 parent
8f2b793
commit 3331af5
Showing
8 changed files
with
77 additions
and
155 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
* Hugo Adan <[email protected]> | ||
* Saran Lim. <[email protected]> | ||
* Rolando Duarte <[email protected]> | ||
* Luis J. Salvatierra <[email protected]> (https://factorlibre.com) |
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,110 +1,31 @@ | ||
# Copyright 2018 Komit <http://komit-consulting.com> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). | ||
import odoo.tests.common as common | ||
from odoo import fields | ||
from odoo.tests import tagged | ||
from odoo.tools import float_compare | ||
|
||
from odoo.addons.account.tests.common import AccountTestInvoicingCommon | ||
|
||
class TestAccountInvoiceChangeCurrency(common.TransactionCase): | ||
def setUp(self): | ||
super().setUp() | ||
|
||
self.precision = self.env["decimal.precision"].precision_get("Payment Terms") | ||
res_users_account_manager = self.env.ref("account.group_account_manager") | ||
self.manager = ( | ||
self.env["res.users"] | ||
.with_context(no_reset_password=True) | ||
.create( | ||
dict( | ||
name="Adviser", | ||
company_id=self.env.ref("base.main_company").id, | ||
login="fm", | ||
email="[email protected]", | ||
groups_id=[(6, 0, [res_users_account_manager.id])], | ||
) | ||
) | ||
) | ||
|
||
# Needed to create invoice | ||
self.account_type = self.env["account.account.type"].create( | ||
{ | ||
"name": "acc type test 2", | ||
"type": "other", | ||
"include_initial_balance": True, | ||
"internal_group": "asset", | ||
} | ||
) | ||
self.account_account_line = self.env["account.account"].create( | ||
{ | ||
"name": "acc inv line test", | ||
"code": "X2021", | ||
"user_type_id": self.account_type.id, | ||
"reconcile": True, | ||
} | ||
) | ||
self.product_1 = self.env["product.product"].create({"name": "Product 1"}) | ||
self.product_2 = self.env["product.product"].create({"name": "Product 2"}) | ||
self.analytic_account = self.env["account.analytic.account"].create( | ||
{"name": "test account"} | ||
) | ||
self.tax_account = self.env["account.account"].search( | ||
[ | ||
( | ||
"user_type_id", | ||
"=", | ||
self.env.ref("account.data_account_type_current_assets").id, | ||
) | ||
], | ||
limit=1, | ||
) | ||
|
||
def create_simple_invoice(self, date=False, context=None, inv_type=None): | ||
if not context: | ||
context = {} | ||
context["default_move_type"] = True | ||
invoice_lines = [ | ||
( | ||
0, | ||
0, | ||
{ | ||
"product_id": self.product_1.id, | ||
"quantity": 5.0, | ||
"price_unit": 142.0, | ||
"name": "Product that cost 142", | ||
"account_id": self.account_account_line.id, | ||
}, | ||
), | ||
( | ||
0, | ||
0, | ||
{ | ||
"product_id": self.product_2.id, | ||
"quantity": 4.0, | ||
"price_unit": 213.0, | ||
"name": "Product that cost 213", | ||
"account_id": self.account_account_line.id, | ||
}, | ||
), | ||
] | ||
invoice = ( | ||
self.env["account.move"] | ||
.with_user(self.manager) | ||
.with_context(**context) | ||
.create( | ||
{ | ||
"partner_id": 1, | ||
"move_type": inv_type or "in_invoice", | ||
"invoice_date": date, | ||
"currency_id": self.env.ref("base.EUR").id, | ||
"invoice_line_ids": invoice_lines, | ||
"state": "draft", | ||
} | ||
) | ||
) | ||
return invoice | ||
@tagged("post_install", "-at_install") | ||
class TestAccountInvoiceChangeCurrency(AccountTestInvoicingCommon): | ||
@classmethod | ||
def setUpClass(cls, chart_template_ref=None): | ||
super().setUpClass(chart_template_ref=chart_template_ref) | ||
decimal_precision_name = ( | ||
cls.env["account.move.line"]._fields["price_unit"]._digits | ||
) | ||
decimal_precision = cls.env["decimal.precision"].search( | ||
[("name", "=", decimal_precision_name)] | ||
) | ||
cls.precision = decimal_precision.digits | ||
|
||
def test_change_invoice_currency(self): | ||
inv = self.create_simple_invoice(fields.Date.today()) | ||
inv = self.init_invoice( | ||
"out_invoice", | ||
products=self.product_a + self.product_b, | ||
invoice_date=fields.Date.today(), | ||
) | ||
before_curr = inv.currency_id | ||
before_amount = inv.amount_total | ||
after_curr = self.env.ref("base.USD") | ||
|
@@ -120,7 +41,11 @@ def test_change_invoice_currency(self): | |
) | ||
|
||
def test_change_validated_invoice_currency(self): | ||
inv = self.create_simple_invoice(fields.Date.today()) | ||
inv = self.init_invoice( | ||
"out_invoice", | ||
products=self.product_a + self.product_b, | ||
invoice_date=fields.Date.today(), | ||
) | ||
before_amount = inv.amount_total | ||
inv.action_post() | ||
# Make sure that we can not change the currency after validated: | ||
|
@@ -133,7 +58,11 @@ def test_change_validated_invoice_currency(self): | |
) | ||
|
||
def test_create_invoice_update_currency(self): | ||
inv = self.create_simple_invoice() | ||
inv = self.init_invoice( | ||
"out_invoice", | ||
products=self.product_a + self.product_b, | ||
invoice_date=fields.Date.today(), | ||
) | ||
before_amount = inv.amount_total | ||
inv.action_account_change_currency() | ||
self.assertEqual( | ||
|
@@ -143,66 +72,49 @@ def test_create_invoice_update_currency(self): | |
) | ||
|
||
def test_custom_rate_update_currency(self): | ||
inv = self.create_simple_invoice(fields.Date.today()) | ||
inv = self.init_invoice( | ||
"out_invoice", | ||
products=self.product_a + self.product_b, | ||
invoice_date=fields.Date.today(), | ||
) | ||
before_amount = inv.amount_total | ||
after_curr = self.env.ref("base.USD") | ||
self.assertEqual(inv.original_currency_id, self.env.ref("base.USD")) | ||
after_curr = self.env.ref("base.EUR") | ||
custom_rate = 1.13208 | ||
inv.write({"currency_id": after_curr.id, "custom_rate": custom_rate}) | ||
inv.write({"custom_rate": custom_rate}) | ||
inv.action_account_change_currency() | ||
expected_value = before_amount * custom_rate | ||
# TODO: Check float comparation, 12013.64 vs 12013.632959999999 | ||
self.assertEqual( | ||
float_compare(inv.amount_total, expected_value, self.precision), | ||
1, | ||
0, | ||
"Total amount of invoice does not match to the expected value", | ||
) | ||
|
||
def test_custom_rate_zero_update_currency(self): | ||
inv = self.create_simple_invoice() | ||
inv = self.init_invoice( | ||
"out_invoice", | ||
products=self.product_a + self.product_b, | ||
invoice_date=fields.Date.today(), | ||
) | ||
before_amount = inv.amount_total | ||
before_curr = inv.currency_id | ||
custom_rate = 0.0 | ||
usd = self.env.ref("base.USD") | ||
eur = self.env.ref("base.EUR") | ||
inv.write({"currency_id": usd.id, "custom_rate": custom_rate}) | ||
inv.write({"custom_rate": custom_rate}) | ||
inv.action_account_change_currency() | ||
expected_value = before_curr._convert( | ||
before_amount, usd, inv.company_id, fields.Date.today() | ||
) | ||
# Comparison 2004.64 vs 2004.67 | ||
self.assertEqual( | ||
float_compare(inv.amount_total, expected_value, 0), | ||
float_compare(inv.amount_total, expected_value, self.precision), | ||
0, | ||
"Total amount of invoice does not match to the expected value", | ||
) | ||
# Change currency and set custom rate 0 | ||
inv.write({"currency_id": eur.id, "custom_rate": custom_rate}) | ||
inv.write({"custom_rate": custom_rate}) | ||
inv.action_account_change_currency() | ||
self.assertEqual( | ||
inv.amount_total, | ||
before_amount, | ||
"Total amount of invoice does not match to the expected value", | ||
) | ||
# Change Again custom rate with old_rate but now without new currency | ||
inv.write({"custom_rate": custom_rate}) | ||
inv.action_account_change_currency() | ||
expected_value = before_curr._convert( | ||
before_amount, eur, inv.company_id, fields.Date.today() | ||
) | ||
self.assertEqual( | ||
inv.amount_total, | ||
expected_value, | ||
"Total amount of invoice does not match to the expected value", | ||
) | ||
# Custom rate with 0 but now without new currency | ||
inv.write({"custom_rate": custom_rate}) | ||
inv.action_account_change_currency() | ||
expected_value = before_curr._convert( | ||
before_amount, eur, inv.company_id, fields.Date.today() | ||
) | ||
self.assertEqual( | ||
inv.amount_total, | ||
before_amount, | ||
|
@@ -224,11 +136,10 @@ def test_custom_rate_zero_update_currency(self): | |
rate = usd_updated_rate.rate | ||
inv.write({"custom_rate": rate}) | ||
inv.action_account_change_currency() | ||
expected_value = before_amount | ||
# TODO: Check float comparation, 1562.0 vs 1562.0 | ||
expected_value = before_amount * rate | ||
self.assertEqual( | ||
inv.amount_total, | ||
expected_value, | ||
float_compare(inv.amount_total, expected_value, 1), | ||
0, | ||
"Total amount of invoice does not match to the expected value", | ||
) | ||
# change custom rate then we trigger the conversion 2 times | ||
|
@@ -250,30 +161,25 @@ def test_custom_rate_zero_update_currency(self): | |
rate = old_rate + 1 | ||
inv.write({"custom_rate": rate}) | ||
inv.action_account_change_currency() | ||
expected_value = before_amount * rate / old_rate | ||
# TODO: Check float comparation, 4107.93 vs 4107.946074605804 | ||
expected_value = before_amount | ||
self.assertEqual( | ||
float_compare(inv.amount_total, expected_value, 1), | ||
0, | ||
"Total amount of invoice does not match to the expected value", | ||
) | ||
inv.action_account_change_currency() | ||
# TODO: Check float comparation, 4107.93 vs 4107.946074605804 | ||
self.assertEqual( | ||
float_compare(inv.amount_total, expected_value, 1), | ||
0, | ||
"Total amount of invoice does not match to the expected value", | ||
) | ||
# Test if there are no currency | ||
inv.write({"currency_id": False}) | ||
self.assertEqual( | ||
inv.custom_rate, | ||
1.0, | ||
"Custom rate of invoice does not match to the expected value", | ||
) | ||
|
||
def test_not_currency_change(self): | ||
inv = self.create_simple_invoice(inv_type="out_invoice") | ||
inv = self.init_invoice( | ||
"out_invoice", | ||
products=self.product_a + self.product_b, | ||
invoice_date=fields.Date.today(), | ||
) | ||
before_amount = inv.amount_total | ||
inv.action_account_change_currency() | ||
self.assertEqual( | ||
|
@@ -284,7 +190,11 @@ def test_not_currency_change(self): | |
|
||
def test_old_invoices(self): | ||
# This simulate an old invoice (no stored original values) | ||
inv = self.create_simple_invoice(fields.Date.today()) | ||
inv = self.init_invoice( | ||
"out_invoice", | ||
products=self.product_a + self.product_b, | ||
invoice_date=fields.Date.today(), | ||
) | ||
inv.write({"original_currency_id": False}) | ||
inv.invoice_line_ids.write({"original_price_unit": False}) | ||
self.assertFalse( | ||
|
1 change: 1 addition & 0 deletions
1
setup/account_invoice_change_currency/odoo/addons/account_invoice_change_currency
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../account_invoice_change_currency |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import setuptools | ||
|
||
setuptools.setup( | ||
setup_requires=['setuptools-odoo'], | ||
odoo_addon=True, | ||
) |