Files
2026-09-25 20:33:07 +03:00

110 lines
4.3 KiB
Python

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")
def _money(value):
return Decimal(str(value)) if value is not None else ZERO
def _round_money(value):
return value.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
def _performed_cost(item, quantity):
if item.unit_price is None:
return ZERO
return _money(item.unit_price) * _money(quantity)
def build_project_dashboard(db, project_obj):
"""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()
completed_by_item = {}
if item_by_id:
lines = db.query(DailyReportItem, DailyReport).join(
DailyReport, DailyReport.id == DailyReportItem.daily_report_id
).filter(
DailyReport.project_id == project_obj.id,
DailyReportItem.work_item_id.in_(item_by_id),
).all()
for line, _report in lines:
completed_by_item[line.work_item_id] = (
completed_by_item.get(line.work_item_id, ZERO) + _money(line.completed_quantity)
)
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))
remaining_total = planned_total - performed_total
completion_percent = (performed_total / planned_total * Decimal("100")) if planned_total else ZERO
system_progress = []
for system in systems:
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,
"completion_percent": percent,
})
over_plan_items = []
for row in planned_items:
item = row.item
actual_quantity = completed_by_item.get(item.id, ZERO)
if actual_quantity > row.quantity:
over_plan_items.append({
"item": item,
"system": item.system,
"planned_quantity": row.quantity,
"actual_quantity": actual_quantity,
"excess_quantity": actual_quantity - row.quantity,
})
recent_reports = db.query(DailyReport).filter(
DailyReport.project_id == project_obj.id
).order_by(DailyReport.report_date.desc(), DailyReport.id.desc()).limit(5).all()
recent_report_rows = []
for report in recent_reports:
report_lines = db.query(DailyReportItem, boq.WorkItem).join(
boq.WorkItem, boq.WorkItem.id == DailyReportItem.work_item_id
).filter(DailyReportItem.daily_report_id == report.id).all()
report_volume = _round_money(sum((
_performed_cost(item, line.completed_quantity) for line, item in report_lines
), ZERO))
system_names = sorted({item.system.name for _line, item in report_lines if item.system})
recent_report_rows.append({
"report": report,
"systems": system_names or ([report.system.name] if report.system else ["??????? ??? ???????"]),
"item_count": len(report_lines),
"volume": report_volume,
})
return {
"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": len(over_plan_items),
"over_plan_items": over_plan_items,
}