diff --git a/account_bank_statement_import/__init__.py b/account_bank_statement_import/__init__.py index 28dc065..0650744 100644 --- a/account_bank_statement_import/__init__.py +++ b/account_bank_statement_import/__init__.py @@ -1,4 +1 @@ -# -*- encoding: utf-8 -*- - -from . import res_partner_bank -from . import account_bank_statement_import +from . import models diff --git a/account_bank_statement_import/__openerp__.py b/account_bank_statement_import/__openerp__.py index faf1367..46c9466 100644 --- a/account_bank_statement_import/__openerp__.py +++ b/account_bank_statement_import/__openerp__.py @@ -8,7 +8,8 @@ 'website': 'https://github.com/OCA/bank-statement-import', 'depends': ['account'], 'data': [ - 'account_bank_statement_import_view.xml', + "views/account_config_settings.xml", + 'views/account_bank_statement_import_view.xml', ], 'demo': [ 'demo/fiscalyear_period.xml', diff --git a/account_bank_statement_import/models/__init__.py b/account_bank_statement_import/models/__init__.py new file mode 100644 index 0000000..3a3c51a --- /dev/null +++ b/account_bank_statement_import/models/__init__.py @@ -0,0 +1,5 @@ +# -*- encoding: utf-8 -*- + +from . import res_partner_bank +from . import account_bank_statement_import +from . import account_config_settings diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/models/account_bank_statement_import.py similarity index 100% rename from account_bank_statement_import/account_bank_statement_import.py rename to account_bank_statement_import/models/account_bank_statement_import.py diff --git a/account_bank_statement_import/models/account_config_settings.py b/account_bank_statement_import/models/account_config_settings.py new file mode 100644 index 0000000..7f270c9 --- /dev/null +++ b/account_bank_statement_import/models/account_config_settings.py @@ -0,0 +1,27 @@ +# -*- 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, fields + + +class AccountConfigSettings(models.TransientModel): + _inherit = 'account.config.settings' + + module_base_bank_account_number_unique = fields.Boolean( + 'Enforce unique account numbers on bank accounts') diff --git a/account_bank_statement_import/res_partner_bank.py b/account_bank_statement_import/models/res_partner_bank.py similarity index 92% rename from account_bank_statement_import/res_partner_bank.py rename to account_bank_statement_import/models/res_partner_bank.py index 110080f..f633715 100644 --- a/account_bank_statement_import/res_partner_bank.py +++ b/account_bank_statement_import/models/res_partner_bank.py @@ -32,7 +32,7 @@ class ResPartnerBank(models.Model): sanitized_acc_number = fields.Char( 'Sanitized Account Number', size=64, readonly=True, - compute='_get_sanitized_account_number', store=True) + compute='_get_sanitized_account_number', store=True, index=True) def _sanitize_account_number(self, acc_number): if acc_number: @@ -64,8 +64,3 @@ class ResPartnerBank(models.Model): return super(ResPartnerBank, self).search( cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count) - - _sql_constraints = [ - ('unique_number', 'unique(sanitized_acc_number)', - 'Account Number must be unique'), - ] diff --git a/account_bank_statement_import/account_bank_statement_import_view.xml b/account_bank_statement_import/views/account_bank_statement_import_view.xml similarity index 100% rename from account_bank_statement_import/account_bank_statement_import_view.xml rename to account_bank_statement_import/views/account_bank_statement_import_view.xml diff --git a/account_bank_statement_import/views/account_config_settings.xml b/account_bank_statement_import/views/account_config_settings.xml new file mode 100644 index 0000000..78d7805 --- /dev/null +++ b/account_bank_statement_import/views/account_config_settings.xml @@ -0,0 +1,17 @@ + + + + + account.config.settings + + + + + + + + + + + + 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..8bcdca3 --- /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 0000000..9e8c202 Binary files /dev/null and b/base_bank_account_number_unique/static/description/icon.png differ diff --git a/base_bank_account_number_unique/tests/__init__.py b/base_bank_account_number_unique/tests/__init__.py new file mode 100644 index 0000000..b6ab1e9 --- /dev/null +++ b/base_bank_account_number_unique/tests/__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 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)