From 923459e4e1522445888a0e1b4442db7abddee030 Mon Sep 17 00:00:00 2001 From: RemiFr82 Date: Mon, 27 May 2024 18:45:07 +0200 Subject: [PATCH] [ADD] res.partner.hierarchy --- ffck_commons/data/ffck_structure_type.xml | 2 +- ffck_commons/i18n/fr.po | 47 ++++++- ffck_commons/models/__init__.py | 1 + ffck_commons/models/res_partner.py | 127 ++++++++++++++++--- ffck_commons/models/res_partner_hierarchy.py | 44 +++++++ ffck_commons/security/ir.model.access.csv | 5 +- ffck_commons/views/res_partner.xml | 38 ++++-- 7 files changed, 229 insertions(+), 35 deletions(-) create mode 100644 ffck_commons/models/res_partner_hierarchy.py diff --git a/ffck_commons/data/ffck_structure_type.xml b/ffck_commons/data/ffck_structure_type.xml index 1fdd133..e5377c7 100644 --- a/ffck_commons/data/ffck_structure_type.xml +++ b/ffck_commons/data/ffck_structure_type.xml @@ -3,7 +3,7 @@ - Fédération + Federation FFCK 1 diff --git a/ffck_commons/i18n/fr.po b/ffck_commons/i18n/fr.po index c9dce0f..59fd25f 100644 --- a/ffck_commons/i18n/fr.po +++ b/ffck_commons/i18n/fr.po @@ -6,8 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 16.0-20230613\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-05-26 23:35+0000\n" -"PO-Revision-Date: 2024-05-26 23:35+0000\n" +"POT-Creation-Date: 2024-05-27 16:36+0000\n" +"PO-Revision-Date: 2024-05-27 16:36+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -63,6 +63,12 @@ msgstr "Créé le" msgid "Departmental" msgstr "Départementale" +#. module: ffck_commons +#: model:ir.model.fields,field_description:ffck_commons.field_res_partner__cdck_partner_ids +#: model:ir.model.fields,field_description:ffck_commons.field_res_users__cdck_partner_ids +msgid "Departmental Comittees" +msgstr "Comités départementaux" + #. module: ffck_commons #: model:ffck.structure.type,name:ffck_commons.ffck_structure_type_cdck msgid "Departmental Committee" @@ -70,6 +76,7 @@ msgstr "Comité Départemental" #. module: ffck_commons #: model:ir.model.fields,field_description:ffck_commons.field_ffck_structure_type__display_name +#: model:ir.model.fields,field_description:ffck_commons.field_res_partner_hierarchy__display_name msgid "Display Name" msgstr "Nom d'affichage" @@ -84,7 +91,7 @@ msgstr "" #: model:ir.model.fields,field_description:ffck_commons.field_res_partner__cdck_partner_code #: model:ir.model.fields,field_description:ffck_commons.field_res_partner__crck_partner_code #: model:ir.model.fields,field_description:ffck_commons.field_res_partner__local_partner_code -#: model:ir.model.fields,field_description:ffck_commons.field_res_partner__partner_code +#: model:ir.model.fields,field_description:ffck_commons.field_res_partner__partner_code #: model:ir.model.fields,field_description:ffck_commons.field_res_users__cdck_partner_code #: model:ir.model.fields,field_description:ffck_commons.field_res_users__crck_partner_code #: model:ir.model.fields,field_description:ffck_commons.field_res_users__local_partner_code @@ -130,13 +137,26 @@ msgstr "Types de structures" msgid "Federation" msgstr "Fédération" +#. module: ffck_commons +#: model_terms:ir.ui.view,arch_db:ffck_commons.view_res_partner_filter_inherit_base +msgid "Hierarchy" +msgstr "Hiérarchie" + +#. module: ffck_commons +#: model:ir.model.fields,field_description:ffck_commons.field_res_partner__hierarchy_id +#: model:ir.model.fields,field_description:ffck_commons.field_res_users__hierarchy_id +msgid "Hierarchy element" +msgstr "Hiérarchie" + #. module: ffck_commons #: model:ir.model.fields,field_description:ffck_commons.field_ffck_structure_type__id +#: model:ir.model.fields,field_description:ffck_commons.field_res_partner_hierarchy__id msgid "ID" msgstr "" #. module: ffck_commons #: model:ir.model.fields,field_description:ffck_commons.field_ffck_structure_type____last_update +#: model:ir.model.fields,field_description:ffck_commons.field_res_partner_hierarchy____last_update msgid "Last Modified on" msgstr "Dernière modification le" @@ -178,8 +198,15 @@ msgstr "Locale" msgid "Local partner" msgstr "Structure locale" +#. module: ffck_commons +#: model:ir.model.fields,field_description:ffck_commons.field_res_partner__local_partner_ids +#: model:ir.model.fields,field_description:ffck_commons.field_res_users__local_partner_ids +msgid "Local structures" +msgstr "Structures locales" + #. module: ffck_commons #: model:ir.model.fields,field_description:ffck_commons.field_ffck_structure_type__name +#: model:ir.model.fields,field_description:ffck_commons.field_res_partner_hierarchy__name msgid "Name" msgstr "Nom" @@ -196,6 +223,7 @@ msgstr "Réseau fédéral" #. module: ffck_commons #: model:ir.model.fields,field_description:ffck_commons.field_ffck_structure_type__parent_id +#: model:ir.model.fields,field_description:ffck_commons.field_res_partner_hierarchy__parent_id msgid "Parent" msgstr "" @@ -204,6 +232,11 @@ msgstr "" msgid "Partner scale" msgstr "Échelle" +#. module: ffck_commons +#: model:ir.model,name:ffck_commons.model_res_partner_hierarchy +msgid "Partners hierarchy" +msgstr "Hiérarchie des partenaires" + #. module: ffck_commons #: model_terms:ir.ui.view,arch_db:ffck_commons.view_res_partner_filter_inherit_base msgid "Persons" @@ -215,6 +248,12 @@ msgstr "Personnes" msgid "Regional" msgstr "Régionale" +#. module: ffck_commons +#: model:ir.model.fields,field_description:ffck_commons.field_res_partner__crck_partner_ids +#: model:ir.model.fields,field_description:ffck_commons.field_res_users__crck_partner_ids +msgid "Regional Comittees" +msgstr "Comités régionaux" + #. module: ffck_commons #: model:ffck.structure.type,name:ffck_commons.ffck_structure_type_crck msgid "Regional Committee" @@ -268,7 +307,7 @@ msgstr "Types de structure" #. module: ffck_commons #: model_terms:ir.ui.view,arch_db:ffck_commons.view_res_partner_filter_inherit_base msgid "Structures" -msgstr "Structures" +msgstr "" #. module: ffck_commons #: model:ffck.structure.type,name:ffck_commons.ffck_structure_type_agra diff --git a/ffck_commons/models/__init__.py b/ffck_commons/models/__init__.py index 86cfff5..12caeb9 100644 --- a/ffck_commons/models/__init__.py +++ b/ffck_commons/models/__init__.py @@ -1,3 +1,4 @@ # -*- coding: utf-8 -*- +from . import res_partner_hierarchy from . import ffck_structure_type from . import res_partner diff --git a/ffck_commons/models/res_partner.py b/ffck_commons/models/res_partner.py index f4ae7f1..4339219 100644 --- a/ffck_commons/models/res_partner.py +++ b/ffck_commons/models/res_partner.py @@ -29,7 +29,7 @@ class ResPartner(models.Model): string="FFCK partner", ondelete="restrict", compute="_get_ffck_partner", - store=False, + store=True, ) ffck_partner_code = fields.Char(string="FFCK", default="0", readonly=True) # CRCK @@ -62,6 +62,42 @@ class ResPartner(models.Model): local_partner_code = fields.Char( related="local_partner_id.partner_code", store=True ) + crck_partner_ids = fields.One2many( + comodel_name="res.partner", + inverse_name="ffck_partner_id", + domain=[("partner_scale", "=", "2")], + string="Regional Comittees", + ) + cdck_partner_ids = fields.One2many( + comodel_name="res.partner", + inverse_name="crck_partner_id", + domain=[("partner_scale", "=", "3")], + string="Departmental Comittees", + ) + local_partner_ids = fields.One2many( + comodel_name="res.partner", + inverse_name="cdck_partner_id", + domain=[("partner_scale", "=", "4")], + string="Local structures", + ) + # Compute hierarchy for searchpanel + hierarchy_id = fields.Many2one( + comodel_name="res.partner.hierarchy", + string="Hierarchy element", + compute="_compute_partner_hierarchy", + store=True, + ) + + @api.depends("ffck_network", "partner_scale", "partner_code") + def _compute_partner_hierarchy(self): + for partner in self: + partner.hierarchy_id = ( + partner.id + if partner.ffck_network + and int(partner.partner_scale) in range(1, 5) + and partner.partner_code + else False + ) @api.model def name_search(self, name="", args=None, operator="ilike", limit=100): @@ -81,6 +117,7 @@ class ResPartner(models.Model): ) return super().name_search(name, args, operator, limit) + @api.depends("name", "ref", "partner_code") def name_get(self): result = [] ffck_partners = self.filtered("ffck_network") @@ -125,7 +162,7 @@ class ResPartner(models.Model): @api.depends("state_id", "ffck_network", "partner_scale") def _get_crck_partner(self): states = self.mapped("state_id") - crck_ok = self.search( + crck_ok = self.with_context(active_test=False).search( [ ("ffck_network", "=", True), ("partner_scale", "=", "2"), @@ -146,30 +183,86 @@ class ResPartner(models.Model): for partner in self - concerned: partner.crck_partner_id = False - @api.depends("country_department_id", "ffck_network", "partner_scale") + @api.depends( + "is_company", + "ffck_network", + "partner_code", + "partner_scale", + "local_partner_id", + "local_partner_id.partner_code", + "country_department_id", + "country_department_id.code", + ) def _get_cdck_partner(self): - depts = self.mapped("country_department_id") - cdck_ok = self.search( + structures = self.filtered( + lambda rp: rp.is_company + and rp.ffck_network + and rp.partner_code + and len(rp.partner_code) >= 2 + and int(rp.partner_scale) in (3, 4) + ) + licensees = self.filtered( + lambda rp: not rp.is_company + and rp.ffck_network + and int(rp.partner_scale) == 5 + and ( + rp.local_partner_id + and rp.local_partner_id.cdck_partner_id + or rp.country_department_id + ) + ) + dept_codes = self.mapped( + lambda rp: rp.partner_code + and len(rp.partner_code) >= 2 + and int(rp.partner_scale) in (3, 4) + and rp.partner_code[:2] + ) + cdck_ok = self.with_context(active_test=False).search( [ ("ffck_network", "=", True), ("partner_scale", "=", "3"), - ("country_department_id", "in", depts.ids), + ("partner_code", "in", ["{}00".format(code) for code in dept_codes]), ] ) - cdck_by_dept = {cdck.country_department_id: cdck for cdck in cdck_ok} - depts_ok = depts & cdck_ok.mapped("country_department_id") - concerned = self.filtered( - lambda rp: rp.ffck_network - and int(rp.partner_scale) >= 3 - and rp.country_department_id in depts_ok - ) - for partner in concerned: + cdck_by_code = {cdck.partner_code[:2]: cdck for cdck in cdck_ok} + # Treat structures + for partner in structures: + partner.cdck_partner_id = cdck_by_code[partner.partner_code[:2]] + # Treat licensees + for partner in licensees: dept = partner.country_department_id - partner.cdck_partner_id = cdck_by_dept[dept] - # treat unconcerned - for partner in self - concerned: + local = partner.local_partner_id + local_cd = local.cdck_partner_id + partner.cdck_partner_id = ( + local_cd if local and local_cd else cdck_by_code[dept.code] + ) + # treat others + for partner in self - (structures | licensees): partner.cdck_partner_id = False + @api.depends( + "ffck_network", + "partner_scale", + "ffck_partner_id", + "crck_partner_id", + "cdck_partner_id", + "local_partner_id", + ) + def _get_ffck_parent(self): + field_by_scale = { + "2": "ffck_partner_id", + "3": "crck_partner_id", + "4": "cdck_partner_id", + "5": "local_partner_id", + } + for partner in self: + if partner.ffck_network and int(partner.partner_scale) in range(2, 6): + partner.ffck_parent_id = getattr( + partner, field_by_scale[partner.partner_scale] + ) + else: + partner.ffck_parent_id = False + # ONCHANGES @api.onchange("partner_code", "company_type", "ffck_network") diff --git a/ffck_commons/models/res_partner_hierarchy.py b/ffck_commons/models/res_partner_hierarchy.py new file mode 100644 index 0000000..2563dcf --- /dev/null +++ b/ffck_commons/models/res_partner_hierarchy.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +from odoo import models, fields, tools + + +class ResPartnerRoot(models.Model): + _name = "res.partner.hierarchy" + _description = "Partners hierarchy" + _auto = False + + name = fields.Char() + active = fields.Boolean() + parent_id = fields.Many2one("res.partner.hierarchy") + + def init(self): + tools.drop_view_if_exists(self.env.cr, self._table) + self.env.cr.execute( + """ + CREATE OR REPLACE VIEW %s AS ( + SELECT DISTINCT id AS id, + active as active, + CONCAT(partner_code, ' - ', ref) AS name, + NULL::int AS parent_id + FROM res_partner WHERE partner_code IS NOT NULL AND ffck_network IS TRUE AND partner_scale = '1' AND (ref IS NOT NULL OR name IS NOT NULL) + UNION ALL + SELECT DISTINCT id AS id, + active as active, + CONCAT(partner_code, ' - ', ref) AS name, + ffck_partner_id AS parent_id + FROM res_partner WHERE partner_code IS NOT NULL AND ffck_network IS TRUE AND partner_scale = '2' AND (ref IS NOT NULL OR name IS NOT NULL) + UNION ALL + SELECT DISTINCT id AS id, + active as active, + CONCAT(partner_code, ' - ', ref) AS name, + crck_partner_id AS parent_id + FROM res_partner WHERE partner_code IS NOT NULL AND ffck_network IS TRUE AND partner_scale = '3' AND (ref IS NOT NULL OR name IS NOT NULL) + UNION ALL + SELECT DISTINCT id AS id, + active as active, + CONCAT(partner_code, ' - ', ref) AS name, + cdck_partner_id AS parent_id + FROM res_partner WHERE partner_code IS NOT NULL AND ffck_network IS TRUE AND partner_scale = '4' AND (ref IS NOT NULL OR name IS NOT NULL) + )""" + % (self._table,) + ) diff --git a/ffck_commons/security/ir.model.access.csv b/ffck_commons/security/ir.model.access.csv index d0d5d35..0c7c786 100644 --- a/ffck_commons/security/ir.model.access.csv +++ b/ffck_commons/security/ir.model.access.csv @@ -1,3 +1,4 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink -access_ir_model_all,All ffck.structure.type,ffck_commons.model_ffck_structure_type,,1,0,0,0 -access_ir_model_group_partner_manager,Partner manager ffck.structure.type,ffck_commons.model_ffck_structure_type,base.group_partner_manager,1,1,1,0 \ No newline at end of file +access_ffck_structure_type_all,All ffck.structure.type,ffck_commons.model_ffck_structure_type,,1,0,0,0 +access_ffck_structure_type_group_partner_manager,Partner manager ffck.structure.type,ffck_commons.model_ffck_structure_type,base.group_partner_manager,1,1,1,0 +access_res_partner_hierarchy_all,All res.partner.hierarchy,ffck_commons.model_res_partner_hierarchy,,1,0,0,0 \ No newline at end of file diff --git a/ffck_commons/views/res_partner.xml b/ffck_commons/views/res_partner.xml index dab701e..0d0496b 100644 --- a/ffck_commons/views/res_partner.xml +++ b/ffck_commons/views/res_partner.xml @@ -5,6 +5,7 @@ res.partner.view.form.inherit res.partner + 30

@@ -12,7 +13,7 @@

- + @@ -29,18 +30,24 @@ + + - - - - - - - - - - + + @@ -52,6 +59,7 @@ res.partner.view.search.inherit res.partner + 30 Persons @@ -59,12 +67,20 @@ Structures + + + + + + + +