Correctly make all transactions match current balance
This commit is contained in:
parent
e19132c33f
commit
db18d88f35
@ -12,12 +12,10 @@ 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'
|
||||||
|
|
||||||
debits = []
|
|
||||||
credits_ = []
|
|
||||||
allTransactions = []
|
allTransactions = []
|
||||||
monthlyTransactions = {}
|
monthlyTransactions = {}
|
||||||
|
|
||||||
@ -34,13 +32,10 @@ for folder in os.listdir():
|
|||||||
for file in os.listdir(folder):
|
for file in os.listdir(folder):
|
||||||
filePath = f'{folder}/{file}'
|
filePath = f'{folder}/{file}'
|
||||||
print(filePath)
|
print(filePath)
|
||||||
#print(readPdfBankStatement(filePath))
|
|
||||||
#exit(0)
|
|
||||||
transactions = readPdfBankStatement(filePath)[3]
|
transactions = readPdfBankStatement(filePath)[3]
|
||||||
pprint(transactions)
|
pprint(transactions)
|
||||||
appendTransactions(transactions, folder)
|
appendTransactions(transactions, folder)
|
||||||
|
|
||||||
#exit(1)
|
|
||||||
os.chdir(f'{MAIN_BANK_ACCOUNT}/')
|
os.chdir(f'{MAIN_BANK_ACCOUNT}/')
|
||||||
|
|
||||||
for folder in sorted(os.listdir()):
|
for folder in sorted(os.listdir()):
|
||||||
@ -54,22 +49,20 @@ for folder in sorted(os.listdir()):
|
|||||||
#break
|
#break
|
||||||
#break
|
#break
|
||||||
|
|
||||||
exit(1)
|
|
||||||
allTransactions.sort(key = operator.itemgetter('date'))
|
allTransactions.sort(key = operator.itemgetter('date'))
|
||||||
print(len(allTransactions))
|
print(len(allTransactions))
|
||||||
pprint(allTransactions)
|
#pprint(allTransactions)
|
||||||
|
|
||||||
|
amount = X_XXX.XX
|
||||||
|
for transaction in allTransactions:
|
||||||
|
if transaction['bank account'] == MAIN_BANK_ACCOUNT:
|
||||||
|
amount += transaction['amount']
|
||||||
|
print(transaction['date'], amount, transaction['amount'])
|
||||||
|
print(amount)
|
||||||
|
|
||||||
sortedMonths = sorted(monthlyTransactions.keys())
|
sortedMonths = sorted(monthlyTransactions.keys())
|
||||||
#pprint(sortedMonths)
|
#pprint(sortedMonths)
|
||||||
# debit or credit?
|
totalMonthlyDifferences = [sum([transaction['amount'] for transaction in monthlyTransactions[month]]) for month in sortedMonths]
|
||||||
totalMonthlyCredits = []
|
|
||||||
totalMonthlyDebits = []
|
|
||||||
totalMonthlyDifferences = []
|
|
||||||
#totalMonthlyDebits = [[ for transaction in monthlyTransactions[month]] for month in sortedMonths]
|
|
||||||
for month in sortedMonths:
|
|
||||||
#for transaction in monthlyTransactions[month]:
|
|
||||||
currentMonthlyTransactions = monthlyTransactions[month]
|
|
||||||
monthlyCredits = []
|
|
||||||
|
|
||||||
fig, ax = plt.subplots()
|
fig, ax = plt.subplots()
|
||||||
plt.title('Monthly debits and credits')
|
plt.title('Monthly debits and credits')
|
||||||
|
24
utils.py
24
utils.py
@ -2,8 +2,8 @@ import subprocess
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import re
|
import re
|
||||||
|
|
||||||
# Source: [the Stack Overflow answer 766377](https://stackoverflow.com/a/766377)
|
# For not-greedy `?`, see Source: [the Stack Overflow answer 766377](https://stackoverflow.com/a/766377)
|
||||||
FIRST_LINE_OF_PAYMENT_REGEX = re.compile('\\ +(\\d{2}\\.\\d{2})\\ +([A-Z\\d /.()*]+?)\\ +(\\d{2}\\.\\d{2})\\ +([\\d ]+,\\d{2})')
|
FIRST_LINE_OF_PAYMENT_REGEX = re.compile('\\ +(\\d{2}\\.\\d{2})\\ +([A-Z\\d /.()*\\-,]+?)\\ +(\\d{2}\\.\\d{2})\\ +([\\d ]+,\\d{2})')
|
||||||
END_PAGE_AFTER_THE_FIRST_ONE_REGEX = re.compile(' +RELEVE ((DE (COMPTE (CHEQUES|D\'EPARGNE LOGEMENT|LEP))|LIVRET (A|JEUNE))|LIVRET DEV. DURABLE ET SOLIDAIRE) +P\\. \\d+/\\d+')
|
END_PAGE_AFTER_THE_FIRST_ONE_REGEX = re.compile(' +RELEVE ((DE (COMPTE (CHEQUES|D\'EPARGNE LOGEMENT|LEP))|LIVRET (A|JEUNE))|LIVRET DEV. DURABLE ET SOLIDAIRE) +P\\. \\d+/\\d+')
|
||||||
SOLDE_CREDITEUR_AU_REGEX = re.compile('\\ +SOLDE CREDITEUR AU (\\d{2}\\.\\d{2}\\.\\d{4})\\ +([\\d ]+,\\d{2})')
|
SOLDE_CREDITEUR_AU_REGEX = re.compile('\\ +SOLDE CREDITEUR AU (\\d{2}\\.\\d{2}\\.\\d{4})\\ +([\\d ]+,\\d{2})')
|
||||||
TOTAL_DES_OPERATIONS_REGEX = re.compile('\\ +TOTAL\\ DES\\ OPERATIONS\\ +([\\d ]+,\\d{2})\\ +([\\d ]+,\\d{2})')
|
TOTAL_DES_OPERATIONS_REGEX = re.compile('\\ +TOTAL\\ DES\\ OPERATIONS\\ +([\\d ]+,\\d{2})\\ +([\\d ]+,\\d{2})')
|
||||||
@ -17,7 +17,6 @@ def getTextFromPdf(pdfPath):
|
|||||||
return execute(['pdftotext', '-layout', pdfPath, '-'])
|
return execute(['pdftotext', '-layout', pdfPath, '-'])
|
||||||
|
|
||||||
def getDatetimeFromFileName(aDatetimeStr):
|
def getDatetimeFromFileName(aDatetimeStr):
|
||||||
#aDatetime = datetime.strptime(aDatetimeStr, '%Y%m%d.pdf')
|
|
||||||
aDatetime = datetime(int(aDatetimeStr[:4]), int(aDatetimeStr[4:6]), 1)
|
aDatetime = datetime(int(aDatetimeStr[:4]), int(aDatetimeStr[4:6]), 1)
|
||||||
return aDatetime
|
return aDatetime
|
||||||
|
|
||||||
@ -60,19 +59,12 @@ def readPdfBankStatement(filePath):
|
|||||||
getIndex = lambda line, type_: line.index(type_) + len(type_)
|
getIndex = lambda line, type_: line.index(type_) + len(type_)
|
||||||
debitIndex = getIndex(line, 'Débit')
|
debitIndex = getIndex(line, 'Débit')
|
||||||
creditIndex = getIndex(line, 'Crédit')
|
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 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:
|
||||||
@ -83,7 +75,6 @@ def readPdfBankStatement(filePath):
|
|||||||
continue
|
continue
|
||||||
# We aren't interested in the content after this line
|
# We aren't interested in the content after this line
|
||||||
else:
|
else:
|
||||||
#print('hey', line)
|
|
||||||
totalDesOperationsRegexMatch = TOTAL_DES_OPERATIONS_REGEX.match(line)
|
totalDesOperationsRegexMatch = TOTAL_DES_OPERATIONS_REGEX.match(line)
|
||||||
totalDesOperationsCreditOnlyRegexMatch = TOTAL_DES_OPERATIONS_CREDIT_ONLY_REGEX.match(line)
|
totalDesOperationsCreditOnlyRegexMatch = TOTAL_DES_OPERATIONS_CREDIT_ONLY_REGEX.match(line)
|
||||||
if totalDesOperationsRegexMatch is not None or totalDesOperationsCreditOnlyRegexMatch is not None:
|
if totalDesOperationsRegexMatch is not None or totalDesOperationsCreditOnlyRegexMatch is not None:
|
||||||
@ -99,7 +90,6 @@ def readPdfBankStatement(filePath):
|
|||||||
break
|
break
|
||||||
firstLineOfPaymentRegexMatch = FIRST_LINE_OF_PAYMENT_REGEX.match(line)
|
firstLineOfPaymentRegexMatch = FIRST_LINE_OF_PAYMENT_REGEX.match(line)
|
||||||
if firstLineOfPaymentRegexMatch is not None:
|
if firstLineOfPaymentRegexMatch is not None:
|
||||||
#print(line)
|
|
||||||
if date is not None:
|
if date is not None:
|
||||||
transactions += [{
|
transactions += [{
|
||||||
'date': getDateFollowing(date, initialDate),
|
'date': getDateFollowing(date, initialDate),
|
||||||
@ -108,19 +98,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)
|
lineLen = len(line)
|
||||||
amount = toFloat(amount)
|
amount = toFloat(amount)
|
||||||
if abs(debitIndex - lineLen) < abs(creditIndex - lineLen):
|
if abs(debitIndex - lineLen) < abs(creditIndex - lineLen):
|
||||||
amount *= -1
|
amount *= -1
|
||||||
#currentAmount -= amount
|
#currentAmount += amount
|
||||||
comment = [firstCommentLine]
|
comment = [firstCommentLine]
|
||||||
elif line != '':
|
elif line != '':
|
||||||
#print(f'comment: {line}')
|
|
||||||
comment += [line.strip()]
|
comment += [line.strip()]
|
||||||
if date is not None:
|
if date is not None:
|
||||||
transactions += [{
|
transactions += [{
|
||||||
@ -131,7 +117,3 @@ 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
|
|
||||||
|
Loading…
Reference in New Issue
Block a user