From 59053e39b4e475061847e0ba2d6ed9e6374ffaf6 Mon Sep 17 00:00:00 2001 From: "Luis J. Salvatierra" Date: Wed, 21 Dec 2022 17:23:34 +0100 Subject: [PATCH] [MIG] account_invoice_change_currency: Migration to 16.0 --- .../__manifest__.py | 2 +- .../i18n/account_invoice_change_currency.pot | 4 +- .../models/account_move.py | 11 +- .../models/account_move_line.py | 3 +- .../readme/CONTRIBUTORS.rst | 1 + .../test_account_invoice_change_currency.py | 204 +++++------------- .../addons/account_invoice_change_currency | 1 + .../account_invoice_change_currency/setup.py | 6 + 8 files changed, 77 insertions(+), 155 deletions(-) create mode 120000 setup/account_invoice_change_currency/odoo/addons/account_invoice_change_currency create mode 100644 setup/account_invoice_change_currency/setup.py diff --git a/account_invoice_change_currency/__manifest__.py b/account_invoice_change_currency/__manifest__.py index 8881a722a6fb..467dfbf57edb 100644 --- a/account_invoice_change_currency/__manifest__.py +++ b/account_invoice_change_currency/__manifest__.py @@ -2,7 +2,7 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). { "name": "Account Invoice - Change Currency", - "version": "15.0.2.0.0", + "version": "16.0.1.0.0", "category": "Accounting & Finance", "summary": "Allows to change currency of Invoice by wizard", "author": "Vauxoo, Komit Consulting, Odoo Community Association (OCA)", diff --git a/account_invoice_change_currency/i18n/account_invoice_change_currency.pot b/account_invoice_change_currency/i18n/account_invoice_change_currency.pot index b8e3574dc241..2849e27b3f41 100644 --- a/account_invoice_change_currency/i18n/account_invoice_change_currency.pot +++ b/account_invoice_change_currency/i18n/account_invoice_change_currency.pot @@ -4,8 +4,10 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 15.0\n" +"Project-Id-Version: Odoo Server 16.0\n" "Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2022-10-11 06:39+0000\n" +"PO-Revision-Date: 2022-10-11 06:39+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" diff --git a/account_invoice_change_currency/models/account_move.py b/account_invoice_change_currency/models/account_move.py index bd7322c74864..1fdbd4a42148 100644 --- a/account_invoice_change_currency/models/account_move.py +++ b/account_invoice_change_currency/models/account_move.py @@ -28,10 +28,12 @@ class AccountMove(models.Model): "This is used to hide custom rate field in the form view.", ) - @api.model - def create(self, values): - values.setdefault("original_currency_id", values.get("currency_id")) - return super().create(values) + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + if "currency_id" in vals and not vals.get("original_currency_id", False): + vals["original_currency_id"] = vals["currency_id"] + return super().create(vals_list) def action_account_change_currency(self): """ @@ -58,7 +60,6 @@ def action_account_change_currency(self): invoice.company_id, invoice_date, ) - invoice._recompute_dynamic_lines(recompute_all_taxes=True) @api.depends("company_id", "currency_id", "invoice_date") def _compute_currency_change_rate(self): diff --git a/account_invoice_change_currency/models/account_move_line.py b/account_invoice_change_currency/models/account_move_line.py index 27a4e156ba1f..9e763fdd94f6 100644 --- a/account_invoice_change_currency/models/account_move_line.py +++ b/account_invoice_change_currency/models/account_move_line.py @@ -16,7 +16,8 @@ class AccountMoveLine(models.Model): @api.model_create_multi def create(self, vals_list): for vals in vals_list: - vals.setdefault("original_price_unit", vals.get("price_unit")) + if "price_unit" in vals: + vals["original_price_unit"] = vals["price_unit"] return super().create(vals_list) def _set_original_price_unit(self): diff --git a/account_invoice_change_currency/readme/CONTRIBUTORS.rst b/account_invoice_change_currency/readme/CONTRIBUTORS.rst index 08d7fb7c2ee3..050685e4b1a4 100644 --- a/account_invoice_change_currency/readme/CONTRIBUTORS.rst +++ b/account_invoice_change_currency/readme/CONTRIBUTORS.rst @@ -2,3 +2,4 @@ * Hugo Adan * Saran Lim. * Rolando Duarte +* Luis J. Salvatierra (https://factorlibre.com) diff --git a/account_invoice_change_currency/tests/test_account_invoice_change_currency.py b/account_invoice_change_currency/tests/test_account_invoice_change_currency.py index 0eaa8df352ff..940dddd92020 100644 --- a/account_invoice_change_currency/tests/test_account_invoice_change_currency.py +++ b/account_invoice_change_currency/tests/test_account_invoice_change_currency.py @@ -1,110 +1,31 @@ # Copyright 2018 Komit # 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="accountmanager@yourcompany.com", - 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( diff --git a/setup/account_invoice_change_currency/odoo/addons/account_invoice_change_currency b/setup/account_invoice_change_currency/odoo/addons/account_invoice_change_currency new file mode 120000 index 000000000000..b34414a4b157 --- /dev/null +++ b/setup/account_invoice_change_currency/odoo/addons/account_invoice_change_currency @@ -0,0 +1 @@ +../../../../account_invoice_change_currency \ No newline at end of file diff --git a/setup/account_invoice_change_currency/setup.py b/setup/account_invoice_change_currency/setup.py new file mode 100644 index 000000000000..28c57bb64031 --- /dev/null +++ b/setup/account_invoice_change_currency/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)