fix: allow authorised fiscal maintenance by the same responsible person
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-10-21 12:39:07 +02:00
parent 226fd308dd
commit 4a4d121275
17 changed files with 615 additions and 39 deletions
@@ -0,0 +1,68 @@
<?php
use App\Support\IsolatedQaDatabase;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
if (! IsolatedQaDatabase::allowsSchemaChanges()) {
throw new LogicException('Ensaie esta correcção apenas numa base QA isolada. A aplicação no destino exige decisão, backup/restauro e acção autenticada.');
}
// Only fiscal rule maintenance changes. Opportunity/zone workflow checks remain intact.
DB::statement('ALTER TABLE simulation_tax_rule_versions DROP CONSTRAINT tax_rule_stage_evidence_check, DROP CONSTRAINT tax_rule_approval_check, DROP CONSTRAINT tax_rule_publication_check');
DB::statement("ALTER TABLE simulation_tax_rule_versions
ADD CONSTRAINT tax_rule_stage_evidence_check CHECK(status NOT IN ('reviewed','rejected','approved','published','suspended','archived') OR (reviewer_id IS NOT NULL AND submitted_at IS NOT NULL AND reviewed_at IS NOT NULL AND length(trim(review_evidence))>=20)),
ADD CONSTRAINT tax_rule_approval_check CHECK(status NOT IN ('approved','published','suspended','archived') OR (approver_id IS NOT NULL AND approved_at IS NOT NULL AND length(trim(approval_evidence))>=20)),
ADD CONSTRAINT tax_rule_publication_check CHECK(status NOT IN ('published','suspended','archived') OR (publisher_id IS NOT NULL AND published_at IS NOT NULL AND length(trim(publication_evidence))>=20))");
DB::statement('ALTER TABLE investment_legal_sources ADD COLUMN fiscal_acceptance jsonb NULL');
DB::statement("ALTER TABLE investment_legal_sources ADD CONSTRAINT investment_legal_source_fiscal_acceptance_check CHECK (
fiscal_acceptance IS NULL OR COALESCE((
fiscal_acceptance->>'scope'='fiscal_parameters' AND
fiscal_acceptance->>'basis'='Tempo-official-sources-20261008' AND
fiscal_acceptance->>'responsible_user_id'=owner_id::text AND
fiscal_acceptance->>'sha256' ~ '^[a-f0-9]{64}$' AND
fiscal_acceptance->>'legal_opinion'='false' AND fiscal_acceptance->>'independent_review'='false' AND
length(trim(fiscal_acceptance->>'citation'))>=10 AND
length(trim(fiscal_acceptance->>'official_origin'))>=10
), false))");
DB::statement('DROP POLICY simulation_tax_rule_versions_public_read ON simulation_tax_rule_versions');
DB::statement("CREATE POLICY simulation_tax_rule_versions_public_read ON simulation_tax_rule_versions FOR SELECT USING (
status='published' AND EXISTS (SELECT 1 FROM investment_legal_sources source
WHERE source.id=simulation_tax_rule_versions.legal_source_id
AND (source.validation_status='validated' OR (source.validation_status='pending'
AND source.fiscal_acceptance->>'scope'='fiscal_parameters'
AND source.fiscal_acceptance->>'basis'='Tempo-official-sources-20261008'
AND source.fiscal_acceptance->>'sha256'=simulation_tax_rule_versions.source_sha256))
AND source.version_no=simulation_tax_rule_versions.source_version_no
AND source.valid_from<=simulation_tax_rule_versions.valid_from
AND (source.valid_until IS NULL OR (simulation_tax_rule_versions.valid_until IS NOT NULL AND source.valid_until>=simulation_tax_rule_versions.valid_until))
AND (simulation_tax_rule_versions.valid_until IS NOT NULL OR source.valid_until IS NULL)))");
}
public function down(): void
{
if (! IsolatedQaDatabase::allowsSchemaChanges()) {
throw new LogicException('Reversão permitida apenas em QA isolado.');
}
$used = DB::table('investment_legal_sources')->whereNotNull('fiscal_acceptance')->exists()
|| DB::table('simulation_tax_rule_versions')->whereRaw('reviewer_id=author_id OR approver_id=author_id OR approver_id=reviewer_id OR publisher_id=author_id OR publisher_id=reviewer_id OR publisher_id=approver_id')->exists();
if ($used) {
throw new LogicException('Reversão recusada: conservar os cadastros e históricos legítimos; desactivar novas execuções em vez de apagar registos.');
}
DB::statement('ALTER TABLE simulation_tax_rule_versions DROP CONSTRAINT tax_rule_stage_evidence_check, DROP CONSTRAINT tax_rule_approval_check, DROP CONSTRAINT tax_rule_publication_check');
DB::statement("ALTER TABLE simulation_tax_rule_versions
ADD CONSTRAINT tax_rule_stage_evidence_check CHECK(status NOT IN ('reviewed','rejected','approved','published','suspended','archived') OR (reviewer_id IS NOT NULL AND reviewer_id<>author_id AND submitted_at IS NOT NULL AND reviewed_at IS NOT NULL AND length(trim(review_evidence))>=20)),
ADD CONSTRAINT tax_rule_approval_check CHECK(status NOT IN ('approved','published','suspended','archived') OR (approver_id IS NOT NULL AND approver_id<>author_id AND approver_id<>reviewer_id AND approved_at IS NOT NULL AND length(trim(approval_evidence))>=20)),
ADD CONSTRAINT tax_rule_publication_check CHECK(status NOT IN ('published','suspended','archived') OR (publisher_id IS NOT NULL AND publisher_id<>author_id AND publisher_id<>reviewer_id AND publisher_id<>approver_id AND published_at IS NOT NULL AND length(trim(publication_evidence))>=20))");
DB::statement('DROP POLICY simulation_tax_rule_versions_public_read ON simulation_tax_rule_versions');
(require __DIR__.'/2026_10_03_190000_allow_public_read_of_published_tax_parameters.php')->up();
DB::statement('ALTER TABLE investment_legal_sources DROP CONSTRAINT investment_legal_source_fiscal_acceptance_check');
DB::statement('ALTER TABLE investment_legal_sources DROP COLUMN fiscal_acceptance');
}
};