authorizeMaintenanceAccess('maintenance.equipment.view'); $business_id = $this->maintenanceUtil->getBusinessId(); $equipment = Equipment::where('business_id', $business_id) ->where('customer_id', $contact_id) ->with(['category', 'productionLine', 'project']) ->orderByDesc('created_at') ->get() ->map(function ($eq) { $arr = $eq->toArray(); $arr['status_label'] = $this->maintenanceUtil->statusLabel($eq->status); return $this->maintenanceUtil->appendJalaliDatesToApiPayload($arr); }); $projects = class_exists(Project::class) ? Project::where('business_id', $business_id)->where('contact_id', $contact_id)->get(['id', 'name']) : collect(); $projectIds = $projects->pluck('id')->filter(); $lines = ProductionLine::where('business_id', $business_id) ->when($projectIds->isNotEmpty(), fn ($q) => $q->whereIn('project_id', $projectIds)) ->when($projectIds->isEmpty(), fn ($q) => $q->whereRaw('1 = 0')) ->with('project:id,name') ->get(); $openWorkOrders = WorkOrder::where('business_id', $business_id) ->whereIn('status', ['open', 'in_progress', 'draft']) ->whereHas('equipment', fn ($q) => $q->where('customer_id', $contact_id)) ->count(); return response()->json([ 'equipment' => $equipment, 'projects' => $projects, 'lines' => $lines, 'open_work_orders' => $openWorkOrders, ]); } }