8 changed files with 122 additions and 7 deletions
			
			
		@ -0,0 +1,62 @@ | 
				
			|||||
 | 
					# -*- coding: utf-8 -*- | 
				
			||||
 | 
					############################################################################# | 
				
			||||
 | 
					# | 
				
			||||
 | 
					#    Cybrosys Technologies Pvt. Ltd. | 
				
			||||
 | 
					# | 
				
			||||
 | 
					#    Copyright (C) 2024-TODAY Cybrosys Technologies(<https://www.cybrosys.com>) | 
				
			||||
 | 
					#    Author: Ammu Raj (<https://www.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): | 
				
			||||
 | 
					    """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 | 
				
			||||
@ -0,0 +1,23 @@ | 
				
			|||||
 | 
					<?xml version="1.0" encoding="UTF-8"?> | 
				
			||||
 | 
					<odoo> | 
				
			||||
 | 
					    <!-- Added the fields in the product template form --> | 
				
			||||
 | 
					    <record id="product_template_only_form_view" model="ir.ui.view"> | 
				
			||||
 | 
					        <field name="name">product.template.form.inherit.tooltip</field> | 
				
			||||
 | 
					        <field name="model">product.template</field> | 
				
			||||
 | 
					        <field name="inherit_id" ref="product.product_template_only_form_view"/> | 
				
			||||
 | 
					        <field name="arch" type="xml"> | 
				
			||||
 | 
					            <xpath expr="//field[@name='product_tooltip']" position="after"> | 
				
			||||
 | 
					                <field name="is_need_secondary_uom" groups="uom.group_uom"/> | 
				
			||||
 | 
					                <field name="secondary_uom_ids" | 
				
			||||
 | 
					                       invisible="is_need_secondary_uom == False" | 
				
			||||
 | 
					                       groups="uom.group_uom" force_save="1"> | 
				
			||||
 | 
					                    <list name="Secondary"> | 
				
			||||
 | 
					                        <field name="secondary_uom_id"/> | 
				
			||||
 | 
					                        <field name="secondary_uom_ratio" column_invisible="1"/> | 
				
			||||
 | 
					                        <field name="example_ratio" force_save="1"/> | 
				
			||||
 | 
					                    </list> | 
				
			||||
 | 
					                </field> | 
				
			||||
 | 
					            </xpath> | 
				
			||||
 | 
					        </field> | 
				
			||||
 | 
					    </record> | 
				
			||||
 | 
					</odoo> | 
				
			||||
					Loading…
					
					
				
		Reference in new issue