forked from OCA/sale-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduct_template.py
More file actions
45 lines (39 loc) · 1.77 KB
/
product_template.py
File metadata and controls
45 lines (39 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Copyright 2019 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
class ProductTemplate(models.Model):
_inherit = "product.template"
is_elaboration = fields.Boolean()
elaboration_profile_id = fields.Many2one(
comodel_name="product.elaboration.profile",
compute="_compute_elaboration_profile_id",
inverse="_inverse_elaboration_profile_id",
store=True,
help="Keep this field empty to use the default value from the product "
"category.",
)
@api.depends("product_variant_ids", "product_variant_ids.elaboration_profile_id")
def _compute_elaboration_profile_id(self):
unique_variants = self.filtered(lambda tmpl: tmpl.product_variant_count == 1)
for template in unique_variants:
template.elaboration_profile_id = (
template.product_variant_ids.elaboration_profile_id
)
for template in self - unique_variants:
template.elaboration_profile_id = False
def _inverse_elaboration_profile_id(self):
for template in self:
if len(template.product_variant_ids) == 1:
template.product_variant_ids.elaboration_profile_id = (
template.elaboration_profile_id
)
@api.model_create_multi
def create(self, vals_list):
templates = super().create(vals_list)
# This is needed to set given values to first variant after creation
for template, vals in zip(templates, vals_list, strict=True):
if vals.get("elaboration_profile_id"):
template.write(
{"elaboration_profile_id": vals["elaboration_profile_id"]}
)
return templates