From 65338960a9e1db1a52d1743ec702e01ec8dfae44 Mon Sep 17 00:00:00 2001 From: Aungkokolin1997 Date: Wed, 31 Jan 2024 09:37:16 +0000 Subject: [PATCH] [REF] mrp: refactor action_unbuild() for stock_move_line creation This refactoring of action_unbuild() enhances its extensibility for stock_move_line creation. Previously, passing additional values to the unbuild move lines necessitated a complete override of the method due to its rigid design. With this refactoring, it is now possible to extend the respective methods for preparing move line values, allowing for the addition of new values or modification of existing ones. closes odoo/odoo#154667 X-original-commit: 9716c3116dec9d3463123914b73d0556ccd07816 Signed-off-by: Steve Van Essche Signed-off-by: Tiffany Chang (tic) --- addons/mrp/models/mrp_unbuild.py | 44 +++++++++++++++++++------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/addons/mrp/models/mrp_unbuild.py b/addons/mrp/models/mrp_unbuild.py index 1d5feb9ca04..629e9fbffbc 100644 --- a/addons/mrp/models/mrp_unbuild.py +++ b/addons/mrp/models/mrp_unbuild.py @@ -128,6 +128,28 @@ class MrpUnbuild(models.Model): if 'done' in self.mapped('state'): raise UserError(_("You cannot delete an unbuild order if the state is 'Done'.")) + def _prepare_finished_move_line_vals(self, finished_move): + return { + 'move_id': finished_move.id, + 'lot_id': self.lot_id.id, + 'quantity': self.product_qty, + 'product_id': finished_move.product_id.id, + 'product_uom_id': finished_move.product_uom.id, + 'location_id': finished_move.location_id.id, + 'location_dest_id': finished_move.location_dest_id.id, + } + + def _prepare_move_line_vals(self, move, origin_move_line, taken_quantity): + return { + 'move_id': move.id, + 'lot_id': origin_move_line.lot_id.id, + 'quantity': taken_quantity, + 'product_id': move.product_id.id, + 'product_uom_id': origin_move_line.product_uom_id.id, + 'location_id': move.location_id.id, + 'location_dest_id': move.location_dest_id.id, + } + def action_unbuild(self): self.ensure_one() self._check_company() @@ -155,15 +177,8 @@ class MrpUnbuild(models.Model): raise UserError(_('Some of your byproducts are tracked, you have to specify a manufacturing order in order to retrieve the correct byproducts.')) for finished_move in finished_moves: - self.env['stock.move.line'].create({ - 'move_id': finished_move.id, - 'lot_id': self.lot_id.id, - 'quantity': self.product_qty, - 'product_id': finished_move.product_id.id, - 'product_uom_id': finished_move.product_uom.id, - 'location_id': finished_move.location_id.id, - 'location_dest_id': finished_move.location_dest_id.id, - }) + finished_move_line_vals = self._prepare_finished_move_line_vals(finished_move) + self.env['stock.move.line'].create(finished_move_line_vals) # TODO: Will fail if user do more than one unbuild with lot on the same MO. Need to check what other unbuild has aready took qty_already_used = defaultdict(float) @@ -179,15 +194,8 @@ class MrpUnbuild(models.Model): # Iterate over all move_lines until we unbuilded the correct quantity. taken_quantity = min(needed_quantity, move_line.quantity - qty_already_used[move_line]) if taken_quantity: - self.env['stock.move.line'].create({ - 'move_id': move.id, - 'lot_id': move_line.lot_id.id, - 'quantity': taken_quantity, - 'product_id': move.product_id.id, - 'product_uom_id': move_line.product_uom_id.id, - 'location_id': move.location_id.id, - 'location_dest_id': move.location_dest_id.id, - }) + move_line_vals = self._prepare_move_line_vals(move, move_line, taken_quantity) + self.env['stock.move.line'].create(move_line_vals) needed_quantity -= taken_quantity qty_already_used[move_line] += taken_quantity else: