feat: add KS-3/payments, tech debt fixes, UI polish, and KS-2 Excel/Word export

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.
This commit is contained in:
Арсентьев Александр
2026-09-27 13:38:45 +03:00
parent 6c608a8779
commit 9cfb341774
57 changed files with 7163 additions and 50 deletions
+66
View File
@@ -8,6 +8,8 @@ from app.services.project_dashboard import build_project_dashboard
from app.services.effective_plan import BaseBoqConfigurationError
from app.models.daily_report import DailyReport
from app.models.ks2 import KS2
from app.models.ks3 import KS3
from app.models.payment import Payment
from app.models.work_journal import WorkJournalEntry
from typing import List
import os
@@ -114,6 +116,24 @@ async def read_project(request: Request, project_id: int, db: Session = Depends(
raise HTTPException(status_code=409, detail=str(exc)) from exc
daily_reports = db.query(DailyReport).filter(DailyReport.project_id == project_id).count()
ks2_count = db.query(KS2).filter(KS2.project_id == project_id).count()
ks3_count = db.query(KS3).filter(KS3.project_id == project_id).count()
payments_count = db.query(Payment).filter(Payment.project_id == project_id).count()
from app.models.agreement import Agreement
agreements_count = db.query(Agreement).filter(Agreement.project_id == project_id).count()
from app.services.payment_service import ks3_payment_summary
payment_summary = ks3_payment_summary(db, project_id)
# Build document tree: base BOQ docs with scoped agreements + project-wide agreements
all_agreements = db.query(Agreement).filter(
Agreement.project_id == project_id
).order_by(Agreement.id).all()
base_docs = [d for d in boq_docs if d.agreement_id is None]
doc_tree = []
for doc in base_docs:
scoped = [a for a in all_agreements if a.boq_document_id == doc.id]
doc_tree.append({"doc": doc, "agreements": scoped})
project_wide = [a for a in all_agreements if a.boq_document_id is None]
journal_tasks = db.query(WorkJournalEntry).filter(
WorkJournalEntry.project_id == project_id,
WorkJournalEntry.type == "task",
@@ -131,8 +151,14 @@ async def read_project(request: Request, project_id: int, db: Session = Depends(
"boq_docs": boq_docs,
"daily_reports_count": daily_reports,
"ks2_count": ks2_count,
"ks3_count": ks3_count,
"payments_count": payments_count,
"agreements_count": agreements_count,
"payment_summary": payment_summary,
"journal_tasks": journal_tasks,
"journal_notes": journal_notes,
"doc_tree": doc_tree,
"project_wide_agreements": project_wide,
**dashboard,
})
@@ -242,3 +268,43 @@ async def import_boq(request: Request, project_id: int, db: Session = Depends(ge
"request": request,
"project": db_project
})
def _has_financial_history(db, project_id):
"""Check if project has issued KS-2/KS-3 or payments."""
from app.models.ks2 import KS2
from app.models.ks3 import KS3
from app.models.payment import Payment
if db.query(KS2).filter(KS2.project_id == project_id, KS2.status == "issued").first():
return True
if db.query(KS3).filter(KS3.project_id == project_id, KS3.status == "issued").first():
return True
if db.query(Payment).filter(Payment.project_id == project_id).first():
return True
return False
@router.post("/{project_id}/delete")
async def delete_project(project_id: int, db: Session = Depends(get_db)):
db_project = db.query(project.Project).filter(project.Project.id == project_id).first()
if db_project is None:
raise HTTPException(status_code=404, detail="Project not found")
if _has_financial_history(db, project_id):
raise HTTPException(status_code=400, detail="Cannot delete project with financial history. Archive it instead.")
try:
db.delete(db_project)
db.commit()
except Exception:
db.rollback()
raise HTTPException(status_code=400, detail="Cannot delete project with related data")
return RedirectResponse(url="/projects/", status_code=303)
@router.post("/{project_id}/archive")
async def archive_project(project_id: int, db: Session = Depends(get_db)):
db_project = db.query(project.Project).filter(project.Project.id == project_id).first()
if db_project is None:
raise HTTPException(status_code=404, detail="Project not found")
db_project.status = "archived"
db.commit()
return RedirectResponse(url=f"/projects/{project_id}", status_code=303)