Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Has been cancelled
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Has been cancelled
Laravel Larastan / larastan (pull_request) Has been cancelled
Laravel Pint / pint (pull_request) Has been cancelled
283 lines
8.5 KiB
PHP
283 lines
8.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\RequestCharacteristic;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
|
|
class CharacteristicApprovalController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function approve(Request $request)
|
|
{
|
|
return $this->updateStatus($request, 'Approved');
|
|
}
|
|
|
|
public function reject(Request $request)
|
|
{
|
|
return $this->updateStatus($request, 'Rejected');
|
|
}
|
|
|
|
public function holdForm(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',
|
|
];
|
|
|
|
$currentStatus = $record->$statusColumn;
|
|
|
|
$currentStatusColumn = $levels[$level];
|
|
|
|
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 by another approver',
|
|
]);
|
|
}
|
|
}
|
|
|
|
$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.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'),
|
|
};
|
|
|
|
$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' => 'Already processed by another approver',
|
|
]);
|
|
}
|
|
}
|
|
|
|
$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.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, 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);
|
|
}
|
|
|
|
$allowedMailStatusByLevel = [
|
|
1 => 'Sent',
|
|
2 => 'Sent-Mail2',
|
|
3 => 'Sent-Mail3',
|
|
];
|
|
|
|
$expectedMailStatus = $allowedMailStatusByLevel[$level] ?? null;
|
|
|
|
if ($record->mail_status != $expectedMailStatus) {
|
|
if ($returnView) {
|
|
return view('approval.already-processed', [
|
|
'status' => 'You are not authorized to act at this level',
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Invalid approval level for current mail status',
|
|
], 403);
|
|
}
|
|
|
|
$updateData = [
|
|
$statusColumn => $status,
|
|
$remarkColumn => $request->input('remark'),
|
|
$approvedAtColumn => Carbon::now(),
|
|
];
|
|
|
|
// 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)
|
|
{
|
|
//
|
|
}
|
|
}
|