Working statiscitcs in bot
This commit is contained in:
BIN
bot/fonts/OpenSans-Regular.cw127.pkl
Normal file
BIN
bot/fonts/OpenSans-Regular.cw127.pkl
Normal file
Binary file not shown.
BIN
bot/fonts/OpenSans-Regular.pkl
Normal file
BIN
bot/fonts/OpenSans-Regular.pkl
Normal file
Binary file not shown.
BIN
bot/fonts/OpenSans-Regular.ttf
Normal file
BIN
bot/fonts/OpenSans-Regular.ttf
Normal file
Binary file not shown.
@@ -452,27 +452,28 @@ async def get_hotels_for_user(user_id):
|
||||
|
||||
|
||||
async def get_reservations(hotel_id, start_date=None, end_date=None):
|
||||
"""Получение статистики бронирований по отелю."""
|
||||
"""Получение статистики бронирований по отелю с гостями."""
|
||||
query = Reservation.objects.filter(hotel_id=hotel_id)
|
||||
if start_date:
|
||||
query = query.filter(check_in__gte=start_date)
|
||||
if end_date:
|
||||
query = query.filter(check_out__lte=end_date)
|
||||
return await sync_to_async(list)(query)
|
||||
|
||||
|
||||
reservations = await sync_to_async(list)(query.prefetch_related('guests'))
|
||||
return reservations
|
||||
|
||||
def generate_pdf_report(hotel_name, reservations, start_date, end_date):
|
||||
"""Генерация PDF отчета."""
|
||||
pdf = FPDF()
|
||||
pdf.add_page()
|
||||
|
||||
# Подключение шрифта, поддерживающего кириллицу
|
||||
font_path = os.path.join("fonts", "OpenSans-Regular.ttf")
|
||||
print(f'\n\n\nПуть к шрифту: {font_path}\n\n\n') # Укажите путь к шрифту DejaVuSans.ttf
|
||||
# Укажите путь к шрифту
|
||||
font_path = os.path.join("bot", "fonts", "OpenSans-Regular.ttf")
|
||||
if not os.path.exists(font_path):
|
||||
raise FileNotFoundError("Шрифт OpenSans-Regular.ttf не найден. Убедитесь, что он находится в папке 'fonts'.")
|
||||
pdf.add_font("DejaVu", "", font_path, uni=True)
|
||||
pdf.set_font("DejaVu", size=12)
|
||||
raise FileNotFoundError(f"Шрифт {font_path} не найден. Убедитесь, что он находится в указанной папке.")
|
||||
|
||||
pdf.add_font("OpenSans", "", font_path, uni=True)
|
||||
pdf.set_font("OpenSans", size=12)
|
||||
|
||||
# Заголовки
|
||||
title = f"Отчет по заселениям: {hotel_name}"
|
||||
@@ -482,29 +483,56 @@ def generate_pdf_report(hotel_name, reservations, start_date, end_date):
|
||||
else "Период: Все время"
|
||||
)
|
||||
|
||||
pdf.cell(200, 10, txt=title, ln=True, align="C")
|
||||
pdf.cell(200, 10, txt=period, ln=True, align="C")
|
||||
pdf.cell(0, 10, txt=title, ln=True, align="C")
|
||||
pdf.cell(0, 10, txt=period, ln=True, align="C")
|
||||
pdf.ln(10)
|
||||
|
||||
# Ширины колонок
|
||||
page_width = pdf.w - 20 # Учитываем отступы
|
||||
col_widths = [page_width * 0.2, # Дата заезда
|
||||
page_width * 0.2, # Дата выезда
|
||||
page_width * 0.15, # Номер
|
||||
page_width * 0.25, # Тип комнаты
|
||||
page_width * 0.1, # Цена
|
||||
page_width * 0.1] # Скидка
|
||||
|
||||
# Заголовки таблицы
|
||||
pdf.set_font("DejaVu", size=10)
|
||||
pdf.cell(40, 10, "Дата заезда", border=1)
|
||||
pdf.cell(40, 10, "Дата выезда", border=1)
|
||||
pdf.cell(30, 10, "Номер", border=1)
|
||||
pdf.cell(50, 10, "Тип комнаты", border=1)
|
||||
pdf.cell(30, 10, "Цена", border=1)
|
||||
pdf.set_font("OpenSans", size=10)
|
||||
headers = ["Дата заезда", "Дата выезда", "Номер", "Тип комнаты", "Цена", "Скидка"]
|
||||
for width, header in zip(col_widths, headers):
|
||||
pdf.cell(width, 10, header, border=1, align="C")
|
||||
pdf.ln()
|
||||
|
||||
# Инициализация сумм
|
||||
total_price = 0
|
||||
total_discount = 0
|
||||
|
||||
# Добавление данных
|
||||
for res in reservations:
|
||||
guests = ", ".join([guest.name for guest in res.guests.all()])
|
||||
pdf.cell(40, 10, res.check_in.strftime('%d.%m.%Y'), border=1)
|
||||
pdf.cell(40, 10, res.check_out.strftime('%d.%m.%Y'), border=1)
|
||||
pdf.cell(30, 10, res.room_number, border=1)
|
||||
pdf.cell(50, 10, res.room_type, border=1)
|
||||
pdf.cell(30, 10, f"{res.price} ₽", border=1)
|
||||
price = res.price or 0
|
||||
discount = res.discount or 0
|
||||
total_price += price
|
||||
total_discount += discount
|
||||
|
||||
pdf.cell(col_widths[0], 10, res.check_in.strftime('%d.%m.%Y'), border=1)
|
||||
pdf.cell(col_widths[1], 10, res.check_out.strftime('%d.%m.%Y'), 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[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.multi_cell(200, 10, f"Гости: {guests}")
|
||||
|
||||
pdf.set_font("OpenSans", size=8)
|
||||
# pdf.multi_cell(page_width, 5, f"Гости: {guests}", border=0)
|
||||
pdf.set_font("OpenSans", size=10)
|
||||
|
||||
# Итоги
|
||||
pdf.ln(5)
|
||||
pdf.set_font("OpenSans", size=12)
|
||||
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")
|
||||
|
||||
BIN
fonts/OpenSans-Regular.cw127.pkl
Normal file
BIN
fonts/OpenSans-Regular.cw127.pkl
Normal file
Binary file not shown.
BIN
fonts/OpenSans-Regular.pkl
Normal file
BIN
fonts/OpenSans-Regular.pkl
Normal file
Binary file not shown.
BIN
fonts/OpenSans-Regular.ttf
Normal file
BIN
fonts/OpenSans-Regular.ttf
Normal file
Binary file not shown.
BIN
reports/Golden Hills 4*_report.pdf
Normal file
BIN
reports/Golden Hills 4*_report.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user