Skip to content

Commit

Permalink
[MIG] account_invoice_change_currency: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ljsalvatierra-factorlibre committed May 26, 2023
1 parent 8f2b793 commit 3331af5
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 155 deletions.
2 changes: 1 addition & 1 deletion account_invoice_change_currency/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions account_invoice_change_currency/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion account_invoice_change_currency/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions account_invoice_change_currency/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
* Hugo Adan <[email protected]>
* Saran Lim. <[email protected]>
* Rolando Duarte <[email protected]>
* Luis J. Salvatierra <[email protected]> (https://factorlibre.com)
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")
Expand All @@ -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:
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions setup/account_invoice_change_currency/setup.py
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,
)

0 comments on commit 3331af5

Please sign in to comment.