diff --git a/partner_country_state_required/README.rst b/partner_country_state_required/README.rst new file mode 100644 index 00000000000..2dfe9de8e0c --- /dev/null +++ b/partner_country_state_required/README.rst @@ -0,0 +1,76 @@ +============================== +Partner Country State Required +============================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:33c513b7c5641477bde1bd7175355dc1d26db1b1e33e4e6f819d6415f0b12058 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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_country_state_required + :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_country_state_required + :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 allows the country state to be required on the partners assigned to countries having fed. states by setting up the *state_required* field of the res.country model. + +**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 +~~~~~~~ + +* ForgeFlow + +Contributors +~~~~~~~~~~~~ + +* Oriol Miranda + +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 00000000000..cc6b6354ad8 --- /dev/null +++ b/partner_country_state_required/__init__.py @@ -0,0 +1,2 @@ +from . import models +from .hooks import post_init_hook diff --git a/partner_country_state_required/__manifest__.py b/partner_country_state_required/__manifest__.py new file mode 100644 index 00000000000..31e53848406 --- /dev/null +++ b/partner_country_state_required/__manifest__.py @@ -0,0 +1,12 @@ +# 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", + "post_init_hook": "post_init_hook", + "installable": True, +} diff --git a/partner_country_state_required/hooks.py b/partner_country_state_required/hooks.py new file mode 100644 index 00000000000..ad903fc0347 --- /dev/null +++ b/partner_country_state_required/hooks.py @@ -0,0 +1,17 @@ +# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html) + +import logging + +_logger = logging.getLogger(__name__) + + +def post_init_hook(cr, registry): + """Set state_required to True""" + _logger.info("Setting state_required to True for all countries.") + query = """ + UPDATE res_country + SET state_required = TRUE; + """ + cr.execute(query) + _logger.info(f"{cr.rowcount} rows updated in res_country") diff --git a/partner_country_state_required/models/__init__.py b/partner_country_state_required/models/__init__.py new file mode 100644 index 00000000000..91fed54d404 --- /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 00000000000..6d7b5731bdf --- /dev/null +++ b/partner_country_state_required/models/res_partner.py @@ -0,0 +1,28 @@ +# Copyright 2023 ForgeFlow S.L. +# 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_required + 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 00000000000..f63a57cf5cf --- /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 00000000000..f0fdef2c82b --- /dev/null +++ b/partner_country_state_required/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module allows the country state to be required on the partners assigned to countries having fed. states by setting up the *state_required* field of the res.country model. diff --git a/partner_country_state_required/static/description/index.html b/partner_country_state_required/static/description/index.html new file mode 100644 index 00000000000..832012371a7 --- /dev/null +++ b/partner_country_state_required/static/description/index.html @@ -0,0 +1,421 @@ + + + + + + +Partner Country State Required + + + +
+

Partner Country State Required

+ + +

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

+

This module allows the country state to be required on the partners assigned to countries having fed. states by setting up the state_required field of the res.country model.

+

Table of contents

+ +
+

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

+
    +
  • ForgeFlow
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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/tests/__init__.py b/partner_country_state_required/tests/__init__.py new file mode 100644 index 00000000000..ca00560833c --- /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 00000000000..97bcd2e7ea3 --- /dev/null +++ b/partner_country_state_required/tests/test_state_required.py @@ -0,0 +1,79 @@ +# Copyright 2023 ForgeFlow S.L. +# 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.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): + self.spain.state_required = True + 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): + self.spain.state_required = True + 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") + + def test_create_partner_with_context(self): + vals = { + "name": "Test Partner 3", + "country_id": self.spain.id, + } + partner = self.partner_model.with_context(no_state_required=True).create(vals) + + self.assertTrue(partner) + + def test_create_partner_country_state_required(self): + self.spain.state_required = False + + vals = { + "name": "Test Partner 2", + "country_id": self.spain.id, + } + partner = self.partner_model.create(vals) + + self.assertTrue(partner) 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 00000000000..5769637a289 --- /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 00000000000..28c57bb6403 --- /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, +)