Add CharacteristicApprovalController with approval, hold, and reject functionalities #266
327
app/Http/Controllers/CharacteristicApprovalController.php
Normal file
327
app/Http/Controllers/CharacteristicApprovalController.php
Normal file
@@ -0,0 +1,327 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\RequestCharacteristic;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
|
||||||
|
class CharacteristicApprovalController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Display a listing of the resource.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function approve(Request $request)
|
||||||
|
{
|
||||||
|
return $this->updateStatus($request, 'Approved');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HOLD
|
||||||
|
*/
|
||||||
|
// public function hold(Request $request)
|
||||||
|
// {
|
||||||
|
// return $this->updateStatus($request, 'Hold');
|
||||||
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* REJECT
|
||||||
|
*/
|
||||||
|
public function reject(Request $request)
|
||||||
|
{
|
||||||
|
return $this->updateStatus($request, 'Rejected');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function holdForm(Request $request)
|
||||||
|
{
|
||||||
|
$id = $request->query('id');
|
||||||
|
// $level = $request->query('level');
|
||||||
|
|
||||||
|
$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'),
|
||||||
|
};
|
||||||
|
|
||||||
|
$currentStatus = $record->$statusColumn;
|
||||||
|
|
||||||
|
if (in_array($currentStatus, ['Approved', 'Rejected'])) {
|
||||||
|
return view('approval.already-processed', [
|
||||||
|
'status' => $currentStatus
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return view('approval.hold-form', compact('id', 'level'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rejectForm(Request $request){
|
||||||
|
$id = $request->query('id');
|
||||||
|
// $level = $request->query('level');
|
||||||
|
$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'),
|
||||||
|
};
|
||||||
|
|
||||||
|
$currentStatus = $record->$statusColumn;
|
||||||
|
|
||||||
|
if (in_array($currentStatus, ['Approved', 'Rejected'])) {
|
||||||
|
return view('approval.already-processed', [
|
||||||
|
'status' => $currentStatus
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return view('approval.reject-form', compact('id', 'level'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function holdSave(Request $request)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'id' => 'required|integer',
|
||||||
|
'level' => 'required|integer',
|
||||||
|
'remark' => 'required|string',
|
||||||
|
]);
|
||||||
|
return $this->updateStatus($request, 'Hold', false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rejectSave(Request $request){
|
||||||
|
$request->validate([
|
||||||
|
'id' => 'required|integer',
|
||||||
|
'level' => 'required|integer',
|
||||||
|
'remark' => 'required|string',
|
||||||
|
]);
|
||||||
|
return $this->updateStatus($request, 'Rejected', false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// protected function updateStatus(Request $request, string $status)
|
||||||
|
// {
|
||||||
|
// $requestId = $request->query('id');
|
||||||
|
// $level = (int) $request->query('level');
|
||||||
|
|
||||||
|
// $record = RequestCharacteristic::findOrFail($requestId);
|
||||||
|
|
||||||
|
// $column = match ($level) {
|
||||||
|
// 1 => 'approver_status1',
|
||||||
|
// 2 => 'approver_status2',
|
||||||
|
// 3 => 'approver_status3',
|
||||||
|
// default => abort(403, 'Invalid approver level'),
|
||||||
|
// };
|
||||||
|
|
||||||
|
// $pendingRecords = RequestCharacteristic::where('plant_id', $record->plant_id)
|
||||||
|
// ->where('machine_id', $record->machine_id)
|
||||||
|
// ->where('aufnr', $record->aufnr)
|
||||||
|
// ->whereNull('approver_status1')
|
||||||
|
// ->whereNull('approver_status2')
|
||||||
|
// ->whereNull('approver_status3')
|
||||||
|
// ->get();
|
||||||
|
|
||||||
|
// if ($pendingRecords->isEmpty()) {
|
||||||
|
// return view('approval.already-processed', [
|
||||||
|
// 'status' => 'No pending records for this group'
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if ($pendingRecords->first()->$column != null) {
|
||||||
|
// return view('approval.already-processed', [
|
||||||
|
// 'status' => $pendingRecords->first()->$column
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Update all records in the group for this approver level
|
||||||
|
// foreach ($pendingRecords as $rec) {
|
||||||
|
// $rec->update([$column => $status]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return match ($status) {
|
||||||
|
// 'Approved' => view('approval.success'),
|
||||||
|
// 'Hold' => view('approval.hold-success'),
|
||||||
|
// 'Rejected' => view('approval.reject-success'),
|
||||||
|
// default => abort(500),
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// protected function updateStatus(Request $request, string $status)
|
||||||
|
// {
|
||||||
|
// $requestId = $request->query('id');
|
||||||
|
// $level = (int) $request->query('level');
|
||||||
|
|
||||||
|
// $record = RequestCharacteristic::findOrFail($requestId);
|
||||||
|
|
||||||
|
// [$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'),
|
||||||
|
// };
|
||||||
|
|
||||||
|
// $pendingRecords = 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)
|
||||||
|
// ->whereNull('approver_status1')
|
||||||
|
// ->whereNull('approver_status2')
|
||||||
|
// ->whereNull('approver_status3')
|
||||||
|
// ->get();
|
||||||
|
|
||||||
|
// if ($pendingRecords->isEmpty()) {
|
||||||
|
// return view('approval.already-processed', [
|
||||||
|
// 'status' => 'No pending records for this group',
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if ($pendingRecords->first()->$statusColumn !== null) {
|
||||||
|
// return view('approval.already-processed', [
|
||||||
|
// 'status' => $pendingRecords->first()->$statusColumn,
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// $updateData = [
|
||||||
|
// $statusColumn => $status,
|
||||||
|
// $remarkColumn => $request->input('remark')
|
||||||
|
// ];
|
||||||
|
|
||||||
|
// if ($status == 'Approved') {
|
||||||
|
// $updateData[$approvedAtColumn] = Carbon::now();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// foreach ($pendingRecords as $rec) {
|
||||||
|
// $rec->update($updateData);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return match ($status) {
|
||||||
|
// 'Approved' => view('approval.success'),
|
||||||
|
// 'Hold' => view('approval.hold-success'),
|
||||||
|
// 'Rejected' => view('approval.reject-success'),
|
||||||
|
// default => abort(500),
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
protected function updateStatus(Request $request, string $status, bool $returnView = true)
|
||||||
|
{
|
||||||
|
$requestId = $request->input('id');
|
||||||
|
$level = (int) $request->input('level');
|
||||||
|
|
||||||
|
$record = RequestCharacteristic::findOrFail($requestId);
|
||||||
|
|
||||||
|
[$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'),
|
||||||
|
};
|
||||||
|
|
||||||
|
$pendingRecords = 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)
|
||||||
|
->whereNull('approver_status1')
|
||||||
|
->whereNull('approver_status2')
|
||||||
|
->whereNull('approver_status3')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$processRecords = 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)
|
||||||
|
->where(function ($query) {
|
||||||
|
$query->whereNotNull('approver_status1')
|
||||||
|
->orWhereNotNull('approver_status2')
|
||||||
|
->orWhereNotNull('approver_status3');
|
||||||
|
})
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$alreadyProcessed = RequestCharacteristic::whereIn($statusColumn, ['Approved', 'Rejected'])->exists();
|
||||||
|
|
||||||
|
if ($alreadyProcessed) {
|
||||||
|
if ($returnView) {
|
||||||
|
return view('approval.already-processed', [
|
||||||
|
'status' => 'Already processed'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'status' => false,
|
||||||
|
'message' => 'This request has already been processed.'
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$updateData = [
|
||||||
|
$statusColumn => $status,
|
||||||
|
$remarkColumn => $request->input('remark')
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($status == 'Approved') {
|
||||||
|
$updateData[$approvedAtColumn] = Carbon::now();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($pendingRecords as $rec) {
|
||||||
|
$rec->update($updateData);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($processRecords as $recd) {
|
||||||
|
$recd->update($updateData);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($returnView) {
|
||||||
|
return match ($status) {
|
||||||
|
'Approved' => view('approval.success'),
|
||||||
|
'Hold' => view('approval.hold-success'),
|
||||||
|
'Rejected' => view('approval.reject-success'),
|
||||||
|
default => abort(500),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json(['status' => true, 'message' => 'Status updated successfully']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a newly created resource in storage.
|
||||||
|
*/
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*/
|
||||||
|
public function show(string $id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the specified resource in storage.
|
||||||
|
*/
|
||||||
|
public function update(Request $request, string $id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the specified resource from storage.
|
||||||
|
*/
|
||||||
|
public function destroy(string $id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user