feat: persist issued document artifacts and invalidate draft artifacts on edit

This commit is contained in:
Арсентьев Александр
2026-09-29 18:17:36 +03:00
parent 1917bc6513
commit c1be111e1a
6 changed files with 1181 additions and 0 deletions
+36
View File
@@ -147,6 +147,15 @@ def save_draft(db: Session, project_id: int, number: str, document_date: date, c
price = _decimal(item.unit_price)
document.items.append(KS2Item(work_item_id=item_id, quantity=quantity,
unit_price=price, amount=price * quantity))
# Successful draft change invalidates existing draft artifacts (same tx).
if ks2_id is not None:
from app.services.document_artifact_service import invalidate_artifacts
invalidate_artifacts(
db,
project_id=project_id,
document_type="KS2",
document_id=document.id,
)
db.commit()
db.refresh(document)
return document
@@ -157,6 +166,7 @@ def save_draft(db: Session, project_id: int, number: str, document_date: date, c
def issue_ks2(db: Session, project_id: int, ks2_id: int):
"""Revalidate current balances, attribute quantity to DR lines oldest-first, and issue atomically."""
artifact = None
try:
document = db.query(KS2).filter(KS2.id == ks2_id, KS2.project_id == project_id).with_for_update().first()
if document is None:
@@ -206,11 +216,37 @@ def issue_ks2(db: Session, project_id: int, ks2_id: int):
f"Не удалось распределить весь объём по источникам Daily Reports для позиции {line.work_item.name}"
)
document.status = "issued"
# Generate and persist the official artifact inside this transaction.
from app.exporters.excel.ks2_renderer import (
TEMPLATE_TYPE as KS2_TEMPLATE_TYPE,
TEMPLATE_VERSION as KS2_TEMPLATE_VERSION,
render_ks2_excel,
)
from app.exporters.ks2_dto import build_ks2_export_dto
from app.services.document_artifact_service import persist_artifact, remove_artifact_file
dto = build_ks2_export_dto(db, document)
content = render_ks2_excel(dto)
artifact = persist_artifact(
db,
project_id=project_id,
document_type="KS2",
document_id=document.id,
format="xlsx",
template_type=KS2_TEMPLATE_TYPE,
template_version=KS2_TEMPLATE_VERSION,
content=content,
dto=dto,
)
db.commit()
db.refresh(document)
return document
except Exception:
db.rollback()
if artifact is not None:
remove_artifact_file(artifact)
raise