72 lines
3.6 KiB
Python
72 lines
3.6 KiB
Python
from io import BytesIO
|
|
from pathlib import Path
|
|
from xml.sax.saxutils import escape
|
|
|
|
from reportlab.lib import colors
|
|
from reportlab.lib.enums import TA_RIGHT
|
|
from reportlab.lib.pagesizes import A4
|
|
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
|
|
from reportlab.lib.units import mm
|
|
from reportlab.pdfbase import pdfmetrics
|
|
from reportlab.pdfbase.ttfonts import TTFont
|
|
from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer, Table, TableStyle
|
|
|
|
|
|
def _font_name():
|
|
candidates = (
|
|
Path("C:/Windows/Fonts/arial.ttf"),
|
|
Path("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"),
|
|
)
|
|
for path in candidates:
|
|
if path.exists():
|
|
pdfmetrics.registerFont(TTFont("TimerrFont", path))
|
|
return "TimerrFont"
|
|
return "Helvetica"
|
|
|
|
|
|
def _duration(minutes):
|
|
hours, remainder = divmod(int(minutes), 60)
|
|
return f"{hours} ч {remainder:02d} мин"
|
|
|
|
|
|
def render_company_pdf(report, company):
|
|
output = BytesIO()
|
|
font = _font_name()
|
|
styles = getSampleStyleSheet()
|
|
normal = ParagraphStyle("TimerrNormal", parent=styles["Normal"], fontName=font, fontSize=9, leading=12)
|
|
heading = ParagraphStyle("TimerrHeading", parent=styles["Heading1"], fontName=font, fontSize=18, leading=22)
|
|
subheading = ParagraphStyle("TimerrSubheading", parent=styles["Heading2"], fontName=font, fontSize=13, leading=16)
|
|
total = ParagraphStyle("TimerrTotal", parent=normal, fontSize=12, alignment=TA_RIGHT)
|
|
document = SimpleDocTemplate(output, pagesize=A4, rightMargin=15 * mm, leftMargin=15 * mm, topMargin=15 * mm, bottomMargin=15 * mm)
|
|
story = [
|
|
Paragraph("Отчёт о выполненных работах", heading),
|
|
Spacer(1, 4 * mm),
|
|
Paragraph(f"<b>Компания:</b> {escape(company.name)}", normal),
|
|
Paragraph(f"<b>Исполнитель:</b> {escape(report['user'].get_full_name() or report['user'].username)}", normal),
|
|
Paragraph(f"<b>Период:</b> {report['month']:02d}.{report['year']}", normal),
|
|
Spacer(1, 4 * mm),
|
|
]
|
|
for company_group in report["companies"]:
|
|
for project_group in company_group["projects"].values():
|
|
story.append(Paragraph(escape(project_group["project"].name), subheading))
|
|
rows = [["Дата", "Задача и описание", "Время", "Ставка", "Сумма"]]
|
|
for task in project_group["tasks"]:
|
|
rows.append([
|
|
task.work_date.strftime("%d.%m.%Y"),
|
|
Paragraph(f"<b>{escape(task.title)}</b><br/>{escape(task.description)}", normal),
|
|
_duration(task.duration_minutes), f"{task.hourly_rate} ₽", f"{task.amount} ₽",
|
|
])
|
|
rows.append(["", "Итого по проекту", _duration(project_group["minutes"]), "", f"{project_group['amount']} ₽"])
|
|
table = Table(rows, colWidths=(22 * mm, 78 * mm, 25 * mm, 25 * mm, 25 * mm), repeatRows=1)
|
|
table.setStyle(TableStyle([
|
|
("FONTNAME", (0, 0), (-1, -1), font), ("FONTSIZE", (0, 0), (-1, -1), 8),
|
|
("BACKGROUND", (0, 0), (-1, 0), colors.HexColor("#e9edf4")),
|
|
("GRID", (0, 0), (-1, -1), .35, colors.HexColor("#aeb7c5")),
|
|
("VALIGN", (0, 0), (-1, -1), "TOP"), ("FONTNAME", (0, -1), (-1, -1), font),
|
|
("TOPPADDING", (0, 0), (-1, -1), 5), ("BOTTOMPADDING", (0, 0), (-1, -1), 5),
|
|
]))
|
|
story.extend((table, Spacer(1, 5 * mm)))
|
|
story.append(Paragraph(f"Итого: {_duration(company_group['minutes'])}, {company_group['amount']} ₽", total))
|
|
document.build(story)
|
|
return output.getvalue()
|