|
|
@@ -2,11 +2,15 @@
|
|
|
|
|
|
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
|
|
|
+class AssociadoImport implements ToCollection, WithCalculatedFormulas
|
|
|
{
|
|
|
public Collection $rows;
|
|
|
|
|
|
@@ -17,11 +21,39 @@ class AssociadoImport implements ToCollection
|
|
|
&& !empty(trim((string) ($row[1] ?? '')))
|
|
|
&& !empty(trim((string) ($row[2] ?? '')));
|
|
|
})->map(function ($row) {
|
|
|
+ $registration = trim((string) $row[0]);
|
|
|
+
|
|
|
return [
|
|
|
- 'registration' => trim((string) $row[0]),
|
|
|
- 'name' => trim((string) $row[1]),
|
|
|
- 'admission_date' => Date::excelToDateTimeObject($row[2] ?? '')->format('Y-m-d'),
|
|
|
+ '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;
|
|
|
+ }
|
|
|
}
|