@@ -10,6 +10,7 @@ use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Tempo\Purchasing\Internal\InternalRequisitions;
|
||||
use Tempo\Purchasing\Internal\RequisitionFailure;
|
||||
@@ -64,7 +65,10 @@ class RequisitionWorkflow
|
||||
} else {
|
||||
abort_unless(app(OrganizationAccess::class)->canRequest($user), 403);
|
||||
}
|
||||
if (! $user->department_id || ! CostCenter::query()->whereKey($data['cost_center_id'])->where('department_id', $user->department_id)->exists()) {
|
||||
$drafts = app(RequisitionDraftInput::class);
|
||||
$data = Validator::make($data, $drafts->rules($user))->validate();
|
||||
$incomplete = $drafts->available() && ! $drafts->complete($user, $data);
|
||||
if (! $user->department_id || ((! $incomplete || ! empty($data['cost_center_id'])) && ! CostCenter::query()->whereKey($data['cost_center_id'] ?? null)->where('department_id', $user->department_id)->exists())) {
|
||||
throw ValidationException::withMessages(['cost_center_id' => 'O departamento ou centro de custo mudou. Actualize o pedido antes de guardar.']);
|
||||
}
|
||||
$requisition = $existing ? Requisition::query()->lockForUpdate()->findOrFail($existing->id) : new Requisition;
|
||||
@@ -73,6 +77,24 @@ class RequisitionWorkflow
|
||||
app(ConflictGuard::class)->assertParticipation($user, $requisition);
|
||||
}
|
||||
$before = $existing ? $requisition->load('items')->toArray() : null;
|
||||
if ($incomplete) {
|
||||
$requisition->fill([
|
||||
'tenant_id' => $user->tenant_id, 'requester_id' => $user->id, 'department_id' => $user->department_id,
|
||||
'cost_center_id' => $data['cost_center_id'] ?? null, 'priority' => $data['priority'] ?? 'normal',
|
||||
'needed_by' => $data['needed_by'] ?? null, 'justification' => $data['justification'] ?? null,
|
||||
'subtotal_minor' => null, 'tax_minor' => null, 'total_minor' => null,
|
||||
'draft_input' => ['incomplete' => true, 'values' => $drafts->preparation($user, $data)],
|
||||
...($existing ? [] : ['comparison_policy' => app(ComparisonPolicy::class)->company(Tenant::query()->findOrFail($user->tenant_id))]),
|
||||
...(Schema::hasColumn('requisitions', 'process_version') ? ['process_version' => $existing ? (int) $requisition->getAttribute('process_version') + 1 : 1] : []),
|
||||
])->save();
|
||||
if (! $existing) {
|
||||
$requisition->update(['number' => sprintf('REQ-%s-%06d', now()->format('Y'), $requisition->id)]);
|
||||
}
|
||||
// Earlier complete lines remain for catalogue identity, never for approval or totals.
|
||||
$this->audit($executor, $existing ? 'requisition.updated' : 'requisition.created', $requisition, $before, $requisition->fresh()->toArray());
|
||||
|
||||
return $requisition;
|
||||
}
|
||||
$items = [];
|
||||
$subtotal = 0;
|
||||
$tax = 0;
|
||||
@@ -106,6 +128,7 @@ class RequisitionWorkflow
|
||||
'cost_center_id' => $data['cost_center_id'], 'priority' => $data['priority'],
|
||||
'needed_by' => $data['needed_by'], 'justification' => $data['justification'],
|
||||
'subtotal_minor' => $subtotal, 'tax_minor' => $tax, 'total_minor' => $subtotal + $tax,
|
||||
...($drafts->available() ? ['draft_input' => null] : []),
|
||||
...($existing ? [] : ['comparison_policy' => app(ComparisonPolicy::class)->company(Tenant::query()->findOrFail($user->tenant_id))]),
|
||||
...(Schema::hasColumn('requisitions', 'process_version') ? ['process_version' => $existing ? (int) $requisition->getAttribute('process_version') + 1 : 1] : []),
|
||||
])->save();
|
||||
|
||||
Reference in New Issue
Block a user