|
| 1 | +from .occurrences import debit_occurrences, credit_occurrences |
| 2 | + |
| 3 | + |
| 4 | +class Statement: |
| 5 | + |
| 6 | + def __init__( |
| 7 | + self, content=None, bank_code=None, branch_number=None, branch_code=None, |
| 8 | + account_number=None, account_code=None, start_date=None, |
| 9 | + start_amount_in_cents=None, start_debit_credit=None, stop_date=None, |
| 10 | + stop_amount_in_cents=None, stop_debit_credit=None, line_quantity=None, |
| 11 | + debit_sum_in_cents=None, credit_sum_in_cents=None, currency=None): |
| 12 | + self.content = content or [] |
| 13 | + self.bank_code = bank_code |
| 14 | + self.branch_number = branch_number |
| 15 | + self.branch_code = branch_code |
| 16 | + self.account_number = account_number |
| 17 | + self.account_code = account_code |
| 18 | + self.start_date = start_date |
| 19 | + self.start_amount_in_cents = start_amount_in_cents |
| 20 | + self.start_debit_credit = start_debit_credit |
| 21 | + self.stop_date = stop_date |
| 22 | + self.stop_amount_in_cents = stop_amount_in_cents |
| 23 | + self.stop_debit_credit = stop_debit_credit |
| 24 | + self.line_quantity = line_quantity |
| 25 | + self.debit_sum_in_cents = debit_sum_in_cents |
| 26 | + self.credit_sum_in_cents = credit_sum_in_cents |
| 27 | + self.currency = currency |
| 28 | + self.amount = 0 |
| 29 | + self.lines = [] |
| 30 | + |
| 31 | + |
| 32 | +class StatementLine: |
| 33 | + |
| 34 | + def __init__(self, content=None, occurrence=None, cpmf=None, date_account=None, |
| 35 | + date_move=None, amountInCents=None, debit_credit=None, |
| 36 | + bank_history_code=None, bank_history_description=None, |
| 37 | + document_number=None, amount=None): |
| 38 | + self.content = content or [] |
| 39 | + self.cpmf = cpmf |
| 40 | + self.date_account = date_account |
| 41 | + self.date_move = date_move |
| 42 | + self.amountInCents = amountInCents |
| 43 | + self.debit_credit = debit_credit |
| 44 | + self.occurrence = occurrence |
| 45 | + self.bank_history_code = bank_history_code |
| 46 | + self.bank_history_description = bank_history_description |
| 47 | + self.document_number = document_number |
| 48 | + self.amount = amount |
| 49 | + |
| 50 | + def occurrenceText(self): |
| 51 | + if self.occurrence and self.debit_credit == 'C': |
| 52 | + return credit_occurrences[self.occurrence] |
| 53 | + elif self.occurrence and self.debit_credit == 'D': |
| 54 | + return debit_occurrences[self.occurrence] |
| 55 | + |
| 56 | + def contentText(self, breakLine="\n"): |
| 57 | + return breakLine.join(self.content) |
| 58 | + |
| 59 | + |
| 60 | +class StatementParser: |
| 61 | + |
| 62 | + def __init__(self, lines): |
| 63 | + self._lines = lines |
| 64 | + self.statement = None |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def parseFile(cls, file): |
| 68 | + lines = file.readlines() |
| 69 | + statement = StatementParser(lines) |
| 70 | + return statement.parseLines() |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def parseText(cls, text): |
| 74 | + lines = text.splitlines()[:-1] |
| 75 | + statement = StatementParser(lines) |
| 76 | + return statement.parseLines() |
| 77 | + |
| 78 | + def _prepare_statement_header(self, line): |
| 79 | + self.statement = Statement( |
| 80 | + content=[line], |
| 81 | + bank_code=line[0:3], |
| 82 | + branch_number=line[52:57], |
| 83 | + branch_code=line[57:58], |
| 84 | + account_number=line[58:70], |
| 85 | + account_code=line[57:58], |
| 86 | + start_date=line[142:150], |
| 87 | + start_amount_in_cents=int(line[150:168]), |
| 88 | + start_debit_credit=line[168:169], |
| 89 | + currency=line[170:173] |
| 90 | + ) |
| 91 | + |
| 92 | + def _prepare_statement_footer(self, line): |
| 93 | + self.statement.content.append(line) |
| 94 | + self.statement.stop_date = line[142:150] |
| 95 | + self.statement.stop_amount_in_cents = int(line[150:168]) |
| 96 | + self.statement.stop_debit_credit = line[168:169] |
| 97 | + self.statement.line_quantity = int(line[170:176]) |
| 98 | + self.statement.debit_sum_in_cents = int(line[176:194]) |
| 99 | + self.statement.credit_sum_in_cents = int(line[194:212]) |
| 100 | + |
| 101 | + def _prepare_statement_line(self, line): |
| 102 | + statement_line = StatementLine() |
| 103 | + statement_line.content.append(line) |
| 104 | + statement_line.occurrence = line[15:17] |
| 105 | + statement_line.cpmf = line[133:134] |
| 106 | + statement_line.date_account = line[134:142] |
| 107 | + statement_line.date_move = line[142:150] |
| 108 | + statement_line.amountInCents = int(line[150:168]) |
| 109 | + statement_line.debit_credit = line[168:169] |
| 110 | + statement_line.occurrence = line[169:172] |
| 111 | + statement_line.bank_history_code = line[172:176] |
| 112 | + statement_line.bank_history_description = line[176:201] |
| 113 | + statement_line.document_number = line[201:240] |
| 114 | + |
| 115 | + if statement_line.debit_credit == 'C': |
| 116 | + statement_line.amount = statement_line.amountInCents / 100 |
| 117 | + elif statement_line.debit_credit == 'D': |
| 118 | + statement_line.amount = - statement_line.amountInCents / 100 |
| 119 | + self.statement.amount += statement_line.amount |
| 120 | + self.statement.lines.append(statement_line) |
| 121 | + |
| 122 | + def parseLines(self): |
| 123 | + for line in self._lines: |
| 124 | + |
| 125 | + if line[7] in ["0", "9"]: |
| 126 | + continue |
| 127 | + if line[7] == "1" and line[8] == "E": |
| 128 | + if not self.statement: |
| 129 | + self._prepare_statement_header(line) |
| 130 | + |
| 131 | + if line[7] == "5": |
| 132 | + self._prepare_statement_footer(line) |
| 133 | + |
| 134 | + if line[7] == "3" and line[13] == "E": |
| 135 | + self._prepare_statement_line(line) |
| 136 | + |
| 137 | + return self.statement |
0 commit comments