Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add report by_month #20

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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