[FIX] mrp_unbuild_subcontracting: Cannot unbuild subcontracting orders with many links

From version 16.0, when we create backorders we are not splitting the Origin Moves. For that reason, when we try to unbuild a subcontracting order that has more than origin move we cannot know which is the real origin.

There has been a PR to fix this in Odoo here: https://github.com/odoo/odoo/pull/148262, but it was reverted after some time due to some problems on the splitting. If we want to allow this, we would need to propose a PR in Odoo that completely fixes this problem.
This commit is contained in:
BernatPForgeFlow
2024-06-03 17:39:45 +02:00
parent 97b40869c1
commit f8cbae3247
2 changed files with 30 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
from datetime import timedelta
from odoo import fields, models
from odoo import _, fields, models
from odoo.exceptions import UserError
from odoo.osv.expression import OR
@@ -14,6 +15,18 @@ class StockPicking(models.Model):
def _prepare_subcontract_unbuild_vals(self, subcontract_move, bom):
subcontract_move.ensure_one()
product = subcontract_move.product_id
mos = subcontract_move.mapped(
"origin_returned_move_id.move_orig_ids.production_id"
)
if len(mos) > 1:
raise UserError(
_(
"It's not possible to create the subcontracting unbuild order\n"
"The subcontract move %(smn)s is linked with more than "
"one manufacturing order: %(jmm)s"
)
% {"smn": subcontract_move.name, "jmm": ",".join(mos.mapped("name"))}
)
vals = {
"company_id": subcontract_move.company_id.id,
"product_id": product.id,
@@ -28,7 +41,7 @@ class StockPicking(models.Model):
"product_qty": subcontract_move.product_uom_qty,
"picking_id": self.id,
"is_subcontracted": True,
"mo_id": subcontract_move.move_orig_ids.move_orig_ids.production_id.id,
"mo_id": mos.id,
"lot_id": subcontract_move.move_orig_ids.lot_ids.id,
}
return vals

View File

@@ -1,3 +1,4 @@
from odoo.exceptions import UserError
from odoo.tests import Form, TransactionCase
@@ -207,17 +208,21 @@ class TestSubcontractingPurchaseFlows(TransactionCase):
)
)
return_wizard._onchange_picking_id()
return_id, _ = return_wizard._create_returns()
with self.assertRaises(UserError):
return_id, _ = return_wizard._create_returns()
return_picking = self.env["stock.picking"].browse(return_id)
return_picking.move_ids.quantity_done = 3
return_picking.button_validate()
self.assertEqual(po.order_line.qty_received, 6)
mo = picking_to_return.mapped("move_ids.move_orig_ids.production_id")
unbuild = self.env["mrp.unbuild"].search([("mo_id", "in", mo.ids)])
self.assertTrue(unbuild.exists())
# This part cannot be tested since we cannot unbuild
# subcontracting orders with more than one origin.
#
# return_picking = self.env["stock.picking"].browse(return_id)
# return_picking.move_ids.quantity_done = 3
# return_picking.button_validate()
#
# self.assertEqual(po.order_line.qty_received, 6)
#
# mo = picking_to_return.mapped("move_ids.move_orig_ids.production_id")
# unbuild = self.env["mrp.unbuild"].search([("mo_id", "in", mo.ids)])
# self.assertTrue(unbuild.exists())
class TestSubcontractingTracking(TransactionCase):