11# Copyright 2023 Camptocamp SA
2+ # Copyright 2025 Jacques-Etienne Baudoux (BCIM) <[email protected] > 23# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
34
45from odoo import api , models
78class ProductTemplate (models .Model ):
89 _inherit = "product.template"
910
10- def write (self , values ):
11- if "route_ids" not in values :
11+ def dis_write (self , values ):
12+ mto_route = self .env .ref ("stock.route_warehouse0_mto" , raise_if_not_found = False )
13+
14+ if "route_ids" not in values or not mto_route :
1215 return super ().write (values )
16+
1317 # As _compute_is_mto cannot use api.depends (or it would reset MTO
1418 # route on variants as soon as there is a change on the template routes),
1519 # we need to check which template in self had MTO route activated
1620 # or deactivated to force the recomputation of is_mto on variants
17- mto_route = self .env .ref ("stock.route_warehouse0_mto" )
18- template_not_mto_before = self .filtered (lambda t : mto_route not in t .route_ids )
21+
22+ templates_not_mto_before = self .filtered (lambda t : mto_route not in t .route_ids )
23+
1924 res = super ().write (values )
25+
2026 templates_mto_after = self .filtered (lambda t : mto_route in t .route_ids )
21- templates_mto_added = template_not_mto_before & templates_mto_after
22- templates_mto_removed = (self - template_not_mto_before ) & (
23- self - templates_mto_after
24- )
27+ templates_mto_added = templates_not_mto_before & templates_mto_after
28+ templates_mto_removed = self - templates_mto_after - templates_not_mto_before
29+
2530 (
2631 templates_mto_added | templates_mto_removed
2732 ).product_variant_ids ._compute_is_mto ()
33+
2834 return res
2935
3036 @api .onchange ("route_ids" )
3137 def onchange_route_ids (self ):
32- mto_route = self .env .ref ("stock.route_warehouse0_mto" , raise_if_not_found = False )
33- if (
34- mto_route not in self ._origin .route_ids
35- and mto_route in self .route_ids ._origin
36- ):
38+ mto_routes = self .env ["stock.route" ].search ([("is_mto" , "=" , True )])
39+ if not mto_routes :
40+ return
41+
42+ origin_routes = (
43+ self ._origin .route_ids if self ._origin else self .env ["stock.route" ]
44+ )
45+ current_routes = (
46+ self .route_ids ._origin if self .route_ids else self .env ["stock.route" ]
47+ )
48+
49+ added_routes = current_routes - origin_routes
50+ if set (mto_routes .ids ) & set (added_routes .ids ):
3751 # Return warning activating MTO route
3852 return {
3953 "warning" : {
@@ -44,10 +58,9 @@ def onchange_route_ids(self):
4458 ),
4559 }
4660 }
47- if (
48- mto_route in self ._origin .route_ids
49- and mto_route not in self .route_ids ._origin
50- ):
61+
62+ removed_routes = origin_routes - current_routes
63+ if set (mto_routes .ids ) & set (removed_routes .ids ):
5164 # Return warning deactivating MTO route
5265 return {
5366 "warning" : {
0 commit comments