From c7c6954df7d1932afe539a17c855cb87acec70b3 Mon Sep 17 00:00:00 2001 From: Laurent Mignon Date: Fri, 20 Mar 2015 11:48:55 +0100 Subject: [PATCH 01/15] [IMP] Backport from master at 04daefd2d101d90daf5782173b95f31f3bd9e1b6 --- account_bank_statement_import/__init__.py | 3 +- .../account_bank_statement_import.py | 9 +- .../account_bank_statement_import_view.xml | 2 +- .../static/description/icon_src.svg | 150 +++++++++--------- account_bank_statement_import_ofx/__init__.py | 3 +- .../__openerp__.py | 5 +- .../account_bank_statement_import_ofx.py | 7 +- .../tests/test_import_bank_statement.py | 6 +- account_bank_statement_import_qif/__init__.py | 1 - .../tests/test_import_bank_statement.py | 3 +- 10 files changed, 92 insertions(+), 97 deletions(-) diff --git a/account_bank_statement_import/__init__.py b/account_bank_statement_import/__init__.py index 70262b2c..0882f8a9 100644 --- a/account_bank_statement_import/__init__.py +++ b/account_bank_statement_import/__init__.py @@ -1,4 +1,3 @@ # -*- encoding: utf-8 -*- -from . import account_bank_statement_import -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: +import account_bank_statement_import diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py index 66c27df9..37ff8511 100644 --- a/account_bank_statement_import/account_bank_statement_import.py +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -123,8 +123,9 @@ class account_bank_statement_import(osv.TransientModel): bank_account_id = None if account_number and len(account_number) > 4: account_number = account_number.replace(' ', '').replace('-', '') - cr.execute("select id from res_partner_bank where replace(replace(acc_number,' ',''),'-','') like %s and journal_id is not null", ('%' + account_number + '%',)) + cr.execute("select id from res_partner_bank where replace(replace(acc_number,' ',''),'-','') = %s", (account_number,)) bank_account_ids = [id[0] for id in cr.fetchall()] + bank_account_ids = self.pool.get('res.partner.bank').search(cr, uid, [('id', 'in', bank_account_ids)], context=context) if bank_account_ids: bank_account_id = bank_account_ids[0] @@ -132,8 +133,6 @@ class account_bank_statement_import(osv.TransientModel): def _get_journal(self, cr, uid, currency_id, bank_account_id, account_number, context=None): """ Find or create the journal """ - if context is None: - context = {} bank_pool = self.pool.get('res.partner.bank') # Find the journal from context or bank account @@ -174,7 +173,7 @@ class account_bank_statement_import(osv.TransientModel): wmca_pool = self.pool.get('wizard.multi.charts.accounts') company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id - vals_account = {'currency_id': currency_id, 'acc_name': account_number, 'account_type': 'bank'} + vals_account = {'currency_id': currency_id, 'acc_name': account_number, 'account_type': 'bank', 'currency_id': currency_id} vals_account = wmca_pool._prepare_bank_account(cr, uid, company, vals_account, context=context) account_id = self.pool.get('account.account').create(cr, uid, vals_account, context=context) @@ -213,7 +212,7 @@ class account_bank_statement_import(osv.TransientModel): if unique_import_id: line_vals['unique_import_id'] = (account_number and account_number + '-' or '') + unique_import_id - if not line_vals.get('partner_id') and not line_vals.get('bank_account_id'): + if not 'bank_account_id' in line_vals or not line_vals['bank_account_id']: # Find the partner and his bank account or create the bank account. The partner selected during the # reconciliation process will be linked to the bank when the statement is closed. partner_id = False diff --git a/account_bank_statement_import/account_bank_statement_import_view.xml b/account_bank_statement_import/account_bank_statement_import_view.xml index 6df58c76..8fc98a45 100644 --- a/account_bank_statement_import/account_bank_statement_import_view.xml +++ b/account_bank_statement_import/account_bank_statement_import_view.xml @@ -23,7 +23,7 @@ - Import Bank Statement + Import ir.actions.act_window account.bank.statement.import form diff --git a/account_bank_statement_import/static/description/icon_src.svg b/account_bank_statement_import/static/description/icon_src.svg index 0443f56c..664381e0 100644 --- a/account_bank_statement_import/static/description/icon_src.svg +++ b/account_bank_statement_import/static/description/icon_src.svg @@ -14,14 +14,14 @@ viewBox="0 0 100 100" width="100px" xml:space="preserve" - inkscape:version="0.91 r" - sodipodi:docname="icon_src.svg" - inkscape:export-filename="icon.png" + inkscape:version="0.48.2 r9819" + sodipodi:docname="1409271720_Noun_Project_100Icon_10px_grid-17.svg" + inkscape:export-filename="/Users/arthurmaniet/Desktop/icon.png" inkscape:export-xdpi="115.2" inkscape:export-ydpi="115.2">image/svg+xml08/12/13 1000.00 Delta PC08/15/13 75.46 Walts Drugs03/03/13 379.00 Epic Technologies03/04/13 20.28 YOUR LOCAL SU03/03/13 421.35 SPRINGFIELD WA03/03/13 379.00 Epic Technologies03/04/13 20.28 YOUR LOCAL SUP08/15/13 75.46 Walts Drugs08/12/13 1000.00 Delta PC03/03/13 421.35 SPRINGFIELD WA03/04/13 20.28 YOUR LOCAL SU03/03/13 379.00 Epic Technologies08/12/13 1000.00 De a PC03/03/13 379.00 E Technologies08/15/13 75.46 Walts Drugs03/04/13 20.28 YOUR LOCAL SU03/03/13 379.00 Epic Technologies08/12/13 1000.00 Delta PC08/15/13 75.46 Walts Drugs Date: Wed, 18 Mar 2015 15:35:17 +0100 Subject: [PATCH 02/15] [FIX] bank_account created by the import must belong to the partner linked to the company of the provided journal PR on master: odoo/odoo#5821 --- .../account_bank_statement_import.py | 8 +- .../tests/__init__.py | 2 + .../tests/test_import_bank_statement.py | 75 +++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 account_bank_statement_import/tests/__init__.py create mode 100644 account_bank_statement_import/tests/test_import_bank_statement.py diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py index 37ff8511..a9ef2525 100644 --- a/account_bank_statement_import/account_bank_statement_import.py +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -197,9 +197,13 @@ class account_bank_statement_import(osv.TransientModel): # while 'counterpart' bank accounts (from which statement transactions originate) don't. # Warning : if company_id is set, the method post_write of class bank will create a journal if journal_id: - vals_acc['partner_id'] = uid + company_id = self.pool['account.journal'].browse( + cr, uid, journal_id, context=context).company_id.id + vals = self.pool['res.partner.bank'].onchange_company_id( + cr, uid, None, company_id, context=None) + vals_acc.update(vals.get('value', {})) vals_acc['journal_id'] = journal_id - vals_acc['company_id'] = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id + vals_acc['company_id'] = company_id return self.pool.get('res.partner.bank').create(cr, uid, vals_acc, context=context) diff --git a/account_bank_statement_import/tests/__init__.py b/account_bank_statement_import/tests/__init__.py new file mode 100644 index 00000000..c3f445c1 --- /dev/null +++ b/account_bank_statement_import/tests/__init__.py @@ -0,0 +1,2 @@ +# -*- encoding: utf-8 -*- +from . import test_import_bank_statement diff --git a/account_bank_statement_import/tests/test_import_bank_statement.py b/account_bank_statement_import/tests/test_import_bank_statement.py new file mode 100644 index 00000000..1c9f7fa8 --- /dev/null +++ b/account_bank_statement_import/tests/test_import_bank_statement.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# This file is part of account_bank_statement_import, +# an Odoo module. +# +# Copyright (c) 2015 ACSONE SA/NV () +# +# 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 . +# +############################################################################## +from openerp.tests.common import TransactionCase + + +class TestAccountBankStatemetImport(TransactionCase): + """Tests for import bank statement file import + (account.bank.statement.import) + """ + + def setUp(self): + super(TestAccountBankStatemetImport, self).setUp() + self.statement_import_model = self.env[ + 'account.bank.statement.import'] + self.account_journal_model = self.env['account.journal'] + self.res_users_model = self.env['res.users'] + + self.journal_id = self.ref('account.bank_journal') + self.base_user_root_id = self.ref('base.user_root') + self.base_user_root = self.res_users_model.browse( + self.base_user_root_id) + + # create a new user that belongs to the same company as + # user_root + self.other_partner_id = self.env['res.partner'].create( + {"name": "My other partner", + "is_company": False, + "email": "test@tes.ttest", + }) + company_id = self.base_user_root.company_id.id + self.other_user_id_a = self.res_users_model.create( + {"partner_id": self.other_partner_id.id, + "company_id": company_id, + "company_ids": [(4, company_id)], + "login": "my_login a", + "name": "my user", + "groups_id": [(4, self.ref('account.group_account_manager'))] + }) + + def test_create_bank_account(self): + """Checks that the bank_account created by the import belongs to the + partner linked to the company of the provided journal + """ + journal = self.account_journal_model.browse(self.journal_id) + expected_id = journal.company_id.partner_id.id + + st_import = self.statement_import_model.sudo(self.other_user_id_a.id) + bank_id = st_import._create_bank_account( + '001251882303', journal_id=self.journal_id) + bank = self.env['res.partner.bank'].browse(bank_id) + + self.assertEqual(bank.partner_id.id, + expected_id) From ec26ad79b9362dc24b9032736dcaca8d7cd64f39 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Thu, 21 May 2015 14:03:51 +0200 Subject: [PATCH 03/15] [IMP] use new API Conflicts: account_bank_statement_import/account_bank_statement_import.py --- account_bank_statement_import/__init__.py | 2 +- account_bank_statement_import/__openerp__.py | 10 +- .../account_bank_statement_import.py | 291 ++++++++++-------- .../tests/test_import_bank_statement.py | 3 +- account_bank_statement_import_ofx/__init__.py | 2 +- .../account_bank_statement_import_ofx.py | 48 +-- .../tests/test_import_bank_statement.py | 27 +- account_bank_statement_import_qif/__init__.py | 1 - .../__openerp__.py | 2 +- .../account_bank_statement_import_qif.py | 77 +++-- .../tests/__init__.py | 2 - .../tests/test_import_bank_statement.py | 35 +-- 12 files changed, 274 insertions(+), 226 deletions(-) diff --git a/account_bank_statement_import/__init__.py b/account_bank_statement_import/__init__.py index 0882f8a9..23cbf368 100644 --- a/account_bank_statement_import/__init__.py +++ b/account_bank_statement_import/__init__.py @@ -1,3 +1,3 @@ # -*- encoding: utf-8 -*- -import account_bank_statement_import +from . import account_bank_statement_import diff --git a/account_bank_statement_import/__openerp__.py b/account_bank_statement_import/__openerp__.py index 216fca54..2c791228 100644 --- a/account_bank_statement_import/__openerp__.py +++ b/account_bank_statement_import/__openerp__.py @@ -1,18 +1,16 @@ # -*- encoding: utf-8 -*- -# noqa: This is a backport from Odoo. OCA has no control over style here. -# flake8: noqa { 'name': 'Account Bank Statement Import', - 'category' : 'Accounting & Finance', + 'category': 'Accounting & Finance', 'version': '1.0', 'author': 'OpenERP SA', 'depends': ['account'], 'demo': [], - 'description' : """Generic Wizard to Import Bank Statements. - + 'description': """Generic Wizard to Import Bank Statements. + Backport from Odoo 9.0 """, - 'data' : [ + 'data': [ 'account_bank_statement_import_view.xml', ], 'demo': [ diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py index a9ef2525..2676e35e 100644 --- a/account_bank_statement_import/account_bank_statement_import.py +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -1,11 +1,7 @@ # -*- coding: utf-8 -*- -# noqa: This is a backport from Odoo. OCA has no control over style here. -# flake8: noqa - import base64 -from openerp import SUPERUSER_ID -from openerp.osv import fields, osv +from openerp import api, models, fields from openerp.tools.translate import _ from openerp.exceptions import Warning @@ -13,54 +9,40 @@ import logging _logger = logging.getLogger(__name__) -class account_bank_statement_line(osv.osv): +class account_bank_statement_line(models.Model): _inherit = "account.bank.statement.line" - _columns = { - # Ensure transactions can be imported only once (if the import format provides unique transaction ids) - 'unique_import_id': fields.char('Import ID', readonly=True, copy=False), - } + # Ensure transactions can be imported only once (if the import format + # provides unique transaction ids) + unique_import_id = fields.Char('Import ID', readonly=True, copy=False) _sql_constraints = [ - ('unique_import_id', 'unique (unique_import_id)', 'A bank account transactions can be imported only once !') + ('unique_import_id', + 'unique (unique_import_id)', + 'A bank account transactions can be imported only once !') ] -class account_bank_statement_import(osv.TransientModel): +class account_bank_statement_import(models.TransientModel): _name = 'account.bank.statement.import' _description = 'Import Bank Statement' - _columns = { - 'data_file': fields.binary('Bank Statement File', required=True, help='Get you bank statements in electronic format from your bank and select them here.'), - } - def import_file(self, cr, uid, ids, context=None): - """ Process the file chosen in the wizard, create bank statement(s) and go to reconciliation. """ - context = dict(context or {}) - #set the active_id in the context, so that any extension module could - #reuse the fields chosen in the wizard if needed (see .QIF for example) - context.update({'active_id': ids[0]}) + data_file = fields.Binary( + 'Bank Statement File', required=True, + help='Get you bank statements in electronic format from your bank ' + 'and select them here.') - data_file = self.browse(cr, uid, ids[0], context=context).data_file - - # The appropriate implementation module returns the required data - currency_code, account_number, stmts_vals = self._parse_file(cr, uid, base64.b64decode(data_file), context=context) - # Check raw data - self._check_parsed_data(cr, uid, stmts_vals, context=context) - # Try to find the bank account and currency in odoo - currency_id, bank_account_id = self._find_additional_data(cr, uid, currency_code, account_number, context=context) - # Find or create the bank journal - journal_id = self._get_journal(cr, uid, currency_id, bank_account_id, account_number, context=context) - # Create the bank account if not already existing - if not bank_account_id and account_number: - self._create_bank_account(cr, uid, account_number, journal_id=journal_id, partner_id=uid, context=context) - # Prepare statement data to be used for bank statements creation - stmts_vals = self._complete_stmts_vals(cr, uid, stmts_vals, journal_id, account_number, context=context) - # Create the bank statements - statement_ids, notifications = self._create_bank_statements(cr, uid, stmts_vals, context=context) - - # Finally dispatch to reconciliation interface - model, action_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'action_bank_reconcile_bank_statements') - action = self.pool[model].browse(cr, uid, action_id, context=context) + @api.multi + def import_file(self): + """ Process the file chosen in the wizard, create bank statement(s) and + go to reconciliation. """ + self.ensure_one() + data_file = base64.b64decode(self.data_file) + statement_ids, notifications = self.with_context( + active_id=self.id)._import_file(data_file) + # dispatch to reconciliation interface + action = self.env.ref( + 'account.action_bank_reconcile_bank_statements') return { 'name': action.name, 'tag': action.tag, @@ -71,33 +53,67 @@ class account_bank_statement_import(osv.TransientModel): 'type': 'ir.actions.client', } - def _parse_file(self, cr, uid, data_file, context=None): - """ Each module adding a file support must extends this method. It processes the file if it can, returns super otherwise, resulting in a chain of responsability. - This method parses the given file and returns the data required by the bank statement import process, as specified below. + @api.model + def _import_file(self, data_file): + """ Create bank statement(s) from file + """ + # The appropriate implementation module returns the required data + currency_code, account_number, stmts_vals = self._parse_file(data_file) + # Check raw data + self._check_parsed_data(stmts_vals) + # Try to find the bank account and currency in odoo + currency_id, bank_account_id = self._find_additional_data( + currency_code, account_number) + # Find or create the bank journal + journal_id = self._get_journal( + currency_id, bank_account_id, account_number) + # Create the bank account if not already existing + if not bank_account_id and account_number: + self._create_bank_account( + account_number, journal_id=journal_id) + # Prepare statement data to be used for bank statements creation + stmts_vals = self._complete_stmts_vals( + stmts_vals, journal_id, account_number) + # Create the bank statements + return self._create_bank_statements(stmts_vals) + + @api.model + def _parse_file(self, data_file): + """ Each module adding a file support must extends this method. It + rocesses the file if it can, returns super otherwise, resulting in a + chain of responsability. + This method parses the given file and returns the data required by + the bank statement import process, as specified below. rtype: triplet (if a value can't be retrieved, use None) - currency code: string (e.g: 'EUR') The ISO 4217 currency code, case insensitive - account number: string (e.g: 'BE1234567890') - The number of the bank account which the statement belongs to - - bank statements data: list of dict containing (optional items marked by o) : + The number of the bank account which the statement belongs + to + - bank statements data: list of dict containing (optional + items marked by o) : - 'name': string (e.g: '000000123') - 'date': date (e.g: 2013-06-26) -o 'balance_start': float (e.g: 8368.56) -o 'balance_end_real': float (e.g: 8888.88) - 'transactions': list of dict containing : - - 'name': string (e.g: 'KBC-INVESTERINGSKREDIET 787-5562831-01') + - 'name': string + (e.g: 'KBC-INVESTERINGSKREDIET 787-5562831-01') - 'date': date - 'amount': float - 'unique_import_id': string -o 'account_number': string - Will be used to find/create the res.partner.bank in odoo + Will be used to find/create the res.partner.bank + in odoo -o 'note': string -o 'partner_name': string -o 'ref': string """ - raise Warning(_('Could not make sense of the given file.\nDid you install the module to support this type of file ?')) + raise Warning(_('Could not make sense of the given file.\nDid you ' + 'install the module to support this type of file ?')) - def _check_parsed_data(self, cr, uid, stmts_vals, context=None): + @api.model + def _check_parsed_data(self, stmts_vals): """ Basic and structural verifications """ if len(stmts_vals) == 0: raise Warning(_('This file doesn\'t contain any statement.')) @@ -110,82 +126,78 @@ class account_bank_statement_import(osv.TransientModel): if no_st_line: raise Warning(_('This file doesn\'t contain any transaction.')) - def _find_additional_data(self, cr, uid, currency_code, account_number, context=None): + @api.model + def _find_additional_data(self, currency_code, account_number): """ Get the res.currency ID and the res.partner.bank ID """ - currency_id = False # So if no currency_code is provided, we'll use the company currency + # if no currency_code is provided, we'll use the company currency + currency_id = False if currency_code: - currency_ids = self.pool.get('res.currency').search(cr, uid, [('name', '=ilike', currency_code)], context=context) - company_currency_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id + currency_ids = self.env['res.currency'].search( + [('name', '=ilike', currency_code)]) + company_currency_id = self.env.user.company_id.currency_id if currency_ids: if currency_ids[0] != company_currency_id: - currency_id = currency_ids[0] + currency_id = currency_ids[0].id bank_account_id = None if account_number and len(account_number) > 4: account_number = account_number.replace(' ', '').replace('-', '') - cr.execute("select id from res_partner_bank where replace(replace(acc_number,' ',''),'-','') = %s", (account_number,)) - bank_account_ids = [id[0] for id in cr.fetchall()] - bank_account_ids = self.pool.get('res.partner.bank').search(cr, uid, [('id', 'in', bank_account_ids)], context=context) + 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( + [('id', 'in', bank_account_ids)], limit=1) if bank_account_ids: - bank_account_id = bank_account_ids[0] + bank_account_id = bank_account_ids[0].id return currency_id, bank_account_id - def _get_journal(self, cr, uid, currency_id, bank_account_id, account_number, context=None): + @api.model + def _get_journal(self, currency_id, bank_account_id, account_number): """ Find or create the journal """ - bank_pool = self.pool.get('res.partner.bank') + bank_model = self.env['res.partner.bank'] # Find the journal from context or bank account - journal_id = context.get('journal_id') + journal_id = self.env.context.get('journal_id') if bank_account_id: - bank_account = bank_pool.browse(cr, uid, bank_account_id, context=context) + bank_account = bank_model.browse(bank_account_id) if journal_id: - if bank_account.journal_id.id and bank_account.journal_id.id != journal_id: - raise Warning(_('The account of this statement is linked to another journal.')) + if (bank_account.journal_id.id and + bank_account.journal_id.id != journal_id): + raise Warning( + _('The account of this statement is linked to ' + 'another journal.')) if not bank_account.journal_id.id: - bank_pool.write(cr, uid, [bank_account_id], {'journal_id': journal_id}, context=context) + bank_model.write({'journal_id': journal_id}) else: if bank_account.journal_id.id: journal_id = bank_account.journal_id.id - # If importing into an existing journal, its currency must be the same as the bank statement + # If importing into an existing journal, its currency must be the same + # as the bank statement if journal_id: - journal_currency_id = self.pool.get('account.journal').browse(cr, uid, journal_id, context=context).currency.id + journal_currency_id = self.env['account.journal'].browse( + journal_id).currency.id if currency_id and currency_id != journal_currency_id: - raise Warning(_('The currency of the bank statement is not the same as the currency of the journal !')) - - # If there is no journal, create one (and its account) - # I think it's too dangerous, so I disable that code by default -- Alexis de Lattre - # -- Totally disabled, Ronald Portier - # if context.get('allow_auto_create_journal') and not journal_id and account_number: - # journal_id = self._create_journal(cr, uid, currency_id, account_number, context=context) - # if bank_account_id: - # bank_pool.write(cr, uid, [bank_account_id], {'journal_id': journal_id}, context=context) + raise Warning(_('The currency of the bank statement is not ' + 'the same as the currency of the journal !')) # If we couldn't find/create a journal, everything is lost if not journal_id: - raise Warning(_('Cannot find in which journal import this statement. Please manually select a journal.')) + raise Warning(_('Cannot find in which journal import this ' + 'statement. Please manually select a journal.')) return journal_id - def _create_journal(self, cr, uid, currency_id, account_number, context=None): - """ Create a journal and its account """ - wmca_pool = self.pool.get('wizard.multi.charts.accounts') - company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id - - vals_account = {'currency_id': currency_id, 'acc_name': account_number, 'account_type': 'bank', 'currency_id': currency_id} - vals_account = wmca_pool._prepare_bank_account(cr, uid, company, vals_account, context=context) - account_id = self.pool.get('account.account').create(cr, uid, vals_account, context=context) - - vals_journal = {'currency_id': currency_id, 'acc_name': _('Bank') + ' ' + account_number, 'account_type': 'bank'} - vals_journal = wmca_pool._prepare_bank_journal(cr, uid, company, vals_journal, account_id, context=context) - return self.pool.get('account.journal').create(cr, uid, vals_journal, context=context) - - def _create_bank_account(self, cr, uid, account_number, journal_id=False, partner_id=False, context=None): + @api.model + @api.returns('res.partner.bank') + def _create_bank_account(self, account_number, journal_id=False): try: - type_model, type_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'bank_normal') - type_id = self.pool.get('res.partner.bank.type').browse(cr, uid, type_id, context=context) - bank_code = type_id.code + bank_type = self.env.ref('base.bank_normal') + bank_code = bank_type.code except ValueError: bank_code = 'bank' account_number = account_number.replace(' ', '').replace('-', '') @@ -193,52 +205,63 @@ class account_bank_statement_import(osv.TransientModel): 'acc_number': account_number, 'state': bank_code, } - # Odoo users bank accounts (which we import statement from) have company_id and journal_id set - # while 'counterpart' bank accounts (from which statement transactions originate) don't. - # Warning : if company_id is set, the method post_write of class bank will create a journal + # Odoo users bank accounts (which we import statement from) have + # company_id and journal_id set while 'counterpart' bank accounts + # (from which statement transactions originate) don't. + # Warning : if company_id is set, the method post_write of class + # bank will create a journal if journal_id: - company_id = self.pool['account.journal'].browse( - cr, uid, journal_id, context=context).company_id.id - vals = self.pool['res.partner.bank'].onchange_company_id( - cr, uid, None, company_id, context=None) + company_id = self.env['account.journal'].browse( + journal_id).company_id.id + vals = self.env['res.partner.bank'].onchange_company_id(company_id) vals_acc.update(vals.get('value', {})) vals_acc['journal_id'] = journal_id vals_acc['company_id'] = company_id - return self.pool.get('res.partner.bank').create(cr, uid, vals_acc, context=context) + return self.env['res.partner.bank'].create(vals_acc) - def _complete_stmts_vals(self, cr, uid, stmts_vals, journal_id, account_number, context=None): + @api.model + def _complete_stmts_vals(self, stmts_vals, journal_id, account_number): for st_vals in stmts_vals: st_vals['journal_id'] = journal_id for line_vals in st_vals['transactions']: unique_import_id = line_vals.get('unique_import_id', False) if unique_import_id: - line_vals['unique_import_id'] = (account_number and account_number + '-' or '') + unique_import_id + line_vals['unique_import_id'] = ( + account_number and account_number + '-' or '') + \ + unique_import_id - if not 'bank_account_id' in line_vals or not line_vals['bank_account_id']: - # Find the partner and his bank account or create the bank account. The partner selected during the - # reconciliation process will be linked to the bank when the statement is closed. + if not line_vals.get('bank_account_id'): + # Find the partner and his bank account or create the bank + # account. The partner selected during the reconciliation + # process will be linked to the bank when the statement is + # closed. partner_id = False bank_account_id = False - identifying_string = line_vals.get('account_number', False) + identifying_string = line_vals.get('account_number') if identifying_string: - identifying_string = identifying_string.replace(' ', '').replace('-', '') - ids = self.pool.get('res.partner.bank').search(cr, uid, [('acc_number', '=', identifying_string)], context=context) - if ids: - bank_account_id = ids[0] - partner_id = self.pool.get('res.partner.bank').browse(cr, uid, bank_account_id, context=context).partner_id.id + bank_model = self.env['res.partner.bank'] + banks = bank_model.search( + [('acc_number', '=', identifying_string)], limit=1) + if banks: + partner_id = banks[0].partner_id.id else: - bank_account_id = self._create_bank_account(cr, uid, identifying_string, context=context) + bank_account_id = self._create_bank_account( + identifying_string).id line_vals['partner_id'] = partner_id line_vals['bank_account_id'] = bank_account_id return stmts_vals - def _create_bank_statements(self, cr, uid, stmts_vals, context=None): - """ Create new bank statements from imported values, filtering out already imported transactions, and returns data used by the reconciliation widget """ - bs_obj = self.pool.get('account.bank.statement') - bsl_obj = self.pool.get('account.bank.statement.line') + @api.model + def _create_bank_statements(self, stmts_vals): + """ Create new bank statements from imported values, filtering out + already imported transactions, and returns data used by the + reconciliation widget + """ + bs_model = self.env['account.bank.statement'] + bsl_model = self.env['account.bank.statement.line'] # Filter out already imported transactions and create statements statement_ids = [] @@ -246,20 +269,25 @@ class account_bank_statement_import(osv.TransientModel): for st_vals in stmts_vals: filtered_st_lines = [] for line_vals in st_vals['transactions']: - if not 'unique_import_id' in line_vals \ + if 'unique_import_id' not in line_vals \ or not line_vals['unique_import_id'] \ - or not bool(bsl_obj.search(cr, SUPERUSER_ID, [('unique_import_id', '=', line_vals['unique_import_id'])], limit=1, context=context)): + or not bool(bsl_model.sudo().search( + [('unique_import_id', '=', + line_vals['unique_import_id'])], + limit=1)): filtered_st_lines.append(line_vals) else: - ignored_statement_lines_import_ids.append(line_vals['unique_import_id']) + ignored_statement_lines_import_ids.append( + line_vals['unique_import_id']) if len(filtered_st_lines) > 0: - # Remove values that won't be used to create records + # Remove values that won't be used to create records st_vals.pop('transactions', None) for line_vals in filtered_st_lines: line_vals.pop('account_number', None) # Create the satement - st_vals['line_ids'] = [[0, False, line] for line in filtered_st_lines] - statement_ids.append(bs_obj.create(cr, uid, st_vals, context=context)) + st_vals['line_ids'] = [[0, False, line] for line in + filtered_st_lines] + statement_ids.append(bs_model.create(st_vals).id) if len(statement_ids) == 0: raise Warning(_('You have already imported that file.')) @@ -269,13 +297,18 @@ class account_bank_statement_import(osv.TransientModel): if num_ignored > 0: notifications += [{ 'type': 'warning', - 'message': _("%d transactions had already been imported and were ignored.") % num_ignored if num_ignored > 1 else _("1 transaction had already been imported and was ignored."), + 'message': _("%d transactions had already been imported and " + "were ignored.") % num_ignored + if num_ignored > 1 + else _("1 transaction had already been imported and " + "was ignored."), 'details': { 'name': _('Already imported items'), 'model': 'account.bank.statement.line', - 'ids': bsl_obj.search(cr, uid, [('unique_import_id', 'in', ignored_statement_lines_import_ids)], context=context) + 'ids': bsl_model.search( + [('unique_import_id', 'in', + ignored_statement_lines_import_ids)]).ids } }] return statement_ids, notifications - diff --git a/account_bank_statement_import/tests/test_import_bank_statement.py b/account_bank_statement_import/tests/test_import_bank_statement.py index 1c9f7fa8..43e59784 100644 --- a/account_bank_statement_import/tests/test_import_bank_statement.py +++ b/account_bank_statement_import/tests/test_import_bank_statement.py @@ -67,9 +67,8 @@ class TestAccountBankStatemetImport(TransactionCase): expected_id = journal.company_id.partner_id.id st_import = self.statement_import_model.sudo(self.other_user_id_a.id) - bank_id = st_import._create_bank_account( + bank = st_import._create_bank_account( '001251882303', journal_id=self.journal_id) - bank = self.env['res.partner.bank'].browse(bank_id) self.assertEqual(bank.partner_id.id, expected_id) diff --git a/account_bank_statement_import_ofx/__init__.py b/account_bank_statement_import_ofx/__init__.py index 99554388..fb5e0c31 100644 --- a/account_bank_statement_import_ofx/__init__.py +++ b/account_bank_statement_import_ofx/__init__.py @@ -1,3 +1,3 @@ # -*- encoding: utf-8 -*- -import account_bank_statement_import_ofx +from . import account_bank_statement_import_ofx diff --git a/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py b/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py index 0442f030..93c89313 100644 --- a/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py +++ b/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py @@ -1,11 +1,9 @@ # -*- coding: utf-8 -*- -# noqa: This is a backport from Odoo. OCA has no control over style here. -# flake8: noqa import logging import StringIO -from openerp.osv import osv +from openerp import api, models from openerp.tools.translate import _ from openerp.exceptions import Warning @@ -17,37 +15,46 @@ except ImportError: _logger.warn("ofxparse not found, OFX parsing disabled.") ofxparser = None -class account_bank_statement_import(osv.TransientModel): + +class account_bank_statement_import(models.TransientModel): _inherit = 'account.bank.statement.import' - def _check_ofx(self, cr, uid, file, context=None): + @api.model + def _check_ofx(self, data_file): if ofxparser is None: return False try: - ofx = ofxparser.parse(file) + ofx = ofxparser.parse(StringIO.StringIO(data_file)) except: return False return ofx - def _parse_file(self, cr, uid, data_file, context=None): - ofx = self._check_ofx(cr, uid, StringIO.StringIO(data_file), context=context) + @api.model + def _parse_file(self, data_file): + ofx = self._check_ofx(data_file) if not ofx: - return super(account_bank_statement_import, self)._parse_file(cr, uid, data_file, context=context) + return super(account_bank_statement_import, self)._parse_file( + data_file) transactions = [] total_amt = 0.00 try: for transaction in ofx.account.statement.transactions: - # Since ofxparse doesn't provide account numbers, we'll have to find res.partner and res.partner.bank here - # (normal behavious is to provide 'account_number', which the generic module uses to find partner/bank) + # Since ofxparse doesn't provide account numbers, we'll have + # to find res.partner and res.partner.bank here + # (normal behavious is to provide 'account_number', which the + # generic module uses to find partner/bank) bank_account_id = partner_id = False - ids = self.pool.get('res.partner.bank').search(cr, uid, [('owner_name', '=', transaction.payee)], context=context) - if ids: - bank_account_id = bank_account_id = ids[0] - partner_id = self.pool.get('res.partner.bank').browse(cr, uid, bank_account_id, context=context).partner_id.id + banks = self.env['res.partner.bank'].search( + [('owner_name', '=', transaction.payee)], limit=1) + if banks: + bank_account = banks[0] + bank_account_id = bank_account.id + partner_id = bank_account.partner_id.id vals_line = { 'date': transaction.date, - 'name': transaction.payee + (transaction.memo and ': ' + transaction.memo or ''), + 'name': transaction.payee + ( + transaction.memo and ': ' + transaction.memo or ''), 'ref': transaction.id, 'amount': transaction.amount, 'unique_import_id': transaction.id, @@ -57,12 +64,15 @@ class account_bank_statement_import(osv.TransientModel): total_amt += float(transaction.amount) transactions.append(vals_line) except Exception, e: - raise Warning(_("The following problem occurred during import. The file might not be valid.\n\n %s" % e.message)) + raise Warning(_("The following problem occurred during import. " + "The file might not be valid.\n\n %s" % e.message)) vals_bank_statement = { 'name': ofx.account.routing_number, 'transactions': transactions, 'balance_start': ofx.account.statement.balance, - 'balance_end_real': float(ofx.account.statement.balance) + total_amt, + 'balance_end_real': + float(ofx.account.statement.balance) + total_amt, } - return ofx.account.statement.currency, ofx.account.number, [vals_bank_statement] + return ofx.account.statement.currency, ofx.account.number, [ + vals_bank_statement] diff --git a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py index 2391fd16..578fe57b 100644 --- a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py +++ b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py @@ -4,29 +4,32 @@ from openerp.tests.common import TransactionCase from openerp.modules.module import get_module_resource + class TestOfxFile(TransactionCase): - """Tests for import bank statement ofx file format (account.bank.statement.import) + """Tests for import bank statement ofx file format + (account.bank.statement.import) """ def setUp(self): super(TestOfxFile, self).setUp() - self.statement_import_model = self.registry('account.bank.statement.import') - self.bank_statement_model = self.registry('account.bank.statement') + self.statement_import_model = self.env['account.bank.statement.import'] + self.bank_statement_model = self.env['account.bank.statement'] def test_ofx_file_import(self): try: from ofxparse import OfxParser as ofxparser except ImportError: - #the Python library isn't installed on the server, the OFX import is unavailable and the test cannot be run + # the Python library isn't installed on the server, the OFX import + # is unavailable and the test cannot be run return True - cr, uid = self.cr, self.uid - ofx_file_path = get_module_resource('account_bank_statement_import_ofx', 'test_ofx_file', 'test_ofx.ofx') + ofx_file_path = get_module_resource( + 'account_bank_statement_import_ofx', + 'test_ofx_file', 'test_ofx.ofx') ofx_file = open(ofx_file_path, 'rb').read().encode('base64') - bank_statement_id = self.statement_import_model.create(cr, uid, dict( - data_file=ofx_file, - )) - self.statement_import_model.import_file(cr, uid, [bank_statement_id]) - statement_id = self.bank_statement_model.search(cr, uid, [('name', '=', '000000123')])[0] - bank_st_record = self.bank_statement_model.browse(cr, uid, statement_id) + bank_statement = self.statement_import_model.create( + dict(data_file=ofx_file)) + bank_statement.import_file() + bank_st_record = self.bank_statement_model.search( + [('name', '=', '000000123')])[0] self.assertEquals(bank_st_record.balance_start, 2156.56) self.assertEquals(bank_st_record.balance_end_real, 1796.56) diff --git a/account_bank_statement_import_qif/__init__.py b/account_bank_statement_import_qif/__init__.py index 7ff3caa8..3bf4df83 100644 --- a/account_bank_statement_import_qif/__init__.py +++ b/account_bank_statement_import_qif/__init__.py @@ -1,3 +1,2 @@ # -*- coding: utf-8 -*- from . import account_bank_statement_import_qif - diff --git a/account_bank_statement_import_qif/__openerp__.py b/account_bank_statement_import_qif/__openerp__.py index 21b9684d..11fd371a 100644 --- a/account_bank_statement_import_qif/__openerp__.py +++ b/account_bank_statement_import_qif/__openerp__.py @@ -4,7 +4,7 @@ { 'name': 'Import QIF Bank Statement', - 'category' : 'Accounting & Finance', + 'category': 'Accounting & Finance', 'version': '1.0', 'author': 'OpenERP SA', 'description': ''' diff --git a/account_bank_statement_import_qif/account_bank_statement_import_qif.py b/account_bank_statement_import_qif/account_bank_statement_import_qif.py index 38f6a5c9..a3aa25f2 100644 --- a/account_bank_statement_import_qif/account_bank_statement_import_qif.py +++ b/account_bank_statement_import_qif/account_bank_statement_import_qif.py @@ -6,41 +6,49 @@ import dateutil.parser import StringIO from openerp.tools.translate import _ -from openerp.osv import osv, fields +from openerp import api, models, fields from openerp.exceptions import Warning -class account_bank_statement_import(osv.TransientModel): + +class account_bank_statement_import(models.TransientModel): _inherit = "account.bank.statement.import" - _columns = { - 'journal_id': fields.many2one('account.journal', string='Journal', help='Accounting journal related to the bank statement you\'re importing. It has be be manually chosen for statement formats which doesn\'t allow automatic journal detection (QIF for example).'), - 'hide_journal_field': fields.boolean('Hide the journal field in the view'), - } + @api.model + def _get_hide_journal_field(self): + return self.env.context.get('journal_id') and True - def _get_hide_journal_field(self, cr, uid, context=None): - return context and 'journal_id' in context or False + journal_id = fields.Many2one( + 'account.journal', string='Journal', + help='Accounting journal related to the bank statement you\'re ' + 'importing. It has be be manually chosen for statement formats which ' + 'doesn\'t allow automatic journal detection (QIF for example).') + hide_journal_field = fields.Boolean( + 'Hide the journal field in the view', default=_get_hide_journal_field) - _defaults = { - 'hide_journal_field': _get_hide_journal_field, - } - - def _get_journal(self, cr, uid, currency_id, bank_account_id, account_number, context=None): - """ As .QIF format does not allow us to detect the journal, we need to let the user choose it. - We set it in context before to call super so it's the same as calling the widget from a journal """ - if context is None: - context = {} - if context.get('active_id'): - record = self.browse(cr, uid, context.get('active_id'), context=context) + @api.model + def _get_journal(self, currency_id, bank_account_id, account_number): + """ As .QIF format does not allow us to detect the journal, we need to + let the user choose it. + We set it in context before to call super so it's the same as + calling the widget from a journal """ + record = self + active_id = self.env.context.get('active_id') + if active_id: + record = self.browse(active_id) if record.journal_id: - context['journal_id'] = record.journal_id.id - return super(account_bank_statement_import, self)._get_journal(cr, uid, currency_id, bank_account_id, account_number, context=context) + record = record.with_context(journal_id=record.journal_id.id) + return super(account_bank_statement_import, record)._get_journal( + currency_id, bank_account_id, account_number) - def _check_qif(self, cr, uid, data_file, context=None): + @api.model + def _check_qif(self, data_file): return data_file.strip().startswith('!Type:') - def _parse_file(self, cr, uid, data_file, context=None): - if not self._check_qif(cr, uid, data_file, context=context): - return super(account_bank_statement_import, self)._parse_file(cr, uid, data_file, context=context) + @api.model + def _parse_file(self, data_file): + if not self._check_qif(data_file): + return super(account_bank_statement_import, self)._parse_file( + data_file) try: file_data = "" @@ -64,7 +72,8 @@ class account_bank_statement_import(osv.TransientModel): if not line: continue if line[0] == 'D': # date of transaction - vals_line['date'] = dateutil.parser.parse(line[1:], fuzzy=True).date() + vals_line['date'] = dateutil.parser.parse( + line[1:], fuzzy=True).date() elif line[0] == 'T': # Total amount total += float(line[1:].replace(',', '')) vals_line['amount'] = float(line[1:].replace(',', '')) @@ -74,10 +83,12 @@ class account_bank_statement_import(osv.TransientModel): vals_line['name'] = 'name' in vals_line and line[1:] + ': ' + vals_line['name'] or line[1:] # Since QIF doesn't provide account numbers, we'll have to find res.partner and res.partner.bank here # (normal behavious is to provide 'account_number', which the generic module uses to find partner/bank) - ids = self.pool.get('res.partner.bank').search(cr, uid, [('owner_name', '=', line[1:])], context=context) - if ids: - vals_line['bank_account_id'] = bank_account_id = ids[0] - vals_line['partner_id'] = self.pool.get('res.partner.bank').browse(cr, uid, bank_account_id, context=context).partner_id.id + banks = self.env['res.partner.bank'].search( + [('owner_name', '=', line[1:])], limit=1) + if banks: + bank_account = banks[0] + vals_line['bank_account_id'] = bank_account.id + vals_line['partner_id'] = bank_account.partner_id.id elif line[0] == 'M': # Memo vals_line['name'] = 'name' in vals_line and vals_line['name'] + ': ' + line[1:] or line[1:] elif line[0] == '^': # end of item @@ -88,11 +99,11 @@ class account_bank_statement_import(osv.TransientModel): else: pass else: - raise Warning(_('This file is either not a bank statement or is not correctly formed.')) - + raise Warning(_('This file is either not a bank statement or is ' + 'not correctly formed.')) + vals_bank_statement.update({ 'balance_end_real': total, 'transactions': transactions }) return None, None, [vals_bank_statement] - diff --git a/account_bank_statement_import_qif/tests/__init__.py b/account_bank_statement_import_qif/tests/__init__.py index 902f7b0b..9ce25a7f 100644 --- a/account_bank_statement_import_qif/tests/__init__.py +++ b/account_bank_statement_import_qif/tests/__init__.py @@ -1,4 +1,2 @@ # -*- coding: utf-8 -*- -# noqa: This is a backport from Odoo. OCA has no control over style here. -# flake8: noqa from . import test_import_bank_statement diff --git a/account_bank_statement_import_qif/tests/test_import_bank_statement.py b/account_bank_statement_import_qif/tests/test_import_bank_statement.py index 8d876503..5c13125e 100644 --- a/account_bank_statement_import_qif/tests/test_import_bank_statement.py +++ b/account_bank_statement_import_qif/tests/test_import_bank_statement.py @@ -1,32 +1,29 @@ # -*- coding: utf-8 -*- -# noqa: This is a backport from Odoo. OCA has no control over style here. -# flake8: noqa + from openerp.tests.common import TransactionCase from openerp.modules.module import get_module_resource + class TestQifFile(TransactionCase): - """Tests for import bank statement qif file format (account.bank.statement.import) + """Tests for import bank statement qif file format + (account.bank.statement.import) """ def setUp(self): super(TestQifFile, self).setUp() - self.statement_import_model = self.registry('account.bank.statement.import') - self.bank_statement_model = self.registry('account.bank.statement') - self.bank_statement_line_model = self.registry('account.bank.statement.line') + self.statement_import_model = self.env['account.bank.statement.import'] + self.statement_line_model = self.env['account.bank.statement.line'] def test_qif_file_import(self): from openerp.tools import float_compare - cr, uid = self.cr, self.uid - qif_file_path = get_module_resource('account_bank_statement_import_qif', 'test_qif_file', 'test_qif.qif') + qif_file_path = get_module_resource( + 'account_bank_statement_import_qif', + 'test_qif_file', 'test_qif.qif') qif_file = open(qif_file_path, 'rb').read().encode('base64') - bank_statement_id = self.statement_import_model.create(cr, uid, dict( - data_file=qif_file, - )) - context = { - 'journal_id': self.registry('ir.model.data').get_object_reference(cr, uid, 'account', 'bank_journal')[1] - } - self.statement_import_model.import_file(cr, uid, [bank_statement_id], context=context) - line_id = self.bank_statement_line_model.search(cr, uid, [('name', '=', 'YOUR LOCAL SUPERMARKET')])[0] - statement_id = self.bank_statement_line_model.browse(cr, uid, line_id).statement_id.id - bank_st_record = self.bank_statement_model.browse(cr, uid, statement_id) - assert float_compare(bank_st_record.balance_end_real, -1896.09, 2) == 0 + bank_statement_improt = self.statement_import_model.with_context( + journal_id=self.ref('account.bank_journal')).create( + dict(data_file=qif_file)) + bank_statement_improt.import_file() + bank_statement = self.statement_line_model.search( + [('name', '=', 'YOUR LOCAL SUPERMARKET')], limit=1)[0].statement_id + assert float_compare(bank_statement.balance_end_real, -1896.09, 2) == 0 From 8181fea31e7ecace117474dbe5158f21f60634ef Mon Sep 17 00:00:00 2001 From: Laurent Mignon Date: Sun, 22 Mar 2015 20:15:48 +0100 Subject: [PATCH 04/15] [IMP] Add sanitezed_acc_number on res.partenr.bank The new field is computed by sanitzing the acc_number. --- account_bank_statement_import/__init__.py | 1 + .../account_bank_statement_import.py | 11 +-- .../res_partner_bank.py | 67 +++++++++++++++++++ .../tests/__init__.py | 1 + .../tests/test_res_partner_bank.py | 62 +++++++++++++++++ 5 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 account_bank_statement_import/res_partner_bank.py create mode 100644 account_bank_statement_import/tests/test_res_partner_bank.py diff --git a/account_bank_statement_import/__init__.py b/account_bank_statement_import/__init__.py index 23cbf368..28dc065c 100644 --- a/account_bank_statement_import/__init__.py +++ b/account_bank_statement_import/__init__.py @@ -1,3 +1,4 @@ # -*- encoding: utf-8 -*- +from . import res_partner_bank from . import account_bank_statement_import diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py index 2676e35e..37d02aec 100644 --- a/account_bank_statement_import/account_bank_statement_import.py +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -141,15 +141,8 @@ class account_bank_statement_import(models.TransientModel): bank_account_id = None 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( - [('id', 'in', bank_account_ids)], limit=1) + [('acc_number', '=', account_number)], limit=1) if bank_account_ids: bank_account_id = bank_account_ids[0].id @@ -200,7 +193,6 @@ class account_bank_statement_import(models.TransientModel): bank_code = bank_type.code except ValueError: bank_code = 'bank' - account_number = account_number.replace(' ', '').replace('-', '') vals_acc = { 'acc_number': account_number, 'state': bank_code, @@ -245,6 +237,7 @@ class account_bank_statement_import(models.TransientModel): banks = bank_model.search( [('acc_number', '=', identifying_string)], limit=1) if banks: + bank_account_id = banks[0].id partner_id = banks[0].partner_id.id else: bank_account_id = self._create_bank_account( diff --git a/account_bank_statement_import/res_partner_bank.py b/account_bank_statement_import/res_partner_bank.py new file mode 100644 index 00000000..2ed1219f --- /dev/null +++ b/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 () +# +# 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 . +# +############################################################################## + +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) diff --git a/account_bank_statement_import/tests/__init__.py b/account_bank_statement_import/tests/__init__.py index c3f445c1..899646a8 100644 --- a/account_bank_statement_import/tests/__init__.py +++ b/account_bank_statement_import/tests/__init__.py @@ -1,2 +1,3 @@ # -*- encoding: utf-8 -*- +from . import test_res_partner_bank from . import test_import_bank_statement diff --git a/account_bank_statement_import/tests/test_res_partner_bank.py b/account_bank_statement_import/tests/test_res_partner_bank.py new file mode 100644 index 00000000..b98540af --- /dev/null +++ b/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 () +# +# 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 . +# +############################################################################## +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) From 5a5c48e35524196590579cbe143c2cf6dfabde1b Mon Sep 17 00:00:00 2001 From: Laurent Mignon Date: Sun, 22 Mar 2015 22:11:24 +0100 Subject: [PATCH 05/15] [IMP] Improve test coverage --- .../tests/test_import_bank_statement.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py index 578fe57b..ad54f3dc 100644 --- a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py +++ b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py @@ -33,3 +33,12 @@ class TestOfxFile(TransactionCase): [('name', '=', '000000123')])[0] self.assertEquals(bank_st_record.balance_start, 2156.56) self.assertEquals(bank_st_record.balance_end_real, 1796.56) + + line = bank_st_record.line_ids[0] + self.assertEquals(line.name, 'Agrolait') + self.assertEquals(line.ref, '219378') + self.assertEquals(line.partner_id.id, self.ref('base.res_partner_2')) + self.assertEquals( + line.bank_account_id.id, + self.ref('account_bank_statement_import.ofx_partner_bank_1')) + From 31fd68de92383716976af97fdfb05a23042ea867 Mon Sep 17 00:00:00 2001 From: Laurent Mignon Date: Mon, 23 Mar 2015 08:25:14 +0100 Subject: [PATCH 06/15] [IMP] Sanitize acc_number to upper case Use wildcard on both sides when opertor contains 'like' --- account_bank_statement_import/res_partner_bank.py | 13 ++++++------- .../tests/test_res_partner_bank.py | 8 ++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/account_bank_statement_import/res_partner_bank.py b/account_bank_statement_import/res_partner_bank.py index 2ed1219f..ae6961df 100644 --- a/account_bank_statement_import/res_partner_bank.py +++ b/account_bank_statement_import/res_partner_bank.py @@ -35,16 +35,15 @@ class ResPartnerBank(models.Model): compute='_get_sanitized_account_number', store=True, index=True) def _sanitize_account_number(self, acc_number): - return re.sub(r'\W+', '', acc_number) + if acc_number: + return re.sub(r'\W+', '', acc_number).upper() + return False @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) + self.sanitized_acc_number = self._sanitize_account_number( + self.acc_number) @api.returns('self') def search(self, cr, user, args, offset=0, limit=None, order=None, @@ -59,7 +58,7 @@ class ResPartnerBank(models.Model): else: value = self._sanitize_account_number(value) if 'like' in op: - value = value + '%' + value = '%' + value + '%' args[pos] = ('sanitized_acc_number', op, value) pos += 1 return super(ResPartnerBank, self).search( diff --git a/account_bank_statement_import/tests/test_res_partner_bank.py b/account_bank_statement_import/tests/test_res_partner_bank.py index b98540af..d93d855d 100644 --- a/account_bank_statement_import/tests/test_res_partner_bank.py +++ b/account_bank_statement_import/tests/test_res_partner_bank.py @@ -60,3 +60,11 @@ class TestResPartnerBank(TransactionCase): self.assertEquals(partner_bank, vals[0]) self.assertEqual(partner_bank.sanitized_acc_number, sanitized_acc_number) + + # search is case insensitive + vals = partner_bank_model.search( + [('acc_number', '=', sanitized_acc_number.lower())]) + self.assertEquals(1, len(vals)) + vals = partner_bank_model.search( + [('acc_number', '=', acc_number.lower())]) + self.assertEquals(1, len(vals)) From 9c9ebe8cda6c4d6ec647ca7893b4adb308243db3 Mon Sep 17 00:00:00 2001 From: Laurent Mignon Date: Fri, 3 Apr 2015 09:04:54 +0200 Subject: [PATCH 07/15] [IMP] Let the res_partner_bank.post_write create the journal if it doesn't exist --- .../account_bank_statement_import.py | 28 +++++++++++++------ .../tests/test_import_bank_statement.py | 8 +++--- .../__openerp__.py | 4 +-- .../demo/demo_data.xml | 27 ------------------ 4 files changed, 24 insertions(+), 43 deletions(-) delete mode 100644 account_bank_statement_import_ofx/demo/demo_data.xml diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py index 37d02aec..74317b0d 100644 --- a/account_bank_statement_import/account_bank_statement_import.py +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -64,13 +64,19 @@ class account_bank_statement_import(models.TransientModel): # Try to find the bank account and currency in odoo currency_id, bank_account_id = self._find_additional_data( currency_code, account_number) + # Create the bank account if not already existing + if not bank_account_id and account_number: + journal_id = self.env.context.get('journal_id') + company_id = self.env.user.company_id.id + if journal_id: + journal = self.env['account.journal'].browse(journal_id) + company_id = journal.company_id.id + bank_account_id = self._create_bank_account( + account_number, company_id=company_id, + currency_id=currency_id).id # Find or create the bank journal journal_id = self._get_journal( currency_id, bank_account_id, account_number) - # Create the bank account if not already existing - if not bank_account_id and account_number: - self._create_bank_account( - account_number, journal_id=journal_id) # Prepare statement data to be used for bank statements creation stmts_vals = self._complete_stmts_vals( stmts_vals, journal_id, account_number) @@ -202,15 +208,19 @@ class account_bank_statement_import(models.TransientModel): # (from which statement transactions originate) don't. # Warning : if company_id is set, the method post_write of class # bank will create a journal - if journal_id: - company_id = self.env['account.journal'].browse( - journal_id).company_id.id + if company_id: vals = self.env['res.partner.bank'].onchange_company_id(company_id) vals_acc.update(vals.get('value', {})) - vals_acc['journal_id'] = journal_id vals_acc['company_id'] = company_id - return self.env['res.partner.bank'].create(vals_acc) + # When the journal is created at same time of the bank account, we need + # to specify the currency to use a first time for the account.account + res_bank = self.env['res.partner.bank'].with_context( + default_currency_id=currency_id).create(vals_acc) + # and a second time on the journal + if currency_id: + res_bank.journal_id.currency = currency_id + return res_bank @api.model def _complete_stmts_vals(self, stmts_vals, journal_id, account_number): diff --git a/account_bank_statement_import/tests/test_import_bank_statement.py b/account_bank_statement_import/tests/test_import_bank_statement.py index 43e59784..9d080d3b 100644 --- a/account_bank_statement_import/tests/test_import_bank_statement.py +++ b/account_bank_statement_import/tests/test_import_bank_statement.py @@ -49,11 +49,11 @@ class TestAccountBankStatemetImport(TransactionCase): "is_company": False, "email": "test@tes.ttest", }) - company_id = self.base_user_root.company_id.id + self.company_id = self.base_user_root.company_id.id self.other_user_id_a = self.res_users_model.create( {"partner_id": self.other_partner_id.id, - "company_id": company_id, - "company_ids": [(4, company_id)], + "company_id": self.company_id, + "company_ids": [(4, self.company_id)], "login": "my_login a", "name": "my user", "groups_id": [(4, self.ref('account.group_account_manager'))] @@ -68,7 +68,7 @@ class TestAccountBankStatemetImport(TransactionCase): st_import = self.statement_import_model.sudo(self.other_user_id_a.id) bank = st_import._create_bank_account( - '001251882303', journal_id=self.journal_id) + '001251882303', company_id=self.company_id) self.assertEqual(bank.partner_id.id, expected_id) diff --git a/account_bank_statement_import_ofx/__openerp__.py b/account_bank_statement_import_ofx/__openerp__.py index c71cd778..88329844 100644 --- a/account_bank_statement_import_ofx/__openerp__.py +++ b/account_bank_statement_import_ofx/__openerp__.py @@ -26,9 +26,7 @@ create periods for the year 2013. """, 'data' : [], 'depends': ['account_bank_statement_import'], - 'demo': [ - 'demo/demo_data.xml', - ], + 'demo': [], 'auto_install': True, 'installable': True, } diff --git a/account_bank_statement_import_ofx/demo/demo_data.xml b/account_bank_statement_import_ofx/demo/demo_data.xml deleted file mode 100644 index 8387f272..00000000 --- a/account_bank_statement_import_ofx/demo/demo_data.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - Bank Journal - (test ofx) - TBNKOFX - bank - - - - - - - - - Your Company - 123456 - - - - bank - - - - - From 1ebe98df7996ceef484d9e5a3cd094e1ef45bdb1 Mon Sep 17 00:00:00 2001 From: Laurent Mignon Date: Fri, 3 Apr 2015 09:06:14 +0200 Subject: [PATCH 08/15] [IMP] Specify the currency to use for the account.account and account.journal created by res_partner_bank.post_write --- .../account_bank_statement_import.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py index 74317b0d..5d9dbaaa 100644 --- a/account_bank_statement_import/account_bank_statement_import.py +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -214,13 +214,11 @@ class account_bank_statement_import(models.TransientModel): vals_acc['company_id'] = company_id # When the journal is created at same time of the bank account, we need - # to specify the currency to use a first time for the account.account - res_bank = self.env['res.partner.bank'].with_context( - default_currency_id=currency_id).create(vals_acc) - # and a second time on the journal - if currency_id: - res_bank.journal_id.currency = currency_id - return res_bank + # to specify the currency to use for the account.account and + # account.journal + return self.env['res.partner.bank'].with_context( + default_currency_id=currency_id, + default_currency=currency_id).create(vals_acc) @api.model def _complete_stmts_vals(self, stmts_vals, journal_id, account_number): From 89afefc3700a0f2fb608684cbfb86b4ce6e09f4c Mon Sep 17 00:00:00 2001 From: Laurent Mignon Date: Fri, 3 Apr 2015 09:24:32 +0200 Subject: [PATCH 09/15] [FIX] backport from MP on upstream until 5f30c6ed09e92e53ad870d1fd54288ac43a4ca9e --- account_bank_statement_import/account_bank_statement_import.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py index 5d9dbaaa..98950c66 100644 --- a/account_bank_statement_import/account_bank_statement_import.py +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -193,7 +193,8 @@ class account_bank_statement_import(models.TransientModel): @api.model @api.returns('res.partner.bank') - def _create_bank_account(self, account_number, journal_id=False): + def _create_bank_account(self, account_number, company_id=False, + currency_id=False): try: bank_type = self.env.ref('base.bank_normal') bank_code = bank_type.code From 5538c234d9660224c6d37b0b1888798c22afc8c9 Mon Sep 17 00:00:00 2001 From: "Ronald Portier (Therp BV)" Date: Thu, 21 May 2015 12:13:47 +0200 Subject: [PATCH 10/15] [FIX] Use unique accounts and improved bank search in base. --- account_bank_statement_import/res_partner_bank.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/account_bank_statement_import/res_partner_bank.py b/account_bank_statement_import/res_partner_bank.py index ae6961df..f70ad987 100644 --- a/account_bank_statement_import/res_partner_bank.py +++ b/account_bank_statement_import/res_partner_bank.py @@ -64,3 +64,8 @@ class ResPartnerBank(models.Model): return super(ResPartnerBank, self).search( cr, user, args, offset=0, limit=None, order=None, context=None, count=False) + + _sql_constraints = [ + ('unique_number', 'unique(sanitized_acc_number)', + 'Account Number must be unique'), + ] From 1dcd07e8931f4519dcbd23535415d379b1db001f Mon Sep 17 00:00:00 2001 From: "Ronald Portier (Therp BV)" Date: Thu, 21 May 2015 12:15:02 +0200 Subject: [PATCH 11/15] [FIX] No index=True needed when specifying unique constraint. --- account_bank_statement_import/res_partner_bank.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/account_bank_statement_import/res_partner_bank.py b/account_bank_statement_import/res_partner_bank.py index f70ad987..72bcc458 100644 --- a/account_bank_statement_import/res_partner_bank.py +++ b/account_bank_statement_import/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, index=True) + compute='_get_sanitized_account_number', store=True) def _sanitize_account_number(self, acc_number): if acc_number: From 0550df383c2b59e3aa17622fc07d931727b488fa Mon Sep 17 00:00:00 2001 From: Laurent Mignon Date: Fri, 5 Jun 2015 12:31:08 +0200 Subject: [PATCH 12/15] [IMP] Extract description to README Add OCA as author, PEP8 --- account_bank_statement_import/README.rst | 55 ++++++++++++++++ account_bank_statement_import/__openerp__.py | 9 +-- .../account_bank_statement_import.py | 4 +- account_bank_statement_import_ofx/README.rst | 63 +++++++++++++++++++ .../__openerp__.py | 35 +++-------- .../account_bank_statement_import_ofx.py | 4 +- .../tests/test_import_bank_statement.py | 9 --- account_bank_statement_import_qif/README.rst | 58 +++++++++++++++++ .../__openerp__.py | 27 +++----- .../account_bank_statement_import_qif.py | 22 ++++--- 10 files changed, 215 insertions(+), 71 deletions(-) create mode 100644 account_bank_statement_import/README.rst create mode 100644 account_bank_statement_import_ofx/README.rst create mode 100644 account_bank_statement_import_qif/README.rst diff --git a/account_bank_statement_import/README.rst b/account_bank_statement_import/README.rst new file mode 100644 index 00000000..e83688f1 --- /dev/null +++ b/account_bank_statement_import/README.rst @@ -0,0 +1,55 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :alt: License: AGPL-3 + +Account Bank Statement Import +============================= + +This module add a generic wizard to import Bank Statements. It also extend +the bank account module to sanitize the account number and extend the search +method to use this field when searching on account_number. + +The module has been initiated by a backport of the new framework developed +by Odoo for V9 at its early stage. It's no more kept in sync with the V9 since +it has reach a stage where maintaining a pure backport of 9.0 in 8.0 is not +feasible anymore + +Known issues / Roadmap +====================== + +* None + +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 +------------ + +* Odoo SA +* Pedro M. Baeza +* Alexis de Lattre +* Laurent Mignon +* Ronald Portier + +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/account_bank_statement_import/__openerp__.py b/account_bank_statement_import/__openerp__.py index 2c791228..1d42ae23 100644 --- a/account_bank_statement_import/__openerp__.py +++ b/account_bank_statement_import/__openerp__.py @@ -3,13 +3,10 @@ 'name': 'Account Bank Statement Import', 'category': 'Accounting & Finance', 'version': '1.0', - 'author': 'OpenERP SA', + 'author': 'OpenERP SA,' + 'Odoo Community Association (OCA)', + 'website': 'https://github.com/OCA/bank-statement-import', 'depends': ['account'], - 'demo': [], - 'description': """Generic Wizard to Import Bank Statements. - - Backport from Odoo 9.0 - """, 'data': [ 'account_bank_statement_import_view.xml', ], diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py index 98950c66..cd7a46cf 100644 --- a/account_bank_statement_import/account_bank_statement_import.py +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -9,7 +9,7 @@ import logging _logger = logging.getLogger(__name__) -class account_bank_statement_line(models.Model): +class AccountBankStatementLine(models.Model): _inherit = "account.bank.statement.line" # Ensure transactions can be imported only once (if the import format @@ -23,7 +23,7 @@ class account_bank_statement_line(models.Model): ] -class account_bank_statement_import(models.TransientModel): +class AccountBankStatementImport(models.TransientModel): _name = 'account.bank.statement.import' _description = 'Import Bank Statement' diff --git a/account_bank_statement_import_ofx/README.rst b/account_bank_statement_import_ofx/README.rst new file mode 100644 index 00000000..6fb12603 --- /dev/null +++ b/account_bank_statement_import_ofx/README.rst @@ -0,0 +1,63 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :alt: License: AGPL-3 + +Import OFX Bank Statement +========================= + +This module allows you to import the machine readable OFX Files in Odoo: they are parsed and stored in human readable format in +Accounting \ Bank and Cash \ Bank Statements. + +Bank Statements may be generated containing a subset of the OFX information (only those transaction lines that are required for the +creation of the Financial Accounting records). + +The module has been initiated by a backport of the new framework developed +by Odoo for V9 at its early stage. It's no more kept in sync with the V9 since +it has reach a stage where maintaining a pure backport of 9.0 in 8.0 is not +feasible anymore + +Installation +============ + +The module requires one additional python lib: + +* `ofxparse `_ + +Known issues / Roadmap +====================== + +* None + +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 +------------ + +* Odoo SA +* Alexis de Lattre +* Laurent Mignon +* Ronald Portier + +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/account_bank_statement_import_ofx/__openerp__.py b/account_bank_statement_import_ofx/__openerp__.py index 88329844..212ac318 100644 --- a/account_bank_statement_import_ofx/__openerp__.py +++ b/account_bank_statement_import_ofx/__openerp__.py @@ -1,32 +1,17 @@ # -*- encoding: utf-8 -*- -# noqa: This is a backport from Odoo. OCA has no control over style here. -# flake8: noqa { 'name': 'Import OFX Bank Statement', - 'category' : 'Accounting & Finance', + 'category': 'Accounting & Finance', 'version': '1.0', - 'author': 'OpenERP SA', - 'depends': ['account_bank_statement_import'], - 'demo': [], - 'description' : """ -Module to import OFX bank statements. -====================================== - -This module allows you to import the machine readable OFX Files in Odoo: they are parsed and stored in human readable format in -Accounting \ Bank and Cash \ Bank Statements. - -Bank Statements may be generated containing a subset of the OFX information (only those transaction lines that are required for the -creation of the Financial Accounting records). - -Backported from Odoo 9.0 - -When testing with the provided test file, make sure the demo data from the -base account_bank_statement_import module has been imported, or manually -create periods for the year 2013. - """, - 'data' : [], - 'depends': ['account_bank_statement_import'], - 'demo': [], + 'author': 'OpenERP SA,' + 'Odoo Community Association (OCA)', + 'website': 'https://github.com/OCA/bank-statement-import', + 'depends': [ + 'account_bank_statement_import' + ], + 'external_dependencies': { + 'python': ['ofxparse'], + }, 'auto_install': True, 'installable': True, } diff --git a/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py b/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py index 93c89313..9358ae12 100644 --- a/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py +++ b/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py @@ -16,7 +16,7 @@ except ImportError: ofxparser = None -class account_bank_statement_import(models.TransientModel): +class AccountBankStatementImport(models.TransientModel): _inherit = 'account.bank.statement.import' @api.model @@ -33,7 +33,7 @@ class account_bank_statement_import(models.TransientModel): def _parse_file(self, data_file): ofx = self._check_ofx(data_file) if not ofx: - return super(account_bank_statement_import, self)._parse_file( + return super(AccountBankStatementImport, self)._parse_file( data_file) transactions = [] diff --git a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py index ad54f3dc..b35afa17 100644 --- a/account_bank_statement_import_ofx/tests/test_import_bank_statement.py +++ b/account_bank_statement_import_ofx/tests/test_import_bank_statement.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -# noqa: This is a backport from Odoo. OCA has no control over style here. -# flake8: noqa from openerp.tests.common import TransactionCase from openerp.modules.module import get_module_resource @@ -16,12 +14,6 @@ class TestOfxFile(TransactionCase): self.bank_statement_model = self.env['account.bank.statement'] def test_ofx_file_import(self): - try: - from ofxparse import OfxParser as ofxparser - except ImportError: - # the Python library isn't installed on the server, the OFX import - # is unavailable and the test cannot be run - return True ofx_file_path = get_module_resource( 'account_bank_statement_import_ofx', 'test_ofx_file', 'test_ofx.ofx') @@ -41,4 +33,3 @@ class TestOfxFile(TransactionCase): self.assertEquals( line.bank_account_id.id, self.ref('account_bank_statement_import.ofx_partner_bank_1')) - diff --git a/account_bank_statement_import_qif/README.rst b/account_bank_statement_import_qif/README.rst new file mode 100644 index 00000000..66411e6d --- /dev/null +++ b/account_bank_statement_import_qif/README.rst @@ -0,0 +1,58 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :alt: License: AGPL-3 + +Module to import QIF bank statements. +===================================== + +This module allows you to import the machine readable QIF Files in Odoo: they are parsed and stored in human readable format in +Accounting \ Bank and Cash \ Bank Statements. + +Important Note +--------------- +Because of the QIF format limitation, we cannot ensure the same transactions aren't imported several times or handle multicurrency. +Whenever possible, you should use a more appropriate file format like OFX. + +The module has been initiated by a backport of the new framework developed +by Odoo for V9 at its early stage. It's no more kept in sync with the V9 since +it has reach a stage where maintaining a pure backport of 9.0 in 8.0 is not +feasible anymore + +Known issues / Roadmap +====================== + +* None + +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 +------------ + +* Odoo SA +* Alexis de Lattre +* Laurent Mignon +* Ronald Portier + +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/account_bank_statement_import_qif/__openerp__.py b/account_bank_statement_import_qif/__openerp__.py index 11fd371a..59eaef80 100644 --- a/account_bank_statement_import_qif/__openerp__.py +++ b/account_bank_statement_import_qif/__openerp__.py @@ -1,28 +1,19 @@ # -*- coding: utf-8 -*- -# noqa: This is a backport from Odoo. OCA has no control over style here. -# flake8: noqa { 'name': 'Import QIF Bank Statement', 'category': 'Accounting & Finance', 'version': '1.0', - 'author': 'OpenERP SA', - 'description': ''' -Module to import QIF bank statements. -====================================== - -This module allows you to import the machine readable QIF Files in Odoo: they are parsed and stored in human readable format in -Accounting \ Bank and Cash \ Bank Statements. - -Important Note ---------------------------------------------- -Because of the QIF format limitation, we cannot ensure the same transactions aren't imported several times or handle multicurrency. -Whenever possible, you should use a more appropriate file format like OFX. -''', + 'author': 'OpenERP SA,' + 'Odoo Community Association (OCA)', + 'website': 'https://github.com/OCA/bank-statement-import', 'images': [], - 'depends': ['account_bank_statement_import'], - 'demo': [], - 'data': ['account_bank_statement_import_qif_view.xml'], + 'depends': [ + 'account_bank_statement_import' + ], + 'data': [ + 'account_bank_statement_import_qif_view.xml' + ], 'auto_install': False, 'installable': True, } diff --git a/account_bank_statement_import_qif/account_bank_statement_import_qif.py b/account_bank_statement_import_qif/account_bank_statement_import_qif.py index a3aa25f2..cac2bc50 100644 --- a/account_bank_statement_import_qif/account_bank_statement_import_qif.py +++ b/account_bank_statement_import_qif/account_bank_statement_import_qif.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -# noqa: This is a backport from Odoo. OCA has no control over style here. -# flake8: noqa import dateutil.parser import StringIO @@ -10,7 +8,7 @@ from openerp import api, models, fields from openerp.exceptions import Warning -class account_bank_statement_import(models.TransientModel): +class AccountBankStatementImport(models.TransientModel): _inherit = "account.bank.statement.import" @api.model @@ -37,7 +35,7 @@ class account_bank_statement_import(models.TransientModel): record = self.browse(active_id) if record.journal_id: record = record.with_context(journal_id=record.journal_id.id) - return super(account_bank_statement_import, record)._get_journal( + return super(AccountBankStatementImport, record)._get_journal( currency_id, bank_account_id, account_number) @api.model @@ -47,7 +45,7 @@ class account_bank_statement_import(models.TransientModel): @api.model def _parse_file(self, data_file): if not self._check_qif(data_file): - return super(account_bank_statement_import, self)._parse_file( + return super(AccountBankStatementImport, self)._parse_file( data_file) try: @@ -80,9 +78,13 @@ class account_bank_statement_import(models.TransientModel): elif line[0] == 'N': # Check number vals_line['ref'] = line[1:] elif line[0] == 'P': # Payee - vals_line['name'] = 'name' in vals_line and line[1:] + ': ' + vals_line['name'] or line[1:] - # Since QIF doesn't provide account numbers, we'll have to find res.partner and res.partner.bank here - # (normal behavious is to provide 'account_number', which the generic module uses to find partner/bank) + vals_line['name'] = ('name' in vals_line and + line[1:] + ': ' + vals_line['name'] or + line[1:]) + # Since QIF doesn't provide account numbers, we'll have to + # find res.partner and res.partner.bank here + # (normal behavious is to provide 'account_number', which + # the generic module uses to find partner/bank) banks = self.env['res.partner.bank'].search( [('owner_name', '=', line[1:])], limit=1) if banks: @@ -90,7 +92,9 @@ class account_bank_statement_import(models.TransientModel): vals_line['bank_account_id'] = bank_account.id vals_line['partner_id'] = bank_account.partner_id.id elif line[0] == 'M': # Memo - vals_line['name'] = 'name' in vals_line and vals_line['name'] + ': ' + line[1:] or line[1:] + vals_line['name'] = ('name' in vals_line and + vals_line['name'] + ': ' + line[1:] or + line[1:]) elif line[0] == '^': # end of item transactions.append(vals_line) vals_line = {} From d29adac20c5ffaf61927646ad2b4ff0f6460e9aa Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Mon, 8 Jun 2015 11:13:41 +0200 Subject: [PATCH 13/15] [ADD] i18n --- .../account_bank_statement_import_ofx.pot | 30 ++++++++++++ .../account_bank_statement_import_qif.pot | 49 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 account_bank_statement_import_ofx/i18n/account_bank_statement_import_ofx.pot create mode 100644 account_bank_statement_import_qif/i18n/account_bank_statement_import_qif.pot diff --git a/account_bank_statement_import_ofx/i18n/account_bank_statement_import_ofx.pot b/account_bank_statement_import_ofx/i18n/account_bank_statement_import_ofx.pot new file mode 100644 index 00000000..1c67dafa --- /dev/null +++ b/account_bank_statement_import_ofx/i18n/account_bank_statement_import_ofx.pot @@ -0,0 +1,30 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * account_bank_statement_import_ofx +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2015-06-08 09:11+0000\n" +"PO-Revision-Date: 2015-06-08 09:11+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: account_bank_statement_import_ofx +#: model:ir.model,name:account_bank_statement_import_ofx.model_account_bank_statement_import +msgid "Import Bank Statement" +msgstr "" + +#. module: account_bank_statement_import_ofx +#: code:addons/account_bank_statement_import_ofx/account_bank_statement_import_ofx.py:67 +#, python-format +msgid "The following problem occurred during import. The file might not be valid.\n" +"\n" +" %s" +msgstr "" + diff --git a/account_bank_statement_import_qif/i18n/account_bank_statement_import_qif.pot b/account_bank_statement_import_qif/i18n/account_bank_statement_import_qif.pot new file mode 100644 index 00000000..add1ceb1 --- /dev/null +++ b/account_bank_statement_import_qif/i18n/account_bank_statement_import_qif.pot @@ -0,0 +1,49 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * account_bank_statement_import_qif +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2015-06-08 09:10+0000\n" +"PO-Revision-Date: 2015-06-08 09:10+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: account_bank_statement_import_qif +#: help:account.bank.statement.import,journal_id:0 +msgid "Accounting journal related to the bank statement you're importing. It has be be manually chosen for statement formats which doesn't allow automatic journal detection (QIF for example)." +msgstr "" + +#. module: account_bank_statement_import_qif +#: code:addons/account_bank_statement_import_qif/account_bank_statement_import_qif.py:62 +#, python-format +msgid "Could not decipher the QIF file." +msgstr "" + +#. module: account_bank_statement_import_qif +#: field:account.bank.statement.import,hide_journal_field:0 +msgid "Hide the journal field in the view" +msgstr "" + +#. module: account_bank_statement_import_qif +#: model:ir.model,name:account_bank_statement_import_qif.model_account_bank_statement_import +msgid "Import Bank Statement" +msgstr "" + +#. module: account_bank_statement_import_qif +#: field:account.bank.statement.import,journal_id:0 +msgid "Journal" +msgstr "" + +#. module: account_bank_statement_import_qif +#: code:addons/account_bank_statement_import_qif/account_bank_statement_import_qif.py:106 +#, python-format +msgid "This file is either not a bank statement or is not correctly formed." +msgstr "" + From 6db13fce6aaf728761bed268d394c88235dcac57 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Mon, 8 Jun 2015 12:25:47 +0200 Subject: [PATCH 14/15] [FIX] Keep icon retouched by @pedrobaeza --- .../static/description/icon_src.svg | 150 +++++++++--------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/account_bank_statement_import/static/description/icon_src.svg b/account_bank_statement_import/static/description/icon_src.svg index 664381e0..0443f56c 100644 --- a/account_bank_statement_import/static/description/icon_src.svg +++ b/account_bank_statement_import/static/description/icon_src.svg @@ -14,14 +14,14 @@ viewBox="0 0 100 100" width="100px" xml:space="preserve" - inkscape:version="0.48.2 r9819" - sodipodi:docname="1409271720_Noun_Project_100Icon_10px_grid-17.svg" - inkscape:export-filename="/Users/arthurmaniet/Desktop/icon.png" + inkscape:version="0.91 r" + sodipodi:docname="icon_src.svg" + inkscape:export-filename="icon.png" inkscape:export-xdpi="115.2" inkscape:export-ydpi="115.2">image/svg+xml08/12/13 1000.00 Delta PC08/15/13 75.46 Walts Drugs03/03/13 379.00 Epic Technologies03/04/13 20.28 YOUR LOCAL SU03/03/13 421.35 SPRINGFIELD WA03/03/13 379.00 Epic Technologies03/04/13 20.28 YOUR LOCAL SUP08/15/13 75.46 Walts Drugs08/12/13 1000.00 Delta PC03/03/13 421.35 SPRINGFIELD WA03/04/13 20.28 YOUR LOCAL SU03/03/13 379.00 Epic Technologies08/12/13 1000.00 De a PC03/03/13 379.00 E Technologies08/15/13 75.46 Walts Drugs03/04/13 20.28 YOUR LOCAL SU03/03/13 379.00 Epic Technologies08/12/13 1000.00 Delta PC08/15/13 75.46 Walts Drugs Date: Mon, 8 Jun 2015 14:15:46 +0200 Subject: [PATCH 15/15] [IMP]Move the field journal_id in the main module. By moving the field in the main module the others parsers can use it without duplicate it --- .../account_bank_statement_import.py | 14 ++++++ .../account_bank_statement_import_view.xml | 6 +++ .../i18n/account_bank_statement_import.pot | 45 ++++++++++++------- .../__openerp__.py | 3 -- .../account_bank_statement_import_qif.py | 10 +---- ...account_bank_statement_import_qif_view.xml | 24 ---------- .../account_bank_statement_import_qif.pot | 8 ++-- 7 files changed, 54 insertions(+), 56 deletions(-) delete mode 100644 account_bank_statement_import_qif/account_bank_statement_import_qif_view.xml diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py index cd7a46cf..c37460f1 100644 --- a/account_bank_statement_import/account_bank_statement_import.py +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -27,6 +27,20 @@ class AccountBankStatementImport(models.TransientModel): _name = 'account.bank.statement.import' _description = 'Import Bank Statement' + @api.model + def _get_hide_journal_field(self): + """ Return False if the journal_id can't be provided by the parsed + file and must be provided by the wizard. + See account_bank_statement_import_qif """ + return True + + journal_id = fields.Many2one( + 'account.journal', string='Journal', + help='Accounting journal related to the bank statement you\'re ' + 'importing. It has be be manually chosen for statement formats which ' + 'doesn\'t allow automatic journal detection (QIF for example).') + hide_journal_field = fields.Boolean( + 'Hide the journal field in the view', default=_get_hide_journal_field) data_file = fields.Binary( 'Bank Statement File', required=True, help='Get you bank statements in electronic format from your bank ' diff --git a/account_bank_statement_import/account_bank_statement_import_view.xml b/account_bank_statement_import/account_bank_statement_import_view.xml index 8fc98a45..6aaa888c 100644 --- a/account_bank_statement_import/account_bank_statement_import_view.xml +++ b/account_bank_statement_import/account_bank_statement_import_view.xml @@ -9,6 +9,12 @@
+ +