rows ?? collect(); $today = Carbon::today()->toDateString(); $expiryDate = Carbon::now()->endOfMonth()->toDateString(); $existing = User::where('type', UserTypeEnum::ASSOCIADO) ->whereNotNull('registration') ->get() ->keyBy('registration'); $importedRegistrations = []; $seenRegistrations = []; $created = 0; $updated = 0; foreach ($rows as $row) { $registration = $row['registration']; $name = $row['name']; if (isset($seenRegistrations[$registration])) { continue; } $seenRegistrations[$registration] = true; $importedRegistrations[] = $registration; if ($existing->has($registration)) { $user = $existing->get($registration); $changed = false; if ($user->name !== $name) { $user->name = $name; $changed = true; } if ($user->status !== UserStatusEnum::ACTIVE) { $user->status = UserStatusEnum::ACTIVE; $user->excluded_at = null; $changed = true; } if ($user->expiry_date?->toDateString() !== $expiryDate) { $user->expiry_date = $expiryDate; $changed = true; } if ($changed) { $user->save(); $updated++; } } else { $firstName = $this->extractFirstName($name); $password = "{$firstName}2026"; User::create([ 'name' => $name, 'password' => Hash::make($password), 'type' => UserTypeEnum::ASSOCIADO, 'status' => UserStatusEnum::ACTIVE, 'registration' => $registration, 'admission_date' => $today, 'expiry_date' => $expiryDate, ]); $created++; } } $inactivated = User::where('type', UserTypeEnum::ASSOCIADO) ->where('status', UserStatusEnum::ACTIVE) ->whereNotNull('registration') ->whereNotIn('registration', $importedRegistrations) ->update([ 'status' => UserStatusEnum::INACTIVE, 'excluded_at' => now(), ]); return [ 'total' => $rows->count(), 'created' => $created, 'updated' => $updated, 'inactivated' => $inactivated, ]; } public function syncAfastamentosFromExcel(string $filePath, int $byUserId): array { $import = new AssociadoImport(); Excel::import($import, $filePath); $rows = $import->rows ?? collect(); $today = Carbon::today()->toDateString(); $expiryDate = Carbon::now()->endOfMonth()->toDateString(); $existing = User::where('type', UserTypeEnum::ASSOCIADO) ->whereNotNull('registration') ->get() ->keyBy('registration'); $seenRegistrations = []; $created = 0; $onLeave = 0; foreach ($rows as $row) { $registration = $row['registration']; $name = $row['name']; if (isset($seenRegistrations[$registration])) { continue; } $seenRegistrations[$registration] = true; if ($existing->has($registration)) { $user = $existing->get($registration); if ($user->status !== UserStatusEnum::ON_LEAVE) { $user->status = UserStatusEnum::ON_LEAVE; $user->on_leave_at = now(); $user->on_leave_by_user_id = $byUserId; $user->save(); $onLeave++; } } else { $firstName = $this->extractFirstName($name); $password = "{$firstName}2026"; User::create([ 'name' => $name, 'password' => Hash::make($password), 'type' => UserTypeEnum::ASSOCIADO, 'status' => UserStatusEnum::ON_LEAVE, 'registration' => $registration, 'admission_date' => $today, 'expiry_date' => $expiryDate, 'on_leave_at' => now(), 'on_leave_by_user_id' => $byUserId, ]); $created++; } } return [ 'total' => $rows->count(), 'created' => $created, 'on_leave' => $onLeave, ]; } private function extractFirstName(string $fullName): string { $firstName = explode(' ', trim($fullName))[0]; return strtolower(Str::ascii($firstName)); } }