Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/Contracts/StageValidatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Contracts;

use App\Models\Project;

interface StageValidatorInterface
{
public function ensureCanAdvance(Project $project): void;
}
13 changes: 0 additions & 13 deletions app/Http/Controllers/ProjectStageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

namespace App\Http\Controllers;

use App\Enums\ProjectStageSlug;
use App\Enums\Role;
use App\Http\Requests\Stages\ReturnStageRequest;
use App\Models\Project;
use App\Models\ProjectStage;
use App\Services\FormalizationService;
use App\Services\NotificationService;
use App\Services\OpeningUpdateService;
use App\Services\ProjectStageService;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;
Expand All @@ -20,8 +17,6 @@ class ProjectStageController extends Controller
public function __construct(
private ProjectStageService $stageService,
private NotificationService $notificationService,
private FormalizationService $formalizationService,
private OpeningUpdateService $openingUpdateService,
) {}

public function advance(
Expand All @@ -32,14 +27,6 @@ public function advance(
try {
$stage->load('project');

if ($stage->slug === ProjectStageSlug::ABERTURA) {
$this->openingUpdateService->ensureCanAdvance($project);
}

if ($stage->slug === ProjectStageSlug::FORMALIZACAO) {
$this->formalizationService->ensureCanAdvance($project);
}

$nextStage = $this->stageService->advance($stage, $request->user());

$this->notificationService->notifyStageAdvanced($stage, $nextStage, $request->user());
Expand Down
59 changes: 58 additions & 1 deletion app/Services/BudgetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace App\Services;

use App\Contracts\StageValidatorInterface;
use App\Enums\DocumentPhase;
use App\Enums\DocumentType;
use App\Models\Budget;
use App\Models\Project;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;

class BudgetService
class BudgetService implements StageValidatorInterface
{
public function create(Project $project, array $data): Budget
{
Expand Down Expand Up @@ -85,4 +89,57 @@ private function installmentData(array $data): array
'observations' => $data['installment_observations'] ?? null,
];
}

public function ensureCanAdvance(Project $project): void
{
$budget = $project->budgets;

if (! $budget) {
throw ValidationException::withMessages([
'budget' => 'Preencha e salve os dados do orçamento antes de tramitar.',
]);
}

$this->validateRequiredFields($project, $budget);
$this->validateRequiredDocuments($project);
}

private function validateRequiredFields(Project $project, Budget $budget): void
{
$currentInstallment = $budget->installments()
->where('installment_number', $project->current_installment_cycle)
->first();

$missingFields = collect();

if (blank($currentInstallment?->notice_installment_number)) {
$missingFields->push('Nº da parcela no edital');
}

if (blank($currentInstallment?->amount)) {
$missingFields->push('Valor da parcela');
}

if ($missingFields->isNotEmpty()) {
throw ValidationException::withMessages([
'budget' => 'Preencha todos os campos obrigatórios antes de tramitar: '
.$missingFields->join(', ')
.'.',
]);
}
}

private function validateRequiredDocuments(Project $project): void
{
$hasBudgetOpinion = $project->documents()
->where('phase', DocumentPhase::BUDGET->value ?? 'budget')
->where('type', DocumentType::DO->value ?? 'do')
->exists();

if (! $hasBudgetOpinion) {
throw ValidationException::withMessages([
'documents' => 'O despacho orçamentário precisa ser gerado antes da tramitação.',
]);
}
}
}
3 changes: 2 additions & 1 deletion app/Services/FormalizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Services;

use App\Contracts\StageValidatorInterface;
use App\Enums\DocumentPhase;
use App\Enums\DocumentType;
use App\Models\File;
Expand All @@ -12,7 +13,7 @@
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;

class FormalizationService
class FormalizationService implements StageValidatorInterface
{
private const OFFICIAL_GAZETTE_FILE_GROUP = 'official_gazette';

Expand Down
3 changes: 2 additions & 1 deletion app/Services/OpeningUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Services;

use App\Contracts\StageValidatorInterface;
use App\Enums\ProfileSnapshotSource;
use App\Models\Opening;
use App\Models\Project;
Expand All @@ -10,7 +11,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;

class OpeningUpdateService
class OpeningUpdateService implements StageValidatorInterface
{
public function __construct(
protected ProfileSnapshotService $snapshotService
Expand Down
16 changes: 16 additions & 0 deletions app/Services/ProjectStageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function advance(

$this->ensureUserHasRole($stage, $user, 'Você não tem permissão para tramitar esta etapa.');

$this->validateStageAdvance($stage, $stage->project);

if ($stage->slug === ProjectStageSlug::MONITORAMENTO) {
$this->ensureIsPrincipalSupervisor($stage->project, $user);
}
Expand All @@ -56,6 +58,20 @@ public function advance(
return $next?->fresh();
}

public function validateStageAdvance(ProjectStage $stage, Project $project): void
{
$validatorClass = match ($stage->slug) {
ProjectStageSlug::ABERTURA => OpeningUpdateService::class,
ProjectStageSlug::FORMALIZACAO => FormalizationService::class,
ProjectStageSlug::ORCAMENTO => BudgetService::class,
default => null,
};

if ($validatorClass) {
app($validatorClass)->ensureCanAdvance($project);
}
}

public function reject(ProjectStage $stage, string $reason, User $user): void
{
if ($stage->status !== ProjectStageStatus::EM_ANDAMENTO) {
Expand Down
Loading
Loading