diff --git a/addons/stock/models/stock_move.py b/addons/stock/models/stock_move.py index c30932f69cb..2890244f906 100644 --- a/addons/stock/models/stock_move.py +++ b/addons/stock/models/stock_move.py @@ -481,6 +481,7 @@ class StockMove(models.Model): move_line_vals = self._prepare_move_line_vals(quantity=0) move_line_vals['lot_id'] = lot.id move_line_vals['lot_name'] = lot.name + move_line_vals['product_uom_id'] = move.product_id.uom_id.id move_line_vals['qty_done'] = 1 move_lines_commands.append((0, 0, move_line_vals)) move.write({'move_line_ids': move_lines_commands}) @@ -851,6 +852,9 @@ class StockMove(models.Model): @api.onchange('lot_ids') def _onchange_lot_ids(self): + quantity_done = sum(ml.product_uom_id._compute_quantity(ml.qty_done, self.product_uom) for ml in self.move_line_ids.filtered(lambda ml: not ml.lot_id and ml.lot_name)) + quantity_done += self.product_id.uom_id._compute_quantity(len(self.lot_ids), self.product_uom) + self.update({'quantity_done': quantity_done}) used_lots = self.env['stock.move.line'].search([ ('company_id', '=', self.company_id.id), ('product_id', '=', self.product_id.id), @@ -858,15 +862,6 @@ class StockMove(models.Model): ('move_id', '!=', self._origin.id), ('state', '!=', 'cancel') ]) - - counter = self.env['stock.move.line'].search_count([ - ('company_id', '=', self.company_id.id), - ('product_id', '=', self.product_id.id), - ('move_id', '=', self._origin.id), - ('lot_id', '=', False), - ('lot_name', '!=', False), - ]) - self.update({'quantity_done': len(self.lot_ids) + counter}) if used_lots: return { 'warning': {'title': _('Warning'), 'message': _('Existing Serial numbers (%s). Please correct the serial numbers encoded.') % ','.join(used_lots.lot_id.mapped('display_name'))} diff --git a/addons/stock/tests/test_move2.py b/addons/stock/tests/test_move2.py index 308ca0dbf5b..54be08e6d91 100644 --- a/addons/stock/tests/test_move2.py +++ b/addons/stock/tests/test_move2.py @@ -2815,3 +2815,17 @@ class TestAutoAssign(TestStockCommon): self.assertEqual(move.quantity_done, 3.0) move.lot_ids = [(3, lot2.id)] self.assertEqual(move.quantity_done, 2.0) + + self.uom_dozen = self.env.ref('uom.product_uom_dozen') + move = self.env['stock.move'].create({ + 'name': 'TestReceiveDozen', + 'location_id': self.supplier_location.id, + 'location_dest_id': self.stock_location.id, + 'product_id': self.product_serial.id, + 'product_uom': self.uom_dozen.id, + 'picking_type_id': self.env.ref('stock.picking_type_in').id, + }) + move.lot_ids = [(4, lot1.id)] + move.lot_ids = [(4, lot2.id)] + move.lot_ids = [(4, lot3.id)] + self.assertEqual(move.quantity_done, 3.0/12.0)