diff --git a/bot/operations/statistics.py b/bot/operations/statistics.py index e2f093e4..10d54a82 100644 --- a/bot/operations/statistics.py +++ b/bot/operations/statistics.py @@ -45,6 +45,7 @@ async def stats_select_period(update: Update, context: ContextTypes.DEFAULT_TYPE context.user_data["selected_hotel"] = hotel_id keyboard = [ + [InlineKeyboardButton("День", callback_data="stats_period_day")], [InlineKeyboardButton("Неделя", callback_data="stats_period_week")], [InlineKeyboardButton("Месяц", callback_data="stats_period_month")], [InlineKeyboardButton("Все время", callback_data="stats_period_all")], @@ -67,19 +68,32 @@ async def generate_statistics(update: Update, context: ContextTypes.DEFAULT_TYPE period = query.data.split("_")[2] now = datetime.now() - if period == "week": - start_date = now - timedelta(days=7) - end_date = now + if period == "day": + start_date = (now - timedelta(days=1)).date() # Вчерашняя дата + end_date = now.date() # Сегодняшняя дата + elif period == "week": + start_date = (now - timedelta(days=7)).date() + end_date = now.date() elif period == "month": - start_date = now - timedelta(days=30) - end_date = now + start_date = (now - timedelta(days=30)).date() + end_date = now.date() else: start_date = None end_date = None - reservations = await sync_to_async(list)( - Reservation.objects.filter(hotel_id=hotel_id).prefetch_related('guests') - ) + # Фильтрация по "дата заезда" + if start_date and end_date: + reservations = await sync_to_async(list)( + Reservation.objects.filter( + hotel_id=hotel_id, + check_in__date__gte=start_date, + check_in__date__lte=end_date + ).prefetch_related('guests') + ) + else: + reservations = await sync_to_async(list)( + Reservation.objects.filter(hotel_id=hotel_id).prefetch_related('guests') + ) if not reservations: await query.edit_message_text("Нет данных для статистики за выбранный период.") @@ -92,6 +106,7 @@ async def generate_statistics(update: Update, context: ContextTypes.DEFAULT_TYPE await query.message.reply_document(document=file, filename=f"{hotel.name}_report.pdf") + async def stats_back(update: Update, context): """Возврат к выбору отеля.""" query = update.callback_query diff --git a/bot/utils/pdf_report.py b/bot/utils/pdf_report.py index 414c44b8..0934d8d1 100644 --- a/bot/utils/pdf_report.py +++ b/bot/utils/pdf_report.py @@ -4,7 +4,7 @@ import os def generate_pdf_report(hotel_name, reservations, start_date, end_date): """Генерация PDF отчета.""" - pdf = FPDF() + pdf = FPDF(orientation='L', unit="mm", format="A4") pdf.add_page() # Укажите путь к шрифту @@ -33,7 +33,7 @@ def generate_pdf_report(hotel_name, reservations, start_date, end_date): # Заголовки таблицы pdf.set_font("OpenSans", size=10) - headers = ["Дата заезда", "Дата выезда", "Номер", "Тип комнаты", "Цена", "Скидка"] + headers = ["Дата заезда", "Дата выезда", "Номер", "Тип комнаты", "Цена"] for width, header in zip(col_widths, headers): pdf.cell(width, 15, header, border=1, align="C") pdf.ln() @@ -52,14 +52,12 @@ def generate_pdf_report(hotel_name, reservations, start_date, end_date): pdf.cell(col_widths[2], 10, res.room_number, border=1) pdf.cell(col_widths[3], 10, res.room_type, border=1) pdf.cell(col_widths[4], 10, f"{price:.2f} р.", border=1, align="R") - pdf.cell(col_widths[5], 10, f"{discount:.2f} р.", border=1, align="R") pdf.ln() pdf.ln(5) pdf.set_font("OpenSans", size=18) pdf.cell(0, 10, "Итоги:", ln=True) pdf.cell(0, 10, f"Общая сумма цен: {total_price:.2f} руб.", ln=True) - pdf.cell(0, 10, f"Общая сумма скидок: {total_discount:.2f} руб. ", ln=True) file_path = os.path.join("reports", f"{hotel_name}_report.pdf") os.makedirs(os.path.dirname(file_path), exist_ok=True) diff --git a/hotels/admin.py b/hotels/admin.py index 9832381f..a4192bcb 100644 --- a/hotels/admin.py +++ b/hotels/admin.py @@ -81,8 +81,8 @@ class ApiRequestLogAdmin(admin.ModelAdmin): @admin.register(Reservation) class ReservationAdmin(admin.ModelAdmin): list_display = ('reservation_id', 'hotel', 'room_number', 'room_type', 'check_in', 'check_out', 'status', 'price', 'discount') - search_fields = ('reservation_id', 'hotel__name', 'room_number', 'room_type', 'status') - list_filter = ('status', 'check_in', 'check_out') + search_fields = ('hotel', 'room_number', 'room_type', 'check_in', 'check_out', 'status', 'price', 'discount') + list_filter = ('hotel', 'room_number', 'room_type', 'check_in', 'check_out', 'status', 'price', 'discount') ordering = ('-check_in',) diff --git a/reports/Golden Hills 3_report.pdf b/reports/Golden Hills 3_report.pdf index 7d188cc7..958c51eb 100644 Binary files a/reports/Golden Hills 3_report.pdf and b/reports/Golden Hills 3_report.pdf differ diff --git a/reports/Golden Hills 4*_report.pdf b/reports/Golden Hills 4*_report.pdf index 476d268f..1c496ef5 100644 Binary files a/reports/Golden Hills 4*_report.pdf and b/reports/Golden Hills 4*_report.pdf differ