9cfb341774
Tasks 008-016: KS-6a journal, KS-3 documents, payments with allocations, tech debt cleanup, financial privacy, document tree, storno mechanism, row-click UI, BOQ collapse/sticky header, and KS-2 export to Excel/Word via shared DTO architecture.
85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
"""Word renderer for KS-2 export using python-docx."""
|
|
import io
|
|
|
|
from docx import Document
|
|
from docx.shared import Pt, Cm
|
|
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
from docx.enum.table import WD_TABLE_ALIGNMENT
|
|
|
|
from app.exporters.ks2_dto import KS2ExportDocument
|
|
|
|
|
|
def render_ks2_word(doc: KS2ExportDocument) -> bytes:
|
|
d = Document()
|
|
h = doc.header
|
|
|
|
# Title
|
|
title = d.add_paragraph()
|
|
title.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
run = title.add_run(f"Акт КС-2 №{h.ks2_number} от {h.ks2_date.strftime('%d.%m.%Y')}")
|
|
run.bold = True
|
|
run.font.size = Pt(14)
|
|
|
|
d.add_paragraph()
|
|
|
|
# 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))
|
|
|
|
for label, value in header_lines:
|
|
p = d.add_paragraph()
|
|
run = p.add_run(label + " ")
|
|
run.bold = True
|
|
p.add_run(value)
|
|
|
|
d.add_paragraph()
|
|
|
|
# Table
|
|
table = d.add_table(rows=1, cols=7)
|
|
table.style = "Table Grid"
|
|
table.alignment = WD_TABLE_ALIGNMENT.CENTER
|
|
|
|
columns = ["№", "Наименование работ", "№ поз. в ведомости", "Ед. изм.", "Количество", "Цена за ед.", "Стоимость"]
|
|
for idx, col_name in enumerate(columns):
|
|
cell = table.rows[0].cells[idx]
|
|
cell.text = col_name
|
|
for paragraph in cell.paragraphs:
|
|
for run in paragraph.runs:
|
|
run.bold = True
|
|
run.font.size = Pt(9)
|
|
|
|
for item in doc.items:
|
|
row = table.add_row()
|
|
row.cells[0].text = str(item.row_number)
|
|
row.cells[1].text = item.name
|
|
row.cells[2].text = item.source_number
|
|
row.cells[3].text = item.unit
|
|
row.cells[4].text = f"{item.quantity:.4f}"
|
|
row.cells[5].text = f"{item.unit_price:.2f}"
|
|
row.cells[6].text = f"{item.amount:.2f}"
|
|
|
|
# Total row
|
|
total_row = table.add_row()
|
|
total_row.cells[0].merge(total_row.cells[5])
|
|
total_row.cells[0].text = "Итого:"
|
|
for paragraph in total_row.cells[0].paragraphs:
|
|
paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
|
|
for run in paragraph.runs:
|
|
run.bold = True
|
|
total_row.cells[6].text = f"{doc.total:.2f}"
|
|
for paragraph in total_row.cells[6].paragraphs:
|
|
for run in paragraph.runs:
|
|
run.bold = True
|
|
|
|
buf = io.BytesIO()
|
|
d.save(buf)
|
|
buf.seek(0)
|
|
return buf.read()
|