From 5f3255be7f4c19d208bba7c3f149494bd1abf144 Mon Sep 17 00:00:00 2001 From: Simon Lejeune Date: Wed, 3 Jan 2018 17:07:01 +0100 Subject: [PATCH] [FIX] stock: _free_reservation to reproduce: - have 4 units of product 1 in stock - create a move for a dozen, reserve it - try to scrap a single unit - an exception "cannot reserve more than available" is raised To reflect the change of reservation after scrapping the quantity, we have to update the reserved quantity on the move line. When updating the quantity on the move line, "quantity_split" is in the quant uom, so it should be converted to the uom of the move line. --- addons/stock/models/stock_move_line.py | 2 +- addons/stock/tests/test_move.py | 41 ++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/addons/stock/models/stock_move_line.py b/addons/stock/models/stock_move_line.py index d2025ebe373..4582c907575 100644 --- a/addons/stock/models/stock_move_line.py +++ b/addons/stock/models/stock_move_line.py @@ -496,7 +496,7 @@ class StockMoveLine(models.Model): candidate.product_qty - quantity, precision_rounding=self.product_uom_id.rounding, rounding_method='UP') - candidate.product_uom_qty = self.product_id.uom_id._compute_quantity(quantity_split, self.product_uom_id, rounding_method='HALF-UP') + candidate.product_uom_qty = self.product_id.uom_id._compute_quantity(quantity_split, candidate.product_uom_id, rounding_method='HALF-UP') quantity -= quantity_split move_to_recompute_state |= candidate.move_id if quantity == 0.0: diff --git a/addons/stock/tests/test_move.py b/addons/stock/tests/test_move.py index 39394a749fa..0e52a87d1ca 100644 --- a/addons/stock/tests/test_move.py +++ b/addons/stock/tests/test_move.py @@ -2690,6 +2690,47 @@ class StockMove(TransactionCase): scrapped_move.quantity_done = 8 self.assertEqual(scrap.scrap_qty, 8, 'Scrap quantity is not updated.') + def test_scrap_5(self): + """ Scrap the product of a reserved move line where the product is reserved in another + unit of measure. Check that the move line is correctly updated after the scrap. + """ + # 4 units are available in stock + self.env['stock.quant']._update_available_quantity(self.product1, self.stock_location, 4) + + # try to reserve a dozen + partner = self.env['res.partner'].create({'name': 'Kimberley'}) + picking = self.env['stock.picking'].create({ + 'name': 'A single picking with one move to scrap', + 'location_id': self.stock_location.id, + 'location_dest_id': self.customer_location.id, + 'partner_id': partner.id, + 'picking_type_id': self.env.ref('stock.picking_type_out').id, + }) + move1 = self.env['stock.move'].create({ + 'name': 'A move to confirm and scrap its product', + 'location_id': self.stock_location.id, + 'location_dest_id': self.customer_location.id, + 'product_id': self.product1.id, + 'product_uom': self.uom_dozen.id, + 'product_uom_qty': 1.0, + 'picking_id': picking.id, + }) + move1._action_confirm() + move1._action_assign() + self.assertEqual(move1.reserved_availability, 0.33) + + # scrap a unit + scrap = self.env['stock.scrap'].create({ + 'product_id': self.product1.id, + 'product_uom_id': self.product1.uom_id.id, + 'scrap_qty': 1, + 'picking_id': picking.id, + }) + scrap.action_validate() + + self.assertEqual(scrap.state, 'done') + self.assertEqual(move1.reserved_availability, 0.25) + def test_in_date_1(self): """ Check that moving a tracked quant keeps the incoming date. """