commit compras2 2711
tests / ci (push) Waiting to run

This commit is contained in:
2026-11-27 18:11:02 +02:00
parent e709d900d1
commit 930064269e
74 changed files with 7506 additions and 40 deletions
+27 -1
View File
@@ -48,6 +48,7 @@ final class ImportEstimates
$latest = $this->current($request);
return ['requisition' => $request->only(['id', 'number', 'status', 'subtotal_minor', 'tax_minor', 'total_minor']),
'legalAmounts' => app(ImportLegalRules::class)->ready() ? app(SupplyPlanning::class)->latest('import_legal_amount')->where('data->requisition_id', $request->id)->get() : [],
'latest' => $latest, 'nativeChanged' => $latest !== null && $latest->snapshot['native'] !== $this->native($request),
'history' => SupplyRecord::query()->where('kind', 'import_estimate')->where('data->requisition_id', $request->id)
->latest('version')->paginate(10, ['id', 'version', 'state', 'reason', 'data', 'snapshot', 'created_at'], 'estimate_page')->withQueryString(),
@@ -74,6 +75,9 @@ final class ImportEstimates
abort_unless($this->ready(), 503, 'Preserve a capacidade de estimativa de importação antes de submeter.');
abort_unless($record->snapshot['native'] === $this->native($request), 409, 'A requisição mudou. Reveja e versione a estimativa antes de submeter.');
abort_unless($record->snapshot['plan']['complete'] === true, 422, 'Existem custos de importação desconhecidos. Complete os pressupostos antes da aprovação e reserva.');
foreach ($record->snapshot['legal_sources'] ?? [] as $source) {
app(ImportLegalEstimates::class)->forPlan($request, $source['estimate_id']);
}
$budget = Budget::query()->whereKey($record->data['budget_id'])->firstOrFail();
abort_unless($budget->approved_by !== null && $budget->only(['id', 'cost_center_id', 'year', 'kind', 'includes_tax']) === $record->snapshot['budget'], 409,
'O orçamento da estimativa mudou. Reveja a preparação.');
@@ -199,6 +203,8 @@ final class ImportEstimates
abort_unless($this->ready(), 503);
$data = Validator::make($input, [
'operation_key' => 'required|uuid', 'version' => 'required|integer|min:0', 'budget_id' => 'required|integer|min:1',
'legal_sources' => 'sometimes|array|max:100', 'legal_sources.*' => 'required|array:estimate_id,charge_key',
'legal_sources.*.estimate_id' => 'required|integer|min:1', 'legal_sources.*.charge_key' => 'required|string|max:100',
'components' => 'required|array|list|min:6|max:100', 'components.*' => 'required|array:key,kind,status,basis,subtotal_minor,tax_minor,total_minor',
'components.*.key' => 'required|string|max:100', 'components.*.kind' => 'required|string',
'components.*.status' => 'required|in:known,unknown,not_applicable', 'components.*.basis' => 'required|string|min:3|max:4000',
@@ -237,12 +243,32 @@ final class ImportEstimates
->where('kind', $request->items()->where('purchase_type', 'equipment')->exists() ? 'investment' : 'operating')
->where('year', now()->year)->whereNotNull('approved_by')->firstOrFail();
$native = $this->native($request);
$legalSources = [];
$usedLegalLines = [];
foreach ($data['legal_sources'] ?? [] as $componentKey => $source) {
$estimate = app(ImportLegalEstimates::class)->forPlan($request, (int) $source['estimate_id']);
$lines = array_values(array_filter($estimate->snapshot['result']['lines'], fn ($line) => $line['key'] === $source['charge_key']));
$components = array_values(array_filter($data['components'], fn ($component) => $component['key'] === $componentKey));
$identity = $estimate->id.':'.$source['charge_key'];
abort_unless(count($lines) === 1 && count($components) === 1 && ! isset($usedLegalLines[$identity]), 422, 'Seleccione uma componente e use cada imposição calculada uma única vez.');
$line = $lines[0];
$component = $components[0];
abort_unless($component['kind'] === $line['kind'], 422, 'A família do encargo deve corresponder à imposição seleccionada.');
$amount = $line['amount_minor'];
abort_unless($amount === null ? $component['status'] === 'unknown'
: ($component['status'] === 'known' && $component['total_minor'] === $amount)
|| ($line['method'] === 'not_applicable' && $component['status'] === 'not_applicable' && $amount === 0), 422,
'O total ou a situação diverge da estimativa legal. Retire explicitamente a ligação para preparar um pressuposto manual.');
$usedLegalLines[$identity] = true;
$legalSources[$componentKey] = ['estimate_id' => $estimate->id, 'estimate_version' => $estimate->version, 'charge_key' => $line['key'],
'amount_minor' => $amount, 'legal_rule' => $estimate->snapshot['legal_rule']];
}
$plan = app(CommonBudget::class)->execute(fn () => BudgetImportEstimate::reconcile($native, $data['components'], (bool) $budget->includes_tax));
$record = SupplyRecord::query()->create(['tenant_id' => $actor->tenant_id, 'kind' => 'import_estimate',
'record_key' => $old->record_key ?? (string) Str::uuid(), 'version' => ($old->version ?? 0) + 1,
'state' => $plan['complete'] ? 'prepared' : 'incomplete', 'created_by' => $actor->id,
'data' => ['requisition_id' => $request->id, 'budget_id' => $budget->id, 'components' => $data['components']],
'snapshot' => ['native' => $native, 'plan' => $plan, 'budget' => $budget->only(['id', 'cost_center_id', 'year', 'kind', 'includes_tax'])],
'snapshot' => ['native' => $native, 'plan' => $plan, 'budget' => $budget->only(['id', 'cost_center_id', 'year', 'kind', 'includes_tax']), 'legal_sources' => $legalSources],
'reason' => $data['reason'], 'operation_key' => $data['operation_key'], 'input_hash' => $hash, 'created_at' => now()]);
app(RequisitionWorkflow::class)->audit($actor, 'import.estimate_prepared', $request, null,
['record_id' => $record->id, 'version' => $record->version, 'complete' => $plan['complete'], 'financial_effects' => false]);