Files
work-volume-tracker/app/exporters/ks2_dto.py
T
Арсентьев Александр bd64bb6307 feat: official KS-2 Excel/Word forms with template markers and single-table flow
Replace the simplified KS-2 exports with the official unified form:

- Excel: official blank as working template, merge-safe row insertion,
  compact layout, header/signer polish, 0.00 money format
- Word: official blank via named template markers (DOC-C4), one dynamic
  works table with repeating header rows, separate closing totals block
  (Итого / Всего по акту), system group rows merged across 8 columns
- Word money formatter (space thousands, comma decimals) and split
  contract date day/month/year boxes
- Issue persists both xlsx and docx artifacts; UI keeps Excel and adds
  Word download
- Daily reports: multi-system item selection in the form

Regression tests for renderers, artifacts and layout; docs/tasks +
docs/implementation for the DOC-C* series.
2026-10-03 19:48:29 +03:00

115 lines
4.4 KiB
Python

"""Shared document DTOs for export renderers. One source of truth for Excel and Word."""
from dataclasses import dataclass, field
from datetime import date
from decimal import Decimal
@dataclass(frozen=True)
class KS2ExportItem:
row_number: int
source_number: str
name: str
unit: str
quantity: Decimal
unit_price: Decimal
amount: Decimal
group_header: str | None = None
group_code: str | None = None
@dataclass(frozen=True)
class KS2ExportHeader:
project_name: str
customer: str
contract_number: str
contract_date: date | None
currency: str
ks2_number: str
ks2_date: date
period_from: date | None
period_to: date | None
comment: str
status: str
# Official requisites (DOC-B10)
investor_name: str = ""
customer_name: str = ""
contractor_name: str = ""
construction_name: str = ""
object_name: str = ""
contract_amount: str = ""
contractor_signer_name: str = ""
contractor_signer_position: str = ""
customer_signer_name: str = ""
customer_signer_position: str = ""
@dataclass(frozen=True)
class KS2ExportDocument:
header: KS2ExportHeader
items: list[KS2ExportItem]
total: Decimal
def build_ks2_export_dto(db, ks2_document) -> KS2ExportDocument:
"""Build the export DTO from a KS2 document. Read-only: does not modify any data."""
from decimal import Decimal as D
project = ks2_document.project
items = []
total = D("0")
current_group = None
for idx, line in enumerate(ks2_document.items, start=1):
wi = line.work_item
amount = D(str(line.amount or 0))
total += amount
# Determine group header from System → Section hierarchy
group_header = None
group_code = None
if wi is not None:
system_name = wi.system.name if wi.system else None
system_code = wi.system.code if wi.system else None
section_name = wi.section.name if wi.section else None
if system_name and system_name != current_group:
group_header = system_name
group_code = system_code
current_group = system_name
elif section_name and system_name is None and section_name != current_group:
group_header = section_name
current_group = section_name
items.append(KS2ExportItem(
row_number=idx,
source_number=wi.source_number or "",
name=wi.name or "",
unit=wi.unit or "",
quantity=D(str(line.quantity or 0)),
unit_price=D(str(line.unit_price or 0)),
amount=amount,
group_header=group_header,
group_code=group_code,
))
header = KS2ExportHeader(
project_name=project.name if project else "",
customer=project.effective_customer_name if project else "",
contract_number=project.contract_number if project and project.contract_number else "",
contract_date=project.contract_date if project else None,
currency=project.currency if project and project.currency else "",
ks2_number=ks2_document.number,
ks2_date=ks2_document.date,
period_from=ks2_document.reporting_period_from,
period_to=ks2_document.reporting_period_to,
comment=ks2_document.comment or "",
status=ks2_document.status or "",
investor_name=project.investor_name if project and project.investor_name else "",
customer_name=project.effective_customer_name if project else "",
contractor_name=project.contractor_name if project and project.contractor_name else "",
construction_name=project.construction_name if project and project.construction_name else "",
object_name=project.object_name if project and project.object_name else "",
contract_amount=str(project.contract_amount) if project and project.contract_amount is not None else "",
contractor_signer_name=project.contractor_signer_name if project and project.contractor_signer_name else "",
contractor_signer_position=project.contractor_signer_position if project and project.contractor_signer_position else "",
customer_signer_name=project.customer_signer_name if project and project.customer_signer_name else "",
customer_signer_position=project.customer_signer_position if project and project.customer_signer_position else "",
)
return KS2ExportDocument(header=header, items=items, total=total)