From 068ab3a5989945dbb96e2834c568345b3c67c6a9 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (Acsone)" Date: Thu, 22 Aug 2013 11:52:09 +0200 Subject: [PATCH] New account statement import plugin for CODA files --- account_statement_coda_import/__init__.py | 22 ++++ account_statement_coda_import/__openerp__.py | 54 +++++++++ .../parser/__init__.py | 22 ++++ .../parser/coda_file_parser.py | 106 ++++++++++++++++++ account_statement_coda_import/statement.py | 48 ++++++++ 5 files changed, 252 insertions(+) create mode 100644 account_statement_coda_import/__init__.py create mode 100644 account_statement_coda_import/__openerp__.py create mode 100644 account_statement_coda_import/parser/__init__.py create mode 100644 account_statement_coda_import/parser/coda_file_parser.py create mode 100644 account_statement_coda_import/statement.py diff --git a/account_statement_coda_import/__init__.py b/account_statement_coda_import/__init__.py new file mode 100644 index 00000000..29698b5e --- /dev/null +++ b/account_statement_coda_import/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Author: Joel Grand-Guillaume +# Copyright 2011-2012 Camptocamp SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +import parser +import statement diff --git a/account_statement_coda_import/__openerp__.py b/account_statement_coda_import/__openerp__.py new file mode 100644 index 00000000..45f34076 --- /dev/null +++ b/account_statement_coda_import/__openerp__.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Author: Laurent Mignon +# Copyright 2013 'ACSONE SA/NV' +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +{'name': "Bank statement CODA import", + 'version': '1.0', + 'author': 'Laurent Mignon (Acsone)', + 'maintainer': 'ACSONE SA/NV', + 'category': 'Finance', + 'complexity': 'normal', + 'depends': [ + 'account_statement_base_import', + 'account_statement_transactionid_completion' + ], + 'description': """ + This module brings generic methods and fields on bank statement to deal with + the importation of coded statement of account from electronic files. + + This module allows you to import your bank transactions with a standard CODA file + (you'll find samples in the 'data' folder). It respects the chosen profile + (model provided by the account_statement_ext module) to generate the entries. + + """, + 'website': 'http://www.acsone.eu', + 'external_dependencies': { + 'python' : ['coda'], + }, + 'init_xml': [], + 'update_xml': [ + ], + 'demo_xml': [], + 'test': [], + 'installable': True, + 'images': [], + 'auto_install': False, + 'license': 'AGPL-3', +} diff --git a/account_statement_coda_import/parser/__init__.py b/account_statement_coda_import/parser/__init__.py new file mode 100644 index 00000000..a70e93fb --- /dev/null +++ b/account_statement_coda_import/parser/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Author: Nicolas Bessi +# Copyright 2011-2012 Camptocamp SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +import coda_file_parser diff --git a/account_statement_coda_import/parser/coda_file_parser.py b/account_statement_coda_import/parser/coda_file_parser.py new file mode 100644 index 00000000..f330c582 --- /dev/null +++ b/account_statement_coda_import/parser/coda_file_parser.py @@ -0,0 +1,106 @@ +# -*- coding: utf-8 -*- +# +# +# Authors: Laurent Mignon +# Copyright (c) 2013 Acsone SA/NV (http://www.acsone.eu) +# All Rights Reserved +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# + +import datetime + +from coda.parser import Parser +from coda.statement import AmountSign, MovementRecordType + +from openerp.osv.osv import except_osv +from openerp.tools.translate import _ +from account_statement_base_import.parser.file_parser import BankStatementImportParser + + +class CodaFileParser(BankStatementImportParser): + + """ + CODA parser that use a define format in coda to import + bank statement. + """ + + def __init__(self, parse_name, parser=Parser(), *args, **kwargs): + self._parser = parser + self._statement = None + super(CodaFileParser, self).__init__(parse_name, *args, **kwargs) + + @classmethod + def parser_for(cls, parser_name): + """ + Used by the new_bank_statement_parser class factory. Return true if + the providen name is coda_transaction + """ + return parser_name == 'coda_transaction' + + def _custom_format(self, *args, **kwargs): + """ + No other work on data are needed in this parser. + """ + return True + + def _pre(self, *args, **kwargs): + """ + No pre-treatment needed for this parser. + """ + return True + + def _parse(self, *args, **kwargs): + """ + Launch the parsing through the CODA Parser + """ + statements = self._parser.parse(self.filebuffer) + if len(statements) > 1: + raise except_osv(_('Not supported CODA file'), + _('Only one statement by CODA file is supported')) + if len(statements) == 1: + self._statement = statements[0] + self.result_row_list = [m for m in self._statement.movements if m.type == MovementRecordType.NORMAL] + return True + + def _validate(self, *args, **kwargs): + """ + No validation needed for this parser + """ + return True + + def get_st_line_vals(self, line, *args, **kwargs): + """ + This method must return a dict of vals that can be passed to create + method of statement line in order to record it. It is the responsibility + of every parser to give this dict of vals, so each one can implement his + own way of recording the lines. + :param: line: a dict of vals that represent a line of result_row_list + :return: dict of values to give to the create method of statement line, + it MUST contain at least: + { + 'name':value, + 'date':value, + 'amount':value, + 'ref':value, + } + """ + amount = line.transaction_amount + if line.transaction_amount_sign == AmountSign.DEBIT: + amount = - amount + return {'name': "\n".join(filter(None, [line.counterparty_name, line.communication])), + 'date': line.entry_date or datetime.datetime.now().date(), + 'amount': amount, + 'ref': line.ref} diff --git a/account_statement_coda_import/statement.py b/account_statement_coda_import/statement.py new file mode 100644 index 00000000..969b19d0 --- /dev/null +++ b/account_statement_coda_import/statement.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Authors: Laurent Mignon +# Copyright (c) 2013 Acsone SA/NV (http://www.acsone.eu) +# All Rights Reserved +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp.osv.orm import Model +from openerp.osv import fields + + +class AccountStatementProfil(Model): + _inherit = "account.statement.profile" + + def get_import_type_selection(self, cr, uid, context=None): + """ + Has to be inherited to add parser + """ + res = super(AccountStatementProfil, self).get_import_type_selection( + cr, uid, context=context) + res.append(('coda_transaction', + 'CODA based transaction')) + return res + + _columns = { + 'import_type': fields.selection( + get_import_type_selection, + 'Type of import', + required=True, + help="Choose here the method by which you want to import " + "bank statement for this profile."), + + }