From 3eb1e7e65ae19d77e27fd66378345e363a770c31 Mon Sep 17 00:00:00 2001 From: lreficent Date: Fri, 28 Jul 2017 09:31:38 +0200 Subject: [PATCH] [FIX] split move to be able to reserve properly when available. --- mrp_mto_with_stock/README.rst | 1 + mrp_mto_with_stock/models/mrp_production.py | 48 ++++++++++++++------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/mrp_mto_with_stock/README.rst b/mrp_mto_with_stock/README.rst index 4c9ffdd47..b004f5b12 100644 --- a/mrp_mto_with_stock/README.rst +++ b/mrp_mto_with_stock/README.rst @@ -29,6 +29,7 @@ To use this module, you need to: #. Go to *Manufacturing* and create a Manufacturing Order. #. Click on *Confirm Production*. +#. Click on *Reserve*. .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot diff --git a/mrp_mto_with_stock/models/mrp_production.py b/mrp_mto_with_stock/models/mrp_production.py index 87e0d107f..b8eabe2eb 100644 --- a/mrp_mto_with_stock/models/mrp_production.py +++ b/mrp_mto_with_stock/models/mrp_production.py @@ -19,7 +19,7 @@ class MrpProduction(models.Model): "All Manufacturing Orders must be confirmed.")) return self.action_assign() - @api.one + @api.multi def action_assign(self): """Reserves available products to the production order but also creates procurements for more items if we cannot reserve enough (MTO with @@ -28,20 +28,38 @@ class MrpProduction(models.Model): # reserve all that is available (standard behaviour): res = super(MrpProduction, self).action_assign() # try to create procurements: - for move in self.move_lines: - if (move.state == 'confirmed' and move.location_id in - move.product_id.mrp_mts_mto_location_ids): - domain = [('product_id', '=', move.product_id.id), - ('move_dest_id', '=', move.id)] - if move.group_id: - domain.append(('group_id', '=', move.group_id.id)) - procurement = self.env['procurement.order'].search(domain) - if not procurement: - qty_to_procure = (move.remaining_qty - - move.reserved_availability) - proc_dict = self._prepare_mto_procurement( - move, qty_to_procure) - self.env['procurement.order'].create(proc_dict) + move_obj = self.env['stock.move'] + for production in self: + for move in production.move_lines: + if (move.state == 'confirmed' and move.location_id in + move.product_id.mrp_mts_mto_location_ids): + domain = [('product_id', '=', move.product_id.id), + ('move_dest_id', '=', move.id)] + if move.group_id: + domain.append(('group_id', '=', move.group_id.id)) + procurement = self.env['procurement.order'].search(domain) + if not procurement: + # We have to split the move because we can't have + # a part of the move that have ancestors and not the + # other else it won't ever be reserved. + qty_to_procure = (move.remaining_qty - + move.reserved_availability) + if qty_to_procure < move.product_uom_qty: + move.do_unreserve() + new_move_id = move_obj.split( + move, + qty_to_procure, + restrict_lot_id=move.restrict_lot_id, + restrict_partner_id=move.restrict_partner_id) + new_move = move_obj.browse( + new_move_id) + move.action_assign() + else: + new_move = move + + proc_dict = self._prepare_mto_procurement( + new_move, qty_to_procure) + self.env['procurement.order'].create(proc_dict) return res def _prepare_mto_procurement(self, move, qty):