Fix #2: Use amount sign to precise if it's a debit or a credit

This commit is contained in:
Benjamin Loison 2024-10-04 01:37:47 +02:00
parent e0c99bc068
commit e19132c33f
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8

View File

@ -50,16 +50,29 @@ def readPdfBankStatement(filePath):
date = None date = None
comment = [] comment = []
transactions = [] transactions = []
debitIndex = None
creditIndex = None
for line in lines: for line in lines:
if not started: if not started:
# We are interested in the content after this line:) # We are interested in the content after this line:)
soldeCrediteurAuRegexMatch = SOLDE_CREDITEUR_AU_REGEX.match(line) soldeCrediteurAuRegexMatch = SOLDE_CREDITEUR_AU_REGEX.match(line)
if soldeCrediteurAuRegexMatch is not None or (COLUMNS_HEADER.match(line) and not firstPage): if COLUMNS_HEADER.match(line) is not None:
getIndex = lambda line, type_: line.index(type_) + len(type_)
debitIndex = getIndex(line, 'Débit')
creditIndex = getIndex(line, 'Crédit')
#print(f'{line.index("Débit") + len("Débit")=}')
#print(f'{line.index("Crédit") + len("Crédit")=}')
if soldeCrediteurAuRegexMatch is not None or (COLUMNS_HEADER.match(line) is not None and not firstPage):
if soldeCrediteurAuRegexMatch is not None: if soldeCrediteurAuRegexMatch is not None:
initialDate = datetime.strptime(soldeCrediteurAuRegexMatch.group(1), '%d.%m.%Y') initialDate = datetime.strptime(soldeCrediteurAuRegexMatch.group(1), '%d.%m.%Y')
initialAmount = toFloat(soldeCrediteurAuRegexMatch.group(2)) initialAmount = toFloat(soldeCrediteurAuRegexMatch.group(2))
print(f'{initialAmount=}') print(f'{initialAmount=}')
#currentAmount = initialAmount #currentAmount = initialAmount
'''
else:
print(f'{line.index("Débit")=}')
print(f'{line.index("Crédit")=}')
'''
started = True started = True
continue continue
else: else:
@ -95,9 +108,15 @@ def readPdfBankStatement(filePath):
#'currentAmount': currentAmount, #'currentAmount': currentAmount,
'comment': '\n'.join(comment) 'comment': '\n'.join(comment)
}] }]
#print('index', lastIndex(line, ' '))
#print(f'!{line}!')
#print(amount, len(line))
date = None date = None
date, firstCommentLine, valeur, amount = firstLineOfPaymentRegexMatch.groups() date, firstCommentLine, valeur, amount = firstLineOfPaymentRegexMatch.groups()
lineLen = len(line)
amount = toFloat(amount) amount = toFloat(amount)
if abs(debitIndex - lineLen) < abs(creditIndex - lineLen):
amount *= -1
#currentAmount -= amount #currentAmount -= amount
comment = [firstCommentLine] comment = [firstCommentLine]
elif line != '': elif line != '':
@ -112,3 +131,7 @@ def readPdfBankStatement(filePath):
'comment': '\n'.join(comment) 'comment': '\n'.join(comment)
}] }]
return initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime return initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime
def lastIndex(myStr, character):
index = myStr[::-1].index(character)
return len(myStr) - index - 1