Add and use isTransactionFromOwnAccounts

This commit is contained in:
Benjamin Loison 2024-10-04 12:47:48 +02:00
parent db18d88f35
commit 91fb433258
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8
2 changed files with 29 additions and 16 deletions

View File

@ -53,29 +53,42 @@ allTransactions.sort(key = operator.itemgetter('date'))
print(len(allTransactions)) print(len(allTransactions))
#pprint(allTransactions) #pprint(allTransactions)
amount = X_XXX.XX import re
for transaction in allTransactions: 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}')
if transaction['bank account'] == MAIN_BANK_ACCOUNT:
amount += transaction['amount']
print(transaction['date'], amount, transaction['amount'])
print(amount)
# 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 \
VIRT_A_CPTE_EMIS_SUR_LE_REGEX.match(comment)#comment.startswith('VIRT CPTE A CPTE EMIS SUR LE')
#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'])]
sortedMonths = sorted(monthlyTransactions.keys()) sortedMonths = sorted(monthlyTransactions.keys())
#pprint(sortedMonths) 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]
totalMonthlyDifferences = [sum([transaction['amount'] for transaction in monthlyTransactions[month]]) for month in sortedMonths] totalMonthlyDifferences = [sum([transaction['amount'] for transaction in monthlyTransactions[month]]) for month in sortedMonths]
totals = [monthlyTransactions[sortedMonths[0]][0]['current amount'] + sum(totalMonthlyDifferences[:monthIndex + 1]) for monthIndex in range(len(sortedMonths))]
fig, ax = plt.subplots() fig, ax = plt.subplots()
plt.title('Monthly debits and credits') plt.title('BNP accounts monthly debits and credits')
plt.xlabel('Date') plt.xlabel('Date')
plt.ylabel('') plt.ylabel('')
ALPHA = 0.5 ALPHA = 0.5
xTicks = range(getMonthIndexSinceEpoch(sortedMonths[0]), getMonthIndexSinceEpoch(sortedMonths[-1]) + 1) xTicks = range(getMonthIndexSinceEpoch(sortedMonths[0]), getMonthIndexSinceEpoch(sortedMonths[-1]) + 1)
totalMonthlyAmountAndLabel = ( totalMonthlyAmountAndLabel = (
#(totalMonthlyDebits, 'Debit'), (totalMonthlyDebits, 'Debit'),
#(totalMonthlyCredits, 'Credit'), (totalMonthlyCredits, 'Credit'),
(totalMonthlyDifferences, 'Difference'), (totalMonthlyDifferences, 'Difference'),
#(totals, 'Total'), (totals, 'Total'),
) )
for totalMonthlyAmount, totalMonthlyLabel in totalMonthlyAmountAndLabel: for totalMonthlyAmount, totalMonthlyLabel in totalMonthlyAmountAndLabel:
plt.bar(xTicks, totalMonthlyAmount, alpha = ALPHA, label = totalMonthlyLabel) plt.bar(xTicks, totalMonthlyAmount, alpha = ALPHA, label = totalMonthlyLabel)

View File

@ -45,7 +45,7 @@ def readPdfBankStatement(filePath):
firstPage = True firstPage = True
initialAmount = None initialAmount = None
initialDate = None initialDate = None
#currentAmount = None currentAmount = None
date = None date = None
comment = [] comment = []
transactions = [] transactions = []
@ -64,7 +64,7 @@ def readPdfBankStatement(filePath):
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
started = True started = True
continue continue
else: else:
@ -95,7 +95,7 @@ def readPdfBankStatement(filePath):
'date': getDateFollowing(date, initialDate), 'date': getDateFollowing(date, initialDate),
'valeur': getDateFollowing(valeur, initialDate), 'valeur': getDateFollowing(valeur, initialDate),
'amount': amount, 'amount': amount,
#'currentAmount': currentAmount, 'current amount': currentAmount,
'comment': '\n'.join(comment) 'comment': '\n'.join(comment)
}] }]
date = None date = None
@ -104,7 +104,7 @@ def readPdfBankStatement(filePath):
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 != '':
comment += [line.strip()] comment += [line.strip()]
@ -113,7 +113,7 @@ def readPdfBankStatement(filePath):
'date': getDateFollowing(date, initialDate), 'date': getDateFollowing(date, initialDate),
'valeur': getDateFollowing(valeur, initialDate), 'valeur': getDateFollowing(valeur, initialDate),
'amount': amount, 'amount': amount,
#'currentAmount': currentAmount, 'current amount': currentAmount,
'comment': '\n'.join(comment) 'comment': '\n'.join(comment)
}] }]
return initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime return initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime