From ea6a58501aebf87ca28769d6a2fe595db2d6c9e9 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Wed, 26 Feb 2014 13:46:30 +0100 Subject: [PATCH] [ADD] mt940, a generic addon for MT940 parsing --- mt940/__init__.py | 21 ++++ mt940/__openerp__.py | 53 +++++++++ mt940/mt940.py | 196 ++++++++++++++++++++++++++++++++++ mt940/static/src/img/icon.png | Bin 0 -> 1142 bytes 4 files changed, 270 insertions(+) create mode 100644 mt940/__init__.py create mode 100644 mt940/__openerp__.py create mode 100644 mt940/mt940.py create mode 100644 mt940/static/src/img/icon.png diff --git a/mt940/__init__.py b/mt940/__init__.py new file mode 100644 index 000000000..bb289e723 --- /dev/null +++ b/mt940/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from . import mt940 diff --git a/mt940/__openerp__.py b/mt940/__openerp__.py new file mode 100644 index 000000000..3a6ee25a3 --- /dev/null +++ b/mt940/__openerp__.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +{ + "name" : "MT940", + "version" : "1.0", + "author" : "Therp BV", + "complexity": "expert", + "description": """ +This addon provides a generic parser for MT940 files. Given that MT940 is a +non-open non-standard of pure evil in the way that every bank cooks up its own +interpretation of it, this addon alone won't help you much. It is rather +intended to be used by other addons to implement the dialect specific to a +certain bank. + +See account_banking_nl_ing_mt940 for an example on how to use it. + """, + "category" : "Dependency", + "depends" : [ + 'account_banking', + ], + "data" : [ + ], + "js": [ + ], + "css": [ + ], + "qweb": [ + ], + "auto_install": False, + "installable": True, + "application": False, + "external_dependencies" : { + 'python' : [], + }, +} diff --git a/mt940/mt940.py b/mt940/mt940.py new file mode 100644 index 000000000..1e103dc40 --- /dev/null +++ b/mt940/mt940.py @@ -0,0 +1,196 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2014 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +""" +Parser for MT940 format files +""" +import re +import datetime +import logging +try: + from openerp.addons.account_banking.parsers.models import\ + mem_bank_statement, mem_bank_transaction + from openerp.tools.misc import DEFAULT_SERVER_DATE_FORMAT +except ImportError: + class mem_bank_statement: + def __init__(self): + self.transactions = [] + class mem_bank_transaction: + pass + DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d" + +class MT940(object): + '''Inherit this class in your account_banking.parsers.models.parser, + define functions to handle the tags you need to handle and adjust static + variables as needed. + + At least, you should override handle_tag_61 and handle_tag_86. Don't forget + to call super. + handle_tag_* functions receive the remainder of the the line (that is, + without ':XX:') and are supposed to write into self.current_transaction''' + + header_lines = 3 + '''One file can contain multiple statements, each with its own poorly + documented header. For now, the best thing to do seems to skip that''' + footer_regex = '^-}$' + footer_regex = '^-XXX$' + 'The line that denotes end of message, we need to create a new statement' + tag_regex = '^:[0-9]{2}[A-Z]*:' + + def __init__(self, *args, **kwargs): + super(MT940, self).__init__(*args, **kwargs) + 'state variables' + self.current_statement = None + 'type account_banking.parsers.models.mem_bank_statement' + self.current_transaction = None + 'type account_banking.parsers.models.mem_bank_transaction' + self.statements = [] + 'parsed statements up to now' + + def parse(self, cr, data): + 'account_banking.parsers.models.parser.parse()''' + iterator = data.split('\r\n').__iter__() + line = None + record_line = '' + try: + while True: + if not self.current_statement: + self.handle_header(cr, line, iterator) + line = iterator.next() + if not self.is_tag(cr, line) and not self.is_footer(cr, line): + record_line = self.append_continuation_line( + cr, record_line, line) + continue + if record_line: + self.handle_record(cr, record_line) + if self.is_footer(cr, line): + self.handle_footer(cr, line, iterator) + record_line = '' + continue + record_line = line + except StopIteration: + pass + + def append_continuation_line(self, cr, line, continuation_line): + '''append a continuation line for a multiline record. + Override and do data cleanups as necessary.''' + return line + continuation_line + + def is_footer(self, cr, line): + '''determine if a line is the footer of a statement''' + return line and bool(re.match(self.footer_regex, line)) + + def is_tag(self, cr, line): + '''determine if a line has a tag''' + return line and bool(re.match(self.tag_regex, line)) + + def handle_header(self, cr, line, iterator): + '''skip header lines, create current statement''' + for i in range(self.header_lines): + iterator.next() + self.current_statement = mem_bank_statement() + + def handle_footer(self, cr, line, iterator): + '''add current statement to list, reset state''' + self.statements.append(self.current_statement) + self.current_statement = None + + def handle_record(self, cr, line): + '''find a function to handle the record represented by line''' + tag_match = re.match(self.tag_regex, line) + tag = tag_match.group(0).strip(':') + if not hasattr(self, 'handle_tag_%s' % tag): + logging.error('Unknown tag %s', tag) + logging.error(line) + return + handler = getattr(self, 'handle_tag_%s' % tag) + handler(cr, line[tag_match.end():]) + + def handle_tag_20(self, cr, data): + '''ignore reference number''' + pass + + def handle_tag_25(self, cr, data): + '''get account owner information''' + self.current_statement.local_account = data + + def handle_tag_28C(self, cr, data): + '''get sequence number _within_this_batch_ - this alone + doesn't provide a unique id!''' + pass + + def handle_tag_60F(self, cr, data): + '''get start balance and currency''' + self.current_statement.local_currency = data[7:10] + self.current_statement.date = str2date(data[1:7]) + self.current_statement.start_balance = \ + (1 if data[0] == 'C' else -1) * str2float(data[10:]) + + def handle_tag_62F(self, cr, data): + '''get ending balance''' + self.current_statement.end_balance = \ + (1 if data[0] == 'C' else -1) * str2float(data[10:]) + + def handle_tag_64(self, cr, data): + '''get current balance in currency''' + pass + + def handle_tag_65(self, cr, data): + '''get future balance in currency''' + pass + + def handle_tag_61(self, cr, data): + '''get transaction values''' + transaction = mem_bank_transaction() + self.current_statement.transactions.append(transaction) + self.current_transaction = transaction + transaction.execution_date = str2date(data[:6]) + '...and the rest already is highly bank dependent' + + def handle_tag_86(self, cr, data): + '''details for previous transaction, here most differences between + banks occur''' + pass + +'utility functions' +def str2date(string, fmt='%y%m%d'): + return datetime.datetime.strptime(string, fmt).strftime( + DEFAULT_SERVER_DATE_FORMAT) + +def str2float(string): + return float(string.replace(',', '.')) + +'testing' +def main(filename): + parser = MT940() + parser.parse(None, open(filename, 'r').read()) + for statement in parser.statements: + print '''statement found for %(local_account)s at %(date)s + with %(local_currency)s%(start_balance)s to %(end_balance)s + ''' % statement.__dict__ + for transaction in statement.transactions: + print ''' + transaction on %(execution_date)s''' % transaction.__dict__ + +if __name__ == '__main__': + import sys + main(*sys.argv[1:]) diff --git a/mt940/static/src/img/icon.png b/mt940/static/src/img/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..4c7ab302908e114888446d84d3493fa726033c1f GIT binary patch literal 1142 zcmeAS@N?(olHy`uVBq!ia0vp^0U*r53?z4+XPOVBSkfJR9T^xl_H+M9WCijWi-X*q z7}lMWc?skwBzpw;GB8xBF)%c=FfjZA3N^f7U???UV0e|lz+g3lfkC`r&aOZkpuXS$ zpAc7|0+Vy!%`Sd3J@*~Rz=H@Xz@vBMNCCrhU++~OAXQKjgnPb5^?zLm6yRy4l)f7mx|d; zx?*(k%?4)UEmyi0Ecrc2=k&YFn|8nX@qd4)(saLN%zo##oL4V9SpH%8W(I{5_Kby- zneS~VhopAyb6lBS&$U5E`gKppbdV7NQIC+SE zKe2ts8LoR*Hw$jqcIEDHSU_mi4;HoUDLTgOJMIHx zO|`@|q9i4;B-JXpC>2OC7#SEE>l#?<8d`)H8e5qfSQ#5>8yHy`7