From cddf577abb991568197f31bf3ba3cad13aabd0ea Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Thu, 10 Oct 2019 13:31:14 +0000 Subject: [PATCH] [FIX] mrp_subcontracting: records components on existing line In a barcode environemtn or a situation with a lot of stock.move.line the subcontract is hard to use with record components because a line is created each time the produce wizard is proccessed. Then each line is duplicate, one with reservation and the other with effective transfer. This commit try to merge them in order to limit the number of lines. Task 2073383 closes odoo/odoo#38689 Signed-off-by: Simon Lejeune (sle) --- .../wizard/mrp_product_produce.py | 64 +++++++++++++++---- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/addons/mrp_subcontracting/wizard/mrp_product_produce.py b/addons/mrp_subcontracting/wizard/mrp_product_produce.py index 439c2f4566c..ceb803858e0 100644 --- a/addons/mrp_subcontracting/wizard/mrp_product_produce.py +++ b/addons/mrp_subcontracting/wizard/mrp_product_produce.py @@ -2,7 +2,7 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import fields, models -from odoo.tools.float_utils import float_is_zero +from odoo.tools.float_utils import float_is_zero, float_compare class MrpProductProduce(models.TransientModel): _inherit = 'mrp.product.produce' @@ -33,18 +33,56 @@ class MrpProductProduce(models.TransientModel): """ After producing, set the move line on the subcontract picking. """ res = super(MrpProductProduce, self)._update_finished_move() if self.subcontract_move_id: - self.env['stock.move.line'].create({ - 'move_id': self.subcontract_move_id.id, - 'picking_id': self.subcontract_move_id.picking_id.id, - 'product_id': self.product_id.id, - 'location_id': self.subcontract_move_id.location_id.id, - 'location_dest_id': self.subcontract_move_id.location_dest_id.id, - 'product_uom_qty': 0, - 'product_uom_id': self.product_uom_id.id, - 'qty_done': self.qty_producing, - 'lot_id': self.finished_lot_id and self.finished_lot_id.id, - 'lot_name': self.finished_lot_id and self.finished_lot_id.name, - }) + quantity = self.qty_producing + if self.finished_lot_id: + move_lines = self.subcontract_move_id.move_line_ids.filtered(lambda ml: ml.lot_id == self.finished_lot_id or not ml.lot_id) + else: + move_lines = self.subcontract_move_id.move_line_ids.filtered(lambda ml: not ml.lot_id) + # Update reservation and quantity done + for ml in move_lines: + rounding = ml.product_uom_id.rounding + if float_compare(quantity, 0, precision_rounding=rounding) <= 0: + break + quantity_to_process = min(quantity, ml.product_uom_qty - ml.qty_done) + quantity -= quantity_to_process + + new_quantity_done = (ml.qty_done + quantity_to_process) + + # on which lot of finished product + if float_compare(new_quantity_done, ml.product_uom_qty, precision_rounding=rounding) >= 0: + ml.write({ + 'qty_done': new_quantity_done, + 'lot_id': self.finished_lot_id and self.finished_lot_id.id, + 'lot_name': self.finished_lot_id and self.finished_lot_id.name, + }) + else: + new_qty_reserved = ml.product_uom_qty - new_quantity_done + default = { + 'product_uom_qty': new_quantity_done, + 'qty_done': new_quantity_done, + 'lot_id': self.finished_lot_id and self.finished_lot_id.id, + 'lot_name': self.finished_lot_id and self.finished_lot_id.name, + } + ml.copy(default=default) + ml.with_context(bypass_reservation_update=True).write({ + 'product_uom_qty': new_qty_reserved, + 'qty_done': 0 + }) + + if float_compare(quantity, 0, precision_rounding=self.product_uom_id.rounding) > 0: + self.env['stock.move.line'].create({ + 'move_id': self.subcontract_move_id.id, + 'picking_id': self.subcontract_move_id.picking_id.id, + 'product_id': self.product_id.id, + 'location_id': self.subcontract_move_id.location_id.id, + 'location_dest_id': self.subcontract_move_id.location_dest_id.id, + 'product_uom_qty': 0, + 'product_uom_id': self.product_uom_id.id, + 'qty_done': quantity, + 'lot_id': self.finished_lot_id and self.finished_lot_id.id, + 'lot_name': self.finished_lot_id and self.finished_lot_id.name, + }) + if not self._get_todo(self.production_id): ml_reserved = self.subcontract_move_id.move_line_ids.filtered(lambda ml: float_is_zero(ml.qty_done, precision_rounding=ml.product_uom_id.rounding) and