|
| 1 | +from .occurrences import debit_occurrences, credit_occurrences |
| 2 | + |
| 3 | + |
| 4 | +class Statement: |
| 5 | + |
| 6 | + def __init__( |
| 7 | + self, content=None, start_date=None, start_amount=None, |
| 8 | + start_debit_credit=None, stop_date=False, stop_amount=None, |
| 9 | + stop_debit_credit=None, line_quantity=None, debit_sum_in_cents=None, |
| 10 | + credit_sum_in_cents=None): |
| 11 | + self.content = content or [] |
| 12 | + self.start_date = start_date |
| 13 | + self.start_amount = start_amount |
| 14 | + self.start_debit_credit = start_debit_credit |
| 15 | + self.stop_date = stop_date |
| 16 | + self.stop_amount = stop_amount |
| 17 | + self.stop_debit_credit = stop_debit_credit |
| 18 | + self.line_quantity = line_quantity |
| 19 | + self.debit_sum_in_cents = debit_sum_in_cents |
| 20 | + self.credit_sum_in_cents = credit_sum_in_cents |
| 21 | + self.lines = [] |
| 22 | + |
| 23 | + |
| 24 | +class StatementLine: |
| 25 | + |
| 26 | + def __init__(self, content=None, occurrence=None, cpmf=None, date_account=None, |
| 27 | + date_move=None, amountInCents=None, debit_credit=None, |
| 28 | + bank_history_code=None, bank_history_description=None, |
| 29 | + document_number=None, errors=None): |
| 30 | + self.content = content or [] |
| 31 | + self.cpmf = cpmf |
| 32 | + self.date_account = date_account |
| 33 | + self.date_move = date_move |
| 34 | + self.amountInCents = amountInCents |
| 35 | + self.debit_credit = debit_credit |
| 36 | + self.occurrence = occurrence |
| 37 | + self.bank_history_code = bank_history_code |
| 38 | + self.bank_history_description = bank_history_description |
| 39 | + self.document_number = document_number |
| 40 | + |
| 41 | + def occurrenceText(self): |
| 42 | + if self.occurrence and self.debit_credit == 'C': |
| 43 | + return credit_occurrences[self.occurrence] |
| 44 | + elif self.occurrence and self.debit_credit == 'D': |
| 45 | + return debit_occurrences[self.occurrence] |
| 46 | + |
| 47 | + |
| 48 | + def contentText(self, breakLine="\n"): |
| 49 | + return breakLine.join(self.content) |
| 50 | + |
| 51 | + |
| 52 | +class StatementParser: |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def parseFile(cls, file): |
| 56 | + lines = file.readlines() |
| 57 | + return cls.parseLines(lines) |
| 58 | + |
| 59 | + @classmethod |
| 60 | + def parseText(cls, text): |
| 61 | + lines = text.splitlines()[:-1] |
| 62 | + return cls.parseLines(lines) |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def parseLines(cls, lines): |
| 66 | + statement = None |
| 67 | + for line in lines: |
| 68 | + if line[7] in ["0", "9"]: |
| 69 | + continue |
| 70 | + if line[7] == "1" and line[8] == "E": |
| 71 | + if not statement: |
| 72 | + statement = Statement( |
| 73 | + content=[line], |
| 74 | + start_date=line[142:150], |
| 75 | + start_amount=line[150:168], |
| 76 | + start_debit_credit=line[168:169], |
| 77 | + ) |
| 78 | + |
| 79 | + if line[7] == "5": |
| 80 | + statement.content.append(line) |
| 81 | + statement.stop_date = line[142:150] |
| 82 | + statement.stop_amount = line[150:169] |
| 83 | + statement.stop_debit_credit = line[169:170] |
| 84 | + statement.line_quantity = line[170:176] |
| 85 | + statement.debit_sum_in_cents = int(line[176:194]) |
| 86 | + statement.credit_sum_in_cents = int(line[194:212]) |
| 87 | + |
| 88 | + if line[7] == "3" and line[13] == "E": |
| 89 | + statement_line = StatementLine() |
| 90 | + statement_line.content.append(line) |
| 91 | + statement_line.occurrence = line[15:17] |
| 92 | + statement_line.cpmf = line[133:134] |
| 93 | + statement_line.date_account = line[134:142] |
| 94 | + statement_line.date_move = line[142:150] |
| 95 | + statement_line.amountInCents = int(line[150:168]) |
| 96 | + statement_line.debit_credit = line[168:169] |
| 97 | + statement_line.occurrence = line[169:172] |
| 98 | + statement_line.bank_history_code = line[172:176] |
| 99 | + statement_line.bank_history_description = line[176:201] |
| 100 | + statement_line.bank_history_description = line[201:240] |
| 101 | + statement.lines.append(statement_line) |
| 102 | + return statement |
0 commit comments