Merge lp:~credativ/banking-addons/6.1-lloyds-corporate into lp:banking-addons/6.1

Proposed by Kinner Vachhani
Status: Merged
Merged at revision: 182
Proposed branch: lp:~credativ/banking-addons/6.1-lloyds-corporate
Merge into: lp:banking-addons/6.1
Diff against target: 252 lines (+232/-0)
4 files modified
account_banking_uk_lloyds_corporate/__init__.py (+24/-0)
account_banking_uk_lloyds_corporate/__openerp__.py (+37/-0)
account_banking_uk_lloyds_corporate/lloydscorporate.py (+157/-0)
account_banking_uk_lloyds_corporate/samplestatement.csv (+14/-0)
To merge this branch: bzr merge lp:~credativ/banking-addons/6.1-lloyds-corporate
Reviewer Review Type Date Requested Status
Holger Brunn (Therp) code review Approve
Stefan Rijnhart (Opener) Approve
Review via email: mp+184196@code.launchpad.net

Description of the change

This modules contains a parser of uk lloyds corporate csv and imports as bank statement.

To post a comment you must log in.
Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

Thanks, looking good. At merge time, we should take care to cherry pick the relevant revisions into 7.0 too, as that should be 100% compatible.

review: Approve
Revision history for this message
Holger Brunn (Therp) (hbrunn) :
review: Approve (code review)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'account_banking_uk_lloyds_corporate'
2=== added file 'account_banking_uk_lloyds_corporate/__init__.py'
3--- account_banking_uk_lloyds_corporate/__init__.py 1970-01-01 00:00:00 +0000
4+++ account_banking_uk_lloyds_corporate/__init__.py 2013-09-05 21:56:06 +0000
5@@ -0,0 +1,24 @@
6+# -*- encoding: utf-8 -*-
7+##############################################################################
8+#
9+# Copyright (C) 2013 credativ Ltd (<http://www.credativ.co.uk>).
10+# All Rights Reserved
11+#
12+# This program is free software: you can redistribute it and/or modify
13+# it under the terms of the GNU Affero General Public License as
14+# published by the Free Software Foundation, either version 3 of the
15+# License, or (at your option) any later version.
16+#
17+# This program is distributed in the hope that it will be useful,
18+# but WITHOUT ANY WARRANTY; without even the implied warranty of
19+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+# GNU Affero General Public License for more details.
21+#
22+# You should have received a copy of the GNU Affero General Public License
23+# along with this program. If not, see <http://www.gnu.org/licenses/>.
24+#
25+##############################################################################
26+
27+import lloydscorporate
28+
29+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
30
31=== added file 'account_banking_uk_lloyds_corporate/__openerp__.py'
32--- account_banking_uk_lloyds_corporate/__openerp__.py 1970-01-01 00:00:00 +0000
33+++ account_banking_uk_lloyds_corporate/__openerp__.py 2013-09-05 21:56:06 +0000
34@@ -0,0 +1,37 @@
35+# -*- encoding: utf-8 -*-
36+##############################################################################
37+#
38+# Copyright (C) 2013 credativ Ltd (<http://www.credativ.co.uk>).
39+# All Rights Reserved
40+#
41+# This program is free software: you can redistribute it and/or modify
42+# it under the terms of the GNU Affero General Public License as
43+# published by the Free Software Foundation, either version 3 of the
44+# License, or (at your option) any later version.
45+#
46+# This program is distributed in the hope that it will be useful,
47+# but WITHOUT ANY WARRANTY; without even the implied warranty of
48+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49+# GNU Affero General Public License for more details.
50+#
51+# You should have received a copy of the GNU Affero General Public License
52+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53+#
54+##############################################################################
55+{
56+ 'name': 'Account Banking Lloyds Corporate CSV import',
57+ 'version': '1.0',
58+ 'license': 'AGPL-3',
59+ 'author': 'credativ Ltd',
60+ 'website': 'http://www.credativ.co.uk',
61+ 'category': 'Account Banking',
62+ 'depends': ['account_banking'],
63+ 'init_xml': [],
64+ 'update_xml': [],
65+ 'demo_xml': [],
66+ 'description': '''
67+ Module to import bank statement CSV for Bank of Lloyds Corporate.
68+ ''',
69+ 'active': False,
70+ 'installable': True,
71+}
72
73=== added file 'account_banking_uk_lloyds_corporate/lloydscorporate.py'
74--- account_banking_uk_lloyds_corporate/lloydscorporate.py 1970-01-01 00:00:00 +0000
75+++ account_banking_uk_lloyds_corporate/lloydscorporate.py 2013-09-05 21:56:06 +0000
76@@ -0,0 +1,157 @@
77+# -*- encoding: utf-8 -*-
78+##############################################################################
79+#
80+# Copyright (C) 2013 credativ Ltd (<http://www.credativ.co.uk>).
81+# All Rights Reserved
82+#
83+# This program is free software: you can redistribute it and/or modify
84+# it under the terms of the GNU Affero General Public License as
85+# published by the Free Software Foundation, either version 3 of the
86+# License, or (at your option) any later version.
87+#
88+# This program is distributed in the hope that it will be useful,
89+# but WITHOUT ANY WARRANTY; without even the implied warranty of
90+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
91+# GNU Affero General Public License for more details.
92+#
93+# You should have received a copy of the GNU Affero General Public License
94+# along with this program. If not, see <http://www.gnu.org/licenses/>.
95+#
96+##############################################################################
97+# Imports LLoyds Corporate format
98+#
99+
100+from account_banking.parsers import models, convert
101+from tools.translate import _
102+import re
103+import osv
104+import logging
105+import csv
106+from StringIO import StringIO
107+from operator import itemgetter
108+
109+logger = logging.getLogger('lloydscorporate_csv_import')
110+
111+class CSVTransaction(models.mem_bank_transaction):
112+
113+ mapping = {
114+ 'execution_date' : 'date',
115+ 'effective_date': 'date',
116+ 'message' : 'description',
117+ 'name' : 'description', # Use description as transaction name
118+ 'balance' : 'Balance', # Store balance from line for calculating statement balances
119+ }
120+
121+ def __init__(self, record, *args, **kwargs):
122+ '''
123+ Transaction creation
124+ '''
125+ super(CSVTransaction, self).__init__(*args, **kwargs)
126+
127+ # Parse date of format 01APR13
128+ record['date'] = convert.str2date(re.sub(r'\W*','',record['Date']), '%d%b%y')
129+
130+ record['description'] = record['Narrative'].strip()
131+
132+ # Mapping of simple items
133+ for key, value in self.mapping.iteritems():
134+ if record.has_key(value):
135+ setattr(self, key, record[value])
136+
137+ # Convert debit/credit to float amount
138+ if len(record['Payments'].strip()):
139+ self.transferred_amount = record['Payments'] and -float(record['Payments']) or 0.0
140+ else:
141+ self.transferred_amount = record['Receipts'] and float(record['Receipts']) or 0.0
142+
143+ # Cheque - set reference
144+ transfer_account = re.match(r'\w*\s\d{1,12}$', record['description'])
145+ if transfer_account:
146+ self.reference = transfer_account.group()
147+
148+ if not self.is_valid():
149+ logger.info("Invalid: %s", record)
150+
151+ def is_valid(self):
152+ '''
153+ We don't have remote_account so override base
154+ '''
155+ return (self.execution_date
156+ and self.transferred_amount and True) or False
157+
158+class Statement(models.mem_bank_statement):
159+
160+ def import_statement(self, record):
161+ self.transactions.append(CSVTransaction(record))
162+
163+def raise_error(message, line):
164+ raise osv.osv.except_osv(_('Import error'),
165+ 'Error in import:%s\n\n%s' % (message, line))
166+
167+class parser(models.parser):
168+ code = 'LLOYDSCORPORATE-CSV'
169+ name = _('Lloyds Corporate CSV Statement IMPORT')
170+ country_code = 'GB'
171+ doc = _('''\
172+ This format is available through
173+ the web interface.
174+ ''')
175+
176+ def parse(self, cr, data):
177+ ''' Lloyds corporate CSV parser'''
178+
179+ data = data.replace('\r','')
180+ csv_header = data.split('\n')[0].replace('"', '').split(',')
181+ header_list = ['Account', 'Date', 'Type', 'Narrative', 'Value Date', 'Payments', 'Receipts', 'Balance']
182+ result = []
183+
184+ #compare header list and process csv if equal
185+ if cmp(csv_header,header_list) != 0:
186+ logger.info("Invalid import Statement:")
187+ logger.info("Expected Header: %s" %(str(header_list)))
188+ logger.info("Header found: %s"%(str(csv_header)))
189+ raise osv.osv.except_osv(_('Import error'),
190+ 'Error in import:%s\n' % (_('Invalid file format')))
191+
192+ bankdata = StringIO(data)
193+ lines = list(csv.DictReader(bankdata))
194+ stmnt = Statement()
195+ # lines as they are imported
196+ if len(lines):
197+ #Store opening balance from first record
198+ line = lines[0]
199+ stmnt.start_balance = line['Balance']
200+ account_number = re.sub('\D', '', line['Account'])
201+
202+ #Assuming if payment and receipts are both null then its opening balance
203+ if not (line['Payments'] and line['Receipts']):
204+ lines = lines[1:]
205+
206+ #Skip records which do not contains transaction type
207+ for line in lines[:-int(len(lines)-map(itemgetter('Type'), lines).index(''))]:
208+ #create Statement lines from csv records
209+ stmnt.import_statement(line)
210+
211+ #Get statement Closing balance from CSV data list
212+ try:
213+ stmnt.end_balance = lines[map(itemgetter('Narrative'), lines).\
214+ index('Closing Ledger Balance')]['Balance']
215+ except ValueError:
216+ raise osv.osv.except_osv(_('Closing Balance'),
217+ _('Statement Closing Balance not found.'))
218+
219+
220+ #GB account number format stored in ERP
221+ stmnt.local_account = account_number[:6] +' '+ account_number[6:]
222+
223+ # Take date of last line of statement
224+ stmnt.date = stmnt.transactions[-1].effective_date
225+
226+ statement_id = self.get_unique_statement_id(
227+ cr, stmnt.date.strftime('%Yw%W'))
228+ stmnt.id = statement_id
229+ result.append(stmnt)
230+ return result
231+
232+
233+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
234
235=== added file 'account_banking_uk_lloyds_corporate/samplestatement.csv'
236--- account_banking_uk_lloyds_corporate/samplestatement.csv 1970-01-01 00:00:00 +0000
237+++ account_banking_uk_lloyds_corporate/samplestatement.csv 2013-09-05 21:56:06 +0000
238@@ -0,0 +1,14 @@
239+"Account","Date","Type","Narrative","Value Date","Payments","Receipts","Balance"
240+"210999-40010000 (GBP)",31MAY13,"","Opening Ledger Balance",,,,30000
241+"210999-40010000 (GBP)",31MAY13,"BGC","AXELOR 56465463 BGC
242+5656546456 30/05","","",20000,50000
243+"210999-40010000 (GBP)",31MAY13,"CR","ARGOS
244+INVOICES PAID","","",10000,60000
245+"210999-40010000 (GBP)",31MAY13,"DR","DEBANHAMS
246+EUROS 307.70","",2000,"",58000
247+"210999-40010000 (GBP)",31MAY13,"DR","HOTEL HILTON PARKLANE","",2000,"",56000
248+"210999-40010000 (GBP)",31MAY13,"DR","TO 30999999999999 TFR","",6000,"",50000
249+"210999-40010000 (GBP)",21JUN13,"","Value of Credits (96)","","",30000,
250+"210999-40010000 (GBP)",21JUN13,"","Value of Debits (43)","",10000,,
251+"210999-40010000 (GBP)",21JUN13,"","Closing Ledger Balance","","","",50000
252+"210999-40010000 (GBP)",21JUN13,"","Closing Cleared Balance","","","",50000

Subscribers

People subscribed via source and target branches