diff --git a/stock_picking_auto_create_lot_qty/models/stock_picking.py b/stock_picking_auto_create_lot_qty/models/stock_picking.py index 7c01e5e12e7f..b4bb691185fb 100644 --- a/stock_picking_auto_create_lot_qty/models/stock_picking.py +++ b/stock_picking_auto_create_lot_qty/models/stock_picking.py @@ -107,25 +107,66 @@ def _set_auto_lot(self): """ pickings = self.filtered(lambda p: p.picking_type_id.auto_create_lot) immediate_pickings = self.filtered(lambda p: p.immediate_transfer) - new_lines = self.env["stock.move.line"].browse() lines, new_lines = self._split_stock_move_lines(pickings) - if immediate_pickings: - return super()._set_auto_lot() + if immediate_pickings and lines: + return self._create_lots_for_immediate_pickings(lines) if new_lines or not lines: return for line in lines: product_id = line.product_id - product_qty = line.reserved_uom_qty - current_product_qty = product_id.uom_id._compute_quantity( - product_qty, - product_id.batch_uom_id or product_id.uom_id, - round=False, - ) + if product_id.create_lot_every_n: + product_qty = line.reserved_uom_qty + current_product_qty = product_id.uom_id._compute_quantity( + product_qty, + product_id.batch_uom_id or product_id.uom_id, + round=False, + ) new_lines |= self._prepare_stock_move_lines_for_lots( product_id, line, current_product_qty ) else: new_lines |= line - for line in new_lines: + + self._set_lotname(new_lines) + + @api.model + def _create_lots_for_immediate_pickings(self, lines): + """ + Create stock move lines for immediate pickings, + handling products that require lot creation. + + Args: + lines (stock.move.line): A recordset of stock move lines to process. + """ + new_lines = self.env["stock.move.line"].browse() + old_lines = self.env["stock.move.line"].browse() + for line in lines: + product_id = line.product_id + + if product_id.create_lot_every_n: + product_qty = line.reserved_uom_qty + current_product_qty = product_id.uom_id._compute_quantity( + product_qty, + product_id.batch_uom_id or product_id.uom_id, + round=False, + ) + old_lines |= line + new_lines |= self._prepare_stock_move_lines_for_lots( + product_id, line, current_product_qty + ) + else: + new_lines |= line + old_lines.update({"qty_done": 0}) + self._set_lotname(new_lines) + + @api.model + def _set_lotname(self, lines): + """ + Set the lot name for each stock move line in the provided recordset. + + Args: + lines (stock.move.line): A recordset of stock move lines to update with lot names. + """ + for line in lines: line.lot_name = line._get_lot_sequence() diff --git a/stock_picking_auto_create_lot_qty/tests/common.py b/stock_picking_auto_create_lot_qty/tests/common.py index 8369ec7b50f8..68e71c780c03 100644 --- a/stock_picking_auto_create_lot_qty/tests/common.py +++ b/stock_picking_auto_create_lot_qty/tests/common.py @@ -52,7 +52,7 @@ def _create_product( ) @classmethod - def _create_picking(cls, multicompany=False): + def _create_picking(cls, multicompany=False, immediate_transfer=False): """ Create a new picking and set the following properties: @@ -68,7 +68,10 @@ def _create_picking(cls, multicompany=False): ) cls.picking = ( cls.env["stock.picking"] - .with_context(default_picking_type_id=picking_type_in.id) + .with_context( + default_picking_type_id=picking_type_in.id, + default_immediate_transfer=immediate_transfer, + ) .with_company(company) .create( { @@ -80,7 +83,9 @@ def _create_picking(cls, multicompany=False): ) @classmethod - def _create_move(cls, product=None, qty=1.0, multicompany=False): + def _create_move( + cls, product=None, qty=1.0, multicompany=False, immediate_transfer=False + ): """Create a new stock move for the given product and quantity Args: @@ -104,6 +109,7 @@ def _create_move(cls, product=None, qty=1.0, multicompany=False): "product_uom": product.uom_id.id, "location_id": cls.supplier_location.id, "location_dest_id": location_dest.id, + "quantity_done": qty if immediate_transfer else 0, "move_line_ids": [ ( 0, diff --git a/stock_picking_auto_create_lot_qty/tests/test_stock_picking_auto_create_lot_qty.py b/stock_picking_auto_create_lot_qty/tests/test_stock_picking_auto_create_lot_qty.py index 6b36f6cae084..d2ccaefbf36e 100644 --- a/stock_picking_auto_create_lot_qty/tests/test_stock_picking_auto_create_lot_qty.py +++ b/stock_picking_auto_create_lot_qty/tests/test_stock_picking_auto_create_lot_qty.py @@ -277,3 +277,38 @@ def test_prepare_stock_move_lines_for_lots_3(self): 5, msg="The number of created lots should be equal to 5", ) + + def test_multiples_allowed_false_immediate_transfer(self): + """ + Test that the serial number is created if the quantity + received is not a multiple of the selected value (Immediate Transfer) + """ + self._create_picking(immediate_transfer=True) + self._create_move( + product=self.product_serial_auto_qty_5_multiples_allowed_false, + qty=8.0, + immediate_transfer=True, + ) + self.picking.button_validate() + # Check the display field + move = self.picking.move_ids.filtered( + lambda m: m.product_id + == self.product_serial_auto_qty_5_multiples_allowed_false + ) + self.assertFalse( + move.display_assign_serial, msg="Do not display the assigned serial number" + ) + + # Search for serials + lot = self.lot_obj.search( + [ + ( + "product_id", + "=", + self.product_serial_auto_qty_5_multiples_allowed_false.id, + ) + ] + ) + self.assertEqual( + len(lot), 2, msg="The number of created lots should be equal to 2" + )