You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

88 lines
4.1 KiB

# -*- coding: utf-8 -*-
#############################################################################
#
# Cybrosys Technologies Pvt. Ltd.
#
# Copyright (C) 2023-TODAY Cybrosys Technologies(<https://www.cybrosys.com>).
# Author: Ayisha Sumayya K, Vivek (odoo@cybrosys.com)
#
# You can modify it under the terms of the GNU AFFERO
# GENERAL PUBLIC LICENSE (AGPL v3), Version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU AFFERO GENERAL PUBLIC LICENSE (AGPL v3) for more details.
#
# You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
# (AGPL v3) along with this program.
# If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
from odoo import api, fields, models
class ProductTemplate(models.Model):
"""
Model for representing product templates with additional fields and methods.
Inherits from 'product.template' model.
"""
_inherit = 'product.template'
product_config_mode = fields.Selection(selection=[('configurator',
"Product Configurator"),
('matrix',
"Order Grid Entry")],
string="Add product mode",
default='configurator',
help="Configurator: choose "
"attribute values to add "
"the matching product variant"
" to the order. "
"\nGrid: add several variants"
" at once from the grid "
"of attribute values")
@api.depends('attribute_line_ids.value_ids.is_custom',
'attribute_line_ids.attribute_id.create_variant')
def _compute_has_configurable_attributes(self):
""" A product is considered configurable if:
- It has dynamic attributes
- It has any attribute line with at least 2 attribute values configured
- It has at least one custom attribute value """
for product in self:
product.has_configurable_attributes = (
any(attribute.create_variant == 'dynamic' for attribute in
product.attribute_line_ids.attribute_id)
or any(len(attribute_line_id.value_ids) >=
2 for attribute_line_id in
product.attribute_line_ids)
or any(attribute_value.is_custom for attribute_value in
product.attribute_line_ids.value_ids)
)
def get_single_product_variant(self):
""" Method used by the product configurator to check if the product is
configurable or not.
We need to open the product configurator if the product:
- is configurable (see has_configurable_attributes)
- has optional products """
res = super().get_single_product_variant()
if res.get('product_id', False):
has_optional_products = False
for optional_product in \
self.product_variant_id.optional_product_ids:
if optional_product.has_dynamic_attributes() or \
optional_product._get_possible_variants(
self.product_variant_id.
product_template_attribute_value_ids
):
has_optional_products = True
break
res.update({'has_optional_products': has_optional_products})
if self.has_configurable_attributes:
res['mode'] = self.product_config_mode
else:
res['mode'] = 'configurator'
return res