From 30139007f94e414f979b8cfec35ff8f1103d55cd Mon Sep 17 00:00:00 2001 From: Pierre Masereel Date: Tue, 3 Nov 2020 09:15:45 +0000 Subject: [PATCH] [FIX] point_of_sale: validate pickings when kits are sold When you are selling a kit in point of sale, the genrated picking is not validated. It is not validated, because the moves are not confirmed, before we set a quantity done, and so the moves are not exploded and it result to the impossibility of calling action_done on the picking. To avoid this problem, we are confirming the moves created, and then set the quantity done on the moves generated from the confirmation. closes odoo/odoo#61260 X-original-commit: 4f2f347c8b21b78efaa702d1b3c91b7f88dd88e0 Signed-off-by: pimodoo --- addons/point_of_sale/models/stock_picking.py | 88 ++++++++++---------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/addons/point_of_sale/models/stock_picking.py b/addons/point_of_sale/models/stock_picking.py index 0eea426c9b5..6efc5b35815 100644 --- a/addons/point_of_sale/models/stock_picking.py +++ b/addons/point_of_sale/models/stock_picking.py @@ -12,7 +12,7 @@ class StockPicking(models.Model): pos_session_id = fields.Many2one('pos.session') pos_order_id = fields.Many2one('pos.order') - + def _prepare_picking_vals(self, partner, picking_type, location_id, location_dest_id): return { 'partner_id': partner.id if partner else False, @@ -68,7 +68,7 @@ class StockPicking(models.Model): pass pickings |= negative_picking return pickings - + def _prepare_stock_move_vals(self, first_line, order_lines): return { 'name': first_line.name, @@ -92,49 +92,51 @@ class StockPicking(models.Model): current_move = self.env['stock.move'].create( self._prepare_stock_move_vals(first_line, order_lines) ) - if first_line.product_id.tracking != 'none' and (self.picking_type_id.use_existing_lots or self.picking_type_id.use_create_lots): - for line in order_lines: - sum_of_lots = 0 - for lot in line.pack_lot_ids.filtered(lambda l: l.lot_name): - if line.product_id.tracking == 'serial': - qty = 1 - else: - qty = abs(line.qty) - ml_vals = current_move._prepare_move_line_vals() - ml_vals.update({'qty_done':qty}) - if self.picking_type_id.use_existing_lots: - existing_lot = self.env['stock.production.lot'].search([ - ('company_id', '=', self.company_id.id), - ('product_id', '=', line.product_id.id), - ('name', '=', lot.lot_name) - ]) - if not existing_lot and self.picking_type_id.use_create_lots: - existing_lot = self.env['stock.production.lot'].create({ - 'company_id': self.company_id.id, - 'product_id': line.product_id.id, - 'name': lot.lot_name, + confirmed_moves = current_move._action_confirm() + for move in confirmed_moves: + if first_line.product_id == move.product_id and first_line.product_id.tracking != 'none' and (self.picking_type_id.use_existing_lots or self.picking_type_id.use_create_lots): + for line in order_lines: + sum_of_lots = 0 + for lot in line.pack_lot_ids.filtered(lambda l: l.lot_name): + if line.product_id.tracking == 'serial': + qty = 1 + else: + qty = abs(line.qty) + ml_vals = move._prepare_move_line_vals() + ml_vals.update({'qty_done':qty}) + if self.picking_type_id.use_existing_lots: + existing_lot = self.env['stock.production.lot'].search([ + ('company_id', '=', self.company_id.id), + ('product_id', '=', line.product_id.id), + ('name', '=', lot.lot_name) + ]) + if not existing_lot and self.picking_type_id.use_create_lots: + existing_lot = self.env['stock.production.lot'].create({ + 'company_id': self.company_id.id, + 'product_id': line.product_id.id, + 'name': lot.lot_name, + }) + ml_vals.update({ + 'lot_id': existing_lot.id, + }) + else: + ml_vals.update({ + 'lot_name': lot.lot_name, }) - ml_vals.update({ - 'lot_id': existing_lot.id, - }) - else: - ml_vals.update({ - 'lot_name': lot.lot_name, - }) - self.env['stock.move.line'].create(ml_vals) - sum_of_lots += qty - if abs(line.qty) != sum_of_lots: - difference_qty = abs(line.qty) - sum_of_lots - ml_vals = current_move._prepare_move_line_vals() - if line.product_id.tracking == 'serial': - ml_vals.update({'qty_done': 1}) - for i in range(int(difference_qty)): - self.env['stock.move.line'].create(ml_vals) - else: - ml_vals.update({'qty_done': difference_qty}) self.env['stock.move.line'].create(ml_vals) - else: - current_move.quantity_done = abs(sum(order_lines.mapped('qty'))) + sum_of_lots += qty + if abs(line.qty) != sum_of_lots: + difference_qty = abs(line.qty) - sum_of_lots + ml_vals = current_move._prepare_move_line_vals() + if line.product_id.tracking == 'serial': + ml_vals.update({'qty_done': 1}) + for i in range(int(difference_qty)): + self.env['stock.move.line'].create(ml_vals) + else: + ml_vals.update({'qty_done': difference_qty}) + self.env['stock.move.line'].create(ml_vals) + else: + move.quantity_done = move.product_uom_qty def _send_confirmation_email(self): # Avoid sending Mail/SMS for POS deliveries