Skip to content

Commit

Permalink
[IMP] sale_order_qty_change_no_recompute: Prevent recalculation of pr…
Browse files Browse the repository at this point in the history
…ice when quantity is changed on sales order line
  • Loading branch information
Yadier-Tecnativa committed Apr 12, 2023
1 parent 8618ebc commit e062f94
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions sale_order_qty_change_no_recompute/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@
# Copyright 2021 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models
from odoo.tools import config
from odoo import api, models


class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

def _onchange_eval(self, field_name, onchange, result):
"""Remove the trigger for the undesired onchange method with this field.
@api.onchange("product_uom_qty")
def _onchange_product_uom_qty(self):
price_sale = self.product_id.lst_price
if self.price_unit != price_sale and self._origin.product_id == self.product_id:
if self.price_unit > 0:
self.price_unit = self.price_unit

We have to act at this place, as `_onchange_methods` is defined as a
property, and thus it can't be inherited due to the conflict of
inheritance between Python and Odoo ORM, so we can consider this as a HACK.
"""
ctx = self.env.context
if field_name in {"product_uom_qty", "product_uom"} and (
not config["test_enable"]
or (config["test_enable"] and ctx.get("prevent_onchange_quantity", False))
):
cls = type(self)
for method in self._onchange_methods.get(field_name, ()):
if method == cls.product_uom_change:
self._onchange_methods[field_name].remove(method)
break
return super()._onchange_eval(field_name, onchange, result)
@api.depends("product_id", "product_uom", "product_uom_qty")
def _compute_price_unit(self):
for line in self:
if line.qty_invoiced > 0:
continue
if not (
line.product_uom and line.product_id and line.order_id.pricelist_id
):
line.price_unit = 0.0
if line._origin.product_id != line.product_id:
price = line.with_company(line.company_id)._get_display_price()
line.price_unit = line.product_id._get_tax_included_unit_price(
line.company_id,
line.order_id.currency_id,
line.order_id.date_order,
"sale",
fiscal_position=line.order_id.fiscal_position_id,
product_price_unit=price,
product_currency=line.currency_id,
)

0 comments on commit e062f94

Please sign in to comment.