<?php
 namespace App\Modules\Addon\MigrateWisecp\Importers; use App\Models\Client; use App\Models\Currency; use App\Modules\Addon\MigrateWisecp\Services\WisecpSourceConnection; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; class ClientImporter extends AbstractWisecpImporter { public function sourceType(): string { return 'client'; } protected function sourceTable(): string { return 'users'; } protected function sourceQuery() { return WisecpSourceConnection::table('users')->where('type', 'member'); } 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; } $address = WisecpSourceConnection::table('users_addresses') ->where('owner_id', $row->id) ->orderByDesc('detouse') ->orderBy('id') ->first(); $wisecpCountryId = (int) ($address->country_id ?? $row->country ?? 0); $countryId = $this->resolveCountryId($wisecpCountryId); $client = Client::create([ 'first_name' => $this->text($row->name ?? null), 'last_name' => $this->text($row->surname ?? null), 'company_name' => $this->text($address->company_name ?? $row->company_name ?? null), 'email' => $email, 'phone' => $this->text($row->phone ?? ($address->phone ?? null)), 'address1' => $this->text($address->address ?? null), 'postcode' => $this->text($address->zipcode ?? null), 'country_id' => $countryId, 'currency_id' => $this->resolveCurrencyId($row->currency ?? null), 'status' => $this->mapStatus((string) ($row->status ?? '')), 'notes' => $this->text($row->notes ?? null), 'password' => Str::random(40), 'registered_at' => $this->normalizeDate($row->creation_time ?? null), ]); $client->password_must_change = true; $client->save(); return $client->id; } protected function resolveCountryId(int $wisecpCountryId): ?int { if ($wisecpCountryId <= 0) { return null; } $iso = WisecpSourceConnection::table('countries') ->where('id', $wisecpCountryId) ->value('a2_iso'); if (! $iso) { return null; } return DB::table('countries')->where('a2_iso', strtoupper(trim($iso)))->value('id'); } protected function resolveCurrencyId(mixed $wisecpCurrencyId): int { $wid = (int) $wisecpCurrencyId; if ($wid > 0) { $mapped = DB::table('wisecp_currency_map') ->where('wisecp_currency_id', $wid) ->value('procp_currency_id'); if ($mapped) { return (int) $mapped; } } return (int) (Currency::where('is_default', true)->value('id') ?? 1); } protected function text(mixed $value): ?string { $v = trim((string) ($value ?? '')); return $v === '' ? null : $v; } protected function mapStatus(string $status): string { return match (strtolower(trim($status))) { 'inactive', 'passive' => 'inactive', 'closed', 'banned', 'deleted' => 'closed', default => 'active', }; } protected function normalizeDate(mixed $date): ?string { $d = trim((string) ($date ?? '')); if ($d === '' || str_starts_with($d, '0000-00-00') || str_starts_with($d, '1881-05-19')) { return null; } return $d; } } 