File manager - Edit - /var/www/html/transparencia/database/seeders/EmendasParlamentaresExercicio2026Seeder.php
Back
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Maatwebsite\Excel\Facades\Excel; class EmendasParlamentaresExercicio2026Seeder extends Seeder { private const PLANILHA_RELATIVE_PATH = 'temp/emendas_parlamentares_2026.xlsx'; /** * Remove todas as emendas de 2026 (incluindo o placeholder "não recebeu") * e importa a planilha oficial do exercício. Demais anos não são alterados. */ public function run(): void { $sheetPath = base_path(self::PLANILHA_RELATIVE_PATH); if (! is_file($sheetPath)) { $this->command?->error('Arquivo não encontrado: '.self::PLANILHA_RELATIVE_PATH); return; } $rows = Excel::toArray([], $sheetPath)[0] ?? []; if (count($rows) < 3) { $this->command?->warn('Planilha de 2026 sem dados após o cabeçalho. Nada foi alterado.'); return; } $payload = []; foreach ($rows as $index => $row) { if ($index < 2) { continue; } $origem = $this->cleanCell($row[0] ?? null); $tipo = $this->cleanCell($row[1] ?? null); $autoria = $this->cleanCell($row[2] ?? null); $formaRepasse = $this->cleanCell($row[3] ?? null); $numero = $this->cleanCell($row[4] ?? null); $anoRaw = $this->cleanCell($row[5] ?? null); $valorPrevistoRaw = $this->cleanCell($row[6] ?? null); $valorRepassadoRaw = $this->cleanCell($row[7] ?? null); $objeto = $this->cleanCell($row[8] ?? null); $funcaoRaw = $this->cleanCell($row[9] ?? null); $subfuncaoRaw = $this->cleanCell($row[10] ?? null); if ($this->isFullRowEmpty([ $origem, $tipo, $autoria, $formaRepasse, $numero, $anoRaw, $valorPrevistoRaw, $valorRepassadoRaw, $objeto, $funcaoRaw, $subfuncaoRaw, ])) { continue; } $anoNormalizado = is_numeric($anoRaw) ? (int) $anoRaw : 0; if ($anoNormalizado !== 2026) { $this->command?->warn('Linha '.($index + 1).": ano {$anoNormalizado} ignorado (esperado 2026)."); continue; } $valorPrevisto = $this->parseMoney($valorPrevistoRaw); $valorRepassado = $this->parseMoney($valorRepassadoRaw); $payload[] = [ 'origem' => $origem ? mb_substr($origem, 0, 255) : '', 'tipo' => $tipo ? mb_substr($tipo, 0, 100) : '', 'autor' => $autoria ? mb_substr($autoria, 0, 255) : '', 'numero_emenda' => $numero ? mb_substr($numero, 0, 30) : '', 'ano_emenda' => $anoNormalizado, 'ano_emenda_raw' => $anoRaw ? mb_substr($anoRaw, 0, 30) : '', 'numero_convenio' => 'N/A', 'situacao_convenio' => 'Não informado', 'objeto' => $objeto ?? '', 'funcao_governo' => $funcaoRaw ?? '', 'funcao_raw' => $funcaoRaw ? mb_substr($funcaoRaw, 0, 255) : '', 'subfuncao_raw' => $subfuncaoRaw ? mb_substr($subfuncaoRaw, 0, 255) : '', 'modalidade' => $formaRepasse ? mb_substr($formaRepasse, 0, 255) : '', 'valor_previsto' => $valorPrevisto, 'valor_previsto_raw' => $valorPrevistoRaw ? mb_substr($valorPrevistoRaw, 0, 60) : '', 'valor_realizado' => $valorRepassado, 'valor_realizado_raw' => $valorRepassadoRaw ? mb_substr($valorRepassadoRaw, 0, 60) : '', 'created_at' => now(), 'updated_at' => now(), ]; } if ($payload === []) { $this->command?->warn('Nenhum registro elegível na planilha de 2026. Nada foi alterado.'); return; } DB::transaction(function () use ($payload) { DB::table('emendas_parlamentares')->where('ano_emenda', 2026)->delete(); DB::table('emendas_parlamentares')->insert($payload); }); $this->command?->info(count($payload).' emenda(s) de 2026 importada(s). Fonte: '.self::PLANILHA_RELATIVE_PATH); } private function cleanCell(mixed $value): ?string { if ($value === null) { return null; } $value = trim((string) $value); if ($value === '') { return null; } return $value; } private function isFullRowEmpty(array $values): bool { foreach ($values as $value) { if ($value !== null && trim((string) $value) !== '') { return false; } } return true; } private function parseMoney(mixed $value): float { if ($value === null || trim((string) $value) === '') { return 0.0; } if (is_numeric($value)) { return (float) $value; } $normalized = preg_replace('/[^\d,.-]/', '', (string) $value); $normalized = str_replace('.', '', $normalized ?? ''); $normalized = str_replace(',', '.', $normalized); return is_numeric($normalized) ? (float) $normalized : 0.0; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings