2024-10-01 20:35:43 +02:00
|
|
|
#!/usr/bin/env python
|
2023-06-21 00:36:27 +02:00
|
|
|
|
2023-11-05 21:10:45 +01:00
|
|
|
import os
|
2024-10-01 20:35:43 +02:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import matplotlib.ticker as ticker
|
2024-10-01 20:57:36 +02:00
|
|
|
from datetime import datetime
|
2024-10-03 19:51:54 +02:00
|
|
|
from utils import getDatetimeFromFileName, getMonthIndexSinceEpoch, getMonthNameFromMonthIndex, readPdfBankStatement
|
2024-10-03 23:47:26 +02:00
|
|
|
import operator
|
2024-10-04 00:53:33 +02:00
|
|
|
from pprint import pprint
|
2023-06-21 00:36:27 +02:00
|
|
|
|
2024-10-03 19:33:54 +02:00
|
|
|
PATH = f'/home/benjamin/Desktop/bens_folder/bazaar/documents/bnp/bank_statements/'
|
2023-06-21 00:36:27 +02:00
|
|
|
|
2024-10-03 17:43:44 +02:00
|
|
|
os.chdir(PATH)
|
2023-06-21 00:36:27 +02:00
|
|
|
|
2024-10-04 02:04:55 +02:00
|
|
|
PRINT_TRANSACTIONS = False
|
2024-10-01 20:35:43 +02:00
|
|
|
|
2024-10-03 19:33:54 +02:00
|
|
|
MAIN_BANK_ACCOUNT = 'compte_de_cheques'
|
|
|
|
|
2024-10-03 21:22:30 +02:00
|
|
|
allTransactions = []
|
2024-10-03 23:47:26 +02:00
|
|
|
monthlyTransactions = {}
|
2024-10-03 21:33:38 +02:00
|
|
|
|
|
|
|
def appendTransactions(transactions, bankAccount):
|
|
|
|
global allTransactions
|
|
|
|
for transaction in transactions:
|
|
|
|
transaction['bank account'] = bankAccount
|
|
|
|
allTransactions += [transaction]
|
2024-10-03 23:47:26 +02:00
|
|
|
date = transaction['date'].replace(day = 1)
|
|
|
|
monthlyTransactions[date] = monthlyTransactions.get(date, []) + [transaction]
|
2024-10-03 21:22:30 +02:00
|
|
|
|
|
|
|
for folder in os.listdir():
|
2024-10-03 19:33:54 +02:00
|
|
|
if folder != MAIN_BANK_ACCOUNT:
|
2024-10-03 21:22:30 +02:00
|
|
|
for file in os.listdir(folder):
|
2024-10-03 20:10:06 +02:00
|
|
|
filePath = f'{folder}/{file}'
|
2024-10-03 21:33:38 +02:00
|
|
|
print(filePath)
|
|
|
|
transactions = readPdfBankStatement(filePath)[3]
|
2024-10-04 00:53:33 +02:00
|
|
|
pprint(transactions)
|
2024-10-03 21:33:38 +02:00
|
|
|
appendTransactions(transactions, folder)
|
2024-10-03 19:33:54 +02:00
|
|
|
|
|
|
|
os.chdir(f'{MAIN_BANK_ACCOUNT}/')
|
|
|
|
|
2024-10-01 20:35:43 +02:00
|
|
|
for folder in sorted(os.listdir()):
|
|
|
|
for file in sorted(os.listdir(folder)):
|
2023-06-21 00:36:27 +02:00
|
|
|
filePath = f'{folder}/{file}'
|
2023-06-21 01:05:48 +02:00
|
|
|
print(filePath)
|
2024-10-03 21:33:38 +02:00
|
|
|
transactions = readPdfBankStatement(filePath)[3]
|
|
|
|
appendTransactions(transactions, MAIN_BANK_ACCOUNT)
|
2024-10-03 19:51:54 +02:00
|
|
|
if PRINT_TRANSACTIONS:
|
2024-10-04 01:15:38 +02:00
|
|
|
pprint(transactions)
|
2024-10-01 20:35:43 +02:00
|
|
|
#break
|
|
|
|
#break
|
2024-10-03 21:33:38 +02:00
|
|
|
|
2024-10-03 21:36:43 +02:00
|
|
|
allTransactions.sort(key = operator.itemgetter('date'))
|
|
|
|
print(len(allTransactions))
|
2024-10-04 02:04:55 +02:00
|
|
|
#pprint(allTransactions)
|
|
|
|
|
2024-10-04 12:47:48 +02:00
|
|
|
import re
|
|
|
|
VIRT_A_CPTE_EMIS_SUR_LE_REGEX = re.compile('VIRT CPTE A CPTE EMIS SUR LE\n(CEL|LEP|LVJ|L\.A|LDD)\\d{23}')
|
|
|
|
|
|
|
|
# Could precise bank account to restrict own account comments.
|
|
|
|
def isTransactionFromOwnAccounts(comment):
|
|
|
|
#if comment.startswith('DEPOT INITIAL DU COMPTE\n'):
|
|
|
|
# print(comment)
|
|
|
|
#if comment.startswith('VIR CPTE A CPTE EMIS /MOTIF '):
|
|
|
|
# print(comment)
|
|
|
|
return comment.startswith('DEPOT INITIAL DU COMPTE\n') or \
|
|
|
|
comment.startswith('VIR CPTE A CPTE EMIS /MOTIF ') or \
|
2024-10-16 20:09:22 +02:00
|
|
|
VIRT_A_CPTE_EMIS_SUR_LE_REGEX.fullmatch(comment)#comment.startswith('VIRT CPTE A CPTE EMIS SUR LE')
|
2024-10-04 12:47:48 +02:00
|
|
|
#comment.startswith('VIR CPTE A CPTE RECU /DE ') or \
|
|
|
|
# and comment.endswith('/REFDO /REFBEN')
|
|
|
|
|
|
|
|
#allTransactions = [transaction for transaction in allTransactions if not isTransactionFromOwnAccounts(transaction['comment'])]
|
2024-10-03 23:47:26 +02:00
|
|
|
sortedMonths = sorted(monthlyTransactions.keys())
|
2024-10-04 12:47:48 +02:00
|
|
|
for month in sortedMonths:
|
|
|
|
monthlyTransactions[month] = [transaction for transaction in monthlyTransactions[month] if not isTransactionFromOwnAccounts(transaction['comment'])]
|
|
|
|
totalMonthlyDebits = [sum([min(transaction['amount'], 0) for transaction in monthlyTransactions[month]]) for month in sortedMonths]
|
|
|
|
totalMonthlyCredits = [sum([max(transaction['amount'], 0) for transaction in monthlyTransactions[month]]) for month in sortedMonths]
|
2024-10-04 02:04:55 +02:00
|
|
|
totalMonthlyDifferences = [sum([transaction['amount'] for transaction in monthlyTransactions[month]]) for month in sortedMonths]
|
2024-10-04 12:47:48 +02:00
|
|
|
totals = [monthlyTransactions[sortedMonths[0]][0]['current amount'] + sum(totalMonthlyDifferences[:monthIndex + 1]) for monthIndex in range(len(sortedMonths))]
|
2024-10-01 20:35:43 +02:00
|
|
|
|
|
|
|
fig, ax = plt.subplots()
|
2024-10-04 12:47:48 +02:00
|
|
|
plt.title('BNP accounts monthly debits and credits')
|
2024-10-01 20:35:43 +02:00
|
|
|
plt.xlabel('Date')
|
|
|
|
plt.ylabel('€')
|
|
|
|
ALPHA = 0.5
|
2024-10-01 20:57:36 +02:00
|
|
|
|
2024-10-03 23:47:26 +02:00
|
|
|
xTicks = range(getMonthIndexSinceEpoch(sortedMonths[0]), getMonthIndexSinceEpoch(sortedMonths[-1]) + 1)
|
2024-10-02 00:33:11 +02:00
|
|
|
totalMonthlyAmountAndLabel = (
|
2024-10-04 12:47:48 +02:00
|
|
|
(totalMonthlyDebits, 'Debit'),
|
|
|
|
(totalMonthlyCredits, 'Credit'),
|
2024-10-02 00:33:11 +02:00
|
|
|
(totalMonthlyDifferences, 'Difference'),
|
2024-10-04 12:47:48 +02:00
|
|
|
(totals, 'Total'),
|
2024-10-02 00:33:11 +02:00
|
|
|
)
|
|
|
|
for totalMonthlyAmount, totalMonthlyLabel in totalMonthlyAmountAndLabel:
|
|
|
|
plt.bar(xTicks, totalMonthlyAmount, alpha = ALPHA, label = totalMonthlyLabel)
|
2024-10-01 20:35:43 +02:00
|
|
|
plt.legend()
|
|
|
|
|
2024-10-02 00:33:11 +02:00
|
|
|
#plt.yscale('symlog')
|
2024-10-01 20:35:43 +02:00
|
|
|
ax.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,}'))
|
2023-06-21 00:36:27 +02:00
|
|
|
|
2024-10-03 18:07:03 +02:00
|
|
|
ticksLabels = [getMonthNameFromMonthIndex(monthIndex) for monthIndex in xTicks]
|
2024-10-01 20:57:36 +02:00
|
|
|
plt.xticks(xTicks, ticksLabels, rotation = 90)
|
|
|
|
#plt.tight_layout()
|
|
|
|
# How to show the horizontal lines for subticks?
|
|
|
|
plt.grid(axis = 'y')
|
|
|
|
|
2024-10-16 20:09:22 +02:00
|
|
|
plt.show()
|