From 9d520a4d9b57dd50829328dc716174cbd86dbdb0 Mon Sep 17 00:00:00 2001 From: Sergio Teruel Date: Mon, 1 Jul 2019 12:08:56 +0200 Subject: [PATCH] [FIX] stock_pack_operation_auto_fill: Avoid auto fill qty_done when an extra_move has been created --- .../models/stock_move.py | 18 ++++++++-- .../tests/test_stock_picking_auto_fill.py | 35 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/stock_pack_operation_auto_fill/models/stock_move.py b/stock_pack_operation_auto_fill/models/stock_move.py index dc1b02aa7bc..cd1aff54cfb 100644 --- a/stock_pack_operation_auto_fill/models/stock_move.py +++ b/stock_pack_operation_auto_fill/models/stock_move.py @@ -8,13 +8,27 @@ class StockMove(models.Model): _inherit = 'stock.move' + def _create_extra_move(self): + """ + When user set on stock move line done a quantity greater than initial + demand Odoo creates an extra stock move with the difference and it is + posible to create an extra stock move line with qty_done = 0 which will + be deleted in _action_done method. + This method set a context variable to prevent set qty_done for these + cases. + """ + my_self = self + if self.picking_id.auto_fill_operation: + my_self = self.with_context(skip_auto_fill_operation=True) + return super(StockMove, my_self)._create_extra_move() + def _prepare_move_line_vals(self, quantity=None, reserved_quant=None): """Auto-assign as done the quantity proposed for the lots""" - self.ensure_one() res = super(StockMove, self)._prepare_move_line_vals( quantity, reserved_quant, ) - if not self.picking_id.auto_fill_operation: + if (self.env.context.get('skip_auto_fill_operation') or + not self.picking_id.auto_fill_operation): return res elif (self.picking_id.picking_type_id.avoid_lot_assignment and res.get('lot_id')): diff --git a/stock_pack_operation_auto_fill/tests/test_stock_picking_auto_fill.py b/stock_pack_operation_auto_fill/tests/test_stock_picking_auto_fill.py index 700a7cfb4a4..b4ada6c56f7 100644 --- a/stock_pack_operation_auto_fill/tests/test_stock_picking_auto_fill.py +++ b/stock_pack_operation_auto_fill/tests/test_stock_picking_auto_fill.py @@ -322,3 +322,38 @@ def test_action_auto_transfer_avoid_assign_lots(self): # The expected result is only opertions with product_id set and self.assertFalse(product_8_op.qty_done) self.assertTrue(product_9_op.qty_done) + + def test_receipt_more_qty_than_demand(self): + # Active auto fill operations although they have assigned lots + self.picking_type_in.write({ + 'auto_fill_operation': True, + 'avoid_lot_assignment': False, + }) + picking_in = self.picking_model.create({ + 'partner_id': self.supplier.id, + 'picking_type_id': self.picking_type_in.id, + 'location_id': self.supplier_location.id, + 'location_dest_id': self.warehouse.wh_input_stock_loc_id.id, + 'move_lines': [(0, 0, { + 'name': self.product_8.display_name, + 'product_id': self.product_8.id, + 'product_uom_qty': 10.0, + 'product_uom': self.product_8.uom_id.id, + 'partner_id': self.supplier.id, + 'location_id': self.supplier_location.id, + 'location_dest_id': self.warehouse.wh_input_stock_loc_id.id, + })] + }) + lot = self.env['stock.production.lot'].create({ + 'product_id': self.product_8.id, + 'name': 'test-lot-0001', + }) + # Receipt more than initial demand and assign a lot to force create an + # extra stock move line which must be deleted. + picking_in.move_line_ids.write({ + 'lot_id': lot.id, + 'qty_done': 50.0, + }) + picking_in.action_done() + # The move should only have a one stock move line. + self.assertEqual(len(picking_in.move_line_ids), 1)