68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
from fpdf import FPDF
|
||
import os
|
||
|
||
|
||
def generate_pdf_report(hotel_name, reservations, start_date, end_date):
|
||
"""Генерация PDF отчета."""
|
||
pdf = FPDF()
|
||
pdf.add_page()
|
||
|
||
# Укажите путь к шрифту
|
||
font_path = os.path.join("bot", "fonts", "OpenSans-Regular.ttf")
|
||
if not os.path.exists(font_path):
|
||
raise FileNotFoundError(f"Шрифт {font_path} не найден. Убедитесь, что он находится в указанной папке.")
|
||
|
||
pdf.add_font("OpenSans", "", font_path, uni=True)
|
||
pdf.set_font("OpenSans", size=12)
|
||
|
||
# Заголовки
|
||
title = f"Отчет по заселениям: {hotel_name}"
|
||
period = (
|
||
f"Период: {start_date.strftime('%d.%m.%Y')} - {end_date.strftime('%d.%m.%Y')}"
|
||
if start_date and end_date
|
||
else "Период: Все время"
|
||
)
|
||
|
||
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("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:
|
||
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.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")
|
||
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
||
pdf.output(file_path)
|
||
return file_path
|