Skip to content

Commit 4046940

Browse files
committed
Add mto_route_product_variant
1 parent 8c24218 commit 4046940

File tree

14 files changed

+287
-0
lines changed

14 files changed

+287
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../stock_product_variant_mto
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import setuptools
2+
3+
setuptools.setup(
4+
setup_requires=['setuptools-odoo'],
5+
odoo_addon=True,
6+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2023 Camptocamp SA
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
3+
4+
{
5+
"name": "Stock Product Variant MTO",
6+
"summary": "Allow to individually set variants as MTO",
7+
"version": "14.0.1.0.0",
8+
"development_status": "Alpha",
9+
"category": "Inventory",
10+
"website": "https://github.com/OCA/stock-workflow",
11+
"author": "Camptocamp SA, Odoo Community Association (OCA)",
12+
"maintainers": ["mmequignon"],
13+
"license": "AGPL-3",
14+
"installable": True,
15+
"auto_install": False,
16+
"depends": ["stock"],
17+
"data": ["views/product_product.xml"],
18+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import product_product
2+
from . import product_template
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2023 Camptocamp SA
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
3+
4+
from odoo import api, models, fields
5+
6+
IS_MTO_HELP = """
7+
Check or Uncheck this field to enable the Make To Order on the variant,
8+
independantly from its template configuration.
9+
"""
10+
11+
class ProductProduct(models.Model):
12+
_inherit = "product.product"
13+
14+
is_mto = fields.Boolean(
15+
string="Variant is MTO",
16+
compute="_compute_is_mto",
17+
inverse="_inverse_is_mto",
18+
store=True,
19+
help=IS_MTO_HELP,
20+
)
21+
22+
@api.depends("product_tmpl_id.route_ids")
23+
def _compute_is_mto(self):
24+
# We only want to force all variants `is_mto` to True when
25+
# the mto route is explicitely set on its template.
26+
# Watching the mto route is not enough, since we might
27+
# have a template with the mto route, and disabled the mto route
28+
# for a few of its variants
29+
# If a user sets another route on the variant, we do not want the
30+
# mto disabled variants to be updated.
31+
# To ensure that, the created `has_mto_route_changed` boolean field
32+
# is set to True only when the MTO route is set on a template.
33+
mto_route = self.env.ref("stock.route_warehouse0_mto", raise_if_not_found=False)
34+
templates = self.product_tmpl_id
35+
# Only variants with a template with the MTO route and has_mto_route_changed
36+
# should be updated.
37+
mto_templates = templates.filtered(
38+
lambda t: mto_route in t.route_ids and t.has_mto_route_changed
39+
)
40+
mto_variants = self.filtered(lambda p: p.product_tmpl_id in mto_templates)
41+
mto_variants.is_mto = True
42+
# For the other variants, keep their current value.
43+
other_variants = self - mto_variants
44+
for variant in other_variants:
45+
variant.is_mto = variant.is_mto
46+
# Then set template's has_mto_route_changed to False, as it
47+
# has been handled above
48+
templates.has_mto_route_changed = False
49+
50+
def _inverse_is_mto(self):
51+
# When all variants of a template are `is_mto == False`, drop the MTO route
52+
# from the template, otherwise do nothing
53+
mto_route = self.env.ref("stock.route_warehouse0_mto", raise_if_not_found=False)
54+
for template in self.product_tmpl_id:
55+
is_mto = False
56+
for variant in template.product_variant_ids:
57+
if variant.is_mto:
58+
is_mto = True
59+
break
60+
# If no variant is mto, then drop the route of the template.
61+
if not is_mto:
62+
template.route_ids = [(3, mto_route.id, 0)]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2023 Camptocamp SA
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
3+
4+
from odoo import models, fields
5+
6+
class ProductTemplate(models.Model):
7+
_inherit = "product.template"
8+
9+
has_mto_route_changed = fields.Boolean()
10+
11+
def write(self, values):
12+
if not "route_ids" in values:
13+
return super().write(values)
14+
# This route_ids change will trigger variant's _compute_is_mto.
15+
# We want to set variant's is_mto to True only if their
16+
# template has been set to True here ↓
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)
19+
res = super().write(values)
20+
template_mto_after = self.filtered(lambda t: mto_route in t.route_ids)
21+
# Templates where mto route has changed are those where
22+
# the mto route has been set
23+
templates_mto_set = template_not_mto_before & template_mto_after
24+
templates_mto_set.has_mto_route_changed = True
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Matthieu Méquignon <[email protected]>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow to individually set variants as Make To Order
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This module is useless on its own.
2+
3+
However, it is meant to give the ability to check if a product variant is MTO,
4+
rather than checking its template's routes.

0 commit comments

Comments
 (0)