[FIX] stock: forbid edition of inventory loss quant

As quant from inventory loss location balances real quantity, it could
be really hazardous to able user to modify it. That said, modify it now
has no effect. So, with this commit, we exit the quant write method in
case of user tries to modify it manually if the quant location is
inventory loss, and we inform the user he can't modify it.

We could set the field as readonly in this case, but it's clearer for
user to inform him/her in case he/she tries to modify the quant than set
the field as readonly and take the risk user don't understand why he/she
can modify some quant and can't modify some other.

It would be preferabler to raise an UserError in the write than do
nothing in the write and return a warning in the onchange, but in case
we raise an error in the write method, user side the field is still in
edit mode and then, every time user clicks somewhere but on Discard, it
will raise the error again and it's not convenient for the user.

Task #2035465
This commit is contained in:
svs-odoo
2019-07-26 12:22:34 +00:00
parent 2971bf6100
commit 70f99448d5
+15
View File
@@ -141,6 +141,9 @@ class StockQuant(models.Model):
def write(self, vals):
""" Override to handle the "inventory mode" and create the inventory move. """
if self._is_inventory_mode() and 'inventory_quantity' in vals:
if any(quant.location_id.usage == 'inventory' for quant in self):
# Do nothing when user tries to modify manually a inventory loss
return
allowed_fields = self._get_inventory_fields_write()
if any([field for field in vals.keys() if field not in allowed_fields]):
raise UserError(_("Quant's edition is restricted, you can't do this operation."))
@@ -327,6 +330,18 @@ class StockQuant(models.Model):
if vals:
self.update(vals)
@api.onchange('inventory_quantity')
def _onchange_inventory_quantity(self):
if self.location_id and self.location_id.usage == 'inventory':
warning = {
'title': _('You cannot modify inventory loss quantity'),
'message': _(
'Editing quantities in an Inventory Adjustment location is forbidden,'
'those locations are used as counterpart when correcting the quantities.'
)
}
return {'warning': warning}
@api.model
def _update_available_quantity(self, product_id, location_id, quantity, lot_id=None, package_id=None, owner_id=None, in_date=None):
""" Increase or decrease `reserved_quantity` of a set of quants for a given set of