From 7e65e05b65f32f2d2a760cebba86aa1ba80d9bac Mon Sep 17 00:00:00 2001 From: AjmalCybro Date: Wed, 9 Jul 2025 11:10:55 +0530 Subject: [PATCH] [UPDT] Jul 9: Updted 'product_multi_uom' --- product_multi_uom/__manifest__.py | 3 +- product_multi_uom/doc/RELEASE_NOTES.md | 5 ++ product_multi_uom/models/__init__.py | 1 + product_multi_uom/models/product_product.py | 13 +++- product_multi_uom/models/product_template.py | 62 +++++++++++++++++++ product_multi_uom/models/sale_order_line.py | 18 ++++-- .../models/secondary_uom_line.py | 4 ++ .../views/product_template_views.xml | 23 +++++++ 8 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 product_multi_uom/models/product_template.py create mode 100644 product_multi_uom/views/product_template_views.xml diff --git a/product_multi_uom/__manifest__.py b/product_multi_uom/__manifest__.py index ef2f778d2..c486e5497 100644 --- a/product_multi_uom/__manifest__.py +++ b/product_multi_uom/__manifest__.py @@ -21,7 +21,7 @@ ############################################################################# { 'name': "Product Multi UoM", - 'version': '18.0.1.0.1', + 'version': '18.0.1.1.2', 'category': 'Sales', 'summary': 'This module help to sell a product with multiple Uom category', 'description': "This versatile module empowers your sales strategy by " @@ -38,6 +38,7 @@ 'data': [ 'security/ir.model.access.csv', 'views/product_product_views.xml', + 'views/product_template_views.xml', 'views/sale_order_line_views.xml', ], 'images': ['static/description/banner.png'], diff --git a/product_multi_uom/doc/RELEASE_NOTES.md b/product_multi_uom/doc/RELEASE_NOTES.md index c29d20521..f11930c22 100644 --- a/product_multi_uom/doc/RELEASE_NOTES.md +++ b/product_multi_uom/doc/RELEASE_NOTES.md @@ -10,3 +10,8 @@ ##### Update - Updated the field position in the product_product model and the _onchange_secondary_uom_id function in the secondary_uom_line model. Also updated the index images. + +#### 08.07.2025 +#### Version 18.0.1.1.2 +##### Update +- Updated the module, add the feature for product model and variant model to ensure smooth functioning for products without any variants, updated view and updated the function. diff --git a/product_multi_uom/models/__init__.py b/product_multi_uom/models/__init__.py index 08d95ed4d..70e21895c 100644 --- a/product_multi_uom/models/__init__.py +++ b/product_multi_uom/models/__init__.py @@ -22,3 +22,4 @@ from . import product_product from . import sale_order_line from . import secondary_uom_line +from . import product_template diff --git a/product_multi_uom/models/product_product.py b/product_multi_uom/models/product_product.py index c731332a8..e22d4dfb0 100644 --- a/product_multi_uom/models/product_product.py +++ b/product_multi_uom/models/product_product.py @@ -34,13 +34,14 @@ class ProductProduct(models.Model): help='Select the secondary UoM and ' 'their ratio', store=True) - @api.onchange('is_need_secondary_uom') + @api.onchange('is_need_secondary_uom', 'uom_id') def _onchange_is_need_secondary_uom(self): """Function that write the default Uom and their ratio to the secondary uom""" base_uom = self.env['uom.uom'].sudo().search( [('category_id', '=', self.uom_id.category_id.id)]) - if not self.secondary_uom_ids: + if not self.secondary_uom_ids or self.uom_id.id not in self.secondary_uom_ids.mapped('secondary_uom_id').ids: + self.secondary_uom_ids = [fields.Command.clear()] for uom in base_uom: self.write({ 'secondary_uom_ids': [(0, 0, { @@ -50,3 +51,11 @@ class ProductProduct(models.Model): f" {self.uom_id.name}", })] }) + + @api.model_create_multi + def create(self, vals_list): + """ Assign the default value to the secondary uom's """ + res = super().create(vals_list) + for rec in res: + rec._onchange_is_need_secondary_uom() + return res diff --git a/product_multi_uom/models/product_template.py b/product_multi_uom/models/product_template.py new file mode 100644 index 000000000..d0b3da031 --- /dev/null +++ b/product_multi_uom/models/product_template.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +############################################################################# +# +# Cybrosys Technologies Pvt. Ltd. +# +# Copyright (C) 2024-TODAY Cybrosys Technologies() +# Author: Ammu Raj () +# +# 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 . +# +############################################################################# +from odoo import api, fields, models + + +class ProductTemplate(models.Model): + """Inherits the 'product.template' for adding the secondary uom""" + _inherit = "product.template" + + is_need_secondary_uom = fields.Boolean(string="Need Secondary UoM's", + help="Enable this field for " + "using the secondary uom") + secondary_uom_ids = fields.One2many('secondary.uom.line', 'product_template_id', + string="Secondary UoM's", + help='Select the secondary UoM and ' + 'their ratio', store=True) + + @api.onchange('is_need_secondary_uom', 'uom_id') + def _onchange_is_need_secondary_uom(self): + """Function that write the default Uom and their ratio to the + secondary uom""" + base_uom = self.env['uom.uom'].sudo().search( + [('category_id', '=', self.uom_id.category_id.id)]) + if not self.secondary_uom_ids or self.uom_id.id not in self.secondary_uom_ids.mapped('secondary_uom_id').ids: + self.secondary_uom_ids = [fields.Command.clear()] + for uom in base_uom: + self.write({ + 'secondary_uom_ids': [(0, 0, { + 'secondary_uom_id': uom.id, + 'secondary_uom_ratio': float(uom.factor_inv), + 'example_ratio': f" 1 {uom.name} = {uom.factor_inv}" + f" {self.uom_id.name}", + })] + }) + + @api.model_create_multi + def create(self, vals_list): + """Assign default value to the product variants""" + res = super().create(vals_list) + for product in res: + if product.is_need_secondary_uom: + product.product_variant_ids.is_need_secondary_uom = True + return res diff --git a/product_multi_uom/models/sale_order_line.py b/product_multi_uom/models/sale_order_line.py index d41a8d569..3028e06bb 100644 --- a/product_multi_uom/models/sale_order_line.py +++ b/product_multi_uom/models/sale_order_line.py @@ -45,21 +45,31 @@ class SaleOrderLine(models.Model): " secondary and if yes then " "make the field readonly") + @api.onchange('product_template_id', 'product_id') + def _onchange_is_secondary_readonly(self): + """ Function to assign value to field is_secondary_readonly """ + self.is_secondary_readonly = self.product_id.is_need_secondary_uom or self.product_template_id.is_need_secondary_uom + @api.onchange('secondary_product_uom_id', 'secondary_product_uom_qty') def _onchange_secondary_product_uom_id(self): """Function that update the product_uom_qty as the value in the secondary uom quantity""" all_uom = [] - if self.product_id.is_need_secondary_uom: + domain = [('secondary_uom_id', '=', self.secondary_product_uom_id.id)] + if self.product_template_id.attribute_line_ids and self.product_id.is_need_secondary_uom: self.is_secondary_readonly = True + domain.append(('product_id', '=', self.product_id.id)) for uom in self.product_id.secondary_uom_ids: all_uom.append(uom.secondary_uom_id.id) + elif self.product_template_id.is_need_secondary_uom: + self.is_secondary_readonly = True + domain.append(('product_template_id', '=', self.product_template_id.id)) + for uom in self.product_template_id.secondary_uom_ids: + all_uom.append(uom.secondary_uom_id.id) if self.is_secondary_readonly: self.product_uom_readonly = True if self.secondary_product_uom_id.id in all_uom: - primary_uom_ratio = self.env['secondary.uom.line'].search( - [('secondary_uom_id', '=', self.secondary_product_uom_id.id), - ('product_id', '=', self.product_id.id)]).mapped( + primary_uom_ratio = self.env['secondary.uom.line'].search(domain).mapped( 'secondary_uom_ratio') converted_uom_qty = primary_uom_ratio[ 0] * self.secondary_product_uom_qty diff --git a/product_multi_uom/models/secondary_uom_line.py b/product_multi_uom/models/secondary_uom_line.py index f0960eaf5..79a66dec2 100644 --- a/product_multi_uom/models/secondary_uom_line.py +++ b/product_multi_uom/models/secondary_uom_line.py @@ -37,6 +37,9 @@ class SecondaryUomLine(models.Model): product_id = fields.Many2one('product.product', readonly=True, string="Product", help="Product having the Secondary UOM") + product_template_id = fields.Many2one('product.template', readonly=True, + string="Product", + help="Product having the Secondary UOM") secondary_uom_ratio = fields.Float(string='Secondary UoM Ratio', help="Choose the ratio with the base" " Unit of Measure.") @@ -61,3 +64,4 @@ class SecondaryUomLine(models.Model): raise ValidationError( _('This Unit of Measure is already exist in the secondary' 'uom list. Please select another uom for secondary uom')) + diff --git a/product_multi_uom/views/product_template_views.xml b/product_multi_uom/views/product_template_views.xml new file mode 100644 index 000000000..3a3457f5f --- /dev/null +++ b/product_multi_uom/views/product_template_views.xml @@ -0,0 +1,23 @@ + + + + + product.template.form.inherit.tooltip + product.template + + + + + + + + + + + + + + +