[FIX] stock: warning when change product tracking

This commit fixes two bugs:

1) When the user changes the tracking of a product template with
   variants, they get a traceback.

2) When the user changes the tracking of a product with quantity
   available, the warning is not shown.

For the 1), it was due to reading `tracking` and `qty_available` fields
on a record set (happens when the template has variants).
For the 2), it was due to the `qty_available` who is wrongly computed in
the onchange because of the records having a "new-id" instead of their
usual id.

How to reproduce:
 - Create a new product with some variants, no tracking;
 - Add quantities for those product variants;
 - Edit the product and change its tracking -> Traceback.

task-2618203

closes odoo/odoo#76071

Co-authored: svs-odoo < svs@odoo.com >
X-original-commit: 75572da12b3ad5d0f930e7e2f5dbac067a350b3c
Signed-off-by: Arnold Moyaux <amoyaux@users.noreply.github.com>
Signed-off-by: Rémy Voet <ryv-odoo@users.noreply.github.com>
This commit is contained in:
vst-odoo
2021-09-13 11:58:15 +00:00
committed by svs-odoo
parent 9850a3ac9b
commit ecae67cee7
+16 -12
View File
@@ -6,10 +6,10 @@ from ast import literal_eval
from collections import defaultdict
from dateutil.relativedelta import relativedelta
from odoo import _, api, fields, models, SUPERUSER_ID
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.osv import expression
from odoo.tools import pycompat,float_is_zero
from odoo.tools import float_is_zero
from odoo.tools.float_utils import float_round
OPERATORS = {
@@ -21,6 +21,7 @@ OPERATORS = {
'!=': py_operator.ne
}
class Product(models.Model):
_inherit = "product.product"
@@ -174,8 +175,9 @@ class Product(models.Model):
res = dict()
for product in self.with_context(prefetch_fields=False):
origin_product_id = product._origin.id
product_id = product.id
if not product_id:
if not origin_product_id:
res[product_id] = dict.fromkeys(
['qty_available', 'free_qty', 'incoming_qty', 'outgoing_qty', 'virtual_available'],
0.0,
@@ -184,14 +186,14 @@ class Product(models.Model):
rounding = product.uom_id.rounding
res[product_id] = {}
if dates_in_the_past:
qty_available = quants_res.get(product_id, [0.0])[0] - moves_in_res_past.get(product_id, 0.0) + moves_out_res_past.get(product_id, 0.0)
qty_available = quants_res.get(origin_product_id, [0.0])[0] - moves_in_res_past.get(origin_product_id, 0.0) + moves_out_res_past.get(origin_product_id, 0.0)
else:
qty_available = quants_res.get(product_id, [0.0])[0]
reserved_quantity = quants_res.get(product_id, [False, 0.0])[1]
qty_available = quants_res.get(origin_product_id, [0.0])[0]
reserved_quantity = quants_res.get(origin_product_id, [False, 0.0])[1]
res[product_id]['qty_available'] = float_round(qty_available, precision_rounding=rounding)
res[product_id]['free_qty'] = float_round(qty_available - reserved_quantity, precision_rounding=rounding)
res[product_id]['incoming_qty'] = float_round(moves_in_res.get(product_id, 0.0), precision_rounding=rounding)
res[product_id]['outgoing_qty'] = float_round(moves_out_res.get(product_id, 0.0), precision_rounding=rounding)
res[product_id]['incoming_qty'] = float_round(moves_in_res.get(origin_product_id, 0.0), precision_rounding=rounding)
res[product_id]['outgoing_qty'] = float_round(moves_out_res.get(origin_product_id, 0.0), precision_rounding=rounding)
res[product_id]['virtual_available'] = float_round(
qty_available + res[product_id]['incoming_qty'] - res[product_id]['outgoing_qty'],
precision_rounding=rounding)
@@ -411,8 +413,8 @@ class Product(models.Model):
product.reordering_max_qty = product_res.get('reordering_max_qty', 0)
@api.onchange('tracking')
def onchange_tracking(self):
if self.tracking != "none" and self.qty_available > 0:
def _onchange_tracking(self):
if any(product.tracking != 'none' and product.qty_available > 0 for product in self):
return {
'warning': {
'title': _('Warning!'),
@@ -807,8 +809,8 @@ class ProductTemplate(models.Model):
)
@api.onchange('tracking')
def onchange_tracking(self):
return self.mapped('product_variant_ids').onchange_tracking()
def _onchange_tracking(self):
return self.mapped('product_variant_ids')._onchange_tracking()
@api.onchange('type')
def _onchange_type(self):
@@ -948,6 +950,7 @@ class ProductTemplate(models.Model):
action = self.env["ir.actions.actions"]._for_xml_id('stock.stock_replenishment_product_product_action')
return action
class ProductCategory(models.Model):
_inherit = 'product.category'
@@ -976,6 +979,7 @@ class ProductCategory(models.Model):
routes |= base_cat.route_ids
category.total_route_ids = routes
class ProductPackaging(models.Model):
_inherit = "product.packaging"