Correctly detect transactions with thousands

This commit is contained in:
Benjamin Loison 2024-10-03 20:36:39 +02:00
parent 9eed49c344
commit c425ebcb00
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8
2 changed files with 27 additions and 20 deletions

View File

@ -10,22 +10,23 @@ PATH = f'/home/benjamin/Desktop/bens_folder/bazaar/documents/bnp/bank_statements
os.chdir(PATH) os.chdir(PATH)
PRINT_TRANSACTIONS = True PRINT_TRANSACTIONS = False
MAIN_BANK_ACCOUNT = 'compte_de_cheques' MAIN_BANK_ACCOUNT = 'compte_de_cheques'
# As far as I know there was no debit yet. # As far as I know there was no debit yet.
otherBankAccountsCredits = {} otherBankAccountsCredits = {}
for folder in os.listdir(): for folder in os.listdir():#[-2:]:
if folder != MAIN_BANK_ACCOUNT: if folder != MAIN_BANK_ACCOUNT:
print(folder) print(folder)
for file in os.listdir(folder): for file in os.listdir(folder):#[-1:]:
print(file) print(file)
filePath = f'{folder}/{file}' filePath = f'{folder}/{file}'
initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime = readPdfBankStatement(filePath) initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime = readPdfBankStatement(filePath)
print(totalMonthlyCredit) #print(totalMonthlyCredit)
print(transactions) #import json
#print(json.dumps(transactions, indent = 4))
otherBankAccountsCredits[fileDatetime] = otherBankAccountsCredits.get(fileDatetime, 0) + totalMonthlyCredit otherBankAccountsCredits[fileDatetime] = otherBankAccountsCredits.get(fileDatetime, 0) + totalMonthlyCredit
#exit(1) #exit(1)
@ -48,13 +49,16 @@ for folder in sorted(os.listdir()):
print(f'Total monthly debit: {totalMonthlyDebit}') print(f'Total monthly debit: {totalMonthlyDebit}')
print(f'Total monthly credit: {totalMonthlyCredit}') print(f'Total monthly credit: {totalMonthlyCredit}')
totalMonthlyDebits += [totalMonthlyDebit] totalMonthlyDebits += [totalMonthlyDebit]
totalMonthlyCredit += otherBankAccountsCredits.get(fileDatetime, 0) otherBankAccountsCredit = otherBankAccountsCredits.get(fileDatetime, 0)
print(f'{otherBankAccountsCredit=}')
totalMonthlyCredit += otherBankAccountsCredit
totalMonthlyCredits += [totalMonthlyCredit] totalMonthlyCredits += [totalMonthlyCredit]
totalMonthlyDifference = totalMonthlyCredit - totalMonthlyDebit totalMonthlyDifference = totalMonthlyCredit - totalMonthlyDebit
totalMonthlyDifferences += [totalMonthlyDifference] totalMonthlyDifferences += [totalMonthlyDifference]
if PRINT_TRANSACTIONS: if PRINT_TRANSACTIONS:
for transaction in transactions: for transaction in transactions:
print(transaction['date'], transaction['valeur'], transaction['amount'], transaction['currentAmount']) # , transaction['currentAmount']
print(transaction['date'], transaction['valeur'], transaction['amount'])
print(transaction['comment']) print(transaction['comment'])
print() print()
if firstDatetime is None: if firstDatetime is None:

View File

