From 26fac7bfa76a4ab7246c5f19d5c9f6593cf52a99 Mon Sep 17 00:00:00 2001 From: "Ronald Portier (Therp BV)" Date: Sat, 13 Jun 2015 01:20:18 +0200 Subject: [PATCH] [ENH] Support zip files. --- .../models/account_bank_statement_import.py | 33 ++++++++++++++++-- .../test_files/test-camt053.xml | 30 ++++++++-------- .../test_files/test-camt053.zip | Bin 0 -> 3111 bytes .../tests/test_import_bank_statement.py | 11 ++++-- .../mt940.py | 2 +- .../test_files/test-camt053.zip | Bin 0 -> 3111 bytes 6 files changed, 55 insertions(+), 21 deletions(-) create mode 100644 account_bank_statement_import_camt/test_files/test-camt053.zip create mode 100644 bank_statement_parse_camt/test_files/test-camt053.zip diff --git a/account_bank_statement_import/models/account_bank_statement_import.py b/account_bank_statement_import/models/account_bank_statement_import.py index 9a8b9136..58d07c53 100644 --- a/account_bank_statement_import/models/account_bank_statement_import.py +++ b/account_bank_statement_import/models/account_bank_statement_import.py @@ -2,6 +2,8 @@ """Framework for importing bank statement files.""" import logging import base64 +from StringIO import StringIO +from zipfile import ZipFile, BadZipfile # BadZipFile in Python >= 3.2 from openerp import api, models, fields from openerp.tools.translate import _ @@ -52,7 +54,7 @@ class AccountBankStatementImport(models.TransientModel): @api.multi def import_file(self): - """ Process the file chosen in the wizard, create bank statement(s) and + """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) @@ -71,13 +73,38 @@ class AccountBankStatementImport(models.TransientModel): 'type': 'ir.actions.client', } + @api.model + def _parse_all_files(self, data_file): + """Parse one file or multiple files from zip-file. + + Return array of statements for further processing. + """ + statements = [] + files = [data_file] + try: + with ZipFile(StringIO(data_file), 'r') as archive: + files = [ + archive.read(filename) for filename in archive.namelist() + if not filename.endswith('/') + ] + except BadZipfile: + pass + # Parse the file(s) + for import_file in files: + # The appropriate implementation module(s) returns the statements. + # Actually we don't care wether all the files have the same + # format. Although unlikely you might mix mt940 and camt files + # in one zipfile. + statements += self._parse_file(import_file) + return statements + @api.model def _import_file(self, data_file): """ Create bank statement(s) from file.""" # The appropriate implementation module returns the required data statement_ids = [] notifications = [] - parse_result = self._parse_file(data_file) + parse_result = self._parse_all_files(data_file) # Check for old version result, with separate currency and account if isinstance(parse_result, tuple) and len(parse_result) == 3: (currency_code, account_number, statements) = parse_result @@ -125,7 +152,7 @@ class AccountBankStatementImport(models.TransientModel): return self._create_bank_statement(stmt_vals) @api.model - def _parse_file(self, data_file): + def _parse_file(self, dummy_data_file): """ 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. diff --git a/account_bank_statement_import_camt/test_files/test-camt053.xml b/account_bank_statement_import_camt/test_files/test-camt053.xml index 1d74d81b..4a2a0473 100644 --- a/account_bank_statement_import_camt/test_files/test-camt053.xml +++ b/account_bank_statement_import_camt/test_files/test-camt053.xml @@ -2,15 +2,15 @@ TESTBANK/NL/1420561226673 - 2013-01-06T16:20:26.673Z + 2014-01-06T16:20:26.673Z 1234Test/1 2 - 2013-01-06T16:20:26.673Z + 2014-01-06T16:20:26.673Z - 2013-01-05T00:00:00.000Z - 2013-01-05T23:59:59.999Z + 2014-01-05T00:00:00.000Z + 2014-01-05T23:59:59.999Z @@ -32,7 +32,7 @@ 15568.27 CRDT
-
2013-01-05
+
2014-01-05
@@ -44,7 +44,7 @@ 15121.12 CRDT
-
2013-01-05
+
2014-01-05
@@ -52,10 +52,10 @@ DBIT BOOK -
2013-01-05
+
2014-01-05
-
2013-01-05
+
2014-01-05
@@ -104,9 +104,9 @@ - Insurance policy 857239PERIOD 01.01.2013 - 31.12.2013 + Insurance policy 857239PERIOD 01.01.2014 - 31.12.2014 - MKB Insurance 859239PERIOD 01.01.2013 - 31.12.2013 + MKB Insurance 859239PERIOD 01.01.2014 - 31.12.2014
@@ -116,10 +116,10 @@ true BOOK -
2013-01-05
+
2014-01-05
-
2013-01-05
+
2014-01-05
@@ -182,10 +182,10 @@ CRDT BOOK -
2013-01-05
+
2014-01-05
-
2013-01-05
+
2014-01-05
@@ -202,7 +202,7 @@ - INNDNL2U20130105000217200000708 + INNDNL2U20140105000217200000708 115 diff --git a/account_bank_statement_import_camt/test_files/test-camt053.zip b/account_bank_statement_import_camt/test_files/test-camt053.zip new file mode 100644 index 0000000000000000000000000000000000000000..ccf5b3c2ba72ad03cc137edcf2ee55f67b7a2952 GIT binary patch literal 3111 zcmeH}=T{S08pZ=5FccF7R764P14xk;5EP_{0RmY=5O4w{AVr!$C=oHlAWJhyReBE! zAqJ!U#$s;Q+O;qQmC7X$#CFp;S53XKp3 z@bIyK0Kng`kJ*<4=k&#UW|xqT(@Tov5YUyorl#QtnL>GL823Dkl9(M6fc0cI%(|Ni z6Q^GLcu71wsM2}%?CvjH!8b!s<&9$V%#|&29fp7$*Z6H3|gs9r@Iab`}w zn70^AGxt!43sw21&ugEeu5!s zB=aSvS|O$Bs(_1W`3N!7kW{?C+9uvd?a-AuvRv(>X!goeIdlV;b{mCeyP1vK$0=9p zhvGziT|Owbw0q_obsli0rv4Sr^-c(wMC`04QcZhDwWKHp@XFHQlHEbhsWMKSBZZOw zA}>C4#%A3WrP-sj`HDO4lA-;!yrBVX&3%#eOMKMU@+IV(nv7lhYS%{;$3{6@D@-?8 zj%$0W-f~sSJ^sQi^P9;MO$$IZFznJn;)6b5Ft;O?XIp3Wvr?Mxgx4Co_F1d*^z6>W z16A~WLf`!H5cIp~GA4ZCR{`G`WEw$zws*gec}$;&fZM zo5o5(B`9uG=!A2Ina*WDMms&g&>^6!qHt5;GqTVR1>*Ec+Z)I*wlOs-1v{7k(SD7cx&3TX>4UUU|z4aL*H}d*MEMlY`)`QpML{Lh>UZ>Y$ z#RRHb?tB{p^?Uws=MQd|l>2$Th%nhp47tF`Rx7B!RoxbGo{x9Ijt)~YQ+V444% zPzs&KG$xTJ)T2(Qz^8|QRB}mXgGS2uVz8>+^ZoN75^~j6MqhVW51<$wUzWDyF|J1 zirME@jT#|^OHAAz3aV0H+~Jxv^`tk-VY6f-Rl*g=@Y-F-Ba{e9gU80nOv-XmG14&C zV(E)5%on5Y=I7h_Gab0duA42{ST}8V<9on@o5adK;Lj4$TmL;iTZRlaNcE zF1&6LTv;hka~iWF8};~H>r~tMthOl-2An0f&eCUPY+h! z>c3ZOHlMoCRY`(}DnQqY19eBIViuA`HhKm_r5I4{dR+8rdB$SfS*>Oa_D7-8&b&W) z{U@*guX)YGD-Hakf-SLEya527J(Uu_PrqBYLI0@RzhO7R`d`NX9R>ex{Ae#A{?quM b!LVoiJE4HE2JwIY3-8{k*?V1uzi)p7q~E=A literal 0 HcmV?d00001 diff --git a/account_bank_statement_import_camt/tests/test_import_bank_statement.py b/account_bank_statement_import_camt/tests/test_import_bank_statement.py index 8a1694f0..d14aca96 100644 --- a/account_bank_statement_import_camt/tests/test_import_bank_statement.py +++ b/account_bank_statement_import_camt/tests/test_import_bank_statement.py @@ -31,11 +31,10 @@ class TestImport(TestStatementFile): { 'remote_account': 'NL46ABNA0499998748', 'transferred_amount': -754.25, - 'value_date': '2013-01-05', + 'value_date': '2014-01-05', 'ref': '435005714488-ABNO33052620', }, ] - # statement name is account number + '-' + date of last 62F line: self._test_statement_import( 'account_bank_statement_import_camt', 'test-camt053.xml', '1234Test/1', @@ -43,3 +42,11 @@ class TestImport(TestStatementFile): start_balance=15568.27, end_balance=15121.12, transactions=transactions ) + + def test_zip_import(self): + """Test import of multiple statements from zip file.""" + self._test_statement_import( + 'account_bank_statement_import_camt', 'test-camt053.zip', + '1234Test/2', # Only name of first statement + local_account='NL77ABNA0574908765', + ) diff --git a/account_bank_statement_import_mt940_base/mt940.py b/account_bank_statement_import_mt940_base/mt940.py index 8677bc7c..9ddd9cef 100644 --- a/account_bank_statement_import_mt940_base/mt940.py +++ b/account_bank_statement_import_mt940_base/mt940.py @@ -111,7 +111,7 @@ class MT940(object): This in fact uses the ING syntax, override in others.""" self.mt940_type = 'General' self.header_lines = 3 # Number of lines to skip - self.header_regex = '^0000 01INGBNL2AXXXX|^{1' + self.header_regex = '^0000 01INGBNL2AXXXX|^{1' # Start of header self.footer_regex = '^-}$|^-XXX$' # Stop processing on seeing this self.tag_regex = '^:[0-9]{2}[A-Z]*:' # Start of new tag self.current_statement = None diff --git a/bank_statement_parse_camt/test_files/test-camt053.zip b/bank_statement_parse_camt/test_files/test-camt053.zip new file mode 100644 index 0000000000000000000000000000000000000000..ccf5b3c2ba72ad03cc137edcf2ee55f67b7a2952 GIT binary patch literal 3111 zcmeH}=T{S08pZ=5FccF7R764P14xk;5EP_{0RmY=5O4w{AVr!$C=oHlAWJhyReBE! zAqJ!U#$s;Q+O;qQmC7X$#CFp;S53XKp3 z@bIyK0Kng`kJ*<4=k&#UW|xqT(@Tov5YUyorl#QtnL>GL823Dkl9(M6fc0cI%(|Ni z6Q^GLcu71wsM2}%?CvjH!8b!s<&9$V%#|&29fp7$*Z6H3|gs9r@Iab`}w zn70^AGxt!43sw21&ugEeu5!s zB=aSvS|O$Bs(_1W`3N!7kW{?C+9uvd?a-AuvRv(>X!goeIdlV;b{mCeyP1vK$0=9p zhvGziT|Owbw0q_obsli0rv4Sr^-c(wMC`04QcZhDwWKHp@XFHQlHEbhsWMKSBZZOw zA}>C4#%A3WrP-sj`HDO4lA-;!yrBVX&3%#eOMKMU@+IV(nv7lhYS%{;$3{6@D@-?8 zj%$0W-f~sSJ^sQi^P9;MO$$IZFznJn;)6b5Ft;O?XIp3Wvr?Mxgx4Co_F1d*^z6>W z16A~WLf`!H5cIp~GA4ZCR{`G`WEw$zws*gec}$;&fZM zo5o5(B`9uG=!A2Ina*WDMms&g&>^6!qHt5;GqTVR1>*Ec+Z)I*wlOs-1v{7k(SD7cx&3TX>4UUU|z4aL*H}d*MEMlY`)`QpML{Lh>UZ>Y$ z#RRHb?tB{p^?Uws=MQd|l>2$Th%nhp47tF`Rx7B!RoxbGo{x9Ijt)~YQ+V444% zPzs&KG$xTJ)T2(Qz^8|QRB}mXgGS2uVz8>+^ZoN75^~j6MqhVW51<$wUzWDyF|J1 zirME@jT#|^OHAAz3aV0H+~Jxv^`tk-VY6f-Rl*g=@Y-F-Ba{e9gU80nOv-XmG14&C zV(E)5%on5Y=I7h_Gab0duA42{ST}8V<9on@o5adK;Lj4$TmL;iTZRlaNcE zF1&6LTv;hka~iWF8};~H>r~tMthOl-2An0f&eCUPY+h! z>c3ZOHlMoCRY`(}DnQqY19eBIViuA`HhKm_r5I4{dR+8rdB$SfS*>Oa_D7-8&b&W) z{U@*guX)YGD-Hakf-SLEya527J(Uu_PrqBYLI0@RzhO7R`d`NX9R>ex{Ae#A{?quM b!LVoiJE4HE2JwIY3-8{k*?V1uzi)p7q~E=A literal 0 HcmV?d00001