chore: update document approval workflow and bug fixes
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\ApprovalWorkflow\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Modules\ApprovalWorkflow\Models\ApprovalChain;
|
||||
use Modules\ApprovalWorkflow\Services\ApprovalService;
|
||||
|
||||
class ApprovalController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ApprovalService $approvalService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Show pending approvals for the current user.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = $request->user();
|
||||
$tab = $request->get('tab', 'pending');
|
||||
|
||||
$query = ApprovalChain::query();
|
||||
|
||||
if ($tab === 'history') {
|
||||
$query->whereHas('steps', function ($q) use ($user) {
|
||||
$q->where('approver_id', $user->id)->where('status', '!=', 'pending');
|
||||
});
|
||||
} else {
|
||||
$query->whereHas('steps', function ($q) use ($user) {
|
||||
$q->where('approver_id', $user->id)->where('status', 'pending');
|
||||
})->whereIn('status', ['pending', 'in_review']);
|
||||
}
|
||||
|
||||
$chains = $query->with(['steps.approver:id,name', 'initiator:id,name'])
|
||||
->latest()
|
||||
->paginate(15)
|
||||
->withQueryString();
|
||||
|
||||
return Inertia::render('ApprovalWorkflow::Approvals/Index', [
|
||||
'approvals' => $chains,
|
||||
'tab' => $tab,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a specific approval chain.
|
||||
*/
|
||||
public function show(ApprovalChain $approvalChain)
|
||||
{
|
||||
$approvalChain->load(['steps.approver:id,name,email', 'initiator:id,name,email', 'approvable']);
|
||||
|
||||
$breakdownData = null;
|
||||
|
||||
if ($approvalChain->approvable) {
|
||||
if (method_exists($approvalChain->approvable, 'items')) {
|
||||
$approvalChain->approvable->load('items');
|
||||
}
|
||||
|
||||
$breakdownData = [
|
||||
'document_number' => $approvalChain->approvable->document_number ?? $approvalChain->approvable->po_number ?? null,
|
||||
'total_cost' => $approvalChain->approvable->total_cost ?? 0,
|
||||
'notes' => $approvalChain->approvable->notes ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
return Inertia::render('ApprovalWorkflow::Approvals/Show', [
|
||||
'chain' => $approvalChain,
|
||||
'breakdownData' => $breakdownData,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve the current step.
|
||||
*/
|
||||
public function approve(Request $request, ApprovalChain $approvalChain)
|
||||
{
|
||||
$request->validate(['notes' => 'nullable|string|max:1000']);
|
||||
|
||||
try {
|
||||
$this->approvalService->approve($approvalChain, $request->user(), $request->notes);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
return back()->with('error', $e->getMessage());
|
||||
}
|
||||
|
||||
return back()->with('success', 'Step approved successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject the current step (rejects entire chain).
|
||||
*/
|
||||
public function reject(Request $request, ApprovalChain $approvalChain)
|
||||
{
|
||||
$request->validate(['notes' => 'required|string|max:1000']);
|
||||
|
||||
try {
|
||||
$this->approvalService->reject($approvalChain, $request->user(), $request->notes);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
return back()->with('error', $e->getMessage());
|
||||
}
|
||||
|
||||
return back()->with('success', 'Approval rejected.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user