diff --git a/addons/product_expiry/tests/test_generate_serial_numbers.py b/addons/product_expiry/tests/test_generate_serial_numbers.py index a881ea4b76e..a7343e66aa4 100644 --- a/addons/product_expiry/tests/test_generate_serial_numbers.py +++ b/addons/product_expiry/tests/test_generate_serial_numbers.py @@ -10,12 +10,6 @@ from odoo.tools.misc import get_lang class TestStockLot(StockGenerateCommon): - def _import_lots(self, lots, move): - location_id = move.location_id - move_lines_vals = move.split_lots(lots) - move_lines_commands = move._generate_serial_move_line_commands(move_lines_vals, location_dest_id=location_id) - move.update({'move_line_ids': move_lines_commands}) - def test_set_multiple_lot_name_with_expiration_date_01(self): """ In a move line's `lot_name` field, pastes a list of lots and expiration dates. Checks the values are correctly interpreted and the expiration dates are correctly created diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index cef54a2a222..6fe8123459e 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -850,16 +850,31 @@ Please change the quantity done or the rounding precision of your unit of measur options = options or self._get_formating_options(lot_text_parts[1:]) for extra_string in lot_text_parts[1:]: field_data = self._convert_string_into_field_data(extra_string, options) - if field_data == "ignore": - # Got an unusable data for this move, updates only the lot_name part. - move_line_vals.update(lot_name=lot_text_parts[0]) - elif field_data: - move_line_vals.update(**field_data, lot_name=lot_text_parts[0]) + if field_data: + lot_text = lot_text_parts[0] + if field_data == "ignore": + # Got an unusable data for this move, updates only the lot_name part. + move_line_vals.update(lot_name=lot_text) + else: + move_line_vals.update(**field_data, lot_name=lot_text) else: # At least this part of the string is erronous and can't be converted, # don't try to guess and simply use the full string as the lot name. move_line_vals['lot_name'] = lot_text break + if self.picking_type_id.use_existing_lots: + lot_id = self.env['stock.lot'].search([ + ('product_id', '=', self.product_id.id), + ('name', '=', lot_text), + ('company_id', '=', self.company_id.id), + ]) + if not lot_id: + lot_id = self.env['stock.lot'].create({ + 'product_id': self.product_id.id, + 'name': lot_text, + 'company_id': self.company_id.id, + }) + move_line_vals['lot_id'] = lot_id.id move_lines_vals.append(move_line_vals) return move_lines_vals diff --git a/addons/stock/tests/test_generate_serial_numbers.py b/addons/stock/tests/test_generate_serial_numbers.py index 1b5a66f55e7..985cdf222ab 100644 --- a/addons/stock/tests/test_generate_serial_numbers.py +++ b/addons/stock/tests/test_generate_serial_numbers.py @@ -35,6 +35,13 @@ class StockGenerateCommon(TransactionCase): cls.Wizard = cls.env['stock.assign.serial'] + + def _import_lots(self, lots, move): + location_id = move.location_id + move_lines_vals = move.split_lots(lots) + move_lines_commands = move._generate_serial_move_line_commands(move_lines_vals, location_dest_id=location_id) + move.update({'move_line_ids': move_lines_commands}) + def get_new_move(self, nbre_of_lines=0, product=False): product = product or self.product_serial move_lines_vals = [Command.create({ @@ -369,3 +376,36 @@ class StockGenerateCommon(TransactionCase): {'quantity': 1, 'lot_name': '003', 'location_dest_id': sub_loc_03.id}, {'quantity': 1, 'lot_name': '004', 'location_dest_id': sub_loc_04.id}, ]) + + def test_import_lots(self): + product_lot = self.env['product.product'].create({ + 'name': 'Tracked by Lots', + 'type': 'product', + 'tracking': 'lot', + }) + lot_id = self.env['stock.lot'].create({ + 'product_id': product_lot.id, + 'name': 'abc', + }) + self.warehouse.in_type_id.use_existing_lots = True + receipt_picking = self.env['stock.picking'].create({ + 'picking_type_id': self.warehouse.in_type_id.id, + 'location_id': self.env.ref('stock.stock_location_suppliers').id, + 'location_dest_id': self.warehouse.lot_stock_id.id, + 'state': 'draft', + }) + move = self.env['stock.move'].create({ + 'name': product_lot.name, + 'product_id': product_lot.id, + 'product_uom': product_lot.uom_id.id, + 'product_uom_qty': 5.0, + 'picking_id': receipt_picking.id, + 'location_id': receipt_picking.location_id.id, + 'location_dest_id': receipt_picking.location_dest_id.id, + }) + self._import_lots("abc;4\ndef", move) + self.assertIn(lot_id, move.move_line_ids.lot_id) + self.assertRecordValues(move.move_line_ids, [ + {'quantity': 4, 'lot_name': 'abc'}, + {'quantity': 1, 'lot_name': 'def'}, + ])