<?php
 namespace App\Modules\Addon\MigrateWhmcs\Importers; use App\Models\Client; use App\Models\Currency; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; class ClientImporter extends AbstractWhmcsImporter { public function sourceType(): string { return 'client'; } protected function sourceTable(): string { return 'tblclients'; } protected function importRow(object $row): ?int { $email = trim((string) ($row->email ?? '')); if ($email !== '' && Client::where('email', $email)->exists()) { $this->report->warn( $this->sourceType(), "Client #{$row->id}: e-posta zaten var ('{$email}') → atlandı (mevcut kayıt korunur)." ); return null; } $countryId = null; $countryCode = strtoupper(trim((string) ($row->country ?? ''))); if ($countryCode !== '') { $countryId = DB::table('countries')->where('a2_iso', $countryCode)->value('id'); } $client = Client::create([ 'first_name' => $this->text($row->firstname ?? null), 'last_name' => $this->text($row->lastname ?? null), 'company_name' => $this->text($row->companyname ?? null), 'email' => $email, 'phone' => $this->text($row->phonenumber ?? null), 'address1' => $this->text($row->address1 ?? null), 'address2' => $this->text($row->address2 ?? null), 'state' => $this->text($row->state ?? null), 'postcode' => $this->text($row->postcode ?? null), 'country_id' => $countryId, 'currency_id' => $this->defaultCurrencyId(), 'status' => $this->mapStatus((string) ($row->status ?? '')), 'tax_exempt' => $this->mapTaxExempt($row->taxexempt ?? null), 'notes' => $this->text($row->notes ?? null), 'password' => Str::random(40), 'registered_at' => $this->normalizeDate($row->datecreated ?? null), ]); $client->password_must_change = true; $client->save(); return $client->id; } protected function text(mixed $value): ?string { $v = trim((string) ($value ?? '')); return $v === '' ? null : $v; } protected function mapStatus(string $whmcsStatus): string { return match (strtolower(trim($whmcsStatus))) { 'inactive' => 'inactive', 'closed' => 'closed', default => 'active', }; } protected function mapTaxExempt(mixed $value): bool { return strtolower(trim((string) ($value ?? ''))) === 'on'; } protected function defaultCurrencyId(): int { return (int) (Currency::where('is_default', true)->value('id') ?? 1); } protected function normalizeDate(mixed $date): ?string { $d = trim((string) ($date ?? '')); if ($d === '' || str_starts_with($d, '0000-00-00')) { return null; } return $d; } } 