Skip to content

Commit

Permalink
Add report by_month
Browse files Browse the repository at this point in the history
  • Loading branch information
HeySlava committed Dec 8, 2023
1 parent ccebaec commit 54a60ee
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
18 changes: 18 additions & 0 deletions bot/handlers/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ async def group_by_day(cb: CallbackQuery, session: Session):
await cb.message.answer(msg)


@router.callback_query(Text('by_month'))
async def group_by_month(cb: CallbackQuery, session: Session):
await cb.answer()

user = user_service.get_user_by_id(
user_id=cb.from_user.id,
session=session,
)

rows = report_service.get_report_by_month(
user=user,
session=session,
)
for msg in chunkineze(rows, chunk_size=50):
if cb.message:
await cb.message.answer(msg)


@router.callback_query(Text('by_category'))
async def group_by_category(cb: CallbackQuery, session: Session):
await cb.answer()
Expand Down
1 change: 1 addition & 0 deletions bot/keyboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
f'Последние {config.last} записей': 'last_n',
'Список расходов за выбранный день': 'custom_day',
'Группировка по дню': 'by_day',
'Группировка по месяцу': 'by_month',
'Группировка по категории': 'by_category',
}

Expand Down
30 changes: 30 additions & 0 deletions bot/migrations/versions/09d3b6f33b87_report_by_month_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Report by month release
Revision ID: 09d3b6f33b87
Revises: 277fe7039415
Create Date: 2023-12-08 22:48:55.609781
"""
from alembic import op


# revision identifiers, used by Alembic.
revision = '09d3b6f33b87'
down_revision = '277fe7039415'
branch_labels = None
depends_on = None


def upgrade() -> None:
msg = (
'Обновление:'
'\n\n'
'Доступна группировка по месяцу для отчета'
)
stmt = f"insert into releases(id, message, is_broadcasted) values (7, '{msg}', 0)"
op.execute(stmt)


def downgrade() -> None:
stmt = 'delete from releases where id = 7'
op.execute(stmt)
28 changes: 28 additions & 0 deletions bot/services/report_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ def get_report_by_day(
return [f'{row.tuple()[1]} | {row.tuple()[3]} {row.tuple()[2]}' for row in rows]


def get_report_by_month(
user: User,
session: Session,
) -> List[str]:
by_month_year = 'month_year'
stmt = (
select(
Expense.user_id,
func.strftime('%m-%Y', Expense.cdate_tz).label(by_month_year),
Expense.unit,
func.sum(Expense.price).label('total'),
)
.where(
Expense.user_id == user.id,
~Expense.is_replenishment,
)
.group_by(Expense.user_id, by_month_year, Expense.unit)
.order_by(by_month_year)
.subquery()
)

return [
f'{getattr(r, by_month_year)} | {r.unit} {r.total}' for
r in
session.execute(select(stmt))
]


def get_report_by_category(
user: User,
session: Session,
Expand Down

0 comments on commit 54a60ee

Please sign in to comment.