From 54e1404ff7fd8b952b29a959d17b19f886bbcf02 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Tue, 10 Feb 2026 14:38:38 +0530 Subject: [PATCH] Refactor approval process: enhance hold and reject forms, improve status messages, and add approveSave method --- .../CharacteristicApprovalController.php | 188 +++++++++++++++--- 1 file changed, 163 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/CharacteristicApprovalController.php b/app/Http/Controllers/CharacteristicApprovalController.php index 2da4d24..f1b412b 100644 --- a/app/Http/Controllers/CharacteristicApprovalController.php +++ b/app/Http/Controllers/CharacteristicApprovalController.php @@ -8,9 +8,6 @@ use Illuminate\Http\Request; class CharacteristicApprovalController extends Controller { - /** - * Display a listing of the resource. - */ public function approve(Request $request) { return $this->updateStatus($request, 'Approved'); @@ -55,11 +52,30 @@ class CharacteristicApprovalController extends Controller foreach ($levels as $lvl => $column) { if ($lvl != $level && in_array($record->$column, ['Approved', 'Rejected'])) { return view('approval.already-processed', [ - 'status' => 'Already processed by another approver', + 'status' => $record->$column, + 'message' => 'Your request has already been processed by another approver', ]); } } + // foreach ($levels as $lvl => $column) { + // if ($record->$column == 'Hold') { + + // if ($lvl == $level) { + // return view('approval.already-processed', [ + // 'status' => 'On Hold', + // ]); + // } + // else + // { + // return view('approval.already-processed', [ + // 'status' => 'Hold', + // 'message' => 'On Hold', + // ]); + // } + // } + // } + $allowedMailStatusByLevel = [ 1 => 'Sent', 2 => 'Sent-Mail2', @@ -69,8 +85,8 @@ class CharacteristicApprovalController extends Controller $expectedMailStatus = $allowedMailStatusByLevel[$level] ?? null; if ($record->mail_status != $expectedMailStatus) { - return view('approval.already-processed', [ - 'status' => 'You are not authorized to act at this level', + return view('approval.approve-level', [ + 'status' => 'Your approval time limit has expired.', ]); } @@ -80,7 +96,6 @@ class CharacteristicApprovalController extends Controller public function rejectForm(Request $request) { $id = $request->query('id'); - // $level = $request->query('level'); $level = (int) $request->query('level'); $record = RequestCharacteristic::findOrFail($id); @@ -111,11 +126,30 @@ class CharacteristicApprovalController extends Controller foreach ($levels as $lvl => $column) { if ($lvl != $level && in_array($record->$column, ['Approved', 'Rejected'])) { return view('approval.already-processed', [ - 'status' => 'Already processed by another approver', + 'status' => $record->$column, + 'message' => 'Your request has already been processed by another approver', ]); } } + + // foreach ($levels as $lvl => $column) { + // if ($record->$column == 'Hold') { + + // if ($lvl == $level) { + // return view('approval.reject-form', compact('id', 'level')); + // } + // else + // { + // return view('approval.already-processed', [ + // 'status' => 'Hold', + // 'message' => 'On Hold', + // ]); + // } + // } + // } + + $allowedMailStatusByLevel = [ 1 => 'Sent', 2 => 'Sent-Mail2', @@ -125,14 +159,90 @@ class CharacteristicApprovalController extends Controller $expectedMailStatus = $allowedMailStatusByLevel[$level] ?? null; if ($record->mail_status != $expectedMailStatus) { - return view('approval.already-processed', [ - 'status' => 'You are not authorized to act at this level', + return view('approval.approve-level', [ + 'status'=> $currentStatus, + 'message' => 'Your approval time limit has expired.', ]); } + return view('approval.reject-form', compact('id', 'level')); } + public function approveForm(Request $request) + { + $id = $request->query('id'); + $level = (int) $request->query('level'); + + $record = RequestCharacteristic::findOrFail($id); + + [$statusColumn, $approvedAtColumn, $remarkColumn] = match ($level) { + 1 => ['approver_status1', 'approved1_at', 'approver_remark1'], + 2 => ['approver_status2', 'approved2_at', 'approver_remark2'], + 3 => ['approver_status3', 'approved3_at', 'approver_remark3'], + default => abort(403, 'Invalid approver level'), + }; + + $levels = [ + 1 => 'approver_status1', + 2 => 'approver_status2', + 3 => 'approver_status3', + ]; + + $currentStatusColumn = $levels[$level]; + + $currentStatus = $record->$statusColumn; + + if (in_array($currentStatus, ['Approved', 'Rejected'])) { + return view('approval.already-processed', [ + 'status' => $currentStatus, + ]); + } + + foreach ($levels as $lvl => $column) { + if ($lvl != $level && in_array($record->$column, ['Approved', 'Rejected'])) { + return view('approval.already-processed', [ + 'status' => $record->$column, + 'message' => 'Your request has already been processed by another approver', + ]); + } + } + + + // foreach ($levels as $lvl => $column) { + // if ($record->$column == 'Hold') { + + // if ($lvl == $level) { + // return view('approval.reject-form', compact('id', 'level')); + // } + // else + // { + // return view('approval.already-processed', [ + // 'status' => 'Hold', + // 'message' => 'On Hold', + // ]); + // } + // } + // } + + $allowedMailStatusByLevel = [ + 1 => 'Sent', + 2 => 'Sent-Mail2', + 3 => 'Sent-Mail3', + ]; + + $expectedMailStatus = $allowedMailStatusByLevel[$level] ?? null; + + if ($record->mail_status != $expectedMailStatus) { + return view('approval.approve-level', [ + 'status'=> $currentStatus, + 'message' => 'Your approval time limit has expired.', + ]); + } + + return view('approval.approve-form', compact('id', 'level')); + } + public function holdSave(Request $request) { $request->validate([ @@ -155,6 +265,17 @@ class CharacteristicApprovalController extends Controller return $this->updateStatus($request, 'Rejected', false); } + public function approveSave(Request $request) + { + $request->validate([ + 'id' => 'required|integer', + 'level' => 'required|integer', + 'remark' => 'nullable|string', + ]); + + return $this->updateStatus($request, 'Approved', false); + } + protected function updateStatus(Request $request, string $status, bool $returnView = true) { $requestId = $request->input('id'); @@ -189,19 +310,27 @@ class CharacteristicApprovalController extends Controller }) ->get(); - $alreadyProcessed = RequestCharacteristic::whereIn($statusColumn, ['Approved', 'Rejected'])->exists(); + $currentStatus = $record->$statusColumn; - if ($alreadyProcessed) { - if ($returnView) { + $levels = [ + 1 => 'approver_status1', + 2 => 'approver_status2', + 3 => 'approver_status3', + ]; + + if (in_array($currentStatus, ['Approved', 'Rejected'])) { + return view('approval.already-processed', [ + 'status' => $currentStatus, + ]); + } + + foreach ($levels as $lvl => $column) { + if ($lvl != $level && in_array($record->$column, ['Approved', 'Rejected'])) { return view('approval.already-processed', [ - 'status' => 'Already processed', + 'status' => $record->$column, + 'message' => 'your request has already been processed by another approver', ]); } - - return response()->json([ - 'status' => false, - 'message' => 'This request has already been processed.', - ], 404); } $allowedMailStatusByLevel = [ @@ -214,8 +343,9 @@ class CharacteristicApprovalController extends Controller if ($record->mail_status != $expectedMailStatus) { if ($returnView) { - return view('approval.already-processed', [ - 'status' => 'You are not authorized to act at this level', + return view('approval.approve-level', [ + 'status' => $currentStatus, + 'message' => 'Your approval time limit has expired.', ]); } @@ -225,16 +355,20 @@ class CharacteristicApprovalController extends Controller ], 403); } + + + $workflowRecords = RequestCharacteristic::where('plant_id', $record->plant_id) + ->where('machine_id', $record->machine_id) + ->where('aufnr', $record->aufnr) + ->where('work_flow_id', $record->work_flow_id) + ->get(); + $updateData = [ $statusColumn => $status, $remarkColumn => $request->input('remark'), $approvedAtColumn => Carbon::now(), ]; - // if ($status == 'Approved') { - // $updateData[$approvedAtColumn] = Carbon::now(); - // } - foreach ($pendingRecords as $rec) { $rec->update($updateData); } @@ -243,6 +377,10 @@ class CharacteristicApprovalController extends Controller $recd->update($updateData); } + foreach ($workflowRecords as $r) { + $r->update($updateData); + } + if ($returnView) { return match ($status) { 'Approved' => view('approval.success'),