Merge lp:~cv.clearcorp/openerp-costa-rica/6.1-bcr_parser into lp:openerp-costa-rica/6.1
- 6.1-bcr_parser
- Merge into 6.1
Proposed by
Carlos Vásquez (ClearCorp)
Status: | Merged |
---|---|
Merged at revision: | 277 |
Proposed branch: | lp:~cv.clearcorp/openerp-costa-rica/6.1-bcr_parser |
Merge into: | lp:openerp-costa-rica/6.1 |
Diff against target: |
316 lines (+183/-63) 2 files modified
l10n_cr_account_banking_cr_bcr/bcr_format.py (+1/-4) l10n_cr_account_banking_cr_bcr/bcr_parser.py (+182/-59) |
To merge this branch: | bzr merge lp:~cv.clearcorp/openerp-costa-rica/6.1-bcr_parser |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
ClearCorp drivers | Pending | ||
Review via email: mp+192949@code.launchpad.net |
Commit message
Description of the change
[FIX] Fix BCRParser. Add three methods with the three versions of the file.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'l10n_cr_account_banking_cr_bcr/bcr_format.py' |
2 | --- l10n_cr_account_banking_cr_bcr/bcr_format.py 2013-03-14 15:57:05 +0000 |
3 | +++ l10n_cr_account_banking_cr_bcr/bcr_format.py 2013-10-28 20:51:22 +0000 |
4 | @@ -100,10 +100,7 @@ |
5 | sub_record = parser.statement_lines(record) #dictionary |
6 | for sub in sub_record: |
7 | self.transactions.append(transaction(sub)) |
8 | - |
9 | - #def _transaction_info(): |
10 | - #self.transaction_info(record) |
11 | - |
12 | + |
13 | def _not_used(): |
14 | logger.info("Didn't use record: %s", record) |
15 | |
16 | |
17 | === modified file 'l10n_cr_account_banking_cr_bcr/bcr_parser.py' |
18 | --- l10n_cr_account_banking_cr_bcr/bcr_parser.py 2013-10-17 00:04:47 +0000 |
19 | +++ l10n_cr_account_banking_cr_bcr/bcr_parser.py 2013-10-28 20:51:22 +0000 |
20 | @@ -75,11 +75,11 @@ |
21 | cad = '' |
22 | list_split = rec.split('\r\n') |
23 | account_number_wizard = kwargs['account_number'] |
24 | - |
25 | + |
26 | #If return True, the account are the same. |
27 | if self.match_account(list_split, account_number_wizard): |
28 | - for l in list_split: |
29 | - #_account_number -> FIRST REVISION |
30 | + for l in list_split: |
31 | + #_account_number -> FIRST REVISION |
32 | if l.find('Movimiento de Cuenta Corriente', 0, len('Movimiento de Cuenta Corriente')) > -1: |
33 | line_dict['account_number'] = self.extract_number(l) |
34 | |
35 | @@ -158,7 +158,7 @@ |
36 | raise osv.except_osv(_('Error'), |
37 | _('Error en la importación! La cuenta especificada en el archivo no coincide con la seleccionada en el asistente de importacion')) |
38 | |
39 | - def statement_lines ( self, rec ): |
40 | + def statement_lines ( self, rec): |
41 | parser = BCRParser() |
42 | mapping = { |
43 | 'execution_date' : '', |
44 | @@ -171,8 +171,6 @@ |
45 | 'transferred_amount': '', |
46 | 'creditmarker': '', |
47 | } |
48 | - |
49 | - lines = [] |
50 | line_dict = {} |
51 | currencycode = '' |
52 | |
53 | @@ -180,7 +178,9 @@ |
54 | entrada = False |
55 | start = 0 |
56 | end = 0 |
57 | + version = 'none' |
58 | |
59 | + #========= Start and end of lines ======# |
60 | for l in list_split: |
61 | if l.find('TOTALES DEL MOVIMIENTO CONTABILIZADO', 0, len('TOTALES DEL MOVIMIENTO CONTABILIZADO')) <= -1: |
62 | end += 1 |
63 | @@ -208,59 +208,61 @@ |
64 | currencycode = 'USD' |
65 | else: |
66 | currencycode = 'CRC' |
67 | - break |
68 | - |
69 | + break |
70 | + |
71 | + #========= VERSION OF THREE COLUMNS FOR DEBIT AND CREDIT =============# |
72 | sub_list = list_split [start:end] |
73 | - for sub in sub_list: |
74 | - #effective_date |
75 | - date_str = '' |
76 | - date_str = self.extract_date_regular_expresion_line(sub,0) |
77 | - date= datetime.strptime(date_str, "%d-%m-%y") |
78 | - mapping['effective_date'] = date #fecha_contable. |
79 | - |
80 | - #execution_date |
81 | - date_str = self.extract_date_regular_expresion_line(sub,1) |
82 | - date = datetime.strptime(date_str, "%d-%m-%y") |
83 | - mapping['execution_date'] = date #fecha_movimiento |
84 | - |
85 | - mapping['local_currency'] = currencycode |
86 | - mapping['transfer_type'] = 'NTRF' |
87 | - mapping['reference'] = parser.extract_number(sub[18:26]) |
88 | - mapping['message'] = sub[27:80] |
89 | - mapping['name'] = sub[27:80] |
90 | - mapping['id'] = sub[27:80] |
91 | - |
92 | - ############### AMOUNTS |
93 | - |
94 | - #substring amount MUST HAVE AT LEAST one character diferent to whitespace and amount must have one character |
95 | - |
96 | - #First version of file: Amounts are in center |
97 | - amount = sub[120:] |
98 | - |
99 | - if not amount.isspace() and len(amount) > 0: #"Return true if there are only whitespace characters in the string and there is at least one character, false otherwise." |
100 | - amount.replace('\t',' ') |
101 | - debit = amount[0:40] |
102 | - credit = amount[40:] |
103 | - |
104 | - #Second version of file: Amounts are in left |
105 | + |
106 | + if len(sub_list) > 0: |
107 | + sub_first = sub_list[0] #Based in first line, decide which version it is |
108 | + |
109 | + #1. Try separate by tab ('\t') (last version) (fields must have, at least, more than 1 of length) |
110 | + fields = sub_first.split('\t') |
111 | + |
112 | + if len(fields) > 1: |
113 | + version = 'third_version' |
114 | + |
115 | + #2. Find where start debit and credit columns |
116 | else: |
117 | - amount = sub[106:] |
118 | - amount.replace('\t',' ') |
119 | + amount = sub_first[106:] |
120 | debit = amount[0:16] |
121 | - credit = amount[16:] |
122 | - |
123 | - if (parser.extract_float(debit) is not ''): #debit |
124 | - cad = parser.extract_float(debit) |
125 | - mapping['transferred_amount'] = -float(cad) |
126 | - mapping['creditmarker'] = 'C' |
127 | - |
128 | - else: #credit |
129 | - cad = parser.extract_float (credit) |
130 | - mapping['transferred_amount'] = float(cad) |
131 | - |
132 | - lines.append(copy(mapping)) |
133 | - |
134 | - return lines |
135 | + |
136 | + debit = debit.replace(',','') |
137 | + debit = debit.replace('.','') |
138 | + debit = re.sub(r'\s', '', debit) |
139 | + |
140 | + if re.match('^[0-9,.]*$', debit): |
141 | + version = 'first_version' |
142 | + |
143 | + else: |
144 | + amount = sub_first[120:] |
145 | + debit = amount[0:40] |
146 | + |
147 | + debit = debit.replace(',','') |
148 | + debit = debit.replace('.','') |
149 | + debit = re.sub(r'\s', '', debit) |
150 | + |
151 | + if re.match('^[0-9,.]*$', debit): |
152 | + version = 'second_version' |
153 | + |
154 | + #=====================================================================# |
155 | + |
156 | + if version != 'none': |
157 | + if version =='first_version': |
158 | + return self.first_version_file(sub_list,mapping,currencycode,parser) |
159 | + |
160 | + elif version == 'second_version': |
161 | + return self.second_version(sub_list,mapping,currencycode,parser) |
162 | + |
163 | + elif version == 'third_version': |
164 | + return self.third_version(sub_list,mapping,currencycode,parser) |
165 | + |
166 | + else: |
167 | + raise osv.except_osv(_('Error'), |
168 | + _('There is not format implementend for this file.')) |
169 | + |
170 | + else: |
171 | + return [] |
172 | |
173 | def parse_stamenent_record( self, rec, **kwargs): |
174 | |
175 | @@ -300,7 +302,128 @@ |
176 | matchdict[field] = date_obj |
177 | |
178 | return matchdict |
179 | - |
180 | + |
181 | + #=============================Auxiliary methods =============================# |
182 | + |
183 | + #=====================Versions of file |
184 | + |
185 | + def first_version_file(self, sub_list,mapping,currencycode,parser): |
186 | + lines = [] |
187 | + for sub in sub_list: |
188 | + #effective_date |
189 | + date_str = '' |
190 | + date_str = self.extract_date_regular_expresion_line(sub,0) |
191 | + date= datetime.strptime(date_str, "%d-%m-%y") |
192 | + mapping['effective_date'] = date #fecha_contable. |
193 | + |
194 | + #execution_date |
195 | + date_str = self.extract_date_regular_expresion_line(sub,1) |
196 | + date = datetime.strptime(date_str, "%d-%m-%y") |
197 | + mapping['execution_date'] = date #fecha_movimiento |
198 | + |
199 | + mapping['local_currency'] = currencycode |
200 | + mapping['transfer_type'] = 'NTRF' |
201 | + mapping['reference'] = parser.extract_number(sub[18:26]) |
202 | + mapping['message'] = sub[27:80] |
203 | + mapping['name'] = sub[27:80] |
204 | + mapping['id'] = sub[27:80] |
205 | + |
206 | + amount = sub[106:] |
207 | + amount.replace('\t',' ') |
208 | + debit = amount[0:16] |
209 | + credit = amount[16:] |
210 | + |
211 | + if (parser.extract_float(debit) is not ''): #debit |
212 | + cad = parser.extract_float(debit) |
213 | + mapping['transferred_amount'] = -float(cad) |
214 | + mapping['creditmarker'] = 'C' |
215 | + |
216 | + else: #credit |
217 | + cad = parser.extract_float (credit) |
218 | + mapping['transferred_amount'] = float(cad) |
219 | + |
220 | + lines.append(copy(mapping)) |
221 | + |
222 | + return lines |
223 | + |
224 | + def second_version (self, sub_list,mapping,currencycode,parser): |
225 | + lines = [] |
226 | + for sub in sub_list: |
227 | + #effective_date |
228 | + date_str = '' |
229 | + date_str = self.extract_date_regular_expresion_line(sub,0) |
230 | + date= datetime.strptime(date_str, "%d-%m-%y") |
231 | + mapping['effective_date'] = date #fecha_contable. |
232 | + |
233 | + #execution_date |
234 | + date_str = self.extract_date_regular_expresion_line(sub,1) |
235 | + date = datetime.strptime(date_str, "%d-%m-%y") |
236 | + mapping['execution_date'] = date #fecha_movimiento |
237 | + |
238 | + mapping['local_currency'] = currencycode |
239 | + mapping['transfer_type'] = 'NTRF' |
240 | + mapping['reference'] = parser.extract_number(sub[18:26]) |
241 | + mapping['message'] = sub[27:80] |
242 | + mapping['name'] = sub[27:80] |
243 | + mapping['id'] = sub[27:80] |
244 | + |
245 | + amount = sub[120:] |
246 | + amount.replace('\t',' ') |
247 | + debit = amount[0:40] |
248 | + credit = amount[40:] |
249 | + |
250 | + if (parser.extract_float(debit) is not ''): #debit |
251 | + cad = parser.extract_float(debit) |
252 | + mapping['transferred_amount'] = -float(cad) |
253 | + mapping['creditmarker'] = 'C' |
254 | + |
255 | + else: #credit |
256 | + cad = parser.extract_float (credit) |
257 | + mapping['transferred_amount'] = float(cad) |
258 | + |
259 | + lines.append(copy(mapping)) |
260 | + |
261 | + return lines |
262 | + |
263 | + def third_version(self, sub_list,mapping,currencycode,parser): |
264 | + lines = [] |
265 | + for l in sub_list: |
266 | + fields = l.split('\t') |
267 | + |
268 | + #effective_date |
269 | + date_str = fields[0] |
270 | + date= datetime.strptime(date_str, "%d-%m-%y") |
271 | + mapping['effective_date'] = date #fecha_contable. |
272 | + |
273 | + #execution_date |
274 | + date_str = fields[1] |
275 | + date = datetime.strptime(date_str, "%d-%m-%y") |
276 | + mapping['execution_date'] = date #fecha_movimiento |
277 | + |
278 | + mapping['local_currency'] = currencycode |
279 | + mapping['transfer_type'] = 'NTRF' |
280 | + mapping['reference'] = parser.extract_number(fields[2]) |
281 | + mapping['message'] = fields[3] |
282 | + mapping['name'] = fields[3] |
283 | + mapping['id'] = fields[3] |
284 | + |
285 | + #Extract debit and credit |
286 | + debit = fields[5] |
287 | + if (parser.extract_float(debit) is not ''): #debit |
288 | + cad = parser.extract_float(debit) |
289 | + mapping['transferred_amount'] = -float(cad) |
290 | + mapping['creditmarker'] = 'C' |
291 | + |
292 | + else: #credit |
293 | + credit = fields[6] |
294 | + cad = parser.extract_float(credit) |
295 | + mapping['transferred_amount'] = float(cad) |
296 | + |
297 | + lines.append(copy(mapping)) |
298 | + |
299 | + return lines |
300 | + |
301 | + #=============================================================== |
302 | def extract_number( self, account_number ): |
303 | cad = '' |
304 | result = re.findall(r'[0-9]+', account_number) |
305 | @@ -309,9 +432,9 @@ |
306 | cad = cad + character |
307 | return cad |
308 | |
309 | - def extract_float ( self, ammount ): |
310 | + def extract_float ( self, amount ): |
311 | cad = '' |
312 | - result = re.findall(r"[-+]?\d*\.\d+|\d+",ammount) |
313 | + result = re.findall(r"[-+]?\d*\.\d+|\d+",amount) |
314 | |
315 | for character in result: |
316 | cad = cad + character |