File manager - Edit - /var/www/html/transparencia/database/seeders/ImportTerceirizadosSeeder.php
Back
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Maatwebsite\Excel\Facades\Excel; use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate; class ImportTerceirizadosSeeder extends Seeder { private const PLANILHAS = [ 'temp/terceirizados/1 DEMONSTRATIVO_DE_SERVIDORES__PJ__TERCEIRIZADOS___IDEAS.xlsx', 'temp/terceirizados/2 DEMONSTRATIVO_DE_SERVIDORES_TERCEIRIZADOS___HSVP.xlsx', 'temp/terceirizados/3 DEMONSTRATIVO_DE_SERVIDORES_TERCEIRIZADOS___MASP.xlsx', 'temp/terceirizados/DEMONSTRATIVO_DE_SERVIDORES__PJ__TERCEIRIZADOS___IDEAS (2).xlsx', ]; public function run(): void { DB::table('terceirizados')->truncate(); $payload = []; foreach (self::PLANILHAS as $relativePath) { $fullPath = base_path($relativePath); if (!is_file($fullPath)) { $this->command?->warn("Planilha não encontrada: {$relativePath}"); continue; } $rows = Excel::toArray([], $fullPath)[0] ?? []; if ($rows === []) { continue; } $headerIndex = $this->findHeaderRowIndex($rows); if ($headerIndex === null) { $this->command?->warn("Cabeçalho não reconhecido: {$relativePath}"); continue; } foreach ($rows as $index => $row) { if ($index <= $headerIndex) { continue; } $exercicio = $this->cleanCell($row[0] ?? null); $nomeEmpregado = $this->cleanCell($row[1] ?? null); $empresa = $this->cleanCell($row[2] ?? null); $cnpj = $this->cleanCell($row[3] ?? null); $cargoAtividade = $this->cleanCell($row[4] ?? null); $lotacao = $this->cleanCell($row[5] ?? null); if ($this->isFullRowEmpty([ $exercicio, $nomeEmpregado, $empresa, $cnpj, $cargoAtividade, $lotacao, ])) { continue; } // Ignora linhas espúrias como "CONTRATO Nº ...". if ( $nomeEmpregado === null && $empresa === null && $cnpj === null && $cargoAtividade === null && $lotacao === null ) { continue; } $payload[] = [ 'exercicio_raw' => $exercicio ? mb_substr($exercicio, 0, 30) : null, 'exercicio_ano' => $this->parseYear($exercicio), 'nome_empregado' => $nomeEmpregado, 'empresa' => $empresa, 'cnpj' => $cnpj ? mb_substr($this->digitsOnly($cnpj), 0, 20) : null, 'cargo_atividade' => $cargoAtividade, 'lotacao_local_exercicio' => $lotacao, 'arquivo_origem' => $relativePath, 'created_at' => now(), 'updated_at' => now(), ]; } } if ($payload === []) { $this->command?->warn('Nenhum registro de terceirizados foi montado para importação.'); return; } foreach (array_chunk($payload, 1000) as $chunk) { DB::table('terceirizados')->insert($chunk); } $this->command?->info(count($payload) . ' registros de terceirizados importados.'); } private function findHeaderRowIndex(array $rows): ?int { foreach ($rows as $index => $row) { $rowText = mb_strtolower(implode(' ', array_map( fn ($value) => trim((string) $value), $row ))); if ( str_contains($rowText, 'exerc') && str_contains($rowText, 'nome') && str_contains($rowText, 'empresa') && str_contains($rowText, 'cnpj') && str_contains($rowText, 'lota') ) { return $index; } } return null; } private function cleanCell(mixed $value): ?string { if ($value === null) { return null; } $value = trim((string) $value); return $value === '' ? null : $value; } private function parseYear(?string $value): ?int { if ($value === null) { return null; } $trimmed = trim($value); if ($trimmed === '') { return null; } // Se vier como ano simples (2024, 2025...), usa diretamente. if (preg_match('/^\d{4}$/', $trimmed) === 1) { $year = (int) $trimmed; if ($year >= 1900 && $year <= 2100) { return $year; } } // Algumas planilhas retornam o exercício como serial de data do Excel. if (is_numeric($trimmed)) { $numeric = (float) $trimmed; if ($numeric > 1000) { try { return (int) ExcelDate::excelToDateTimeObject($numeric)->format('Y'); } catch (\Throwable $e) { // Segue para os parsers textuais abaixo. } } } if (preg_match('/(19|20)\d{2}/', $trimmed, $matches) === 1) { return (int) $matches[0]; } if (preg_match('/\/(\d{2})$/', $trimmed, $matches) === 1) { return 2000 + (int) $matches[1]; } return null; } private function isFullRowEmpty(array $values): bool { foreach ($values as $value) { if ($value !== null && trim((string) $value) !== '') { return false; } } return true; } private function digitsOnly(?string $value): string { return preg_replace('/\D+/', '', (string) $value) ?? ''; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings