feat: add agreements foundation
This commit is contained in:
@@ -2,6 +2,7 @@ from decimal import Decimal, ROUND_HALF_UP
|
||||
|
||||
from app.models import boq
|
||||
from app.models.daily_report import DailyReport, DailyReportItem
|
||||
from app.services.effective_plan import build_effective_plan
|
||||
|
||||
ZERO = Decimal("0.00")
|
||||
|
||||
@@ -14,14 +15,6 @@ def _round_money(value):
|
||||
return value.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
|
||||
|
||||
|
||||
def _item_plan_cost(item):
|
||||
if item.amount is not None:
|
||||
return _money(item.amount)
|
||||
if item.unit_price is not None and item.quantity is not None:
|
||||
return _money(item.unit_price) * _money(item.quantity)
|
||||
return ZERO
|
||||
|
||||
|
||||
def _performed_cost(item, quantity):
|
||||
if item.unit_price is None:
|
||||
return ZERO
|
||||
@@ -29,22 +22,15 @@ def _performed_cost(item, quantity):
|
||||
|
||||
|
||||
def build_project_dashboard(db, project_obj):
|
||||
"""Build project dashboard aggregates from persisted BOQ and daily report rows."""
|
||||
current_boq = db.query(boq.BoqDocument).filter(
|
||||
boq.BoqDocument.project_id == project_obj.id
|
||||
).order_by(boq.BoqDocument.created_at.desc(), boq.BoqDocument.id.desc()).first()
|
||||
|
||||
"""Build plan from base BOQ + approved changes; keep daily-report facts canonical."""
|
||||
effective = build_effective_plan(db, project_obj.id)
|
||||
planned_items = effective["items"]
|
||||
items = [row.item for row in planned_items]
|
||||
item_by_id = {item.id: item for item in items}
|
||||
systems = db.query(boq.System).filter(
|
||||
boq.System.project_id == project_obj.id
|
||||
).order_by(boq.System.sort_order, boq.System.id).all()
|
||||
|
||||
items = []
|
||||
if current_boq:
|
||||
items = db.query(boq.WorkItem).filter(
|
||||
boq.WorkItem.boq_document_id == current_boq.id
|
||||
).order_by(boq.WorkItem.id).all()
|
||||
item_by_id = {item.id: item for item in items}
|
||||
|
||||
completed_by_item = {}
|
||||
if item_by_id:
|
||||
lines = db.query(DailyReportItem, DailyReport).join(
|
||||
@@ -58,7 +44,7 @@ def build_project_dashboard(db, project_obj):
|
||||
completed_by_item.get(line.work_item_id, ZERO) + _money(line.completed_quantity)
|
||||
)
|
||||
|
||||
planned_total = _round_money(sum((_item_plan_cost(item) for item in items), ZERO))
|
||||
planned_total = _round_money(sum((row.amount for row in planned_items), ZERO))
|
||||
performed_total = _round_money(sum((
|
||||
_performed_cost(item, completed_by_item.get(item.id, ZERO)) for item in items
|
||||
), ZERO))
|
||||
@@ -67,10 +53,10 @@ def build_project_dashboard(db, project_obj):
|
||||
|
||||
system_progress = []
|
||||
for system in systems:
|
||||
system_items = [item for item in items if item.system_id == system.id]
|
||||
plan = sum((_item_plan_cost(item) for item in system_items), ZERO)
|
||||
actual = sum((_performed_cost(item, completed_by_item.get(item.id, ZERO))
|
||||
for item in system_items), ZERO)
|
||||
system_rows = [row for row in planned_items if row.item.system_id == system.id]
|
||||
plan = sum((row.amount for row in system_rows), ZERO)
|
||||
actual = sum((_performed_cost(row.item, completed_by_item.get(row.item.id, ZERO))
|
||||
for row in system_rows), ZERO)
|
||||
percent = actual / plan * Decimal("100") if plan else ZERO
|
||||
system_progress.append({
|
||||
"system": system, "planned_cost": plan, "performed_cost": actual,
|
||||
@@ -78,17 +64,17 @@ def build_project_dashboard(db, project_obj):
|
||||
})
|
||||
|
||||
over_plan_items = []
|
||||
for item in items:
|
||||
for row in planned_items:
|
||||
item = row.item
|
||||
actual_quantity = completed_by_item.get(item.id, ZERO)
|
||||
if item.quantity is not None and actual_quantity > _money(item.quantity):
|
||||
if actual_quantity > row.quantity:
|
||||
over_plan_items.append({
|
||||
"item": item,
|
||||
"system": item.system,
|
||||
"planned_quantity": _money(item.quantity),
|
||||
"planned_quantity": row.quantity,
|
||||
"actual_quantity": actual_quantity,
|
||||
"excess_quantity": actual_quantity - _money(item.quantity),
|
||||
"excess_quantity": actual_quantity - row.quantity,
|
||||
})
|
||||
warning_count = len(over_plan_items)
|
||||
|
||||
recent_reports = db.query(DailyReport).filter(
|
||||
DailyReport.project_id == project_obj.id
|
||||
@@ -110,13 +96,14 @@ def build_project_dashboard(db, project_obj):
|
||||
})
|
||||
|
||||
return {
|
||||
"current_boq": current_boq,
|
||||
"current_boq": effective["base_boq"],
|
||||
"base_boq_documents": effective["base_boq_documents"],
|
||||
"planned_cost": planned_total,
|
||||
"performed_cost": performed_total,
|
||||
"remaining_cost": remaining_total,
|
||||
"completion_percent": completion_percent,
|
||||
"system_progress": system_progress,
|
||||
"recent_reports": recent_report_rows,
|
||||
"over_plan_count": warning_count,
|
||||
"over_plan_count": len(over_plan_items),
|
||||
"over_plan_items": over_plan_items,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user