Browse Source

[IMP] Add sanitezed_acc_number on res.partenr.bank

The new field is computed by sanitzing the acc_number.
pull/19/head
Laurent Mignon 10 years ago
committed by Laurent Mignon (ACSONE)
parent
commit
8181fea31e
  1. 1
      account_bank_statement_import/__init__.py
  2. 11
      account_bank_statement_import/account_bank_statement_import.py
  3. 67
      account_bank_statement_import/res_partner_bank.py
  4. 1
      account_bank_statement_import/tests/__init__.py
  5. 62
      account_bank_statement_import/tests/test_res_partner_bank.py

1
account_bank_statement_import/__init__.py

@ -1,3 +1,4 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
from . import res_partner_bank
from . import account_bank_statement_import from . import account_bank_statement_import

11
account_bank_statement_import/account_bank_statement_import.py

@ -141,15 +141,8 @@ class account_bank_statement_import(models.TransientModel):
bank_account_id = None bank_account_id = None
if account_number and len(account_number) > 4: if account_number and len(account_number) > 4:
account_number = account_number.replace(' ', '').replace('-', '')
cr = self.env.cr
cr.execute(
"select id from res_partner_bank "
"where replace(replace(acc_number,' ',''),'-','') = %s",
(account_number,))
bank_account_ids = [val[0] for val in cr.fetchall()]
bank_account_ids = self.env['res.partner.bank'].search( bank_account_ids = self.env['res.partner.bank'].search(
[('id', 'in', bank_account_ids)], limit=1)
[('acc_number', '=', account_number)], limit=1)
if bank_account_ids: if bank_account_ids:
bank_account_id = bank_account_ids[0].id bank_account_id = bank_account_ids[0].id
@ -200,7 +193,6 @@ class account_bank_statement_import(models.TransientModel):
bank_code = bank_type.code bank_code = bank_type.code
except ValueError: except ValueError:
bank_code = 'bank' bank_code = 'bank'
account_number = account_number.replace(' ', '').replace('-', '')
vals_acc = { vals_acc = {
'acc_number': account_number, 'acc_number': account_number,
'state': bank_code, 'state': bank_code,
@ -245,6 +237,7 @@ class account_bank_statement_import(models.TransientModel):
banks = bank_model.search( banks = bank_model.search(
[('acc_number', '=', identifying_string)], limit=1) [('acc_number', '=', identifying_string)], limit=1)
if banks: if banks:
bank_account_id = banks[0].id
partner_id = banks[0].partner_id.id partner_id = banks[0].partner_id.id
else: else:
bank_account_id = self._create_bank_account( bank_account_id = self._create_bank_account(

67
account_bank_statement_import/res_partner_bank.py

@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This file is part of account_bank_statement_import,
# an Odoo module.
#
# Copyright (c) 2015 ACSONE SA/NV (<http://acsone.eu>)
#
# account_bank_statement_importis 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.
#
# account_bank_statement_import 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 account_bank_statement_import_coda.
# If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import re
from openerp import api, models, fields
class ResPartnerBank(models.Model):
_inherit = 'res.partner.bank'
sanitized_acc_number = fields.Char(
'Sanitized Account Number', size=64, readonly=True,
compute='_get_sanitized_account_number', store=True, index=True)
def _sanitize_account_number(self, acc_number):
return re.sub(r'\W+', '', acc_number)
@api.one
@api.depends('acc_number')
def _get_sanitized_account_number(self):
value = self.acc_number
if not value:
self.sanitized_acc_number = False
else:
self.sanitized_acc_number = self._sanitize_account_number(value)
@api.returns('self')
def search(self, cr, user, args, offset=0, limit=None, order=None,
context=None, count=False):
pos = 0
while pos < len(args):
if args[pos][0] == 'acc_number':
op = args[pos][1]
value = args[pos][2]
if hasattr(value, '__iter__'):
value = [self._sanitize_account_number(i) for i in value]
else:
value = self._sanitize_account_number(value)
if 'like' in op:
value = value + '%'
args[pos] = ('sanitized_acc_number', op, value)
pos += 1
return super(ResPartnerBank, self).search(
cr, user, args, offset=0, limit=None, order=None, context=None,
count=False)

1
account_bank_statement_import/tests/__init__.py

@ -1,2 +1,3 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
from . import test_res_partner_bank
from . import test_import_bank_statement from . import test_import_bank_statement

62
account_bank_statement_import/tests/test_res_partner_bank.py

@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# This file is part of account_bank_statement_import,
# an Odoo module.
#
# Copyright (c) 2015 ACSONE SA/NV (<http://acsone.eu>)
#
# account_bank_statement_import 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.
#
# account_bank_statement_import 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 account_bank_statement_import_coda.
# If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.tests.common import TransactionCase
class TestResPartnerBank(TransactionCase):
"""Tests acc_number
"""
def test_sanitized_acc_number(self):
partner_bank_model = self.env['res.partner.bank']
acc_number = " BE-001 2518823 03 "
vals = partner_bank_model.search([('acc_number', '=', acc_number)])
self.assertEquals(0, len(vals))
partner_bank = partner_bank_model.create({
'acc_number': acc_number,
'partner_id': self.ref('base.res_partner_2'),
'state': 'bank',
})
vals = partner_bank_model.search([('acc_number', '=', acc_number)])
self.assertEquals(1, len(vals))
self.assertEquals(partner_bank, vals[0])
vals = partner_bank_model.search([('acc_number', 'in', [acc_number])])
self.assertEquals(1, len(vals))
self.assertEquals(partner_bank, vals[0])
self.assertEqual(partner_bank.acc_number, acc_number)
# sanitaze the acc_number
sanitized_acc_number = 'BE001251882303'
vals = partner_bank_model.search(
[('acc_number', '=', sanitized_acc_number)])
self.assertEquals(1, len(vals))
self.assertEquals(partner_bank, vals[0])
vals = partner_bank_model.search(
[('acc_number', 'in', [sanitized_acc_number])])
self.assertEquals(1, len(vals))
self.assertEquals(partner_bank, vals[0])
self.assertEqual(partner_bank.sanitized_acc_number,
sanitized_acc_number)
Loading…
Cancel
Save