Decisão do proprietário de 04/12/2026: o investidor não escolhe a taxa de IVA; ela decorre da actividade (CAE) do plano. Saúde privada (CAE 86), ensino privado reconhecido e formação profissional usam a taxa reduzida do artigo 17-A da Lei 22/2022; as restantes actividades usam a taxa geral. O IVA mensal das vendas mostra a taxa, a base legal e as condições da lei para o investidor confirmar. A taxa reduzida é uma versão governada própria (IVA-REDUZIDA-17A-BASE-LIQUIDA), com ficha em database/qa, registo QA por fiscal:qa-register-iva --reduced, pré-visualização QA e ligação por INVESTMOZ_IVA_REDUCED_RULE_ID/VERSION. Enquanto não estiver publicada, o IVA das actividades a 5% fica por determinar e nunca usa a taxa geral em substituição. Os resultados anteriores à taxa geral continuam actuais. Decisões e pendências da sessão registadas no manual existente. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
173 lines
10 KiB
PHP
173 lines
10 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\InvestmentLegalSource;
|
|
use App\Models\SimulationTaxRuleVersion;
|
|
use App\Support\StructuredData;
|
|
use Brick\Math\BigRational;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
/** Admission freezes eligibility without granting the public consumer governance write access. */
|
|
final class SimulationPublishedFiscal
|
|
{
|
|
/**
|
|
* Published IVA versions this consumer may use, one per operation. The reduced rate is the one of artigo 17-A,
|
|
* Lei 22/2022 (CIVA republicado), for the operations of artigo 15(10): private health, recognised private
|
|
* education, private vocational training and personal lessons.
|
|
*/
|
|
public const OPERATIONS = [
|
|
'internal_general_net' => ['config' => '', 'rule_code' => 'IVA-GERAL-BASE-LIQUIDA', 'scope_key' => 'mz.internal-general-net.mzn',
|
|
'parameter' => 'IVA_GENERAL_RATE', 'expression_key' => 'iva_general_rate', 'label' => 'IVA geral sobre base líquida'],
|
|
'internal_reduced_17a_net' => ['config' => 'reduced_', 'rule_code' => 'IVA-REDUZIDA-17A-BASE-LIQUIDA', 'scope_key' => 'mz.internal-reduced-17a-net.mzn',
|
|
'parameter' => 'IVA_REDUCED_17A_RATE', 'expression_key' => 'iva_reduced_17a_rate', 'label' => 'IVA à taxa reduzida do artigo 17-A sobre base líquida'],
|
|
];
|
|
|
|
/**
|
|
* @return array<array-key, mixed>
|
|
*/
|
|
public function availability(): array
|
|
{
|
|
$enabled = (bool) config('simulation-fiscal-consumer.enabled');
|
|
$eligible = $enabled && $this->boundRule()->where('status', 'published')->exists();
|
|
$reduced = $enabled && $this->boundRule('internal_reduced_17a_net')->where('status', 'published')->exists();
|
|
|
|
return ['enabled' => $eligible, 'reduced_enabled' => $reduced, 'coverage_start' => '2026-01-01', 'coverage_end' => '2026-12-31',
|
|
'message' => $eligible ? 'IVA geral sobre base líquida disponível; enquadramento declarado.'
|
|
: 'O cálculo de IVA geral sobre base líquida está indisponível para novas execuções. Os históricos mantêm-se acessíveis.',
|
|
'reduced_message' => $reduced ? 'IVA à taxa reduzida de 5% (artigo 17-A) disponível; enquadramento declarado.'
|
|
: 'O IVA à taxa reduzida de 5% (artigo 17-A) ainda não está publicado para novas execuções.'];
|
|
}
|
|
|
|
/**
|
|
* @return Builder<SimulationTaxRuleVersion>
|
|
*/
|
|
private function boundRule(string $operation = 'internal_general_net'): Builder
|
|
{
|
|
$binding = self::OPERATIONS[$operation] ?? null;
|
|
$ruleId = $binding ? (string) config('simulation-fiscal-consumer.'.$binding['config'].'rule_id') : '';
|
|
$version = $binding ? (int) config('simulation-fiscal-consumer.'.$binding['config'].'rule_version') : 0;
|
|
if (! Str::isUuid($ruleId) || $version < 1) {
|
|
return SimulationTaxRuleVersion::query()->whereRaw('false');
|
|
}
|
|
|
|
return SimulationTaxRuleVersion::query()->whereKey($ruleId)->where('rule_version', $version)
|
|
->where('rule_code', $binding['rule_code'])->where('component', 'IVA')->where('scope_key', $binding['scope_key']);
|
|
}
|
|
|
|
/**
|
|
* @param array<array-key, mixed> $input
|
|
* @return array<array-key, mixed>
|
|
*/
|
|
public function admit(array $input, bool $missingBase = false): array
|
|
{
|
|
if (isset($input['monthly_sales'])) {
|
|
return $this->admitMonthly($input);
|
|
}
|
|
// Called inside createPayload, after idempotent replay, never by the worker.
|
|
abort_unless(DB::transactionLevel() > 0, 500);
|
|
abort_unless(config('simulation-fiscal-consumer.enabled'), 503,
|
|
'O cálculo de IVA está temporariamente indisponível. Os dados e históricos foram preservados.');
|
|
$operation = (string) ($input['operation'] ?? 'internal_general_net');
|
|
$binding = self::OPERATIONS[$operation] ?? null;
|
|
abort_unless($binding, 422, 'Operação de IVA não suportada. As entradas foram preservadas.');
|
|
DB::select('select pg_advisory_xact_lock_shared(261021, hashtext(?))', [(string) config('simulation-fiscal-consumer.'.$binding['config'].'rule_id')]);
|
|
$rule = $this->boundRule($operation)->first();
|
|
abort_unless($rule && $rule->status === 'published', 503,
|
|
'A versão autorizada de IVA não está disponível para novas execuções. Os históricos foram preservados.');
|
|
abort_if($input['reference_date'] < '2026-01-01' || $input['reference_date'] > '2026-12-31', 422,
|
|
'Este cálculo abrange apenas exigibilidade em 2026. As entradas foram preservadas.');
|
|
abort_if(collect(StructuredData::collectionItems($rule->rule_definition['nodes'] ?? []))->contains(fn (array $node): bool => ($node['op'] ?? null) === 'round'), 422,
|
|
'A versão seleccionada não é compatível com o cálculo exacto deste consumidor.');
|
|
DB::select('select pg_advisory_xact_lock_shared(261022, hashtext(?))', [(string) $rule->legal_source_id]);
|
|
$source = InvestmentLegalSource::whereKey($rule->legal_source_id)->firstOrFail();
|
|
// Publish calculation provenance, not private governance notes or staff identifiers.
|
|
$version = array_intersect_key($rule->attributesToArray(), array_flip([
|
|
'id', 'rule_code', 'rule_version', 'component', 'scope_key', 'status', 'valid_from', 'valid_until',
|
|
'legal_source_id', 'source_version_no', 'source_sha256', 'rule_definition', 'parameters', 'rounding_policy',
|
|
]));
|
|
$version['source'] = array_intersect_key($source->attributesToArray(), array_flip([
|
|
'id', 'title', 'url', 'diploma', 'article', 'competent_entity', 'valid_from', 'valid_until', 'validation_status', 'version_no',
|
|
]));
|
|
$resolution = app(SimulationTaxParameterResolver::class)->resolvePublishedForPeriod('IVA',
|
|
$input['reference_date'], $input['reference_date'], ['currency' => $input['currency'], 'operation' => $input['operation']]);
|
|
$rate = collect($resolution)->firstWhere('parameter_code', $binding['parameter']);
|
|
$missing = [];
|
|
if ($missingBase) {
|
|
$missing[] = 'taxable_sales';
|
|
}
|
|
$evaluation = null;
|
|
if (! ($input['scope_declared'] ?? false)) {
|
|
$missing[] = 'operation_scope';
|
|
}
|
|
if (($rate['status'] ?? null) !== 'resolved' || ($rate['unit'] ?? null) !== 'percent'
|
|
|| ($rate['rule_id'] ?? null) !== (string) $rule->id
|
|
|| (int) ($rate['rule_version'] ?? 0) !== (int) $rule->rule_version) {
|
|
$missing[] = 'eligible_parameter';
|
|
}
|
|
if ($missing === []) {
|
|
$evaluation = app(SimulationTaxRuleExpression::class)->evaluate($rule->rule_definition,
|
|
['base_tributavel' => $input['base']], [$binding['expression_key'] => $rate['value']]);
|
|
}
|
|
|
|
return app(SimulationFiscalSnapshot::class)->build($version, $input, $resolution, $evaluation, $missing, true);
|
|
}
|
|
|
|
/**
|
|
* @param array<array-key, mixed> $input
|
|
* @return array<array-key, mixed>
|
|
*/
|
|
private function admitMonthly(array $input): array
|
|
{
|
|
$rows = [];
|
|
$baseTotal = BigRational::zero();
|
|
$vatTotal = BigRational::zero();
|
|
$complete = true;
|
|
$previous = null;
|
|
foreach ($input['monthly_sales'] as $sale) {
|
|
abort_unless($previous === null || $sale['date'] === CarbonImmutable::parse($previous)->addMonth()->format('Y-m-d'), 422, 'A projecção deve conter meses consecutivos, sem duplicações.');
|
|
$previous = $sale['date'];
|
|
$known = $sale['amount'] !== null && $sale['amount'] !== '';
|
|
$net = $input['sales_vat_basis'] === 'net';
|
|
$operation = (string) ($input['operation'] ?? 'internal_general_net');
|
|
$month = $this->admit(['base' => $known && $net ? $sale['amount'] : null,
|
|
'currency' => $input['currency'], 'reference_date' => $sale['date'],
|
|
'operation' => $operation, 'scope_declared' => $input['scope_declared']], ! $known || ! $net);
|
|
$snapshot ??= $month;
|
|
$rate = collect(StructuredData::collectionItems($month['fiscal_provenance']['resolution']))->firstWhere('parameter_code', self::OPERATIONS[$operation]['parameter']);
|
|
$vat = $month['result']['value'];
|
|
$status = ! $known ? 'missing_sales' : (! $net ? 'unsupported_basis' : ($vat === null ? 'indeterminate' : 'calculated'));
|
|
$rows[] = ['date' => $sale['date'], 'sales_declared' => $sale['amount'],
|
|
'net_sales' => $known && $net ? $sale['amount'] : null,
|
|
'rate' => $vat !== null && ($rate['status'] ?? null) === 'resolved' ? $rate['value'] : null,
|
|
'vat' => $vat, 'gross_sales' => $vat === null ? null : (string) BigRational::of($sale['amount'])->plus($vat)->toBigDecimal(),
|
|
'status' => $status, 'rule_id' => $month['fiscal_provenance']['rule']['id'],
|
|
'rule_version' => $month['fiscal_provenance']['rule']['rule_version']];
|
|
if ($vat === null) {
|
|
$complete = false;
|
|
} else {
|
|
$baseTotal = $baseTotal->plus($sale['amount']);
|
|
$vatTotal = $vatTotal->plus($vat);
|
|
}
|
|
}
|
|
$value = $complete ? (string) $vatTotal->toBigDecimal() : null;
|
|
$snapshot['base'] = $complete ? (string) $baseTotal->toBigDecimal() : null;
|
|
$snapshot['indicative_total'] = $value;
|
|
$snapshot['result']['value'] = $value;
|
|
$snapshot['result']['status'] = $complete ? 'calculated' : 'indeterminate';
|
|
$snapshot['result']['missing_inputs'] = $complete ? [] : ['monthly_sales_or_net_basis_or_scope'];
|
|
$snapshot['result']['explanation'] = 'IVA mensal sobre vendas declaradas sem IVA, pela versão publicada congelada na admissão. Valores com IVA incluído ou sem informação permanecem indeterminados. Não é imposto final a pagar.';
|
|
$snapshot['presentation']['value'] = $value;
|
|
$snapshot['intermediate'] = null;
|
|
$snapshot['monthly_projection'] = ['input' => $input, 'rows' => $rows,
|
|
'cash_treatment' => 'Informação separada da tesouraria: os recebimentos declarados não são aumentados pelo IVA nem o IVA é deduzido como custo ou imposto a pagar. Calendário de cobrança e liquidação fiscal não é inferido.'];
|
|
|
|
return $snapshot;
|
|
}
|
|
}
|