argument('path'); if (! is_file($path)) { $this->error("File not found: {$path}"); return static::FAILURE; } $handle = fopen($path, 'r'); if (! $handle) { $this->error("Unable to open file: {$path}"); return static::FAILURE; } $header = fgetcsv($handle); if (! $header) { $this->error('CSV file is empty.'); fclose($handle); return static::FAILURE; } $header = array_map('trim', $header); $required = [ 'full_name', 'email', 'phone', 'address_line_1', 'address_line_2', 'city', 'postal_code', 'emergency_contact_name', 'emergency_contact_phone', 'membership_started_at', 'membership_expires_at', ]; foreach ($required as $column) { if (! in_array($column, $header, true)) { $this->error("Missing required column: {$column}"); fclose($handle); return static::FAILURE; } } $indexes = array_flip($header); $createdUsers = 0; $updatedMembers = 0; while (($row = fgetcsv($handle)) !== false) { $email = trim($row[$indexes['email']] ?? ''); if ($email === '') { continue; } $fullName = trim($row[$indexes['full_name']] ?? ''); $memberNumber = isset($indexes['member_number']) ? trim($row[$indexes['member_number']] ?? '') : ''; $nationalId = isset($indexes['national_id']) ? trim($row[$indexes['national_id']] ?? '') : ''; $phone = trim($row[$indexes['phone']] ?? ''); $phoneHome = isset($indexes['phone_home']) ? trim($row[$indexes['phone_home']] ?? '') : ''; $phoneFax = isset($indexes['phone_fax']) ? trim($row[$indexes['phone_fax']] ?? '') : ''; $birthDate = isset($indexes['birth_date']) ? trim($row[$indexes['birth_date']] ?? '') : ''; $gender = isset($indexes['gender']) ? trim($row[$indexes['gender']] ?? '') : ''; $identityType = isset($indexes['identity_type']) ? trim($row[$indexes['identity_type']] ?? '') : ''; $identityOtherText = isset($indexes['identity_other_text']) ? trim($row[$indexes['identity_other_text']] ?? '') : ''; $occupation = isset($indexes['occupation']) ? trim($row[$indexes['occupation']] ?? '') : ''; $employer = isset($indexes['employer']) ? trim($row[$indexes['employer']] ?? '') : ''; $jobTitle = isset($indexes['job_title']) ? trim($row[$indexes['job_title']] ?? '') : ''; $appliedAt = isset($indexes['applied_at']) ? trim($row[$indexes['applied_at']] ?? '') : ''; $started = trim($row[$indexes['membership_started_at']] ?? ''); $expires = trim($row[$indexes['membership_expires_at']] ?? ''); $address1 = trim($row[$indexes['address_line_1']] ?? ''); $address2 = trim($row[$indexes['address_line_2']] ?? ''); $city = trim($row[$indexes['city']] ?? ''); $postal = trim($row[$indexes['postal_code']] ?? ''); $emergencyName = trim($row[$indexes['emergency_contact_name']] ?? ''); $emergencyPhone = trim($row[$indexes['emergency_contact_phone']] ?? ''); $user = User::where('email', $email)->first(); $isNewUser = false; if (! $user) { $user = User::create([ 'name' => $fullName !== '' ? $fullName : $email, 'email' => $email, 'password' => Str::random(32), ]); $isNewUser = true; $createdUsers++; } $member = Member::updateOrCreate( ['user_id' => $user->id], [ 'member_number' => $memberNumber !== '' ? $memberNumber : null, 'full_name' => $fullName !== '' ? $fullName : $user->name, 'email' => $email, 'national_id' => $nationalId !== '' ? $nationalId : null, 'phone' => $phone !== '' ? $phone : null, 'phone_home' => $phoneHome !== '' ? $phoneHome : null, 'phone_fax' => $phoneFax !== '' ? $phoneFax : null, 'birth_date' => $birthDate !== '' ? $birthDate : null, 'gender' => $gender !== '' ? $gender : null, 'identity_type' => $identityType !== '' ? $identityType : null, 'identity_other_text' => $identityOtherText !== '' ? $identityOtherText : null, 'occupation' => $occupation !== '' ? $occupation : null, 'employer' => $employer !== '' ? $employer : null, 'job_title' => $jobTitle !== '' ? $jobTitle : null, 'applied_at' => $appliedAt !== '' ? $appliedAt : null, 'address_line_1' => $address1 ?: null, 'address_line_2' => $address2 ?: null, 'city' => $city ?: null, 'postal_code' => $postal ?: null, 'emergency_contact_name' => $emergencyName ?: null, 'emergency_contact_phone' => $emergencyPhone ?: null, 'membership_started_at' => $started !== '' ? $started : null, 'membership_expires_at' => $expires !== '' ? $expires : null, ], ); $updatedMembers++; if ($isNewUser) { $token = Password::createToken($user); Mail::to($user)->queue(new MemberActivationMail($user, $token)); AuditLogger::log('user.activation_link_sent', $user, [ 'email' => $user->email, ]); } } fclose($handle); $this->info("Users created: {$createdUsers}"); $this->info("Members imported/updated: {$updatedMembers}"); return static::SUCCESS; } }