From 0634acb78fd77b78e984ba7fa5c5071c38e6bafc Mon Sep 17 00:00:00 2001 From: "Ronald Portier (Therp BV)" Date: Mon, 15 Dec 2014 10:18:55 +0100 Subject: [PATCH] [RFR] Move backported bank import modules from bank-payment to bank-statement-import repository. --- account_bank_statement_import_qif/__init__.py | 4 + .../__openerp__.py | 32 ++++++++ .../account_bank_statement_import_qif.py | 77 +++++++++++++++++++ .../test_qif_file/test_qif.qif | 21 +++++ .../tests/__init__.py | 8 ++ .../tests/test_import_bank_statement.py | 30 ++++++++ 6 files changed, 172 insertions(+) create mode 100644 account_bank_statement_import_qif/__init__.py create mode 100644 account_bank_statement_import_qif/__openerp__.py create mode 100644 account_bank_statement_import_qif/account_bank_statement_import_qif.py create mode 100644 account_bank_statement_import_qif/test_qif_file/test_qif.qif create mode 100644 account_bank_statement_import_qif/tests/__init__.py create mode 100644 account_bank_statement_import_qif/tests/test_import_bank_statement.py diff --git a/account_bank_statement_import_qif/__init__.py b/account_bank_statement_import_qif/__init__.py new file mode 100644 index 0000000..784a476 --- /dev/null +++ b/account_bank_statement_import_qif/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +from . import account_bank_statement_import_qif + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/account_bank_statement_import_qif/__openerp__.py b/account_bank_statement_import_qif/__openerp__.py new file mode 100644 index 0000000..d06c90a --- /dev/null +++ b/account_bank_statement_import_qif/__openerp__.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# noqa: This is a backport from Odoo. OCA has no control over style here. +# flake8: noqa +{ + 'name': 'Import QIF Bank Statement', + '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. + +Bank Statements may be generated containing a subset of the QIF 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. +''', + 'images' : [], + 'depends': ['account_bank_statement_import'], + 'demo': [], + 'data': [], + 'auto_install': False, + 'installable': True, +} + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: 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 new file mode 100644 index 0000000..a95656e --- /dev/null +++ b/account_bank_statement_import_qif/account_bank_statement_import_qif.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +# noqa: This is a backport from Odoo. OCA has no control over style here. +# flake8: noqa + +import dateutil.parser +import base64 +from tempfile import TemporaryFile + +from openerp.tools.translate import _ +from openerp.osv import osv + +from openerp.addons.account_bank_statement_import import account_bank_statement_import as ibs + +ibs.add_file_type(('qif', 'QIF')) + +class account_bank_statement_import(osv.TransientModel): + _inherit = "account.bank.statement.import" + + def process_qif(self, cr, uid, data_file, journal_id=False, context=None): + """ Import a file in the .QIF format""" + try: + fileobj = TemporaryFile('wb+') + fileobj.write(base64.b64decode(data_file)) + fileobj.seek(0) + file_data = "" + for line in fileobj.readlines(): + file_data += line + fileobj.close() + if '\r' in file_data: + data_list = file_data.split('\r') + else: + data_list = file_data.split('\n') + header = data_list[0].strip() + header = header.split(":")[1] + except: + raise osv.except_osv(_('Import Error!'), _('Please check QIF file format is proper or not.')) + line_ids = [] + vals_line = {} + total = 0 + if header == "Bank": + vals_bank_statement = {} + for line in data_list: + line = line.strip() + if not line: + continue + if line[0] == 'D': # date of transaction + vals_line['date'] = dateutil.parser.parse(line[1:], fuzzy=True).date() + if vals_line.get('date') and not vals_bank_statement.get('period_id'): + period_ids = self.pool.get('account.period').find(cr, uid, vals_line['date'], context=context) + vals_bank_statement.update({'period_id': period_ids and period_ids[0] or False}) + elif line[0] == 'T': # Total amount + total += float(line[1:].replace(',', '')) + vals_line['amount'] = float(line[1:].replace(',', '')) + elif line[0] == 'N': # Check number + vals_line['ref'] = line[1:] + elif line[0] == 'P': # Payee + bank_account_id, partner_id = self._detect_partner(cr, uid, line[1:], identifying_field='owner_name', context=context) + vals_line['partner_id'] = partner_id + vals_line['bank_account_id'] = bank_account_id + vals_line['name'] = 'name' in vals_line and line[1:] + ': ' + vals_line['name'] or line[1:] + 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 + line_ids.append((0, 0, vals_line)) + vals_line = {} + elif line[0] == '\n': + line_ids = [] + else: + pass + else: + raise osv.except_osv(_('Error!'), _('Cannot support this Format !Type:%s.') % (header,)) + vals_bank_statement.update({'balance_end_real': total, + 'line_ids': line_ids, + 'journal_id': journal_id}) + return [vals_bank_statement] + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/account_bank_statement_import_qif/test_qif_file/test_qif.qif b/account_bank_statement_import_qif/test_qif_file/test_qif.qif new file mode 100644 index 0000000..5388e6d --- /dev/null +++ b/account_bank_statement_import_qif/test_qif_file/test_qif.qif @@ -0,0 +1,21 @@ +!Type:Bank +D8/12/13 +T-1,000.00 +PDelta PC +^ +D8/15/13 +T-75.46 +PWalts Drugs +^ +D3/3/13 +T-379.00 +PEpic Technologies +^ +D3/4/13 +T-20.28 +PYOUR LOCAL SUPERMARKET +^ +D3/3/13 +T-421.35 +PSPRINGFIELD WATER UTILITY +^ diff --git a/account_bank_statement_import_qif/tests/__init__.py b/account_bank_statement_import_qif/tests/__init__.py new file mode 100644 index 0000000..389df58 --- /dev/null +++ b/account_bank_statement_import_qif/tests/__init__.py @@ -0,0 +1,8 @@ +# -*- 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 +checks = [ + 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 new file mode 100644 index 0000000..6838129 --- /dev/null +++ b/account_bank_statement_import_qif/tests/test_import_bank_statement.py @@ -0,0 +1,30 @@ +# -*- 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) + """ + + 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') + + 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 = open(qif_file_path, 'rb').read().encode('base64') + bank_statement_id = self.statement_import_model.create(cr, uid, dict( + file_type='qif', + data_file=qif_file, + )) + self.statement_import_model.parse_file(cr, uid, [bank_statement_id]) + 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