diff --git a/account_invoice_pricelist/__manifest__.py b/account_invoice_pricelist/__manifest__.py index 82c9a50b3a9..72b2c74e773 100644 --- a/account_invoice_pricelist/__manifest__.py +++ b/account_invoice_pricelist/__manifest__.py @@ -2,7 +2,7 @@ { "name": "Account - Pricelist on Invoices", - "version": "14.0.1.0.0", + "version": "15.0.1.0.0", "summary": "Add partner pricelist on invoices", "category": "Accounting & Finance", "author": "GRAP," "Therp BV," "Tecnativa," "Odoo Community Association (OCA)", diff --git a/account_invoice_pricelist/models/account_move.py b/account_invoice_pricelist/models/account_move.py index a1d3da3a14e..4e3894589b0 100644 --- a/account_invoice_pricelist/models/account_move.py +++ b/account_invoice_pricelist/models/account_move.py @@ -75,31 +75,31 @@ def _onchange_product_id_account_invoice_pricelist(self): @api.onchange("product_uom_id") def _onchange_uom_id(self): - for sel in self: - if ( - sel.move_id.is_invoice() - and sel.move_id.state == "draft" - and sel.move_id.pricelist_id - ): - price_unit = sel._get_computed_price_unit() - taxes = sel._get_computed_taxes() - if taxes and sel.move_id.fiscal_position_id: - price_subtotal = sel._get_price_total_and_subtotal( - price_unit=price_unit, taxes=taxes - )["price_subtotal"] - accounting_vals = sel._get_fields_onchange_subtotal( - price_subtotal=price_subtotal, - currency=self.move_id.company_currency_id, - ) - amount_currency = accounting_vals["amount_currency"] - price_unit = sel._get_fields_onchange_balance( - amount_currency=amount_currency - ).get("price_unit", price_unit) - sel.with_context(check_move_validity=False).update( - {"price_unit": price_unit} + if ( + self.move_id.is_invoice() + and self.move_id.state == "draft" + and self.move_id.pricelist_id + ): + price_unit = self._get_computed_price_unit() + taxes = self._get_computed_taxes() + if taxes and self.move_id.fiscal_position_id: + price_subtotal = self._get_price_total_and_subtotal( + price_unit=price_unit, taxes=taxes + )["price_subtotal"] + accounting_vals = self._get_fields_onchange_subtotal( + price_subtotal=price_subtotal, + currency=self.move_id.company_currency_id, ) - else: - super(AccountMoveLine, self)._onchange_uom_id() + amount_currency = accounting_vals["amount_currency"] + price_unit = self._get_fields_onchange_balance( + amount_currency=amount_currency + ).get("price_unit", price_unit) + self.with_context(check_move_validity=False).update( + {"price_unit": price_unit} + ) + return + else: + return super(AccountMoveLine, self)._onchange_uom_id() def _get_real_price_currency(self, product, rule_id, qty, uom, pricelist_id): PricelistItem = self.env["product.pricelist.item"] @@ -188,19 +188,17 @@ def _get_price_with_pricelist(self): ) self.with_context(check_move_validity=False).discount = 0.0 else: - product_context = dict( - self.env.context, + final_price, rule_id = self.move_id.pricelist_id.with_context( partner_id=self.move_id.partner_id.id, date=self.move_id.invoice_date or fields.Date.today(), uom=self.product_uom_id.id, - ) - final_price, rule_id = self.move_id.pricelist_id.with_context( - product_context ).get_product_price_rule( self.product_id, self.quantity or 1.0, self.move_id.partner_id ) base_price, currency = self.with_context( - product_context + partner_id=self.move_id.partner_id.id, + date=self.move_id.invoice_date or fields.Date.today(), + uom=self.product_uom_id.id, )._get_real_price_currency( self.product_id, rule_id, diff --git a/account_invoice_pricelist/tests/test_account_move_pricelist.py b/account_invoice_pricelist/tests/test_account_move_pricelist.py index ddb9673f3a5..38d3358dc4d 100644 --- a/account_invoice_pricelist/tests/test_account_move_pricelist.py +++ b/account_invoice_pricelist/tests/test_account_move_pricelist.py @@ -3,6 +3,7 @@ import hashlib import inspect +from odoo.exceptions import UserError from odoo.tests import common from odoo.addons.sale.models.sale import SaleOrderLine as upstream @@ -125,6 +126,7 @@ def setUpClass(cls): } ) cls.euro_currency = cls.env["res.currency"].search([("name", "=", "EUR")]) + cls.usd_currency = cls.env["res.currency"].search([("name", "=", "USD")]) cls.sale_pricelist_with_discount_in_euros = cls.ProductPricelist.create( { "name": "Test Sale pricelist - 4", @@ -316,3 +318,10 @@ def test_upstream_file_hash(self): func = inspect.getsource(upstream._get_real_price_currency).encode() func_hash = hashlib.md5(func).hexdigest() self.assertIn(func_hash, VALID_HASHES) + + def test_check_currency(self): + with self.assertRaises(UserError): + self.invoice.currency_id = self.usd_currency.id + self.invoice.write( + {"pricelist_id": self.sale_pricelist_with_discount_in_euros.id} + )