added analysis

This commit is contained in:
wt
2022-10-22 12:07:34 +07:00
parent ceb4536118
commit 254eec26b7
16 changed files with 374 additions and 49 deletions
+73 -48
View File
@@ -10,7 +10,8 @@ import locales
def analysis_month(message):
transactions = Transaction.select().where(
Transaction.user == User.get(username=message.chat.id),
Transaction.month == datetime.datetime.today().strftime('%m')
Transaction.month == datetime.datetime.today().strftime('%m'),
Transaction.year == datetime.datetime.today().strftime('%Y')
)
incomes = 0
consuptions = 0
@@ -19,29 +20,37 @@ def analysis_month(message):
incomes += i.sum
elif i.type == 'consuption':
consuptions += i.sum
vals = [incomes, consuptions]
labels = []
if locales.LOCALE == 'ru':
labels = ['Доходы', 'Расходы']
elif locales.LOCALE == 'en':
labels = ['Incomes', 'Consuptions']
exp = (0.1, 0)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/diagramallmonth.png')
mes = labels[0] + ' : +' + str(vals[0]) + '\n' + labels[1] + ' : -' + str(vals[1]) + '\n'
with open('diagrams/diagramallmonth.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
mes = ''
if len(transactions) == 0:
if locales.LOCALE == 'ru':
mes += 'Нет транзакций за этот месяц\n'
elif locales.LOCALE == 'en':
mes += 'No transactions for this month\n'
bot.send_message(message.chat.id, mes)
else:
vals = [incomes, consuptions]
labels = []
if locales.LOCALE == 'ru':
labels = ['Доходы', 'Расходы']
elif locales.LOCALE == 'en':
labels = ['Incomes', 'Consuptions']
exp = (0.1, 0)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/month/diagramallmonth.png')
mes = labels[0] + ' : +' + str(vals[0]) + '\n' + labels[1] + ' : -' + str(vals[1]) + '\n'
with open('diagrams/month/diagramallmonth.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
bot.send_message(message.chat.id, mes)
def analysis_month_rm(message):
transactions = Transaction.select().where(
Transaction.user == User.get(username=message.chat.id),
Transaction.month == datetime.datetime.now().strftime('%m'),
Transaction.year == datetime.datetime.today().strftime('%Y'),
Transaction.type == 'consuption'
)
vals = []
@@ -54,23 +63,31 @@ def analysis_month_rm(message):
labels.append(i.category)
vals.append(i.sum)
exp = []
for _ in labels:
exp.append(0.1)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/rmanalysismonth.png')
mes = ''
pos = 0
for i in labels:
mes += i + ' : -' + str(vals[pos]) + currencyes.currensyes['ru-ruble'] + '\n'
pos += 1
with open('diagrams/rmanalysismonth.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
if len(transactions) == 0:
if locales.LOCALE == 'ru':
mes += 'Нет расходов за этот месяц\n'
elif locales.LOCALE == 'en':
mes += 'No consuptions for this month\n'
bot.send_message(message.chat.id, mes)
else:
exp = []
for _ in labels:
exp.append(0.1)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/month/rmanalysismonth.png')
mes = ''
pos = 0
for i in labels:
mes += i + ' : -' + str(vals[pos]) + currencyes.currensyes['ru-ruble'] + '\n'
pos += 1
with open('diagrams/month/rmanalysismonth.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
bot.send_message(message.chat.id, mes)
def have_in_vals(category, labels):
@@ -86,6 +103,7 @@ def analysis_month_add(message):
transactions = Transaction.select().where(
Transaction.user == User.get(username=message.chat.id),
Transaction.month == datetime.datetime.now().strftime('%m'),
Transaction.year == datetime.datetime.today().strftime('%Y'),
Transaction.type == 'incom'
)
vals = []
@@ -97,21 +115,28 @@ def analysis_month_add(message):
else:
labels.append(i.category)
vals.append(i.sum)
exp = []
for _ in labels:
exp.append(0.1)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/addanalysismonth.png')
mes = ''
pos = 0
for i in labels:
mes += i + ' : +' + str(vals[pos]) + currencyes.currensyes['ru-ruble'] + '\n'
pos += 1
with open('diagrams/addanalysismonth.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
if len(transactions) == 0:
if locales.LOCALE == 'ru':
mes += 'Нет доходов за этот месяц\n'
elif locales.LOCALE == 'en':
mes += 'No incomes for this month\n'
bot.send_message(message.chat.id, mes)
else:
exp = []
for _ in labels:
exp.append(0.1)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/month/addanalysismonth.png')
mes = ''
pos = 0
for i in labels:
mes += i + ' : +' + str(vals[pos]) + currencyes.currensyes['ru-ruble'] + '\n'
pos += 1
with open('diagrams/month/addanalysismonth.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
bot.send_message(message.chat.id, mes)
+135
View File
@@ -0,0 +1,135 @@
from bot import bot
from database.models import Transaction, User
import matplotlib.pyplot as plt
import locales
import datetime
import currencyes
from analysismonth import have_in_vals
def analysis_today(message):
transactions = Transaction.select().where(
Transaction.user == User.get(username=message.chat.id),
Transaction.month == datetime.datetime.today().strftime('%m'),
Transaction.year == datetime.datetime.today().strftime('%Y'),
Transaction.day == datetime.datetime.today().strftime('%d')
)
incomes = 0
consuptions = 0
for i in transactions:
if i.type == 'incom':
incomes += i.sum
elif i.type == 'consuption':
consuptions += i.sum
mes = ''
if len(transactions) == 0:
if locales.LOCALE == 'ru':
mes += 'Нет транзакций за сегодня\n'
elif locales.LOCALE == 'en':
mes += 'No transactions today\n'
bot.send_message(message.chat.id, mes)
else:
vals = [incomes, consuptions]
labels = []
if locales.LOCALE == 'ru':
labels = ['Доходы', 'Расходы']
elif locales.LOCALE == 'en':
labels = ['Incomes', 'Consuptions']
exp = (0.1, 0)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/today/diagramalltoday.png')
mes = labels[0] + ' : +' + str(vals[0]) + '\n' + labels[1] + ' : -' + str(vals[1]) + '\n'
with open('diagrams/today/diagramalltoday.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
bot.send_message(message.chat.id, mes)
def analysis_today_rm(message):
transactions = Transaction.select().where(
Transaction.user == User.get(username=message.chat.id),
Transaction.month == datetime.datetime.now().strftime('%m'),
Transaction.year == datetime.datetime.today().strftime('%Y'),
Transaction.day == datetime.datetime.today().strftime('%d'),
Transaction.type == 'consuption'
)
vals = []
labels = []
for i in transactions:
if have_in_vals(i.category, labels)[0]:
pos = have_in_vals(i.category, labels)[1]
vals[pos] += i.sum
else:
labels.append(i.category)
vals.append(i.sum)
mes = ''
if len(transactions) == 0:
if locales.LOCALE == 'ru':
mes += 'Нет расходов за сегодня\n'
elif locales.LOCALE == 'en':
mes += 'No сonsuptions today\n'
bot.send_message(message.chat.id, mes)
else:
exp = []
for _ in labels:
exp.append(0.1)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/today/rmanalysistoday.png')
mes = ''
pos = 0
for i in labels:
mes += i + ' : -' + str(vals[pos]) + currencyes.currensyes['ru-ruble'] + '\n'
pos += 1
with open('diagrams/today/rmanalysistoday.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
bot.send_message(message.chat.id, mes)
def analysis_today_add(message):
transactions = Transaction.select().where(
Transaction.user == User.get(username=message.chat.id),
Transaction.month == datetime.datetime.now().strftime('%m'),
Transaction.year == datetime.datetime.today().strftime('%Y'),
Transaction.day == datetime.datetime.today().strftime('%d'),
Transaction.type == 'incom'
)
vals = []
labels = []
for i in transactions:
if have_in_vals(i.category, labels)[0]:
pos = have_in_vals(i.category, labels)[1]
vals[pos] += i.sum
else:
labels.append(i.category)
vals.append(i.sum)
mes = ''
if len(transactions) == 0:
if locales.LOCALE == 'ru':
mes += 'Нет доходов за сегодня\n'
elif locales.LOCALE == 'en':
mes += 'No incomes today\n'
bot.send_message(message.chat.id, mes)
else:
exp = []
for _ in labels:
exp.append(0.1)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/today/addanalysistoday.png')
mes = ''
pos = 0
for i in labels:
mes += i + ' : +' + str(vals[pos]) + currencyes.currensyes['ru-ruble'] + '\n'
pos += 1
with open('diagrams/today/addanalysistoday.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
bot.send_message(message.chat.id, mes)
+129
View File
@@ -0,0 +1,129 @@
from bot import bot
from database.models import Transaction, User
import locales
import matplotlib.pyplot as plt
import datetime
from analysismonth import have_in_vals
import currencyes
def analysis_year(message):
transactions = Transaction.select().where(
Transaction.user == User.get(username=message.chat.id),
Transaction.year == datetime.datetime.today().strftime('%Y')
)
incomes = 0
consuptions = 0
for i in transactions:
if i.type == 'incom':
incomes += i.sum
elif i.type == 'consuption':
consuptions += i.sum
mes = ''
if len(transactions) == 0:
if locales.LOCALE == 'ru':
mes += 'Нет транзакций за этот год\n'
elif locales.LOCALE == 'en':
mes += 'No transactions for this year\n'
bot.send_message(message.chat.id, mes)
else:
vals = [incomes, consuptions]
labels = []
if locales.LOCALE == 'ru':
labels = ['Доходы', 'Расходы']
elif locales.LOCALE == 'en':
labels = ['Incomes', 'Consuptions']
exp = (0.1, 0)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/year/diagramallyear.png')
mes = labels[0] + ' : +' + str(vals[0]) + '\n' + labels[1] + ' : -' + str(vals[1]) + '\n'
with open('diagrams/year/diagramallyear.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
bot.send_message(message.chat.id, mes)
def analysis_year_rm(message):
transactions = Transaction.select().where(
Transaction.user == User.get(username=message.chat.id),
Transaction.year == datetime.datetime.today().strftime('%Y'),
Transaction.type == 'consuption'
)
vals = []
labels = []
for i in transactions:
if have_in_vals(i.category, labels)[0]:
pos = have_in_vals(i.category, labels)[1]
vals[pos] += i.sum
else:
labels.append(i.category)
vals.append(i.sum)
mes = ''
if len(transactions) == 0:
if locales.LOCALE == 'ru':
mes += 'Нет расходов за этот год\n'
elif locales.LOCALE == 'en':
mes += 'No consuptions for this year\n'
bot.send_message(message.chat.id, mes)
else:
exp = []
for _ in labels:
exp.append(0.1)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/year/rmanalysisyear.png')
mes = ''
pos = 0
for i in labels:
mes += i + ' : -' + str(vals[pos]) + currencyes.currensyes['ru-ruble'] + '\n'
pos += 1
with open('diagrams/year/rmanalysisyear.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
bot.send_message(message.chat.id, mes)
def analysis_year_add(message):
transactions = Transaction.select().where(
Transaction.user == User.get(username=message.chat.id),
Transaction.year == datetime.datetime.today().strftime('%Y'),
Transaction.type == 'incom'
)
vals = []
labels = []
for i in transactions:
if have_in_vals(i.category, labels)[0]:
pos = have_in_vals(i.category, labels)[1]
vals[pos] += i.sum
else:
labels.append(i.category)
vals.append(i.sum)
mes = ''
if len(transactions) == 0:
if locales.LOCALE == 'ru':
mes += 'Нет доходов за этот год\n'
elif locales.LOCALE == 'en':
mes += 'No incomes for this year\n'
bot.send_message(message.chat.id, mes)
else:
exp = []
for _ in labels:
exp.append(0.1)
plt.pie(vals, labels=labels, explode=exp, wedgeprops=dict(width=0.5))
plt.legend()
plt.savefig('diagrams/year/addanalysisyear.png')
mes = ''
pos = 0
for i in labels:
mes += i + ' : +' + str(vals[pos]) + currencyes.currensyes['ru-ruble'] + '\n'
pos += 1
with open('diagrams/year/addanalysisyear.png', 'rb') as diagram:
bot.send_photo(message.chat.id, diagram)
bot.send_message(message.chat.id, mes)
+1 -1
View File
@@ -1,5 +1,5 @@
import telebot
TOKEN = '5123679189:AAHPLBfbPfuCiNK9qCcykcB2pRG-J4OEyrw'
TOKEN = '5425337391:AAHA89sTMj9ndxrQZpWc2aCs5NT5QPMxBBs'
bot = telebot.TeleBot(TOKEN)
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

+22
View File
@@ -14,6 +14,17 @@ ru = {
8. /rmtoday - расходы за сегодня
9. /addall - доходы за все время
10. /rmall - расходы за все время
11. /rmmonth - расходы за этот месяц
12. /addmonth - доходы за этот месяц
13. /analysismonth - диаграмма транзакций за этот месяц
14. /rmanalysismonth - диаграмма расходов за этот месяц
15. /addanalysismonth - диаграмма доходов за этот месяц
16. /analysisyear - диаграмма транзакций за этот год
17. /rmanalysisyear - диаграмма расходов за этот год
18. /addanalysisyear - диаграмма доходов за этот год
19. /analysistoday - диаграмма транзакций за сегодня
20. /rmanalysistoday - диаграмма расходов за сегодня
21. /addanalysistoday - диаграмма доходов за сегодня
''',
'error404_message': 'Я не могу это сделать.',
'erradd': 'Используйте:\n1. /add [сумма]\n2. + [сумма]\n3. /add [сумма] [категория]\n4. + [сумма] [категория]',
@@ -34,6 +45,17 @@ Hi i am finance bot and i can do this:
8. /rmtoday - consuptions today
9. /addall - incomes to all time
10. /rmall - consuptions to all time
11. /rmmonth - consuptions for this month
12. /addmonth - incomes for this month
13. /analysismonth - diagram of transactions for this month
14. /rmanalysismonth - diagram of consuptions for this month
15. /addanalysismonth - diagram of incomes for this month
16. /analysisyear - diagram of transactions for this year
17. /rmanalysisyear - diagram of consuptions for this year
18. /addanalysisyear - diagram of incomes for this year
19. /analysistoday - diagram of transactions today
20. /rmanalysistoday - diagram of consuptions today
21. /addanalysistoday - diagram of incomes today
''',
'error404_message': 'I can\'t do this.',
'erradd': 'Use:\n1. /add [sum]\n2. + [sum]\n3. /add [sum] [category]\n4. + [sum] [category]',
+14
View File
@@ -7,6 +7,8 @@ from database.models import User, Transaction
from currencyes import currensyes
from multiprocessing import Process
from analysismonth import analysis_month, analysis_month_rm, analysis_month_add
from analysisyear import analysis_year, analysis_year_rm, analysis_year_add
from analysistoday import analysis_today, analysis_today_rm, analysis_today_add
# функция ограничения количества знаков после запятой
@@ -245,6 +247,18 @@ def main():
Process(target=analysis_month_rm, args=(message, )).start()
elif message.text.split(' ')[0] == '/addanalysismonth':
Process(target=analysis_month_add, args=(message,)).start()
elif message.text.split(' ')[0] == '/analysisyear':
Process(target=analysis_year, args=(message, )).start()
elif message.text.split(' ')[0] == '/rmanalysisyear':
Process(target=analysis_year_rm, args=(message, )).start()
elif message.text.split(' ')[0] == '/addanalysisyear':
Process(target=analysis_year_add, args=(message, )).start()
elif message.text.split(' ')[0] == '/analysistoday':
Process(target=analysis_today, args=(message,)).start()
elif message.text.split(' ')[0] == '/rmanalysistoday':
Process(target=analysis_today_rm, args=(message,)).start()
elif message.text.split(' ')[0] == '/addanalysistoday':
Process(target=analysis_today_add, args=(message,)).start()
else:
mes = None
if locales.LOCALE == 'ru':