From 263164fc0eb2df5ffff3458645f6a8472a0cf0b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20V=C3=A9zina?= Date: Sat, 30 Dec 2023 20:06:04 -0500 Subject: [PATCH] still working on is_property --- .../models/res_partner.py | 96 ++++++++++--------- .../views/res_partner_views.xml | 3 +- 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/bemade_sethy_configuration/models/res_partner.py b/bemade_sethy_configuration/models/res_partner.py index d8d4c15..e3f89b7 100644 --- a/bemade_sethy_configuration/models/res_partner.py +++ b/bemade_sethy_configuration/models/res_partner.py @@ -29,7 +29,8 @@ class ResPartner(models.Model): is_property = fields.Boolean( string='Is Property', default=False, - compute='_compute_is_property' + compute='_compute_is_property', + store=True ) # True if property, False if not interest_level = fields.Selection( @@ -54,10 +55,24 @@ class ResPartner(models.Model): string="Property ids" ) - @api.depends('relation_owner_ids') + @api.onchange('category_id') + @api.depends('category_id') def _compute_is_property(self): - for partner in self: - partner.is_property = partner.category_id.name == 'Property' + property_tag = self.env.ref('bemade_sethy_configuration.partner_tag_property', raise_if_not_found=False) + for record 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 + else: + record.is_property = record.category_id & property_tag + + @api.depends('relation_property_ids') + def _compute_property_count(self): + for record in self: + record.property_count = len(record.relation_property_ids) + record.is_owner = record.property_count > 0 @api.depends('relation_all_ids') def _compute_owner_ids(self): @@ -66,7 +81,6 @@ class ResPartner(models.Model): lambda line: ( line.type_id.name == 'Owner' and not line.is_inverse) ) - @api.depends('relation_all_ids') def _compute_property_ids(self): for record in self: @@ -74,53 +88,45 @@ class ResPartner(models.Model): lambda line: ( line.type_id.name == 'Owner' and line.is_inverse) ) - - @api.onchange('is_property') - def _onchange_is_property(self): - """ Set is_company to True when is_property is set to True. """ - if self.is_property: - self.is_company = True - - @api.onchange('is_company') - def _onchange_is_company(self): - """ Set is_property to False when is_company is set to False. """ - if not self.is_company: - self.is_property = False - @api.onchange('lot_number') def _onchange_lot_number(self): for lot in self: if lot.lot_number: lot.name = "Lot " + str(lot.lot_number) - @api.depends('relation_property_ids') - def _compute_property_count(self): - for record in self: - record.property_count = len(record.relation_property_ids) - record.is_owner = record.property_count > 0 + # @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): - # 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. + # 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: - # 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 + 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 diff --git a/bemade_sethy_configuration/views/res_partner_views.xml b/bemade_sethy_configuration/views/res_partner_views.xml index 745c4c1..9365d27 100644 --- a/bemade_sethy_configuration/views/res_partner_views.xml +++ b/bemade_sethy_configuration/views/res_partner_views.xml @@ -7,8 +7,7 @@ -