From 010d9e09185bd9d689f0d52962bc632e98d2f782 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Thu, 30 Jul 2015 11:36:25 +0200 Subject: [PATCH] [ADD] base_bank_account_number_unique --- base_bank_account_number_unique/README.rst | 48 ++++++++++++++++ base_bank_account_number_unique/__init__.py | 21 +++++++ .../__openerp__.py | 33 +++++++++++ base_bank_account_number_unique/hooks.py | 54 ++++++++++++++++++ .../i18n/base_bank_account_number_unique.pot | 36 ++++++++++++ base_bank_account_number_unique/i18n/nl.po | 38 ++++++++++++ .../models/__init__.py | 20 +++++++ .../models/res_partner_bank.py | 39 +++++++++++++ .../static/description/icon.png | Bin 0 -> 428 bytes .../tests/__init__.py | 20 +++++++ .../test_base_bank_account_number_unique.py | 42 ++++++++++++++ 11 files changed, 351 insertions(+) create mode 100644 base_bank_account_number_unique/README.rst create mode 100644 base_bank_account_number_unique/__init__.py create mode 100644 base_bank_account_number_unique/__openerp__.py create mode 100644 base_bank_account_number_unique/hooks.py create mode 100644 base_bank_account_number_unique/i18n/base_bank_account_number_unique.pot create mode 100644 base_bank_account_number_unique/i18n/nl.po create mode 100644 base_bank_account_number_unique/models/__init__.py create mode 100644 base_bank_account_number_unique/models/res_partner_bank.py create mode 100644 base_bank_account_number_unique/static/description/icon.png create mode 100644 base_bank_account_number_unique/tests/__init__.py create mode 100644 base_bank_account_number_unique/tests/test_base_bank_account_number_unique.py diff --git a/base_bank_account_number_unique/README.rst b/base_bank_account_number_unique/README.rst new file mode 100644 index 0000000..d5dd3f2 --- /dev/null +++ b/base_bank_account_number_unique/README.rst @@ -0,0 +1,48 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :alt: License: AGPL-3 +Unique bank account numbers +=========================== + +It can be desirable to be able to rely on a bank account number identifying exactly one partner. This module allows you to enforce this, so that an account number is unique in the system. + +Installation +============ + +During installation, the module checks if your bank account numbers are unique already. If this is not the case, you won't be able to install the module until duplicates are fixed. + +The error message only shows the first few duplicates, in order to find all of them, use the following statement:: + + with res_partner_bank_sanitized as (select id, acc_number, regexp_replace(acc_number, '\W+', '', 'g') acc_number_sanitized from res_partner_bank), + res_partner_bank_sanitized_grouped as (select array_agg(id) ids, acc_number_sanitized, count(*) amount from res_partner_bank_sanitized group by acc_number_sanitized) + select * from res_partner_bank_sanitized_grouped where amount > 1; + +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 +`here `_. + +Credits +======= + +Contributors +------------ + +* Holger Brunn + +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 http://odoo-community.org. diff --git a/base_bank_account_number_unique/__init__.py b/base_bank_account_number_unique/__init__.py new file mode 100644 index 0000000..856516e --- /dev/null +++ b/base_bank_account_number_unique/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# This module copyright (C) 2015 Therp BV . +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from . import models +from .hooks import post_init_hook diff --git a/base_bank_account_number_unique/__openerp__.py b/base_bank_account_number_unique/__openerp__.py new file mode 100644 index 0000000..9fb5f15 --- /dev/null +++ b/base_bank_account_number_unique/__openerp__.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# This module copyright (C) 2015 Therp BV . +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +{ + "name": "Unique bank account numbers", + "version": "1.0", + "author": "Therp BV,Odoo Community Association (OCA)", + "license": "AGPL-3", + "category": "Accounting & Finance", + "summary": "Enforce uniqueness on bank accounts", + "depends": [ + 'account_bank_statement_import', + ], + "post_init_hook": "post_init_hook", + "auto_install": False, + "installable": True, +} diff --git a/base_bank_account_number_unique/hooks.py b/base_bank_account_number_unique/hooks.py new file mode 100644 index 0000000..76da96f --- /dev/null +++ b/base_bank_account_number_unique/hooks.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# This module copyright (C) 2015 Therp BV . +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from openerp import _, SUPERUSER_ID, exceptions + + +def post_init_hook(cr, pool): + '''check if your constraint was actually inserted, raise otherwise''' + if not pool['ir.model.constraint'].search(cr, SUPERUSER_ID, [ + ('name', '=', 'res_partner_bank_unique_number'), + ('model.model', '=', 'res.partner.bank'), + ]): + max_account_numbers = 10 + cr.execute( + """ + with + res_partner_bank_sanitized as + (select id, acc_number, regexp_replace(acc_number, '\W+', '', 'g') + acc_number_sanitized from res_partner_bank), + res_partner_bank_sanitized_grouped as + (select array_agg(id) ids, acc_number_sanitized, count(*) amount + from res_partner_bank_sanitized group by acc_number_sanitized) + select acc_number_sanitized from res_partner_bank_sanitized_grouped + where amount > 1 limit %s; + """, + (max_account_numbers,)) + duplicates = [acc_number for acc_number, in cr.fetchall()] + message = _( + "Module installation can't proceed as you have duplicate " + "account numbers in your system already. Please clean that up " + "and try again.\n" + "The following shows the first %d duplicate account numbers\n" + "%s\n" + "(if you see less than %d, those are the only duplicates)") % ( + max_account_numbers, '\n'.join(duplicates), + max_account_numbers, + ) + raise exceptions.Warning(message) diff --git a/base_bank_account_number_unique/i18n/base_bank_account_number_unique.pot b/base_bank_account_number_unique/i18n/base_bank_account_number_unique.pot new file mode 100644 index 0000000..e710165 --- /dev/null +++ b/base_bank_account_number_unique/i18n/base_bank_account_number_unique.pot @@ -0,0 +1,36 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_bank_account_number_unique +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2015-07-30 09:32+0000\n" +"PO-Revision-Date: 2015-07-30 09:32+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_bank_account_number_unique +#: sql_constraint:res.partner.bank:0 +msgid "Account Number must be unique" +msgstr "" + +#. module: base_bank_account_number_unique +#: model:ir.model,name:base_bank_account_number_unique.model_res_partner_bank +msgid "Bank Accounts" +msgstr "" + +#. module: base_bank_account_number_unique +#: code:addons/base_bank_account_number_unique/hooks.py:44 +#, python-format +msgid "Module installation can't proceed as you have duplicate account numbers in your system already. Please clean that up and try again.\n" +"The following shows the first %d duplicate account numbers\n" +"%s\n" +"(if you see less than %d, those are the only duplicates)" +msgstr "" + diff --git a/base_bank_account_number_unique/i18n/nl.po b/base_bank_account_number_unique/i18n/nl.po new file mode 100644 index 0000000..8853628 --- /dev/null +++ b/base_bank_account_number_unique/i18n/nl.po @@ -0,0 +1,38 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_bank_account_number_unique +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2015-07-30 09:32+0000\n" +"PO-Revision-Date: 2015-07-30 09:32+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_bank_account_number_unique +#: sql_constraint:res.partner.bank:0 +msgid "Account Number must be unique" +msgstr "Rekeningnummer dient uniek te zijn" + +#. module: base_bank_account_number_unique +#: model:ir.model,name:base_bank_account_number_unique.model_res_partner_bank +msgid "Bank Accounts" +msgstr "Rekeningnummers" + +#. module: base_bank_account_number_unique +#: code:addons/base_bank_account_number_unique/hooks.py:44 +#, python-format +msgid "Module installation can't proceed as you have duplicate account numbers in your system already. Please clean that up and try again.\n" +"The following shows the first %d duplicate account numbers\n" +"%s\n" +"(if you see less than %d, those are the only duplicates)" +msgstr "De installatie kan niet door gaan omdat er al dubbele rekeningnummer in het systeem staan. Schoon dat op en probeer het opnieuw.\n" +"Er volgen de eerste %d dubbele rekeningnummers\n" +"%s\n" +"(indien er minder dan %d getoond worden, zijn er niet meer dubbele rekeningnummers)" diff --git a/base_bank_account_number_unique/models/__init__.py b/base_bank_account_number_unique/models/__init__.py new file mode 100644 index 0000000..eeb9f7e --- /dev/null +++ b/base_bank_account_number_unique/models/__init__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# This module copyright (C) 2015 Therp BV . +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from . import res_partner_bank diff --git a/base_bank_account_number_unique/models/res_partner_bank.py b/base_bank_account_number_unique/models/res_partner_bank.py new file mode 100644 index 0000000..29ae7ad --- /dev/null +++ b/base_bank_account_number_unique/models/res_partner_bank.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# This module copyright (C) 2015 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from openerp import models + + +class ResPartnerBank(models.Model): + _inherit = 'res.partner.bank' + + def copy_data(self, cr, uid, id, default=None, context=None): + if default is None: + default = {} + if context is None: + context = {} + if 'acc_number' not in default and 'default_acc_number' not in context: + default['acc_number'] = '' + return super(ResPartnerBank, self).copy_data( + cr, uid, id, default=default, context=context) + + _sql_constraints = [ + ('unique_number', 'unique(sanitized_acc_number)', + 'Account Number must be unique'), + ] diff --git a/base_bank_account_number_unique/static/description/icon.png b/base_bank_account_number_unique/static/description/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..9e8c20297a85768989e075d077b56894a0e26738 GIT binary patch literal 428 zcmeAS@N?(olHy`uVBq!ia0vp^0U*r51|<6gKdl8)Y)RhkE)4%caKYZ?lYt_f1s;*b z3=G`DAk4@xYmNj^kiEpy*OmP~yBvol)8WIeCxAkdC9V-A&iT2ysd*&~&PAz-C8;S2 z<(VZJ3hti10pX2&;tULoBAzaeAr-gY-rDGU*ny$#;{69&qP#-V228rVtkEfKQfZ#& zIYe|0u)Se)Y~pj0R}GxI2oAW_FAfdbJ;k>6D~Rjq L>gTe~DWM4f0. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from . import test_base_bank_account_number_unique diff --git a/base_bank_account_number_unique/tests/test_base_bank_account_number_unique.py b/base_bank_account_number_unique/tests/test_base_bank_account_number_unique.py new file mode 100644 index 0000000..de36ea9 --- /dev/null +++ b/base_bank_account_number_unique/tests/test_base_bank_account_number_unique.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# This module copyright (C) 2015 Therp BV . +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from openerp.tests.common import TransactionCase +from openerp import exceptions +from ..hooks import post_init_hook + + +class TestBaseBankAccountNumberUnique(TransactionCase): + def test_base_bank_account_number_unique(self): + # drop our constraint, insert nonunique account numbers and see if + # the init hook catches this + self.env['ir.model.constraint'].search([ + ('name', '=', 'res_partner_bank_unique_number'), + ('model.model', '=', 'res.partner.bank'), + ])._module_data_uninstall() + self.env['res.partner.bank'].create({ + 'acc_number': 'BE1234567890', + 'state': 'bank', + }) + self.env['res.partner.bank'].create({ + 'acc_number': 'BE 1234 567 890', + 'state': 'bank', + }) + with self.assertRaises(exceptions.Warning): + post_init_hook(self.cr, self.registry)