65 lines
3.9 KiB
PHP
65 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ExistingBusiness;
|
|
use App\Models\ExistingBusinessObject;
|
|
use App\Models\User;
|
|
use App\Services\Mne\BusinessRepository;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
final class ExistingBusinessIntake
|
|
{
|
|
public function transition(User $actor, string $id, int $version, string $action, string $note, bool $consent = false, ?string $confirmationCode = null): ExistingBusiness
|
|
{
|
|
Validator::make(compact('action', 'note'), [
|
|
'action' => ['required', 'in:submitted,in_review,needs_information,withdrawn'],
|
|
'note' => ['required', 'string', 'min:10', 'max:2000'],
|
|
])->validate();
|
|
|
|
return DB::transaction(function () use ($actor, $id, $version, $action, $note, $consent, $confirmationCode) {
|
|
$business = app(BusinessRepository::class)->find($id, true);
|
|
Gate::forUser($actor)->authorize(in_array($action, ['submitted', 'withdrawn'], true) ? 'owns' : 'review', $business);
|
|
abort_unless($business->version_no === $version, 409, 'Versão desactualizada.');
|
|
$allowed = ['submitted' => ['draft', 'needs_information'], 'in_review' => ['submitted'],
|
|
'needs_information' => ['in_review'], 'withdrawn' => ['draft', 'submitted', 'in_review', 'needs_information']];
|
|
abort_unless(in_array($business->status, $allowed[$action], true), 409, 'Transição indisponível neste estado.');
|
|
if ($action === 'submitted') {
|
|
if (($business->payload['wizard_version'] ?? null) === '1') {
|
|
BusinessFormSchema::validateSubmission($business->payload);
|
|
$requiredKind = ['asset_sale' => 'asset', 'asset_lease' => 'asset', 'real_estate' => 'property',
|
|
'agricultural_investment' => 'agricultural_investment', 'land_right' => 'land_right',
|
|
'mining_license' => 'license', 'forestry_license' => 'license', 'capital_increase' => 'capital_request',
|
|
'participation_sale' => 'participation'][$business->modality] ?? null;
|
|
if ($requiredKind !== null && ! ExistingBusinessObject::where('business_id', $business->id)->where('kind', $requiredKind)->exists()) {
|
|
throw ValidationException::withMessages(['objects' => 'Declare o elemento aplicável na etapa 6: '.$requiredKind]);
|
|
}
|
|
Validator::make(['confirmation_code' => $confirmationCode, 'sector_id' => $business->sector_id], [
|
|
'confirmation_code' => ['required', Rule::in([$business->code])],
|
|
'sector_id' => ['required', Rule::exists('sectors', 'id')->whereNull('deleted_at')->where('is_active', true)],
|
|
])->validate();
|
|
}
|
|
Validator::make([...$business->payload, 'consent' => $consent], [
|
|
'description' => ['required', 'string', 'min:30'], 'source_reference' => ['required', 'string', 'min:10'],
|
|
'valid_until' => ['required', 'date_format:Y-m-d', 'after_or_equal:today'], 'consent' => ['accepted'],
|
|
])->validate();
|
|
}
|
|
$business->status = $action;
|
|
$business->version_no++;
|
|
$business->save();
|
|
DB::table('existing_business_reviews')->insert(['id' => (string) Str::uuid(), 'business_id' => $id, 'actor_id' => $actor->id,
|
|
'version_no' => $business->version_no, 'action' => $action,
|
|
'note' => Crypt::encryptString(json_encode(['note' => $note, 'review_consent' => $action === 'submitted' ? $consent : null], JSON_THROW_ON_ERROR))]);
|
|
app(ExistingBusinessDrafts::class)->snapshot($business, $actor, $action, $note);
|
|
|
|
return $business;
|
|
});
|
|
}
|
|
}
|