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:
+65
-1
@@ -1,9 +1,10 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, Form, UploadFile, File
|
||||
from fastapi.responses import RedirectResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from app.database import database
|
||||
from app.models import project, boq
|
||||
from app.models.daily_report import DailyReport, DailyReportItem
|
||||
from app.importer import excel_importer
|
||||
from typing import List
|
||||
import os
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
@@ -11,6 +12,19 @@ from pathlib import Path
|
||||
router = APIRouter()
|
||||
UPLOAD_DIR = Path(__file__).resolve().parents[1] / "uploads"
|
||||
|
||||
|
||||
def _completed_quantities(db, project_id, items):
|
||||
item_ids = [item.id for item in items]
|
||||
if not item_ids:
|
||||
return {}
|
||||
rows = db.query(DailyReportItem.work_item_id, DailyReportItem.completed_quantity).join(
|
||||
DailyReport, DailyReport.id == DailyReportItem.daily_report_id
|
||||
).filter(DailyReport.project_id == project_id, DailyReportItem.work_item_id.in_(item_ids)).all()
|
||||
totals = {}
|
||||
for wid, qty in rows:
|
||||
totals[wid] = totals.get(wid, Decimal("0")) + Decimal(str(qty or 0))
|
||||
return totals
|
||||
|
||||
# Dependency
|
||||
def get_db():
|
||||
db = database.SessionLocal()
|
||||
@@ -79,8 +93,58 @@ async def view_boq_document(request: Request, project_id: int, document_id: int,
|
||||
"document_title": document_title,
|
||||
"systems": tree, "total": total,
|
||||
"unassigned_items": [item for item in items if item.system_id is None or item.section_id is None],
|
||||
"completed_by_item": _completed_quantities(db, project_id, items),
|
||||
"has_history": _has_history(db, items),
|
||||
})
|
||||
|
||||
|
||||
def _has_history(db, items):
|
||||
"""Check if BOQ items participate in DailyReports / KS2 / KS3 / Payments."""
|
||||
item_ids = [i.id for i in items]
|
||||
if not item_ids:
|
||||
return False
|
||||
if db.query(DailyReportItem).filter(DailyReportItem.work_item_id.in_(item_ids)).first():
|
||||
return True
|
||||
from app.models.ks2 import KS2Item
|
||||
if db.query(KS2Item).filter(KS2Item.work_item_id.in_(item_ids)).first():
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@router.post("/{project_id}/documents/{document_id}/delete")
|
||||
async def delete_boq_document(project_id: int, document_id: int, db: Session = Depends(get_db)):
|
||||
document = db.query(boq.BoqDocument).filter(
|
||||
boq.BoqDocument.id == document_id, boq.BoqDocument.project_id == project_id
|
||||
).first()
|
||||
if document is None:
|
||||
raise HTTPException(status_code=404, detail="Document not found")
|
||||
items = db.query(boq.WorkItem).filter(boq.WorkItem.boq_document_id == document_id).all()
|
||||
if _has_history(db, items):
|
||||
raise HTTPException(status_code=400, detail="Cannot delete document with history. Archive it instead.")
|
||||
try:
|
||||
for item in items:
|
||||
db.delete(item)
|
||||
db.delete(document)
|
||||
db.commit()
|
||||
except Exception:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail="Cannot delete document with related data")
|
||||
return RedirectResponse(f"/boq/{project_id}", status_code=303)
|
||||
|
||||
|
||||
@router.post("/{project_id}/documents/{document_id}/archive")
|
||||
async def archive_boq_document(project_id: int, document_id: int, db: Session = Depends(get_db)):
|
||||
document = db.query(boq.BoqDocument).filter(
|
||||
boq.BoqDocument.id == document_id, boq.BoqDocument.project_id == project_id
|
||||
).first()
|
||||
if document is None:
|
||||
raise HTTPException(status_code=404, detail="Document not found")
|
||||
if document.version == "archived":
|
||||
raise HTTPException(status_code=400, detail="Already archived")
|
||||
document.version = "archived"
|
||||
db.commit()
|
||||
return RedirectResponse(f"/boq/{project_id}/documents/{document_id}", status_code=303)
|
||||
|
||||
@router.post("/{project_id}/import/upload")
|
||||
async def upload_excel_file(
|
||||
request: Request,
|
||||
|
||||
Reference in New Issue
Block a user