From 44a841120754ea57f8bbc03d6fb08821dca7f69d Mon Sep 17 00:00:00 2001 From: OriolMForgeFlow Date: Thu, 16 Nov 2023 10:16:50 +0100 Subject: [PATCH] [IMP] partner_company_type: allow to set up countries and states, display shortcut in name --- partner_company_type/README.rst | 3 +- partner_company_type/__manifest__.py | 2 +- .../migrations/16.0.1.0.1/post-migration.py | 11 +++ partner_company_type/models/res_partner.py | 33 ++++++- .../models/res_partner_company_type.py | 40 ++++++++- partner_company_type/readme/CONTRIBUTORS.rst | 1 + .../static/description/index.html | 3 +- .../tests/test_res_partner_company_type.py | 88 ++++++++++++++++++- partner_company_type/views/res_partner.xml | 1 + .../views/res_partner_company_type.xml | 4 + 10 files changed, 177 insertions(+), 9 deletions(-) create mode 100644 partner_company_type/migrations/16.0.1.0.1/post-migration.py diff --git a/partner_company_type/README.rst b/partner_company_type/README.rst index 4fdcdd1750f3..2a8ed38ac233 100644 --- a/partner_company_type/README.rst +++ b/partner_company_type/README.rst @@ -7,7 +7,7 @@ Partner Company Type !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:fa1211014d9e73379ebfffe2725e096a7a7a85e59920a677fd13393fd66ccb43 + !! source digest: sha256:4104bf3343650a9334425baaa43c49ec1e449445415e9f3f87bf4bd30dff0224 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png @@ -62,6 +62,7 @@ Contributors * Denis Roussel (https://acsone.eu) * Gilles Meyomesse (https://acsone.eu) * Kitti U. (http://ecosoft.co.th) +* Oriol Miranda (https://forgeflow.com) Other credits ~~~~~~~~~~~~~ diff --git a/partner_company_type/__manifest__.py b/partner_company_type/__manifest__.py index 1ecb50d9924a..feba54129d15 100644 --- a/partner_company_type/__manifest__.py +++ b/partner_company_type/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Partner Company Type", "summary": "Adds a company type to partner that are companies", - "version": "16.0.1.0.0", + "version": "16.0.1.0.1", "license": "AGPL-3", "author": "ACSONE SA/NV,Odoo Community Association (OCA)", "website": "https://github.com/OCA/partner-contact", diff --git a/partner_company_type/migrations/16.0.1.0.1/post-migration.py b/partner_company_type/migrations/16.0.1.0.1/post-migration.py new file mode 100644 index 000000000000..c81edd9a41e7 --- /dev/null +++ b/partner_company_type/migrations/16.0.1.0.1/post-migration.py @@ -0,0 +1,11 @@ +# Copyright 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo.tools import drop_constraint + + +def migrate(cr, version): + + drop_constraint( + cr, "res_partner_company_type", "res_partner_company_type_name_uniq" + ) diff --git a/partner_company_type/models/res_partner.py b/partner_company_type/models/res_partner.py index 26140f1cc38e..529c889b7c25 100644 --- a/partner_company_type/models/res_partner.py +++ b/partner_company_type/models/res_partner.py @@ -1,7 +1,8 @@ # Copyright 2017-2018 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import _, api, fields, models +from odoo.exceptions import UserError class ResPartner(models.Model): @@ -10,3 +11,33 @@ class ResPartner(models.Model): partner_company_type_id = fields.Many2one( comodel_name="res.partner.company.type", string="Legal Form" ) + + @api.constrains( + "country_id", + "state_id", + "partner_company_type_id", + ) + def _check_partner_company_type_country_state(self): + for rec in self: + if not rec.partner_company_type_id: + return + if ( + rec.partner_company_type_id.country_ids + and rec.country_id not in rec.partner_company_type_id.country_ids + ): + raise UserError( + _( + "You must select a Legal Form from the country " + "of the partner address." + ) + ) + if ( + rec.partner_company_type_id.state_ids + and rec.state_id not in rec.partner_company_type_id.state_ids + ): + raise UserError( + _( + "You must select a Legal Form from the state " + "of the partner address." + ) + ) diff --git a/partner_company_type/models/res_partner_company_type.py b/partner_company_type/models/res_partner_company_type.py index cf0643e821f0..d42032240936 100644 --- a/partner_company_type/models/res_partner_company_type.py +++ b/partner_company_type/models/res_partner_company_type.py @@ -1,7 +1,8 @@ # Copyright 2017-2018 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import api, fields, models +from odoo.exceptions import ValidationError class ResPartnerCompanyType(models.Model): @@ -12,6 +13,37 @@ class ResPartnerCompanyType(models.Model): name = fields.Char(string="Title", required=True, translate=True) shortcut = fields.Char(string="Abbreviation", translate=True) - _sql_constraints = [ - ("name_uniq", "unique (name)", "Partner Company Type already exists!") - ] + country_ids = fields.Many2many("res.country", string="Countries") + state_ids = fields.Many2many( + "res.country.state", + string="States", + domain="[('country_id', 'in', country_ids)]", + ) + + def name_get(self): + res = [] + for record in self: + name = record.name + if record.shortcut: + name = name + " - " + record.shortcut + res.append((record.id, name)) + return res + + @api.constrains("name", "shortcut") + def _check_unique_name_shortcut(self): + for rec in self: + if ( + self.env["res.partner.company.type"].search_count( + [ + ("name", "=", rec.name), + ("shortcut", "=", rec.shortcut), + ] + ) + > 1 + ): + if rec.shortcut: + raise ValidationError( + f"Name ({rec.name}) already exists with this shortcut ({rec.shortcut})!" + ) + else: + raise ValidationError(f"Name ({rec.name}) already exists!") diff --git a/partner_company_type/readme/CONTRIBUTORS.rst b/partner_company_type/readme/CONTRIBUTORS.rst index 33a8011d6792..bad58abe84b8 100644 --- a/partner_company_type/readme/CONTRIBUTORS.rst +++ b/partner_company_type/readme/CONTRIBUTORS.rst @@ -1,3 +1,4 @@ * Denis Roussel (https://acsone.eu) * Gilles Meyomesse (https://acsone.eu) * Kitti U. (http://ecosoft.co.th) +* Oriol Miranda (https://forgeflow.com) diff --git a/partner_company_type/static/description/index.html b/partner_company_type/static/description/index.html index 935605733740..0d94ee030626 100644 --- a/partner_company_type/static/description/index.html +++ b/partner_company_type/static/description/index.html @@ -367,7 +367,7 @@

Partner Company Type

!! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!! source digest: sha256:fa1211014d9e73379ebfffe2725e096a7a7a85e59920a677fd13393fd66ccb43 +!! source digest: sha256:4104bf3343650a9334425baaa43c49ec1e449445415e9f3f87bf4bd30dff0224 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Beta License: AGPL-3 OCA/partner-contact Translate me on Weblate Try me on Runboat

Adds a title to partner that are companies. @@ -408,6 +408,7 @@

Contributors

  • Denis Roussel <denis.roussel@acsone.eu> (https://acsone.eu)
  • Gilles Meyomesse <gilles.meyomesse@acsone.eu> (https://acsone.eu)
  • Kitti U. <kittiu@ecosoft.co.th> (http://ecosoft.co.th)
  • +
  • Oriol Miranda <oriol.miranda@forgeflow.com> (https://forgeflow.com)
  • diff --git a/partner_company_type/tests/test_res_partner_company_type.py b/partner_company_type/tests/test_res_partner_company_type.py index 0a8dab0fccc0..86f01caf4d6f 100644 --- a/partner_company_type/tests/test_res_partner_company_type.py +++ b/partner_company_type/tests/test_res_partner_company_type.py @@ -4,6 +4,7 @@ from psycopg2 import IntegrityError from odoo import tools +from odoo.exceptions import UserError from odoo.tests.common import TransactionCase @@ -15,8 +16,93 @@ def setUpClass(cls): "partner_company_type.res_partner_company_type_sa" ) + cls.company_type_model = cls.env["res.partner.company.type"] + + cls.country_es = cls.env.ref("base.es") + cls.country_us = cls.env.ref("base.us") + cls.state_california = cls.env.ref("base.state_us_5") + cls.state_colorado = cls.env.ref("base.state_us_6") + + cls.partner_id = cls.env.ref("base.res_partner_2") + + cls.company_type_es = cls.company_type_model.create( + { + "name": "Company Type Spain", + "shortcut": "ESP", + "country_ids": cls.country_es.ids, + } + ) + + cls.company_type_us = cls.company_type_model.create( + { + "name": "Company Type EEUU", + "shortcut": "USA", + "country_ids": cls.country_us.ids, + } + ) + + cls.company_type_us_ca = cls.company_type_model.create( + { + "name": "Company Type EEUU California", + "shortcut": "CA", + "country_ids": cls.country_us.ids, + "state_ids": cls.state_california.ids, + } + ) + cls.company_type_us_co = cls.company_type_model.create( + { + "name": "Company Type EEUU Colorado", + "shortcut": "CO", + "country_ids": cls.country_us.ids, + "state_ids": cls.state_colorado.ids, + } + ) + def test_00_duplicate(self): # Test Duplicate Company type with self.assertRaises(IntegrityError), tools.mute_logger("odoo.sql_db"): - self.company_type.create(dict(name=self.company_type.name)) + self.company_type.create( + dict(name=self.company_type.name, shortcut=self.company_type.shortcut) + ) + + def test_01_partner_company_type_country(self): + """Test to set up a partner_company_type_id according to the country of the partner_id, + check that raises an error if they don't match.""" + + self.partner_id.write( + { + "country_id": self.country_es.id, + "partner_company_type_id": self.company_type_es.id, + } + ) + self.assertEqual(self.partner_id.partner_company_type_id, self.company_type_es) + + with self.assertRaises(UserError): + self.partner_id.write( + { + "partner_company_type_id": self.company_type_us.id, + } + ) + + def test_02_partner_company_type_state(self): + """Test to set up an entity_legal_form_id according to the state of the partner_id, + check that raises an error if they don't match.""" + + self.partner_id.write( + { + "country_id": self.country_us.id, + "state_id": self.state_california.id, + "partner_company_type_id": self.company_type_us_ca.id, + } + ) + self.assertEqual( + self.partner_id.partner_company_type_id, self.company_type_us_ca + ) + + with self.assertRaises(UserError): + self.partner_id.write( + { + "partner_company_type_id": self.company_type_us_co.id, + } + ) diff --git a/partner_company_type/views/res_partner.xml b/partner_company_type/views/res_partner.xml index d7c663d2a05b..0339c1acfcd7 100644 --- a/partner_company_type/views/res_partner.xml +++ b/partner_company_type/views/res_partner.xml @@ -12,6 +12,7 @@ name="partner_company_type_id" options='{"no_open": True}' attrs="{'invisible': [('is_company', '=', False)]}" + domain="[ '|', ('country_ids', '=', False), ('country_ids', 'in', [country_id]), '|', ('state_ids', '=', False), ('state_ids', 'in', [state_id])]" /> diff --git a/partner_company_type/views/res_partner_company_type.xml b/partner_company_type/views/res_partner_company_type.xml index 6527ae630451..9959d3b1ccb2 100644 --- a/partner_company_type/views/res_partner_company_type.xml +++ b/partner_company_type/views/res_partner_company_type.xml @@ -12,6 +12,8 @@ + + @@ -36,6 +38,8 @@ + +