[REF] sale, sale_stock: decrease sale order lines usability
* If the user tries to decrease the quantity of a sale order line below the received quantity, we raise a warning telling him to create a return first. * As we do not propagate the decrase to the already created pickings, we post a message in the chatter of these pickings informing the pickers of the change.
This commit is contained in:
committed by
Simon Lejeune
parent
5e5cd08123
commit
1168e73a72
+21
-19
@@ -743,32 +743,34 @@ class SaleOrderLine(models.Model):
|
||||
|
||||
return line
|
||||
|
||||
def _update_line_quantity(self, values):
|
||||
orders = self.mapped('order_id')
|
||||
for order in orders:
|
||||
order_lines = self.filtered(lambda x: x.order_id == order)
|
||||
msg = ""
|
||||
if any([values['product_uom_qty'] < x.product_uom_qty for x in order_lines]):
|
||||
msg += "<b>" + _(
|
||||
'The ordered quantity has been decreased. Do not forget to take it into account on your invoices and delivery orders.') + '</b>'
|
||||
msg += "<ul>"
|
||||
for line in order_lines:
|
||||
msg += "<li> %s:" % (line.product_id.display_name,)
|
||||
msg += "<br/>" + _("Ordered Quantity") + ": %s -> %s <br/>" % (
|
||||
line.product_uom_qty, float(values['product_uom_qty']),)
|
||||
if line.product_id.type in ('consu', 'product'):
|
||||
msg += _("Delivered Quantity") + ": %s <br/>" % (line.qty_delivered,)
|
||||
msg += _("Invoiced Quantity") + ": %s <br/>" % (line.qty_invoiced,)
|
||||
msg += "</ul>"
|
||||
order.message_post(body=msg)
|
||||
|
||||
@api.multi
|
||||
def write(self, values):
|
||||
lines = False
|
||||
changed_lines = False
|
||||
if 'product_uom_qty' in values:
|
||||
precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
|
||||
lines = self.filtered(
|
||||
lambda r: r.state == 'sale' and float_compare(r.product_uom_qty, values['product_uom_qty'], precision_digits=precision) == -1)
|
||||
changed_lines = self.filtered(
|
||||
lambda r: r.state == 'sale' and float_compare(r.product_uom_qty, values['product_uom_qty'], precision_digits=precision) != 0)
|
||||
if changed_lines:
|
||||
orders = self.mapped('order_id')
|
||||
for order in orders:
|
||||
order_lines = changed_lines.filtered(lambda x: x.order_id == order)
|
||||
msg = ""
|
||||
if any([values['product_uom_qty'] < x.product_uom_qty for x in order_lines]):
|
||||
msg += "<b>" + _('The ordered quantity has been decreased. Do not forget to take it into account on your invoices and delivery orders.') + '</b>'
|
||||
msg += "<ul>"
|
||||
for line in order_lines:
|
||||
msg += "<li> %s:" % (line.product_id.display_name,)
|
||||
msg += "<br/>" + _("Ordered Quantity") + ": %s -> %s <br/>" % (line.product_uom_qty, float(values['product_uom_qty']),)
|
||||
if line.product_id.type in ('consu', 'product'):
|
||||
msg += _("Delivered Quantity") + ": %s <br/>" % (line.qty_delivered,)
|
||||
msg += _("Invoiced Quantity") + ": %s <br/>" % (line.qty_invoiced,)
|
||||
msg += "</ul>"
|
||||
order.message_post(body=msg)
|
||||
self.filtered(
|
||||
lambda r: r.state == 'sale' and float_compare(r.product_uom_qty, values['product_uom_qty'], precision_digits=precision) != 0)._update_line_quantity(values)
|
||||
result = super(SaleOrderLine, self).write(values)
|
||||
if lines:
|
||||
lines._action_procurement_create()
|
||||
|
||||
@@ -158,6 +158,10 @@ class SaleOrderLine(models.Model):
|
||||
@api.onchange('product_uom_qty')
|
||||
def _onchange_product_uom_qty(self):
|
||||
if self.state == 'sale' and self.product_id.type in ['product', 'consu'] and self.product_uom_qty < self._origin.product_uom_qty:
|
||||
# Do not display this warning if the new quantity is below the delivered
|
||||
# one; the `write` will raise an `UserError` anyway.
|
||||
if self.product_uom_qty < self.qty_delivered:
|
||||
return {}
|
||||
warning_mess = {
|
||||
'title': _('Ordered quantity decreased!'),
|
||||
'message' : _('You are decreasing the ordered quantity! Do not forget to manually update the delivery order if needed.'),
|
||||
@@ -242,3 +246,13 @@ class SaleOrderLine(models.Model):
|
||||
break
|
||||
|
||||
return is_available
|
||||
|
||||
def _update_line_quantity(self, values):
|
||||
if self.mapped('qty_delivered') and values['product_uom_qty'] < max(self.mapped('qty_delivered')):
|
||||
raise UserError('You cannot decrease the ordered quantity below your delivered quantity.\n'
|
||||
'Create a return first.')
|
||||
for line in self:
|
||||
pickings = self.order_id.picking_ids.filtered(lambda p: p.state not in ('done', 'cancel'))
|
||||
pickings.message_post("The quantity of %s has been updated from %d to %d in %s" %
|
||||
(line.product_id.name, line.product_uom_qty, values['product_uom_qty'], self.order_id.name))
|
||||
super(SaleOrderLine, self)._update_line_quantity(values)
|
||||
|
||||
Reference in New Issue
Block a user