Files
Touchh/bot/utils/pdf_report.py
2024-12-09 17:00:32 +09:00

66 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fpdf import FPDF
import os
def generate_pdf_report(hotel_name, reservations, start_date, end_date):
"""Генерация PDF отчета."""
pdf = FPDF(orientation='L', unit="mm", format="A4")
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 - 10
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, 15, 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 %H:%M'), border=1)
pdf.cell(col_widths[1], 10, res.check_out.strftime('%d.%m.%Y %H:%M'), 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.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)
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