from decimal import Decimal, ROUND_HALF_UP from app.models import boq from app.models.daily_report import DailyReport, DailyReportItem 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 _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 return _money(item.unit_price) * _money(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() 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( 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((_item_plan_cost(item) for item in 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_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) 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 item in items: actual_quantity = completed_by_item.get(item.id, ZERO) if item.quantity is not None and actual_quantity > _money(item.quantity): over_plan_items.append({ "item": item, "system": item.system, "planned_quantity": _money(item.quantity), "actual_quantity": actual_quantity, "excess_quantity": actual_quantity - _money(item.quantity), }) warning_count = len(over_plan_items) 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": current_boq, "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_items": over_plan_items, }