Files
GSB-Construction/Modules/ApprovalWorkflow/app/Services/ApprovalService.php
Ajjj 936ae1c6b2
Some checks failed
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled
Tests / PHP 8.5 (push) Has been cancelled
feat: implement comprehensive dashboard controller and modular administrative and project management interfaces
2026-08-03 18:28:52 +08:00

134 lines
4.3 KiB
PHP

<?php
namespace Modules\ApprovalWorkflow\Services;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Modules\ApprovalWorkflow\Enums\ApprovalChainStatus;
use Modules\ApprovalWorkflow\Enums\ApprovalStepStatus;
use Modules\ApprovalWorkflow\Events\ApprovalCompleted;
use Modules\ApprovalWorkflow\Events\ApprovalRequired;
use Modules\ApprovalWorkflow\Events\StepApproved;
use Modules\ApprovalWorkflow\Events\StepRejected;
use Modules\ApprovalWorkflow\Models\ApprovalChain;
use Modules\ApprovalWorkflow\Models\ApprovalStep;
class ApprovalService
{
/**
* Create a new approval chain for any morphable model.
*
* @param array<int, int> $approverIds — ordered list of user IDs
*/
public function createChain(
Model $approvable,
array $approverIds,
string $type = 'general',
?int $initiatedBy = null,
?string $notes = null,
): ApprovalChain {
$chain = ApprovalChain::create([
'approvable_type' => $approvable->getMorphClass(),
'approvable_id' => $approvable->getKey(),
'type' => $type,
'status' => ApprovalChainStatus::Pending,
'initiated_by' => $initiatedBy,
'notes' => $notes,
]);
foreach ($approverIds as $order => $userId) {
ApprovalStep::create([
'approval_chain_id' => $chain->id,
'approver_id' => $userId,
'order' => $order,
'status' => ApprovalStepStatus::Pending,
]);
}
// Auto-transition to InReview and notify first approver
$chain->transitionTo(ApprovalChainStatus::InReview);
$firstStep = $chain->currentStep();
if ($firstStep) {
ApprovalRequired::dispatch($chain, $firstStep);
}
return $chain->load('steps.approver');
}
/**
* Approve the current pending step.
*/
public function approve(ApprovalChain $chain, User $approver, ?string $notes = null): ApprovalChain
{
$step = $this->getCurrentStepForApprover($chain, $approver);
$step->update([
'approver_id' => $approver->id,
'status' => ApprovalStepStatus::Approved,
'notes' => $notes,
'acted_at' => now(),
]);
StepApproved::dispatch($chain, $step);
// One admin/manager approval completes the chain for estimations, requisitions, and single-step requests
$chain->steps()->where('status', ApprovalStepStatus::Pending)->update([
'status' => ApprovalStepStatus::Skipped,
]);
$chain->transitionTo(ApprovalChainStatus::Approved);
ApprovalCompleted::dispatch($chain);
return $chain->fresh('steps.approver');
}
/**
* Reject the current pending step (rejects the entire chain).
*/
public function reject(ApprovalChain $chain, User $approver, ?string $notes = null): ApprovalChain
{
$step = $this->getCurrentStepForApprover($chain, $approver);
$step->update([
'status' => ApprovalStepStatus::Rejected,
'notes' => $notes,
'acted_at' => now(),
]);
// Skip remaining steps
$chain->steps()
->where('order', '>', $step->order)
->where('status', 'pending')
->update(['status' => ApprovalStepStatus::Skipped->value]);
$chain->transitionTo(ApprovalChainStatus::Rejected);
StepRejected::dispatch($chain, $step);
ApprovalCompleted::dispatch($chain);
return $chain->fresh('steps.approver');
}
public function getNextApprover(ApprovalChain $chain): ?User
{
$step = $chain->currentStep();
return $step?->approver;
}
private function getCurrentStepForApprover(ApprovalChain $chain, User $approver): ApprovalStep
{
$step = $chain->currentStep();
if (!$step) {
throw new \InvalidArgumentException('No pending step found in this approval chain.');
}
$isAdmin = $approver->user_type === 'admin' || $approver->hasRole(['Super Admin', 'admin', 'Main Contractor Admin']);
if ($step->approver_id !== $approver->id && !$isAdmin) {
throw new \InvalidArgumentException('You are not the current approver for this step.');
}
return $step;
}
}