diff --git a/partner_ref_unique/README.rst b/partner_ref_unique/README.rst new file mode 100644 index 000000000..e4a903827 --- /dev/null +++ b/partner_ref_unique/README.rst @@ -0,0 +1,59 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +======================== +Partner unique reference +======================== + +Add an unique constraint to partner ref field + +Usage +===== + +To use this module, you need to: + +* Go to your company settings and choose an option on section *Partner* in tab + *Configuration*. +* Try to create two partners with the same ref. + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/134/9.0 + +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 smashing it by providing a detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Antonio Espinosa +* Vicent Cubells + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +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. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/partner_ref_unique/__init__.py b/partner_ref_unique/__init__.py new file mode 100644 index 000000000..5a5c25ccb --- /dev/null +++ b/partner_ref_unique/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Antiun Ingenieria S.L. - Antonio Espinosa +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import models diff --git a/partner_ref_unique/__openerp__.py b/partner_ref_unique/__openerp__.py new file mode 100644 index 000000000..0b8490f6f --- /dev/null +++ b/partner_ref_unique/__openerp__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Antiun Ingenieria S.L. - Antonio Espinosa +# Copyright 2017 Tecnativa - Vicent Cubells +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +{ + "name": "Partner unique reference", + "summary": "Add an unique constraint to partner ref field", + "version": "9.0.1.0.0", + "category": "Customer Relationship Management", + "website": "http://www.antiun.com", + "author": "Antiun IngenierĂ­a S.L., " + "Tecnativa, " + "Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": [ + "base", + ], + "data": [ + "views/res_company_view.xml", + ], +} diff --git a/partner_ref_unique/models/__init__.py b/partner_ref_unique/models/__init__.py new file mode 100644 index 000000000..f33d10808 --- /dev/null +++ b/partner_ref_unique/models/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Antiun Ingenieria S.L. - Antonio Espinosa +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import res_company +from . import res_partner diff --git a/partner_ref_unique/models/res_company.py b/partner_ref_unique/models/res_company.py new file mode 100644 index 000000000..0cae5c203 --- /dev/null +++ b/partner_ref_unique/models/res_company.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Antiun Ingenieria S.L. - Antonio Espinosa +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp import models, fields + + +class ResCompany(models.Model): + _inherit = "res.company" + + partner_ref_unique = fields.Selection( + selection=[ + ('none', 'None'), + ('companies', 'Only companies'), + ('all', 'All partners'), + ], string="Unique partner reference for", default="none") diff --git a/partner_ref_unique/models/res_partner.py b/partner_ref_unique/models/res_partner.py new file mode 100644 index 000000000..14adeb0e6 --- /dev/null +++ b/partner_ref_unique/models/res_partner.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Antiun Ingenieria S.L. - Antonio Espinosa +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp import models, api, _ +from openerp.exceptions import ValidationError + + +class ResPartner(models.Model): + _inherit = "res.partner" + + @api.multi + @api.constrains('ref', 'is_company', 'company_id') + def _check_ref(self): + for partner in self: + mode = partner.company_id.partner_ref_unique + if (partner.ref and ( + mode == 'all' or + (mode == 'companies' and partner.is_company))): + domain = [ + ('id', '!=', partner.id), + ('ref', '=', partner.ref), + ('customer', '=', True), + ] + if mode == 'companies': + domain.append(('is_company', '=', True)) + other = self.search(domain) + + # active_test is False when called from + # base.partner.merge.automatic.wizard + if other and self.env.context.get("active_test", True): + raise ValidationError( + _("This reference is equal to partner '%s'") % + other[0].display_name) diff --git a/partner_ref_unique/static/description/icon.png b/partner_ref_unique/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/partner_ref_unique/static/description/icon.png differ diff --git a/partner_ref_unique/tests/__init__.py b/partner_ref_unique/tests/__init__.py new file mode 100644 index 000000000..00432e6ea --- /dev/null +++ b/partner_ref_unique/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Tecnativa - Vicent Cubells +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import test_res_partner_ref diff --git a/partner_ref_unique/tests/test_res_partner_ref.py b/partner_ref_unique/tests/test_res_partner_ref.py new file mode 100644 index 000000000..32ff32e0c --- /dev/null +++ b/partner_ref_unique/tests/test_res_partner_ref.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Tecnativa - Vicent Cubells +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp.tests import common +from openerp.exceptions import ValidationError + + +class TestResPartnerRefUnique(common.SavepointCase): + @classmethod + def setUpClass(cls): + super(TestResPartnerRefUnique, cls).setUpClass() + cls.company = cls.env.ref('base.main_company') + partner_obj = cls.env['res.partner'] + cls.partner1 = partner_obj.create({ + 'name': 'Partner1', + }) + cls.partner2 = partner_obj.create({ + 'name': 'Partner2', + }) + + def test_check_ref(self): + # Test can create/modify partners with same ref + self.company.partner_ref_unique = 'none' + self.partner1.ref = 'same_ref' + self.partner2.ref = 'same_ref' + self.assertEqual(self.partner1.ref, self.partner2.ref) + self.partner2.ref = False + + # Test can't create/modify partner with same ref + self.company.partner_ref_unique = 'all' + with self.assertRaises(ValidationError): + self.partner2.ref = 'same_ref' + + # Test can't create/modify companies with same ref + self.company.partner_ref_unique = 'companies' + self.partner2.ref = 'same_ref' + self.assertEqual(self.partner1.ref, self.partner2.ref) + self.partner2.ref = False + self.partner1.is_company = True + self.partner2.is_company = True + with self.assertRaises(ValidationError): + self.partner2.ref = 'same_ref' diff --git a/partner_ref_unique/views/res_company_view.xml b/partner_ref_unique/views/res_company_view.xml new file mode 100644 index 000000000..c8cb6f485 --- /dev/null +++ b/partner_ref_unique/views/res_company_view.xml @@ -0,0 +1,22 @@ + + + + + + Add partner_ref_unique field + res.company + + + + + + + + + + + + +