From 7c0d0c120af690ca0fa4c2aee585c26251f4e7ff Mon Sep 17 00:00:00 2001 From: OriolMForgeFlow Date: Tue, 17 Oct 2023 11:04:22 +0200 Subject: [PATCH] [ADD] partner_country_state_required --- partner_country_state_required/README.rst | 80 +++++++++++++++++++ partner_country_state_required/__init__.py | 1 + .../__manifest__.py | 11 +++ .../models/__init__.py | 1 + .../models/res_partner.py | 27 +++++++ .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 1 + .../tests/__init__.py | 1 + .../tests/test_state_required.py | 58 ++++++++++++++ .../addons/partner_country_state_required | 1 + setup/partner_country_state_required/setup.py | 6 ++ 11 files changed, 188 insertions(+) create mode 100644 partner_country_state_required/README.rst create mode 100644 partner_country_state_required/__init__.py create mode 100644 partner_country_state_required/__manifest__.py create mode 100644 partner_country_state_required/models/__init__.py create mode 100644 partner_country_state_required/models/res_partner.py create mode 100644 partner_country_state_required/readme/CONTRIBUTORS.rst create mode 100644 partner_country_state_required/readme/DESCRIPTION.rst create mode 100644 partner_country_state_required/tests/__init__.py create mode 100644 partner_country_state_required/tests/test_state_required.py create mode 120000 setup/partner_country_state_required/odoo/addons/partner_country_state_required create mode 100644 setup/partner_country_state_required/setup.py diff --git a/partner_country_state_required/README.rst b/partner_country_state_required/README.rst new file mode 100644 index 000000000000..608bc61984dd --- /dev/null +++ b/partner_country_state_required/README.rst @@ -0,0 +1,80 @@ +============================== +Partner Country State Required +============================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:1e663abc24a3fe67cd3201724c9557a49243c69cde035d79c97d641a274d2502 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpartner--contact-lightgray.png?logo=github + :target: https://github.com/OCA/partner-contact/tree/16.0/partner_company_default + :alt: OCA/partner-contact +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/partner-contact-16-0/partner-contact-16-0-partner_company_default + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/partner-contact&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module assigns default company to res.parnter. + +Background +~~~~~~~~~~ + +Up to 12.0, Odoo used to assign a company by default when partner was added, +however the default company proposal has been removed since 13.0 with multi-company refactoring. +This causes inconveniences to users who want to seggregate partner records between companies. + +ref. https://github.com/odoo/odoo/commit/25714692b23624d04a43862db26ebcf802b89399 + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Quartile Limited + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/partner-contact `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/partner_country_state_required/__init__.py b/partner_country_state_required/__init__.py new file mode 100644 index 000000000000..0650744f6bc6 --- /dev/null +++ b/partner_country_state_required/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/partner_country_state_required/__manifest__.py b/partner_country_state_required/__manifest__.py new file mode 100644 index 000000000000..5a4dcffc226e --- /dev/null +++ b/partner_country_state_required/__manifest__.py @@ -0,0 +1,11 @@ +# Copyright 2023 ForgeFlow +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Partner Country State Required", + "version": "16.0.1.0.0", + "author": "ForgeFlow, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/partner-contact", + "depends": ["base"], + "license": "AGPL-3", + "installable": True, +} diff --git a/partner_country_state_required/models/__init__.py b/partner_country_state_required/models/__init__.py new file mode 100644 index 000000000000..91fed54d404e --- /dev/null +++ b/partner_country_state_required/models/__init__.py @@ -0,0 +1 @@ +from . import res_partner diff --git a/partner_country_state_required/models/res_partner.py b/partner_country_state_required/models/res_partner.py new file mode 100644 index 000000000000..1fc010442134 --- /dev/null +++ b/partner_country_state_required/models/res_partner.py @@ -0,0 +1,27 @@ +# Copyright 2023 ForgeFlow +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import _, api, models +from odoo.exceptions import ValidationError + + +class ResPartner(models.Model): + _inherit = "res.partner" + + @api.constrains("country_id", "state_id") + def _check_state_required(self): + if self.env.context.get("no_state_required"): + return + + for record in self: + if ( + record.country_id + and record.country_id.state_ids + and not record.state_id + ): + raise ValidationError( + _( + "Please specify a state for the address when selecting " + "a country with available states." + ) + ) diff --git a/partner_country_state_required/readme/CONTRIBUTORS.rst b/partner_country_state_required/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000000..f63a57cf5cf2 --- /dev/null +++ b/partner_country_state_required/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Oriol Miranda diff --git a/partner_country_state_required/readme/DESCRIPTION.rst b/partner_country_state_required/readme/DESCRIPTION.rst new file mode 100644 index 000000000000..f5bf54931165 --- /dev/null +++ b/partner_country_state_required/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module forces partners assigned to countries having fed. states should have a state assigned. diff --git a/partner_country_state_required/tests/__init__.py b/partner_country_state_required/tests/__init__.py new file mode 100644 index 000000000000..ca00560833c9 --- /dev/null +++ b/partner_country_state_required/tests/__init__.py @@ -0,0 +1 @@ +from . import test_state_required diff --git a/partner_country_state_required/tests/test_state_required.py b/partner_country_state_required/tests/test_state_required.py new file mode 100644 index 000000000000..a5094dee1978 --- /dev/null +++ b/partner_country_state_required/tests/test_state_required.py @@ -0,0 +1,58 @@ +# Copyright 2023 ForgeFlow +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase + + +class TestCountryStateRequired(TransactionCase): + @classmethod + def setUpClass(cls): + super(TestCountryStateRequired, cls).setUpClass() + # cls.spain = cls.env["res.country"] + cls.partner_model = cls.env["res.partner"] + cls.spain = cls.env.ref("base.es") + cls.state_bcn = cls.env.ref("base.state_es_b") + + def test_create_partner(self): + vals = { + "name": "Test Partner 1", + "country_id": self.spain.id, + } + with self.assertRaisesRegex( + ValidationError, + "Please specify a state for the address when selecting " + "a country with available states.", + ): + self.partner_model.create(vals) + + vals["state_id"] = self.state_bcn.id + partner = self.partner_model.create(vals) + + self.assertTrue(partner) + + def test_write_partner(self): + vals = { + "name": "Test Partner 2", + } + partner = self.partner_model.create(vals) + + with self.assertRaisesRegex( + ValidationError, + "Please specify a state for the address when selecting " + "a country with available states.", + ): + partner.write( + { + "country_id": self.spain.id, + } + ) + + write_vals = { + "country_id": self.spain.id, + "state_id": self.state_bcn.id, + } + + partner.write(write_vals) + + self.assertEqual(partner.state_id.code, "B") diff --git a/setup/partner_country_state_required/odoo/addons/partner_country_state_required b/setup/partner_country_state_required/odoo/addons/partner_country_state_required new file mode 120000 index 000000000000..5769637a2898 --- /dev/null +++ b/setup/partner_country_state_required/odoo/addons/partner_country_state_required @@ -0,0 +1 @@ +../../../../partner_country_state_required \ No newline at end of file diff --git a/setup/partner_country_state_required/setup.py b/setup/partner_country_state_required/setup.py new file mode 100644 index 000000000000..28c57bb64031 --- /dev/null +++ b/setup/partner_country_state_required/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)