Files
work-volume-tracker/app/exporters/ks2_dto.py
T
2026-09-30 16:42:02 +03:00

96 lines
3.6 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
@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")
for idx, line in enumerate(ks2_document.items, start=1):
wi = line.work_item
amount = D(str(line.amount or 0))
total += amount
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,
))
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)