commit21
This commit is contained in:
@@ -20,6 +20,14 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class SupplierEvaluationController extends Controller
|
||||
{
|
||||
private function actor(Request $request): User
|
||||
{
|
||||
$actor = $request->user('web');
|
||||
abort_unless($actor instanceof User, 403);
|
||||
|
||||
return $actor;
|
||||
}
|
||||
|
||||
private function ready(): void
|
||||
{
|
||||
abort_unless(Schema::hasTable('supplier_evaluations'), 503, 'A instalação do módulo de avaliações ainda não foi concluída.');
|
||||
@@ -43,7 +51,7 @@ class SupplierEvaluationController extends Controller
|
||||
|
||||
public function index(Request $request, SupplierEvaluationWorkflow $workflow): Response
|
||||
{
|
||||
$actor = $workflow->reader($request->user());
|
||||
$actor = $workflow->reader($this->actor($request));
|
||||
$filters = $request->validate(['supplier_id' => 'nullable|integer|min:1', 'status' => 'nullable|in:draft,review,returned,approved']);
|
||||
$ready = Schema::hasTable('supplier_evaluations');
|
||||
$records = $ready ? SupplierEvaluation::query()->visibleTo($actor)
|
||||
@@ -54,7 +62,7 @@ class SupplierEvaluationController extends Controller
|
||||
'title' => $e->payload['title'], 'period_start' => $e->payload['period_start'], 'period_end' => $e->payload['period_end'],
|
||||
]) : null;
|
||||
|
||||
return $this->render($request, 'suppliers/evaluations', ['ready' => $ready, 'records' => $records, 'filters' => $filters,
|
||||
return $this->render($request, 'suppliers/evaluations', ['ready' => $ready, 'canCreate' => $ready && $workflow->canPrepare($actor), 'records' => $records, 'filters' => $filters,
|
||||
'suppliers' => Supplier::query()->orderBy('name')->get(['id', 'name', 'nuit']),
|
||||
'orders' => PurchaseOrder::query()->visibleTo($actor)->orderByDesc('id')->get(['id', 'number', 'supplier_id'])]);
|
||||
}
|
||||
@@ -71,7 +79,7 @@ class SupplierEvaluationController extends Controller
|
||||
|
||||
private function view(Request $request, int $id, SupplierEvaluationWorkflow $workflow, bool $report): Response
|
||||
{
|
||||
$actor = $workflow->reader($request->user());
|
||||
$actor = $workflow->reader($this->actor($request));
|
||||
$this->ready();
|
||||
$evaluation = SupplierEvaluation::query()->visibleTo($actor)->findOrFail($id);
|
||||
$improvementWorkflow = app(SupplierImprovementWorkflow::class);
|
||||
@@ -89,21 +97,21 @@ class SupplierEvaluationController extends Controller
|
||||
$next = SupplierEvaluation::query()->visibleTo($actor)->where('previous_id', $id)->value('id');
|
||||
|
||||
return $this->render($request, 'suppliers/evaluation', ['evaluation' => $workflow->snapshot($evaluation), 'reportOnly' => $report,
|
||||
'canEdit' => ! $report && $writable && in_array($evaluation->status, ['draft', 'returned'], true),
|
||||
'canReview' => ! $report && $writable && $evaluation->status === 'review' && ! in_array($actor->id, $evaluation->contributors, true),
|
||||
'canRevise' => ! $report && $writable && $evaluation->status === 'approved' && $next === null,
|
||||
'canEdit' => ! $report && $workflow->canAct($actor, $evaluation, 'edit'),
|
||||
'canReview' => ! $report && $workflow->canAct($actor, $evaluation, 'review'),
|
||||
'canRevise' => ! $report && $workflow->canAct($actor, $evaluation, 'revise') && $next === null,
|
||||
'nextRevisionId' => $next, 'history' => $history, 'people' => $people,
|
||||
'responses' => ['ready' => $responsesReady, 'canCreate' => ! $report && $responsesReady && $writable && $evaluation->status === 'approved',
|
||||
'history' => $responseHistory,
|
||||
'records' => $responses->map(fn (SupplierResponse $r) => [...app(SupplierResponseWorkflow::class)->snapshot($r),
|
||||
'canEdit' => ! $report && $writable && in_array($r->status, ['draft', 'returned'], true),
|
||||
'canReview' => ! $report && $writable && $r->status === 'review' && ! in_array($actor->id, $r->contributors, true)])],
|
||||
'canEdit' => ! $report && app(SupplierResponseWorkflow::class)->canAct($actor, $evaluation, $r, 'edit'),
|
||||
'canReview' => ! $report && app(SupplierResponseWorkflow::class)->canAct($actor, $evaluation, $r, 'review')])],
|
||||
'improvements' => ['ready' => $measuresReady, 'canCreate' => ! $report && $measuresReady && $writable && $evaluation->status === 'approved',
|
||||
'today' => today()->toDateString(),
|
||||
'records' => $measures->map(fn (SupplierImprovement $m) => [...$improvementWorkflow->snapshot($m),
|
||||
'canPlan' => ! $report && $writable && in_array($m->status, ['open', 'returned'], true),
|
||||
'canExecute' => ! $report && $writable && $actor->id === $m->responsible_id && in_array($m->status, ['open', 'returned'], true),
|
||||
'canReview' => ! $report && $writable && $m->status === 'review' && ! in_array($actor->id, $m->contributors, true)]),
|
||||
'canPlan' => ! $report && $improvementWorkflow->canAct($actor, $evaluation, $m, 'plan'),
|
||||
'canExecute' => ! $report && $improvementWorkflow->canAct($actor, $evaluation, $m, 'execute'),
|
||||
'canReview' => ! $report && $improvementWorkflow->canAct($actor, $evaluation, $m, 'review')]),
|
||||
'history' => $measureHistory,
|
||||
'assignees' => $report ? [] : User::query()->where('tenant_id', $actor->tenant_id)->orderBy('name')->get()->filter(fn (User $u) => $u->hasFunctions(SupplierEvaluationWorkflow::ROLES) && (! $u->departmentScoped() || ($u->department_id !== null && $u->department_id === $evaluation->department_id)))->map(fn (User $u) => $u->only(['id', 'name']))->values()],
|
||||
'orders' => PurchaseOrder::query()->visibleTo($actor)->where('supplier_id', $evaluation->supplier_id)->orderByDesc('id')->get(['id', 'number', 'supplier_id']),
|
||||
@@ -124,7 +132,7 @@ class SupplierEvaluationController extends Controller
|
||||
{
|
||||
$this->ready();
|
||||
$data = $request->validate(['supplier_id' => 'required|integer|min:1', 'operation_key' => 'required|uuid']);
|
||||
$evaluation = $workflow->create($request->user(), (int) $data['supplier_id'], $data['operation_key'], $this->payload($request));
|
||||
$evaluation = $workflow->create($this->actor($request), (int) $data['supplier_id'], $data['operation_key'], $this->payload($request));
|
||||
|
||||
return to_route('supplier-evaluations.show', $evaluation->id)->with('supplier_evaluation_notice', 'Rascunho guardado. Os dados podem ser recuperados nesta avaliação.');
|
||||
}
|
||||
@@ -133,7 +141,7 @@ class SupplierEvaluationController extends Controller
|
||||
{
|
||||
$this->ready();
|
||||
$data = $request->validate(['version' => 'required|integer|min:1']);
|
||||
$workflow->save($request->user(), $evaluation, (int) $data['version'], $this->payload($request));
|
||||
$workflow->save($this->actor($request), $evaluation, (int) $data['version'], $this->payload($request));
|
||||
|
||||
return to_route('supplier-evaluations.show', $evaluation)->with('supplier_evaluation_notice', 'Alterações guardadas no histórico.');
|
||||
}
|
||||
@@ -143,7 +151,7 @@ class SupplierEvaluationController extends Controller
|
||||
$this->ready();
|
||||
$data = $request->validate(['version' => 'required|integer|min:1', 'reason' => $action === 'submit' ? 'nullable|string|max:4000' : 'required|string|min:10|max:4000',
|
||||
'payload' => 'prohibited', 'supplier_id' => 'prohibited', 'tenant_id' => 'prohibited', 'reviewed_by' => 'prohibited']);
|
||||
$workflow->transition($request->user(), $evaluation, (int) $data['version'], $action, $data['reason'] ?? null);
|
||||
$workflow->transition($this->actor($request), $evaluation, (int) $data['version'], $action, $data['reason'] ?? null);
|
||||
|
||||
return to_route('supplier-evaluations.show', $evaluation)->with('supplier_evaluation_notice', 'Estado e decisão registados.');
|
||||
}
|
||||
@@ -152,7 +160,7 @@ class SupplierEvaluationController extends Controller
|
||||
{
|
||||
$this->ready();
|
||||
$data = $request->validate(['version' => 'required|integer|min:1']);
|
||||
$revision = $workflow->revise($request->user(), $evaluation, (int) $data['version']);
|
||||
$revision = $workflow->revise($this->actor($request), $evaluation, (int) $data['version']);
|
||||
|
||||
return to_route('supplier-evaluations.show', $revision->id)->with('supplier_evaluation_notice', 'Nova revisão preparada. A avaliação aprovada anterior mantém-se preservada.');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user