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

Subscribers

People subscribed via source and target branches