feat: add agreements foundation

This commit is contained in:
Александр Арсентьев
2026-09-25 18:41:08 +03:00
parent 2376778828
commit d0b5745f8f
14 changed files with 697 additions and 61 deletions
+20 -19
View File
@@ -9,6 +9,7 @@ from sqlalchemy.orm import Session
from app.database.database import SessionLocal
from app.models import boq, project
from app.models.daily_report import DailyReport, DailyReportItem
from app.services.effective_plan import build_effective_plan
router = APIRouter()
@@ -46,14 +47,17 @@ def _report_rows(db: Session, project_id: int, system_id: int, exclude_report_id
)
if exclude_report_id is not None:
totals = totals.filter(DailyReport.id != exclude_report_id)
totals = totals.group_by(DailyReportItem.work_item_id).subquery()
items = db.query(boq.WorkItem, func.coalesce(totals.c.completed, 0)).outerjoin(
totals, totals.c.work_item_id == boq.WorkItem.id
).filter(
boq.WorkItem.project_id == project_id,
boq.WorkItem.system_id == system_id,
).order_by(boq.WorkItem.section_id, boq.WorkItem.id).all()
return [{"item": item, "accumulated": Decimal(str(completed or 0))} for item, completed in items]
totals = {item_id: Decimal(str(value or 0)) for item_id, value in
totals.group_by(DailyReportItem.work_item_id).all()}
plan = build_effective_plan(db, project_id)
rows = []
for planned in plan["items"]:
item = planned.item
if item.system_id == system_id:
rows.append({"item": item, "planned_quantity": planned.quantity,
"accumulated": totals.get(item.id, Decimal("0"))})
rows.sort(key=lambda row: (row["item"].section_id or 0, row["item"].id))
return rows
def _render_form(request, db, project_obj, system_id=None, report=None, errors=None):
@@ -75,9 +79,9 @@ def _render_form(request, db, project_obj, system_id=None, report=None, errors=N
rows = _report_rows(db, project_obj.id, system_id, exclude_id)
for row in rows:
row["current_quantity"] = current_quantities.get(row["item"].id)
planned = row["item"].quantity
planned = row["planned_quantity"]
current = Decimal(str(row["current_quantity"] or 0))
row["over_plan"] = planned is not None and row["accumulated"] + current > Decimal(str(planned))
row["over_plan"] = row["accumulated"] + current > planned
return request.app.templates.TemplateResponse("projects/daily_report_form.html", {
"request": request, "project": project_obj, "systems": systems,
"selected_system": selected_system, "rows": rows, "report": report,
@@ -144,9 +148,9 @@ async def view_daily_report(request: Request, project_id: int, report_id: int,
if line is None:
continue
quantity = Decimal(str(line.completed_quantity))
planned = row["item"].quantity
planned = row["planned_quantity"]
row.update(current_quantity=quantity,
over_plan=planned is not None and row["accumulated"] + quantity > Decimal(str(planned)))
over_plan=row["accumulated"] + quantity > planned)
if row["over_plan"]:
warnings.append(row)
displayed.append(row)
@@ -215,10 +219,7 @@ def _quantities_from_form(form):
def _validate_work_items(db, project_id, system_id, quantities):
valid_ids = {row[0] for row in db.query(boq.WorkItem.id).filter(
boq.WorkItem.project_id == project_id,
boq.WorkItem.system_id == system_id,
boq.WorkItem.id.in_(quantities.keys()),
).all()}
if valid_ids != set(quantities):
raise HTTPException(status_code=400, detail="Одна или несколько позиций не принадлежат выбранной системе проекта")
effective_ids = {row.item.id for row in build_effective_plan(db, project_id)["items"]
if row.item.system_id == system_id}
if effective_ids.intersection(quantities) != set(quantities):
raise HTTPException(status_code=400, detail="One or more work items are not part of the selected system effective plan")
+5 -1
View File
@@ -5,6 +5,7 @@ from app.database import database
from app.models import project
from app.models import boq
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.work_journal import WorkJournalEntry
from typing import List
@@ -106,7 +107,10 @@ async def read_project(request: Request, project_id: int, db: Session = Depends(
boq_docs = db.query(boq.BoqDocument).filter(
boq.BoqDocument.project_id == project_id
).order_by(boq.BoqDocument.created_at.desc(), boq.BoqDocument.id.desc()).all()
dashboard = build_project_dashboard(db, db_project)
try:
dashboard = build_project_dashboard(db, db_project)
except BaseBoqConfigurationError as exc:
raise HTTPException(status_code=409, detail=str(exc)) from exc
daily_reports = db.query(DailyReport).filter(DailyReport.project_id == project_id).count()
journal_tasks = db.query(WorkJournalEntry).filter(
WorkJournalEntry.project_id == project_id,