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))
#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)
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 \
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())
#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]
totals = [monthlyTransactions[sortedMonths[0]][0]['current amount'] + sum(totalMonthlyDifferences[:monthIndex + 1]) for monthIndex in range(len(sortedMonths))]
fig, ax = plt.subplots()
plt.title('Monthly debits and credits')
plt.title('BNP accounts monthly debits and credits')
plt.xlabel('Date')
plt.ylabel('')
ALPHA = 0.5
xTicks = range(getMonthIndexSinceEpoch(sortedMonths[0]), getMonthIndexSinceEpoch(sortedMonths[-1]) + 1)
totalMonthlyAmountAndLabel = (
#(totalMonthlyDebits, 'Debit'),
#(totalMonthlyCredits, 'Credit'),
(totalMonthlyDebits, 'Debit'),
(totalMonthlyCredits, 'Credit'),
(totalMonthlyDifferences, 'Difference'),
#(totals, 'Total'),
(totals, 'Total'),
)
for totalMonthlyAmount, totalMonthlyLabel in totalMonthlyAmountAndLabel:
plt.bar(xTicks, totalMonthlyAmount, alpha = ALPHA, label = totalMonthlyLabel)

View File

@ -45,7 +45,7 @@ def readPdfBankStatement(filePath):
firstPage = True
initialAmount = None
initialDate = None
#currentAmount = None
currentAmount = None
date = None
comment = []
transactions = []
@ -64,7 +64,7 @@ def readPdfBankStatement(filePath):
initialDate = datetime.strptime(soldeCrediteurAuRegexMatch.group(1), '%d.%m.%Y')
initialAmount = toFloat(soldeCrediteurAuRegexMatch.group(2))
print(f'{initialAmount=}')
#currentAmount = initialAmount
currentAmount = initialAmount
started = True
continue
else:
@ -95,7 +95,7 @@ def readPdfBankStatement(filePath):
'date': getDateFollowing(date, initialDate),
'valeur': getDateFollowing(valeur, initialDate),
'amount': amount,
#'currentAmount': currentAmount,
'current amount': currentAmount,
'comment': '\n'.join(comment)
}]
date = None
@ -104,7 +104,7 @@ def readPdfBankStatement(filePath):
amount = toFloat(amount)
if abs(debitIndex - lineLen) < abs(creditIndex - lineLen):
amount *= -1
#currentAmount += amount
currentAmount += amount
comment = [firstCommentLine]
elif line != '':
comment += [line.strip()]
@ -113,7 +113,7 @@ def readPdfBankStatement(filePath):
'date': getDateFollowing(date, initialDate),
'valeur': getDateFollowing(valeur, initialDate),
'amount': amount,
#'currentAmount': currentAmount,
'current amount': currentAmount,
'comment': '\n'.join(comment)
}]
return initialAmount, totalMonthlyDebit, totalMonthlyCredit, transactions, fileDatetime