filters, reports

This commit is contained in:
2024-12-09 17:00:32 +09:00
parent e76a80fb2f
commit fbc2b0e1b0
5 changed files with 27 additions and 14 deletions

View File

@@ -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,16 +68,29 @@ 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
# Фильтрация по "дата заезда"
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')
)
@@ -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

View File

@@ -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)

View File

@@ -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',)

Binary file not shown.

Binary file not shown.