@ -2,11 +2,11 @@ import subprocess
from datetime import datetime from datetime import datetime
import re import re
FIRST_LINE_OF_PAYMENT_REGEX = re.compile('\\d{2}\\.\\d{2} \\d{2}\\.\\d{2} \\d+,\\d{2}') FIRST_LINE_OF_PAYMENT_REGEX = re.compile('(\\d{2}\\.\\d{2}) (\\d{2}\\.\\d{2}) ([\\d ]+,\\d{2})')
END_PAGE_AFTER_THE_FIRST_ONE_REGEX = re.compile('P\\. \\d+/\\d+') END_PAGE_AFTER_THE_FIRST_ONE_REGEX = re.compile('P\\. \\d+/\\d+')
SOLDE_CREDITEUR_AU_REGEX = re.compile('SOLDE CREDITEUR AU \\d{2}\\.\\d{2}\\.\\d{4}') SOLDE_CREDITEUR_AU_REGEX = re.compile('SOLDE CREDITEUR AU \\d{2}\\.\\d{2}\\.\\d{4}')
TOTAL_DES_OPERATIONS_REGEX = re.compile('TOTAL\\ DES\\ OPERATIONS\\ ([0-9 ]+,\\d{2})\\ ([0-9 ]+,\\d{2})') TOTAL_DES_OPERATIONS_REGEX = re.compile('TOTAL\\ DES\\ OPERATIONS\\ ([\\d ]+,\\d{2})\\ ([\\d ]+,\\d{2})')
TOTAL_DES_OPERATIONS_CREDIT_ONLY_REGEX = re.compile('TOTAL\\ DES\\ OPERATIONS\\ ([0-9 ]+,\\d{2})') TOTAL_DES_OPERATIONS_CREDIT_ONLY_REGEX = re.compile('TOTAL\\ DES\\ OPERATIONS\\ ([\\d ]+,\\d{2})')
def execute(command): def execute(command):
return subprocess.check_output(command).decode('utf-8') return subprocess.check_output(command).decode('utf-8')
@ -25,15 +25,19 @@ def getMonthIndexSinceEpoch(aDatetime):
def getMonthNameFromMonthIndex(monthIndex): def getMonthNameFromMonthIndex(monthIndex):
return datetime((monthIndex - 1) // 12, 1 + (monthIndex - 1) % 12, 1).strftime('%b %Y') return datetime((monthIndex - 1) // 12, 1 + (monthIndex - 1) % 12, 1).strftime('%b %Y')
def toFloat(group):
return float(group.replace(',', '.').replace(' ', ''))
def readPdfBankStatement(filePath): def readPdfBankStatement(filePath):
file = filePath.split('/')[-1] file = filePath.split('/')[-1]
fileDatetime = getDatetimeFromFileName(file) fileDatetime = getDatetimeFromFileName(file)
content = getTextFromPdf(filePath) content = getTextFromPdf(filePath)
lines = content.splitlines() lines = content.splitlines()
#print('\n'.join(lines))
started = False started = False
firstPage = True firstPage = True
initialAmount = None initialAmount = None
currentAmount = None #currentAmount = None
date = None date = None
comment = [] comment = []
transactions = [] transactions = []
@ -43,7 +47,7 @@ def readPdfBankStatement(filePath):
if SOLDE_CREDITEUR_AU_REGEX.match(line) is not None or (line.startswith('Date Nature des opérations Valeur Débit Crédit') and not firstPage): if SOLDE_CREDITEUR_AU_REGEX.match(line) is not None or (line.startswith('Date Nature des opérations Valeur Débit Crédit') and not firstPage):
if SOLDE_CREDITEUR_AU_REGEX.match(line): if SOLDE_CREDITEUR_AU_REGEX.match(line):
initialAmount = float(SOLDE_CREDITEUR_AU_REGEX.sub('', line).replace(',', '.').replace(' ', '')) initialAmount = float(SOLDE_CREDITEUR_AU_REGEX.sub('', line).replace(',', '.').replace(' ', ''))
currentAmount = initialAmount #currentAmount = initialAmount
started = True started = True
continue continue
else: else:
@ -57,27 +61,26 @@ def readPdfBankStatement(filePath):
totalDesOperationsRegexMatch = TOTAL_DES_OPERATIONS_REGEX.match(line) totalDesOperationsRegexMatch = TOTAL_DES_OPERATIONS_REGEX.match(line)
# Note that transfer between accounts will be noted in both debits and credits, as trying to cancel would make benefits show as negative debit which does not make sense. # Note that transfer between accounts will be noted in both debits and credits, as trying to cancel would make benefits show as negative debit which does not make sense.
# Cannot just consider January as benefits only as `20240122.pdf` also contains an additional transfer between my accounts. # Cannot just consider January as benefits only as `20240122.pdf` also contains an additional transfer between my accounts.
toFloat = lambda group: float(group.replace(',', '.').replace(' ', ''))
if totalDesOperationsRegexMatch is not None: if totalDesOperationsRegexMatch is not None:
totalMonthlyDebit, totalMonthlyCredit = [toFloat(group) for group in totalDesOperationsRegexMatch.groups()] totalMonthlyDebit, totalMonthlyCredit = [toFloat(group) for group in totalDesOperationsRegexMatch.groups()]
else: else:
totalMonthlyCredit = toFloat(TOTAL_DES_OPERATIONS_CREDIT_ONLY_REGEX.match(line).group(1)) totalMonthlyCredit = toFloat(TOTAL_DES_OPERATIONS_CREDIT_ONLY_REGEX.match(line).group(1))
totalMonthlyDebit = 0 totalMonthlyDebit = 0
break break
if FIRST_LINE_OF_PAYMENT_REGEX.match(line) is not None: firstLineOfPaymentRegexMatch = FIRST_LINE_OF_PAYMENT_REGEX.match(line)
#print(line) if firstLineOfPaymentRegexMatch is not None:
if date is not None: if date is not None:
transactions += [{ transactions += [{
'date': date, 'date': date,
'valeur': valeur, 'valeur': valeur,
'amount': amount, 'amount': amount,
'currentAmount': currentAmount, #'currentAmount': currentAmount,
'comment': '\n'.join(comment) 'comment': '\n'.join(comment)
}] }]
date = None date = None
date, valeur, amount = line.split() date, valeur, amount = firstLineOfPaymentRegexMatch.groups()
amount = float(amount.replace(',', '.')) amount = toFloat(amount)
currentAmount -= amount #currentAmount -= amount
comment = [] comment = []
else: else:
comment += [line] comment += [line]
@ -86,7 +89,7 @@ def readPdfBankStatement(filePath):
'date': date, 'date': date,
'valeur': valeur, 'valeur': valeur,
'amount': amount, 'amount': amount,
'currentAmount': currentAmount, #'currentAmount': currentAmount,
'comment': '\n'.join(comment) 'comment': '\n'.join(comment)
}] }]
return initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime return initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime