filters, reports
This commit is contained in:
@@ -45,6 +45,7 @@ async def stats_select_period(update: Update, context: ContextTypes.DEFAULT_TYPE
|
|||||||
context.user_data["selected_hotel"] = hotel_id
|
context.user_data["selected_hotel"] = hotel_id
|
||||||
|
|
||||||
keyboard = [
|
keyboard = [
|
||||||
|
[InlineKeyboardButton("День", callback_data="stats_period_day")],
|
||||||
[InlineKeyboardButton("Неделя", callback_data="stats_period_week")],
|
[InlineKeyboardButton("Неделя", callback_data="stats_period_week")],
|
||||||
[InlineKeyboardButton("Месяц", callback_data="stats_period_month")],
|
[InlineKeyboardButton("Месяц", callback_data="stats_period_month")],
|
||||||
[InlineKeyboardButton("Все время", callback_data="stats_period_all")],
|
[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]
|
period = query.data.split("_")[2]
|
||||||
|
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
if period == "week":
|
if period == "day":
|
||||||
start_date = now - timedelta(days=7)
|
start_date = (now - timedelta(days=1)).date() # Вчерашняя дата
|
||||||
end_date = now
|
end_date = now.date() # Сегодняшняя дата
|
||||||
|
elif period == "week":
|
||||||
|
start_date = (now - timedelta(days=7)).date()
|
||||||
|
end_date = now.date()
|
||||||
elif period == "month":
|
elif period == "month":
|
||||||
start_date = now - timedelta(days=30)
|
start_date = (now - timedelta(days=30)).date()
|
||||||
end_date = now
|
end_date = now.date()
|
||||||
else:
|
else:
|
||||||
start_date = None
|
start_date = None
|
||||||
end_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)(
|
reservations = await sync_to_async(list)(
|
||||||
Reservation.objects.filter(hotel_id=hotel_id).prefetch_related('guests')
|
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")
|
await query.message.reply_document(document=file, filename=f"{hotel.name}_report.pdf")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def stats_back(update: Update, context):
|
async def stats_back(update: Update, context):
|
||||||
"""Возврат к выбору отеля."""
|
"""Возврат к выбору отеля."""
|
||||||
query = update.callback_query
|
query = update.callback_query
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import os
|
|||||||
|
|
||||||
def generate_pdf_report(hotel_name, reservations, start_date, end_date):
|
def generate_pdf_report(hotel_name, reservations, start_date, end_date):
|
||||||
"""Генерация PDF отчета."""
|
"""Генерация PDF отчета."""
|
||||||
pdf = FPDF()
|
pdf = FPDF(orientation='L', unit="mm", format="A4")
|
||||||
pdf.add_page()
|
pdf.add_page()
|
||||||
|
|
||||||
# Укажите путь к шрифту
|
# Укажите путь к шрифту
|
||||||
@@ -33,7 +33,7 @@ def generate_pdf_report(hotel_name, reservations, start_date, end_date):
|
|||||||
|
|
||||||
# Заголовки таблицы
|
# Заголовки таблицы
|
||||||
pdf.set_font("OpenSans", size=10)
|
pdf.set_font("OpenSans", size=10)
|
||||||
headers = ["Дата заезда", "Дата выезда", "Номер", "Тип комнаты", "Цена", "Скидка"]
|
headers = ["Дата заезда", "Дата выезда", "Номер", "Тип комнаты", "Цена"]
|
||||||
for width, header in zip(col_widths, headers):
|
for width, header in zip(col_widths, headers):
|
||||||
pdf.cell(width, 15, header, border=1, align="C")
|
pdf.cell(width, 15, header, border=1, align="C")
|
||||||
pdf.ln()
|
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[2], 10, res.room_number, border=1)
|
||||||
pdf.cell(col_widths[3], 10, res.room_type, 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[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()
|
||||||
|
|
||||||
pdf.ln(5)
|
pdf.ln(5)
|
||||||
pdf.set_font("OpenSans", size=18)
|
pdf.set_font("OpenSans", size=18)
|
||||||
pdf.cell(0, 10, "Итоги:", ln=True)
|
pdf.cell(0, 10, "Итоги:", ln=True)
|
||||||
pdf.cell(0, 10, f"Общая сумма цен: {total_price:.2f} руб.", 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")
|
file_path = os.path.join("reports", f"{hotel_name}_report.pdf")
|
||||||
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
||||||
|
|||||||
@@ -81,8 +81,8 @@ class ApiRequestLogAdmin(admin.ModelAdmin):
|
|||||||
@admin.register(Reservation)
|
@admin.register(Reservation)
|
||||||
class ReservationAdmin(admin.ModelAdmin):
|
class ReservationAdmin(admin.ModelAdmin):
|
||||||
list_display = ('reservation_id', 'hotel', 'room_number', 'room_type', 'check_in', 'check_out', 'status', 'price', 'discount')
|
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')
|
search_fields = ('hotel', 'room_number', 'room_type', 'check_in', 'check_out', 'status', 'price', 'discount')
|
||||||
list_filter = ('status', 'check_in', 'check_out')
|
list_filter = ('hotel', 'room_number', 'room_type', 'check_in', 'check_out', 'status', 'price', 'discount')
|
||||||
ordering = ('-check_in',)
|
ordering = ('-check_in',)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user