# -*- coding: utf-8 -*- """Implement BankStatementParser for MT940 IBAN ING files.""" ############################################################################## # # Copyright (C) 2014-2015 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 . # ############################################################################## import re from openerp.addons.account_bank_statement_import_mt940_base.mt940 import ( MT940, str2amount, get_subfields, handle_common_subfields) class MT940Parser(MT940): """Parser for ing MT940 bank statement import files.""" tag_61_regex = re.compile( r'^(?P\d{6})(?P\d{0,4})' r'(?P[CD])(?P\d+,\d{2})N(?P.{3})' r'(?P\w{0,16})' r'(//(?P\w{14})/TRCD/(?P\w{0,34})){0,1}' ) def __init__(self): """Initialize parser - override at least header_regex.""" super(MT940Parser, self).__init__() self.mt940_type = 'ING' def handle_tag_61(self, data): """get transaction values""" super(MT940Parser, self).handle_tag_61(data) re_61 = self.tag_61_regex.match(data) if not re_61: raise ValueError("Cannot parse %s" % data) parsed_data = re_61.groupdict() self.current_transaction.transferred_amount = ( str2amount(parsed_data['sign'], parsed_data['amount'])) self.current_transaction.eref = parsed_data['reference'] self.current_transaction.id = parsed_data['ingid'] def handle_tag_86(self, data): """Parse 86 tag containing reference data.""" if not self.current_transaction: return codewords = ['RTRN', 'BENM', 'ORDP', 'CSID', 'BUSP', 'MARF', 'EREF', 'PREF', 'REMI', 'ID', 'PURP', 'ULTB', 'ULTD', 'CREF', 'IREF', 'CNTP', 'ULTC', 'EXCH', 'CHGS'] subfields = get_subfields(data, codewords) transaction = self.current_transaction # If we have no subfields, set message to whole of data passed: if not subfields: transaction.message = data else: handle_common_subfields(transaction, subfields) # Prevent handling tag 86 later for non transaction details: self.current_transaction = None