|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Http\Controllers\Api; |
| 4 | + |
| 5 | +use ProcessMaker\Http\Controllers\Controller; |
| 6 | +use ProcessMaker\Models\Process; |
| 7 | +use ProcessMaker\Models\ProcessRequest; |
| 8 | +use ProcessMaker\Models\ProcessRequestToken; |
| 9 | + |
| 10 | +class CaseController extends Controller |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Get stage information for cases |
| 14 | + */ |
| 15 | + public function getStagePerCase($case_number = null) |
| 16 | + { |
| 17 | + if (!empty($case_number)) { |
| 18 | + $responseData = $this->getSpecificCaseStages($case_number); |
| 19 | + |
| 20 | + return response()->json($responseData); |
| 21 | + } |
| 22 | + |
| 23 | + $responseData = [ |
| 24 | + 'parentRequest' => [], |
| 25 | + 'requestCount' => 0, |
| 26 | + 'all_stages' => [], |
| 27 | + 'current_stage' => [], |
| 28 | + 'stages_per_case' => $this->getDefaultCaseStages(), |
| 29 | + ]; |
| 30 | + |
| 31 | + return response()->json($responseData); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Get specific case stages information |
| 36 | + * @param string $caseNumber The unique identifier of the case to retrieve stages for |
| 37 | + * @return array |
| 38 | + */ |
| 39 | + private function getSpecificCaseStages($caseNumber) |
| 40 | + { |
| 41 | + $allRequests = ProcessRequest::where('case_number', $caseNumber)->get(); |
| 42 | + // Check if any requests were found |
| 43 | + if ($allRequests->isEmpty()) { |
| 44 | + return $this->getDefaultCaseStages(); |
| 45 | + } |
| 46 | + $parentRequest = null; |
| 47 | + $requestCount = $allRequests->count(); |
| 48 | + // Search the parent request parent_request_id and load $request |
| 49 | + foreach ($allRequests as $request) { |
| 50 | + if (is_null($request->parent_request_id)) { |
| 51 | + $parentRequest = $request; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + $stagesPerCase = $this->getStagesSummary($parentRequest); |
| 57 | + |
| 58 | + return [ |
| 59 | + 'parentRequest' => [ |
| 60 | + 'id' => $parentRequest->id, |
| 61 | + 'case_number' => $parentRequest->case_number, |
| 62 | + 'status' => $parentRequest->status, |
| 63 | + 'completed_at' => $parentRequest->completed_at, |
| 64 | + ], |
| 65 | + 'requestCount' => $requestCount, |
| 66 | + 'all_stages' => [], |
| 67 | + 'current_stage' => [], |
| 68 | + 'stages_per_case' => $stagesPerCase, |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get default case stages with status handling |
| 74 | + * |
| 75 | + * @param string|null $status The status to set for the stages |
| 76 | + * @return array |
| 77 | + */ |
| 78 | + private function getDefaultCaseStages($status = null) |
| 79 | + { |
| 80 | + return [ |
| 81 | + [ |
| 82 | + 'id' => 0, |
| 83 | + 'name' => 'In Progress', |
| 84 | + 'status' => $this->mapStatus($status, 'In Progress'), |
| 85 | + 'completed_at' => '', |
| 86 | + ], |
| 87 | + [ |
| 88 | + 'id' => 0, |
| 89 | + 'name' => 'Completed', |
| 90 | + 'status' => $this->mapStatus($status, 'Completed'), |
| 91 | + 'completed_at' => '', |
| 92 | + ], |
| 93 | + ]; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Map the status for each stage based on the input status |
| 98 | + * |
| 99 | + * @param string|null $status The input status to map |
| 100 | + * @param string $stageName The name of the stage ('In Progress' or 'Completed') |
| 101 | + * @return string The mapped status |
| 102 | + */ |
| 103 | + private function mapStatus($status, $stageName) |
| 104 | + { |
| 105 | + if ($status === 'COMPLETED') { |
| 106 | + return 'Done'; |
| 107 | + } |
| 108 | + |
| 109 | + if ($status === 'ACTIVE') { |
| 110 | + return match ($stageName) { |
| 111 | + 'In Progress' => 'In Progress', |
| 112 | + 'Completed' => 'Pending', |
| 113 | + default => 'Pending' |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + return 'Pending'; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Get the stages summary based on the provided request. |
| 122 | + * |
| 123 | + * @param $requestId |
| 124 | + * @return array An array of stage results, each containing the stage ID, name, status, |
| 125 | + * and completion date. |
| 126 | + */ |
| 127 | + private function getStagesSummary(ProcessRequest $request) |
| 128 | + { |
| 129 | + $requestId = $request->id; |
| 130 | + $processId = $request->process_id; |
| 131 | + $process = Process::where('id', $processId)->first(); |
| 132 | + if ($process && !empty($process->stages)) { |
| 133 | + $allStages = $process->stages; |
| 134 | + } else { |
| 135 | + // Return the default stages if the process does not have |
| 136 | + return $this->getDefaultCaseStages($request->status); |
| 137 | + } |
| 138 | + |
| 139 | + $allCurrentStages = ProcessRequestToken::where('process_request_id', $requestId) |
| 140 | + ->select('stage_id', 'stage_name', 'status', 'completed_at') |
| 141 | + ->get() |
| 142 | + ->toArray(); |
| 143 | + if (empty($allCurrentStages)) { |
| 144 | + // TO_DO: define what happen if the process does not have task, is a valid use case |
| 145 | + } |
| 146 | + |
| 147 | + // Helper to map status |
| 148 | + $mapStatus = function ($status) { |
| 149 | + if ($status === 'CLOSED') { |
| 150 | + return 'Done'; |
| 151 | + } elseif ($status === 'ACTIVE') { |
| 152 | + return 'In Progress'; |
| 153 | + } else { |
| 154 | + return 'Pending'; |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + $stageResult = []; |
| 159 | + // Initialize stage counts with zero for all stages |
| 160 | + foreach ($allStages as $stage) { |
| 161 | + $stageData = [ |
| 162 | + 'id' => $stage['id'], |
| 163 | + 'name' => $stage['name'], |
| 164 | + 'status' => 'Pending', |
| 165 | + 'completed_at' => '', |
| 166 | + ]; |
| 167 | + |
| 168 | + foreach ($allCurrentStages as $task) { |
| 169 | + if ($task['stage_id'] === $stage['id']) { |
| 170 | + $stageData['status'] = $mapStatus($task['status']); |
| 171 | + $stageData['completed_at'] = $task['completed_at'] ?? ''; |
| 172 | + break; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + $stageResult[] = $stageData; |
| 177 | + } |
| 178 | + |
| 179 | + return $stageResult; |
| 180 | + } |
| 181 | +} |
0 commit comments