feat: polish document exports and fix filename extensions
This commit is contained in:
@@ -1,19 +1,24 @@
|
||||
"""Excel renderer for KS-2 export using openpyxl."""
|
||||
import io
|
||||
from decimal import Decimal
|
||||
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.styles import Alignment, Border, Font, Side
|
||||
from openpyxl.utils import get_column_letter
|
||||
|
||||
from app.exporters.common import (
|
||||
MONEY_FMT,
|
||||
MONEY_TEXT,
|
||||
QTY_FMT,
|
||||
QTY_TEXT,
|
||||
format_period,
|
||||
project_requisite_lines,
|
||||
)
|
||||
from app.exporters.ks2_dto import KS2ExportDocument
|
||||
|
||||
THIN = Side(style="thin")
|
||||
BORDER = Border(left=THIN, right=THIN, top=THIN, bottom=THIN)
|
||||
HEADER_FONT = Font(bold=True, size=11)
|
||||
TITLE_FONT = Font(bold=True, size=14)
|
||||
MONEY_FMT = '#,##0.00'
|
||||
QTY_FMT = '#,##0.0000'
|
||||
|
||||
|
||||
def render_ks2_excel(doc: KS2ExportDocument) -> bytes:
|
||||
@@ -23,28 +28,28 @@ def render_ks2_excel(doc: KS2ExportDocument) -> bytes:
|
||||
|
||||
h = doc.header
|
||||
row = 1
|
||||
col_count = 7
|
||||
|
||||
# Title
|
||||
ws.merge_cells(start_row=row, start_column=1, end_row=row, end_column=7)
|
||||
ws.merge_cells(start_row=row, start_column=1, end_row=row, end_column=col_count)
|
||||
c = ws.cell(row=row, column=1, value=f"Акт КС-2 №{h.ks2_number} от {h.ks2_date.strftime('%d.%m.%Y')}")
|
||||
c.font = TITLE_FONT
|
||||
c.alignment = Alignment(horizontal="center")
|
||||
row += 1
|
||||
|
||||
# Header block
|
||||
header_lines = [
|
||||
("Проект:", h.project_name),
|
||||
("Заказчик:", h.customer),
|
||||
("Договор:", h.contract_number),
|
||||
]
|
||||
if h.period_from and h.period_to:
|
||||
header_lines.append(("Период:", f"{h.period_from.strftime('%d.%m.%Y')} — {h.period_to.strftime('%d.%m.%Y')}"))
|
||||
if h.comment:
|
||||
header_lines.append(("Комментарий:", h.comment))
|
||||
|
||||
# Unified project requisites
|
||||
header_lines = project_requisite_lines(
|
||||
project_name=h.project_name,
|
||||
customer=h.customer,
|
||||
contract_number=h.contract_number,
|
||||
contract_date=h.contract_date,
|
||||
currency=h.currency,
|
||||
period_text=format_period(h.period_from, h.period_to),
|
||||
extra=[("Комментарий:", h.comment)],
|
||||
)
|
||||
for label, value in header_lines:
|
||||
ws.cell(row=row, column=1, value=label).font = HEADER_FONT
|
||||
ws.merge_cells(start_row=row, start_column=2, end_row=row, end_column=7)
|
||||
ws.merge_cells(start_row=row, start_column=2, end_row=row, end_column=col_count)
|
||||
ws.cell(row=row, column=2, value=value)
|
||||
row += 1
|
||||
|
||||
@@ -100,6 +105,8 @@ def render_ks2_excel(doc: KS2ExportDocument) -> bytes:
|
||||
for col, width in widths.items():
|
||||
ws.column_dimensions[get_column_letter(col)].width = width
|
||||
|
||||
ws.freeze_panes = ws.cell(row=header_row + 1, column=1)
|
||||
|
||||
buf = io.BytesIO()
|
||||
wb.save(buf)
|
||||
buf.seek(0)
|
||||
|
||||
Reference in New Issue
Block a user