| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace App\Imports;
- use Carbon\Carbon;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Log;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
- use PhpOffice\PhpSpreadsheet\Shared\Date;
- use Throwable;
- class AssociadoImport implements ToCollection, WithCalculatedFormulas
- {
- public Collection $rows;
- public function collection(Collection $rows): void
- {
- $this->rows = $rows->skip(1)->values()->filter(function ($row) {
- return !empty(trim((string) ($row[0] ?? '')))
- && !empty(trim((string) ($row[1] ?? '')))
- && !empty(trim((string) ($row[2] ?? '')));
- })->map(function ($row) {
- $registration = trim((string) $row[0]);
- return [
- 'registration' => $registration,
- 'name' => trim((string) $row[1]),
- 'admission_date' => $this->resolveAdmissionDate($row[2] ?? null, $registration),
- ];
- })->values();
- }
- private function resolveAdmissionDate($rawValue, string $registration): ?string
- {
- try {
- if (is_int($rawValue) || is_float($rawValue)) {
- return Date::excelToDateTimeObject($rawValue)->format('Y-m-d');
- }
- if (is_string($rawValue) && is_numeric($rawValue)) {
- return Date::excelToDateTimeObject((float) $rawValue)->format('Y-m-d');
- }
- if (is_string($rawValue) && $rawValue !== '' && $rawValue[0] !== '=') {
- return Carbon::parse($rawValue)->format('Y-m-d');
- }
- } catch (Throwable $e) {
- // falls through to warning below
- }
- Log::warning('AssociadoImport: could not resolve admission_date, leaving null', [
- 'registration' => $registration,
- 'raw_value' => is_scalar($rawValue) ? $rawValue : json_encode($rawValue),
- ]);
- return null;
- }
- }
|