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.
69 lines
2.0 KiB
Python
69 lines
2.0 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
|
|
ks2_number: str
|
|
ks2_date: date
|
|
period_from: date | None
|
|
period_to: date | None
|
|
comment: 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.customer if project and project.customer else "",
|
|
contract_number=project.contract_number if project and project.contract_number 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 "",
|
|
)
|
|
return KS2ExportDocument(header=header, items=items, total=total)
|