From cc8677c8f9f24fa2242bd2bd54dcc67fb23f9adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20V=C3=A9zina?= Date: Sun, 31 Dec 2023 15:32:54 -0500 Subject: [PATCH] more function --- bemade_sethy_configuration/__init__.py | 2 + bemade_sethy_configuration/__manifest__.py | 2 + bemade_sethy_configuration/models/__init__.py | 1 + .../models/res_config_settings.py | 12 +++ .../models/res_partner.py | 90 ++++++++++-------- .../security/ir.model.access.csv | 3 + .../views/property_view.xml | 27 +++++- .../views/res_partner_views.xml | 93 ++++++++++++------- bemade_sethy_configuration/wizard/__init__.py | 1 + .../wizard/create_property_wizard.py | 10 ++ .../wizard/select_property_wizard.py | 10 ++ 11 files changed, 173 insertions(+), 78 deletions(-) create mode 100644 bemade_sethy_configuration/models/res_config_settings.py create mode 100644 bemade_sethy_configuration/security/ir.model.access.csv create mode 100644 bemade_sethy_configuration/wizard/__init__.py create mode 100644 bemade_sethy_configuration/wizard/create_property_wizard.py create mode 100644 bemade_sethy_configuration/wizard/select_property_wizard.py diff --git a/bemade_sethy_configuration/__init__.py b/bemade_sethy_configuration/__init__.py index 95dfc91..d94c5f5 100644 --- a/bemade_sethy_configuration/__init__.py +++ b/bemade_sethy_configuration/__init__.py @@ -1,2 +1,4 @@ # -*- coding: utf-8 -*- from . import models +from . import wizard + diff --git a/bemade_sethy_configuration/__manifest__.py b/bemade_sethy_configuration/__manifest__.py index 3770f67..6dbd6c9 100644 --- a/bemade_sethy_configuration/__manifest__.py +++ b/bemade_sethy_configuration/__manifest__.py @@ -35,6 +35,8 @@ # 'views/membership_membership_line.xml', 'views/property_view.xml', 'views/res_partner_views.xml', + 'security/ir.model.access.csv', + ], 'demo': [ # Reference to demo data files diff --git a/bemade_sethy_configuration/models/__init__.py b/bemade_sethy_configuration/models/__init__.py index 91fed54..0d7a048 100644 --- a/bemade_sethy_configuration/models/__init__.py +++ b/bemade_sethy_configuration/models/__init__.py @@ -1 +1,2 @@ from . import res_partner +from . import res_config_settings diff --git a/bemade_sethy_configuration/models/res_config_settings.py b/bemade_sethy_configuration/models/res_config_settings.py new file mode 100644 index 0000000..b89d6b0 --- /dev/null +++ b/bemade_sethy_configuration/models/res_config_settings.py @@ -0,0 +1,12 @@ +from odoo import models, api + +class ResConfigSettings(models.TransientModel): + _inherit = 'res.config.settings' + + def set_values(self): + res = super().set_values() + category_id = self.env.ref('bemade_sethy_configuration.partner_tag_property', raise_if_not_found=False) + # If the category_id record exists, we set its ID in ir.config_parameter + if category_id: + self.env['ir.config_parameter'].sudo().set_param('bemade_sethy_configuration.partner_tag_property', category_id.id) + return res \ No newline at end of file diff --git a/bemade_sethy_configuration/models/res_partner.py b/bemade_sethy_configuration/models/res_partner.py index e3f89b7..d2a2078 100644 --- a/bemade_sethy_configuration/models/res_partner.py +++ b/bemade_sethy_configuration/models/res_partner.py @@ -7,6 +7,10 @@ from odoo.addons.website.models import ir_http class ResPartner(models.Model): _inherit = 'res.partner' + @api.model + def _property_category_id(self): + return self.env.ref('bemade_sethy_configuration.partner_tag_property', raise_if_not_found=False) + surface = fields.Float(string='Surface (ha)') # terrain lot_number = fields.Text(string='Lot Number') # terrain @@ -30,6 +34,7 @@ class ResPartner(models.Model): string='Is Property', default=False, compute='_compute_is_property', + inverse='_inverse_is_property', store=True ) # True if property, False if not @@ -55,18 +60,25 @@ class ResPartner(models.Model): string="Property ids" ) - @api.onchange('category_id') @api.depends('category_id') def _compute_is_property(self): property_tag = self.env.ref('bemade_sethy_configuration.partner_tag_property', raise_if_not_found=False) - for record in self: + for partner in self: # Check if the property tag exists. If it doesn't, set is_property to False. # Because module installation order is not guaranteed, the tag may not exist yet when this method is called. # This is due to store=True on the is_property field. if property_tag is None: - record.is_property = False + partner.is_property = False else: - record.is_property = record.category_id & property_tag + partner.is_property = property_tag and property_tag in partner.category_id + + def _inverse_is_property(self): + property_tag = self.env.ref('bemade_sethy_configuration.partner_tag_property', raise_if_not_found=False) + for partner in self: + if partner.is_property and property_tag not in partner.category_id and property_tag: + partner.category_id |= property_tag + elif not partner.is_property and property_tag in partner.category_id: + partner.category_id -= property_tag @api.depends('relation_property_ids') def _compute_property_count(self): @@ -86,7 +98,7 @@ class ResPartner(models.Model): for record in self: record.relation_property_ids = record.relation_all_ids.filtered( lambda line: ( - line.type_id.name == 'Owner' and line.is_inverse) + line.type_id.name == 'Owner' and line.is_inverse) ) @api.onchange('lot_number') def _onchange_lot_number(self): @@ -94,39 +106,39 @@ class ResPartner(models.Model): if lot.lot_number: lot.name = "Lot " + str(lot.lot_number) - # @api.model_create_multi - # def create(self, vals_list): - # # Get the property tag reference outside the loop to avoid repeated searches. - # property_tag = self.env.ref('bemade_sethy_configuration.partner_tag_property', raise_if_not_found=False) - # # Iterate over each set of values in the list. - # for vals in vals_list: - # # Check if 'is_property' is in the values of the current record. - # if 'is_property' in vals: - # if vals['is_property'] and property_tag: - # # Add the tag to category_id if is_property is True. - # vals['category_id'] = [(4, property_tag.id)] - # elif not vals['is_property'] and property_tag: - # # Remove the tag from category_id if is_property is False. - # vals['category_id'] = [(3, property_tag.id)] - # # Call super and pass the modified vals_list. - # return super(ResPartner, self).create(vals_list) - # - # @api.onchange('is_property') - # def _inverse_is_property(self): - # property_tag = self.env.ref('bemade_sethy_configuration.partner_tag_property', raise_if_not_found=False) - # for partner in self: - # if partner.is_property: - # partner.category_id |= property_tag - # else: - # partner.category_id -= property_tag - @api.model_create_multi def create(self, vals_list): - # if 'state_id' not in values or 'country_id' not in values, then set it from the current user's company - for vals in vals_list: - if 'state_id' not in vals or 'country_id' not in vals: - user_company = self.env.user.company_id - if 'state_id' not in vals and user_company.state_id: - vals['state_id'] = user_company.state_id.id - vals['country_id'] = user_company.country_id.id - return super(Partner, self).create(vals) \ No newline at end of file + # Create Partner records using the super function + records = super().create(vals_list) + user_company = self.env.user.company_id + + # Update the records where 'state_id' or 'country_id' isn't provided + for record in records: + if not record.state_id or not record.country_id: + if not record.state_id and user_company.state_id: + record.write({ + 'state_id': user_company.state_id.id, + }) + if not record.country_id and user_company.country_id: + record.write({ + 'country_id': user_company.country_id.id + }) + if record.name.startswith("Lot "): + record.lot_number = record.name[4:] + record._inverse_is_property() + return records + + @api.onchange('name') + def _onchange_name(self): + for record in self: + if record.name and record.name.startswith("Lot "): + record.lot_number = record.name[4:] + + def write(self, vals): + result = super(ResPartner, self).write(vals) + self._inverse_is_property() + return result + + @api.onchange('category_id') + def _onchange_category_id(self): + self._compute_is_property() \ No newline at end of file diff --git a/bemade_sethy_configuration/security/ir.model.access.csv b/bemade_sethy_configuration/security/ir.model.access.csv new file mode 100644 index 0000000..2c6ae27 --- /dev/null +++ b/bemade_sethy_configuration/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_create_property_wizard_salesman,"Access Create Property Wizard Salesman",model_create_property_wizard,sales_team.group_sale_salesman,1,1,1,1 +access_create_property_wizard_manager,"Access Create Property Wizard Manager",model_create_property_wizard,sales_team.group_sale_manager,1,1,1,1 \ No newline at end of file diff --git a/bemade_sethy_configuration/views/property_view.xml b/bemade_sethy_configuration/views/property_view.xml index bda88ac..a0d34ee 100644 --- a/bemade_sethy_configuration/views/property_view.xml +++ b/bemade_sethy_configuration/views/property_view.xml @@ -1,6 +1,23 @@ + + res.partner + + + + + + + + + + + + + + + Properties res.partner tree,form + [('is_property','=',True)] + {'default_is_property': True} \ No newline at end of file diff --git a/bemade_sethy_configuration/views/res_partner_views.xml b/bemade_sethy_configuration/views/res_partner_views.xml index 9365d27..c3914b7 100644 --- a/bemade_sethy_configuration/views/res_partner_views.xml +++ b/bemade_sethy_configuration/views/res_partner_views.xml @@ -1,13 +1,20 @@ + + Add Property + create.property.wizard + form + new + + bemade_sethy_configuration.res_partner.defaults.form res.partner - + @@ -65,46 +72,52 @@ - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + +