MigrateAsteriaCustomers.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\Asteria\Magento1CustomerReader;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Hash;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Schema;
  11. use Illuminate\Support\Str;
  12. use Webkul\Customer\Models\CustomerAddress;
  13. /**
  14. * Migrates storefront customers, addresses, and newsletter subscriptions from Asteria (Magento 1.x).
  15. *
  16. * Usage
  17. * ─────
  18. * php artisan customers:migrate-asteria
  19. * php artisan customers:migrate-asteria --batch-size=500
  20. * php artisan customers:migrate-asteria --reset-progress
  21. * php artisan customers:migrate-asteria --dry-run
  22. */
  23. class MigrateAsteriaCustomers extends Command
  24. {
  25. protected $signature = 'customers:migrate-asteria
  26. {--batch-size=500 : Number of Magento customers per batch}
  27. {--reset-progress : Ignore saved progress and start from entity_id=0}
  28. {--dry-run : Count records without writing}
  29. {--connection=asteria : Laravel DB connection for the Asteria database}';
  30. protected $description = 'Migrate Asteria (Magento 1.x) customers, addresses, and newsletter subscriptions into Bagisto';
  31. private const MAGENTO_NEWSLETTER_SUBSCRIBED = 1;
  32. private const PROGRESS_KEY = 'migrate_asteria_customers_last_id';
  33. private const INSERT_CHUNK = 200;
  34. private int $groupId;
  35. private mixed $channelId;
  36. private string $placeholderPassword;
  37. /** @var array<string, int> */
  38. private array $usedPhones = [];
  39. public function handle(): int
  40. {
  41. $connection = (string) $this->option('connection');
  42. $batchSize = max(1, (int) $this->option('batch-size'));
  43. $resetProgress = (bool) $this->option('reset-progress');
  44. $dryRun = (bool) $this->option('dry-run');
  45. DB::disableQueryLog();
  46. try {
  47. DB::connection($connection)->getPdo();
  48. } catch (\Throwable $e) {
  49. $this->error("Cannot connect to Asteria DB (connection='{$connection}'): ".$e->getMessage());
  50. return self::FAILURE;
  51. }
  52. foreach (['customer_entity', 'eav_attribute'] as $table) {
  53. if (! Schema::connection($connection)->hasTable($table)) {
  54. $this->error("Asteria table '{$table}' is missing on connection '{$connection}'.");
  55. return self::FAILURE;
  56. }
  57. }
  58. if (! Schema::hasColumn('customers', 'migrated_from_asteria_id')
  59. || ! Schema::hasColumn('customers', 'legacy_password')
  60. || ! Schema::hasColumn('customers', 'customer_source')) {
  61. $this->error('customers.migrated_from_asteria_id / legacy_password / customer_source are missing. Run php artisan migrate.');
  62. return self::FAILURE;
  63. }
  64. $groupId = DB::table('customer_groups')->where('code', 'general')->value('id');
  65. if (! $groupId) {
  66. $this->error("Bagisto customer group 'general' was not found.");
  67. return self::FAILURE;
  68. }
  69. $this->groupId = (int) $groupId;
  70. $this->channelId = core()->getDefaultChannel()?->id ?? core()->getCurrentChannel()?->id;
  71. $this->placeholderPassword = Hash::make(Str::random(32));
  72. $reader = new Magento1CustomerReader($connection);
  73. $lastId = $resetProgress ? 0 : (int) Cache::get(self::PROGRESS_KEY, 0);
  74. if ($resetProgress) {
  75. Cache::forget(self::PROGRESS_KEY);
  76. }
  77. if ($lastId > 0) {
  78. $this->line("Resuming from Asteria entity_id > {$lastId} (use --reset-progress to restart).");
  79. }
  80. $this->usedPhones = DB::table('customers')
  81. ->whereNotNull('phone')
  82. ->where('phone', '!=', '')
  83. ->pluck('phone')
  84. ->map(fn ($phone) => mb_strtolower((string) $phone))
  85. ->flip()
  86. ->all();
  87. $created = 0;
  88. $linked = 0;
  89. $skipped = 0;
  90. $addressesImported = 0;
  91. $batchNumber = 0;
  92. $this->info($dryRun ? '[DRY RUN] Scanning Magento customers…' : 'Migrating Magento customers…');
  93. do {
  94. $started = microtime(true);
  95. $customers = $reader->fetchCustomers($lastId, $batchSize);
  96. if ($customers->isEmpty()) {
  97. break;
  98. }
  99. $batchNumber++;
  100. $lastId = (int) $customers->max('entity_id');
  101. $asteriaIds = $customers->pluck('entity_id')->map(fn ($id) => (int) $id)->all();
  102. $emails = $customers
  103. ->pluck('email')
  104. ->map(fn ($email) => strtolower(trim((string) $email)))
  105. ->filter()
  106. ->all();
  107. $addresses = $reader->fetchAddresses($asteriaIds)->groupBy(fn (array $row) => (int) $row['parent_id']);
  108. $subscribers = $reader->fetchSubscribers($asteriaIds, $emails);
  109. if ($dryRun) {
  110. $created += $customers->count();
  111. $addressesImported += $addresses->flatten(1)->count();
  112. $this->line(sprintf(
  113. ' Batch #%d: %d customers, %d addresses (last entity_id=%d) [skipped – dry-run] (%.1fs)',
  114. $batchNumber,
  115. $customers->count(),
  116. $addresses->flatten(1)->count(),
  117. $lastId,
  118. microtime(true) - $started
  119. ));
  120. continue;
  121. }
  122. $result = $this->persistBatch($customers, $addresses, $subscribers);
  123. Cache::put(self::PROGRESS_KEY, $lastId, now()->addDays(30));
  124. $created += $result['created'];
  125. $linked += $result['linked'];
  126. $skipped += $result['skipped'];
  127. $addressesImported += $result['addresses'];
  128. $this->line(sprintf(
  129. ' Batch #%d: created=%d linked=%d skipped=%d addresses=%d (last entity_id=%d) (%.1fs)',
  130. $batchNumber,
  131. $result['created'],
  132. $result['linked'],
  133. $result['skipped'],
  134. $result['addresses'],
  135. $lastId,
  136. microtime(true) - $started
  137. ));
  138. Log::info('MigrateAsteriaCustomers: batch '.$batchNumber.', last_id='.$lastId);
  139. } while ($customers->count() === $batchSize);
  140. $subscriptions = $this->importAllNewsletterSubscribers($reader, $batchSize, $dryRun);
  141. $this->newLine();
  142. $this->info("Done. Batches: {$batchNumber}, created: {$created}, linked: {$linked}, skipped: {$skipped}, addresses: {$addressesImported}, subscriptions: {$subscriptions}.");
  143. return self::SUCCESS;
  144. }
  145. /**
  146. * @param Collection<int, array<string, mixed>> $customers
  147. * @param Collection<int, Collection<int, array<string, mixed>>> $addresses
  148. * @param Collection<int, array<string, mixed>> $subscribers
  149. * @return array{created: int, linked: int, skipped: int, addresses: int}
  150. */
  151. private function persistBatch(Collection $customers, Collection $addresses, Collection $subscribers): array
  152. {
  153. $now = now()->format('Y-m-d H:i:s');
  154. $created = 0;
  155. $linked = 0;
  156. $skipped = 0;
  157. $importedAddresses = 0;
  158. [$subscribersByCustomerId, $subscribersByEmail] = $this->indexLatestSubscribers($subscribers);
  159. $asteriaIds = $customers->pluck('entity_id')->map(fn ($id) => (int) $id)->unique()->values()->all();
  160. $emails = $customers
  161. ->pluck('email')
  162. ->map(fn ($email) => strtolower(trim((string) $email)))
  163. ->filter(fn ($email) => $email !== '' && filter_var($email, FILTER_VALIDATE_EMAIL))
  164. ->unique()
  165. ->values()
  166. ->all();
  167. [$byAsteriaId, $byEmail] = $this->loadExistingCustomers($asteriaIds, $emails);
  168. $insertCustomers = [];
  169. $linkUpdates = [];
  170. $sourceUpdates = [];
  171. $addressJobs = [];
  172. $seenEmails = [];
  173. foreach ($customers as $row) {
  174. $email = strtolower(trim((string) ($row['email'] ?? '')));
  175. $asteriaId = (int) $row['entity_id'];
  176. if ($email === '' || ! filter_var($email, FILTER_VALIDATE_EMAIL)) {
  177. $skipped++;
  178. continue;
  179. }
  180. $existing = $byAsteriaId[$asteriaId] ?? $byEmail[$email] ?? null;
  181. if ($existing || isset($seenEmails[$email])) {
  182. if ($existing && empty($existing->migrated_from_asteria_id)) {
  183. $linkUpdates[(int) $existing->id] = $asteriaId;
  184. $existing->migrated_from_asteria_id = $asteriaId;
  185. $byAsteriaId[$asteriaId] = $existing;
  186. }
  187. if ($existing) {
  188. $addressJobs[] = ['customer' => $existing, 'row' => $row];
  189. $linked++;
  190. $source = $this->mapCustomerSource($row['source'] ?? $row['customer_source'] ?? null);
  191. if ($source !== null) {
  192. $sourceUpdates[(int) $existing->id] = $source;
  193. }
  194. } else {
  195. $skipped++;
  196. }
  197. continue;
  198. }
  199. $seenEmails[$email] = true;
  200. $phone = $this->uniquePhone((string) ($row['telephone'] ?? ''), $this->usedPhones);
  201. $subscriber = $subscribersByCustomerId[$asteriaId] ?? $subscribersByEmail[$email] ?? null;
  202. $insertCustomers[$asteriaId] = [
  203. 'migrated_from_asteria_id' => $asteriaId,
  204. 'first_name' => $this->requiredName($row['firstname'] ?? null, $email),
  205. 'last_name' => trim((string) ($row['lastname'] ?? '')) ?: '-',
  206. 'gender' => $this->mapGender($row['gender'] ?? null),
  207. 'date_of_birth' => $this->mapDate($row['dob'] ?? null),
  208. 'email' => $email,
  209. 'phone' => $phone,
  210. 'password' => $this->placeholderPassword,
  211. 'legacy_password' => $this->nullableString($row['password_hash'] ?? null),
  212. 'api_token' => Str::random(80),
  213. 'customer_group_id' => $this->groupId,
  214. 'channel_id' => $this->channelId,
  215. 'customer_source' => $this->mapCustomerSource($row['source'] ?? $row['customer_source'] ?? null),
  216. 'subscribed_to_news_letter' => $this->isMagentoSubscribed($subscriber) ? 1 : 0,
  217. 'status' => ((int) ($row['is_active'] ?? 1)) === 1 ? 1 : 0,
  218. 'is_verified' => 1,
  219. 'is_suspended' => 0,
  220. 'created_at' => ! empty($row['created_at']) ? $row['created_at'] : $now,
  221. 'updated_at' => $now,
  222. ];
  223. $addressJobs[] = ['asteria_id' => $asteriaId, 'row' => $row, 'email' => $email, 'first_name' => $insertCustomers[$asteriaId]['first_name'], 'last_name' => $insertCustomers[$asteriaId]['last_name'], 'phone' => $phone];
  224. $created++;
  225. }
  226. DB::transaction(function () use (
  227. $insertCustomers,
  228. $linkUpdates,
  229. $sourceUpdates,
  230. $addressJobs,
  231. $addresses,
  232. $subscribers,
  233. &$importedAddresses
  234. ) {
  235. if ($insertCustomers !== []) {
  236. $this->insertRows('customers', array_values($insertCustomers));
  237. $inserted = DB::table('customers')
  238. ->whereIn('migrated_from_asteria_id', array_keys($insertCustomers))
  239. ->get(['id', 'migrated_from_asteria_id', 'email', 'first_name', 'last_name', 'phone']);
  240. $byInsertedAsteriaId = $inserted->keyBy(fn ($row) => (int) $row->migrated_from_asteria_id);
  241. } else {
  242. $byInsertedAsteriaId = collect();
  243. }
  244. foreach ($linkUpdates as $customerId => $asteriaId) {
  245. DB::table('customers')
  246. ->where('id', $customerId)
  247. ->whereNull('migrated_from_asteria_id')
  248. ->update(['migrated_from_asteria_id' => $asteriaId]);
  249. }
  250. foreach ($sourceUpdates as $customerId => $source) {
  251. DB::table('customers')
  252. ->where('id', $customerId)
  253. ->update(['customer_source' => $source]);
  254. }
  255. $customerIdsForAddresses = [];
  256. foreach ($addressJobs as $job) {
  257. if (isset($job['customer'])) {
  258. $customerIdsForAddresses[] = (int) $job['customer']->id;
  259. } elseif (isset($job['asteria_id'])) {
  260. $inserted = $byInsertedAsteriaId->get((int) $job['asteria_id']);
  261. if ($inserted) {
  262. $customerIdsForAddresses[] = (int) $inserted->id;
  263. }
  264. }
  265. }
  266. $existingAddressIds = $this->loadExistingAddressIds($customerIdsForAddresses);
  267. $addressInserts = [];
  268. foreach ($addressJobs as $job) {
  269. $row = $job['row'];
  270. $addressRows = $addresses->get((int) $row['entity_id'], collect());
  271. if ($addressRows->isEmpty()) {
  272. continue;
  273. }
  274. if (isset($job['customer'])) {
  275. $customerId = (int) $job['customer']->id;
  276. $email = (string) $job['customer']->email;
  277. $firstName = (string) $job['customer']->first_name;
  278. $lastName = (string) $job['customer']->last_name;
  279. $phone = $job['customer']->phone;
  280. } else {
  281. $inserted = $byInsertedAsteriaId->get((int) $job['asteria_id']);
  282. if (! $inserted) {
  283. continue;
  284. }
  285. $customerId = (int) $inserted->id;
  286. $email = (string) $inserted->email;
  287. $firstName = (string) $inserted->first_name;
  288. $lastName = (string) $inserted->last_name;
  289. $phone = $inserted->phone;
  290. }
  291. $existingIds = $existingAddressIds[$customerId] ?? [];
  292. foreach ($this->buildAddressRows($customerId, $row, $addressRows, $email, $firstName, $lastName, $phone, $existingIds) as $addressRow) {
  293. $addressInserts[] = $addressRow;
  294. $importedAddresses++;
  295. }
  296. }
  297. if ($addressInserts !== []) {
  298. $this->insertRows('addresses', $addressInserts);
  299. }
  300. $this->persistSubscriberRows($subscribers);
  301. });
  302. return [
  303. 'created' => $created,
  304. 'linked' => $linked,
  305. 'skipped' => $skipped,
  306. 'addresses' => $importedAddresses,
  307. ];
  308. }
  309. private function importAllNewsletterSubscribers(Magento1CustomerReader $reader, int $batchSize, bool $dryRun): int
  310. {
  311. if (! Schema::hasTable('subscribers_list')) {
  312. $this->warn('subscribers_list is missing; skipping newsletter import.');
  313. return 0;
  314. }
  315. $synced = 0;
  316. $lastId = 0;
  317. do {
  318. $rows = $reader->fetchSubscribersAfter($lastId, $batchSize);
  319. if ($rows->isEmpty()) {
  320. break;
  321. }
  322. $lastId = (int) $rows->max('subscriber_id');
  323. $synced += $this->persistSubscriberRows($rows, $dryRun);
  324. } while ($rows->count() === $batchSize);
  325. return $synced;
  326. }
  327. /**
  328. * @param Collection<int, array<string, mixed>> $subscribers
  329. */
  330. private function persistSubscriberRows(Collection $subscribers, bool $dryRun = false): int
  331. {
  332. if ($subscribers->isEmpty()) {
  333. return 0;
  334. }
  335. [$byCustomerId, $byEmail] = $this->indexLatestSubscribers($subscribers);
  336. if ($byCustomerId === [] && $byEmail === []) {
  337. return 0;
  338. }
  339. [$customersByAsteriaId, $customersByEmail] = $this->loadCustomersForSubscribers(
  340. array_keys($byCustomerId),
  341. array_keys($byEmail)
  342. );
  343. $flagOn = [];
  344. $flagOff = [];
  345. foreach ($byCustomerId as $asteriaId => $sub) {
  346. $customer = $customersByAsteriaId[$asteriaId] ?? null;
  347. if (! $customer) {
  348. continue;
  349. }
  350. if ($this->isMagentoSubscribed($sub)) {
  351. $flagOn[(int) $customer->id] = true;
  352. } else {
  353. $flagOff[(int) $customer->id] = true;
  354. }
  355. }
  356. foreach ($byEmail as $email => $sub) {
  357. $customer = $customersByEmail[$email] ?? null;
  358. if (! $customer || isset($flagOn[(int) $customer->id]) || isset($flagOff[(int) $customer->id])) {
  359. continue;
  360. }
  361. if ($this->isMagentoSubscribed($sub)) {
  362. $flagOn[(int) $customer->id] = true;
  363. } else {
  364. $flagOff[(int) $customer->id] = true;
  365. }
  366. }
  367. $listRows = [];
  368. foreach ($byEmail as $email => $sub) {
  369. if ($email === '' || ! filter_var($email, FILTER_VALIDATE_EMAIL)) {
  370. continue;
  371. }
  372. $customer = $customersByEmail[$email] ?? null;
  373. if (! $customer) {
  374. $asteriaId = (int) ($sub['customer_id'] ?? 0);
  375. $customer = $asteriaId > 0 ? ($customersByAsteriaId[$asteriaId] ?? null) : null;
  376. }
  377. if (! $customer && ! $this->isMagentoSubscribed($sub)) {
  378. continue;
  379. }
  380. $listRows[$email] = [
  381. 'email' => $email,
  382. 'is_subscribed' => $this->isMagentoSubscribed($sub) ? 1 : 0,
  383. 'token' => $this->nullableString($sub['subscriber_confirm_code'] ?? null),
  384. 'customer_id' => $customer ? (int) $customer->id : null,
  385. 'channel_id' => $customer?->channel_id ?? $this->channelId,
  386. 'changed_at' => $sub['change_status_at'] ?? null,
  387. ];
  388. }
  389. $synced = count($listRows);
  390. if ($dryRun) {
  391. return $synced;
  392. }
  393. $flagOffIds = array_keys(array_diff_key($flagOff, $flagOn));
  394. $flagOnIds = array_keys($flagOn);
  395. if ($flagOffIds !== []) {
  396. DB::table('customers')->whereIn('id', $flagOffIds)->update(['subscribed_to_news_letter' => 0]);
  397. }
  398. if ($flagOnIds !== []) {
  399. DB::table('customers')->whereIn('id', $flagOnIds)->update(['subscribed_to_news_letter' => 1]);
  400. }
  401. if ($listRows === []) {
  402. return 0;
  403. }
  404. $existing = $this->loadExistingSubscriberLists(array_keys($listRows));
  405. $now = now()->format('Y-m-d H:i:s');
  406. $inserts = [];
  407. foreach ($listRows as $email => $row) {
  408. $existingRow = $existing[$email] ?? null;
  409. if ($existingRow) {
  410. DB::table('subscribers_list')->where('id', $existingRow->id)->update([
  411. 'is_subscribed' => $row['is_subscribed'],
  412. 'customer_id' => $row['customer_id'] ?? $existingRow->customer_id,
  413. 'updated_at' => $now,
  414. ]);
  415. continue;
  416. }
  417. $inserts[] = [
  418. 'email' => $row['email'],
  419. 'is_subscribed' => $row['is_subscribed'],
  420. 'token' => $row['token'] ?? uniqid(),
  421. 'customer_id' => $row['customer_id'],
  422. 'channel_id' => $row['channel_id'] ?? $this->channelId,
  423. 'created_at' => $this->mapDateTime($row['changed_at'] ?? null) ?? $now,
  424. 'updated_at' => $now,
  425. ];
  426. }
  427. if ($inserts !== []) {
  428. $this->insertRows('subscribers_list', $inserts);
  429. }
  430. return $synced;
  431. }
  432. /**
  433. * @param Collection<int, array<string, mixed>> $subscribers
  434. * @return array{0: array<int, array<string, mixed>>, 1: array<string, array<string, mixed>>}
  435. */
  436. private function indexLatestSubscribers(Collection $subscribers): array
  437. {
  438. $byCustomerId = [];
  439. $byEmail = [];
  440. foreach ($subscribers as $sub) {
  441. $customerId = (int) ($sub['customer_id'] ?? 0);
  442. $email = strtolower(trim((string) ($sub['subscriber_email'] ?? '')));
  443. if ($customerId > 0) {
  444. $byCustomerId[$customerId] = $this->newerSubscriber($byCustomerId[$customerId] ?? null, $sub);
  445. }
  446. if ($email !== '') {
  447. $byEmail[$email] = $this->newerSubscriber($byEmail[$email] ?? null, $sub);
  448. }
  449. }
  450. return [$byCustomerId, $byEmail];
  451. }
  452. /**
  453. * @param array<string, mixed>|null $current
  454. * @param array<string, mixed> $candidate
  455. * @return array<string, mixed>
  456. */
  457. private function newerSubscriber(?array $current, array $candidate): array
  458. {
  459. if ($current === null) {
  460. return $candidate;
  461. }
  462. $currentAt = (string) ($current['change_status_at'] ?? '');
  463. $candidateAt = (string) ($candidate['change_status_at'] ?? '');
  464. if ($candidateAt !== $currentAt) {
  465. return $candidateAt > $currentAt ? $candidate : $current;
  466. }
  467. return ((int) ($candidate['subscriber_id'] ?? 0)) >= ((int) ($current['subscriber_id'] ?? 0))
  468. ? $candidate
  469. : $current;
  470. }
  471. /**
  472. * @param array<string, mixed>|null $subscriber
  473. */
  474. private function isMagentoSubscribed(?array $subscriber): bool
  475. {
  476. return $subscriber !== null
  477. && ((int) ($subscriber['subscriber_status'] ?? 0)) === self::MAGENTO_NEWSLETTER_SUBSCRIBED;
  478. }
  479. /**
  480. * @param array<int, int> $asteriaIds
  481. * @param array<int, string> $emails
  482. * @return array{0: array<int, object>, 1: array<string, object>}
  483. */
  484. private function loadCustomersForSubscribers(array $asteriaIds, array $emails): array
  485. {
  486. $asteriaIds = array_values(array_unique(array_filter(array_map('intval', $asteriaIds))));
  487. $emails = array_values(array_unique(array_filter($emails)));
  488. $byAsteriaId = [];
  489. $byEmail = [];
  490. $select = ['id', 'email', 'channel_id', 'migrated_from_asteria_id'];
  491. if ($asteriaIds !== []) {
  492. foreach (DB::table('customers')->select($select)->whereIn('migrated_from_asteria_id', $asteriaIds)->get() as $customer) {
  493. $byAsteriaId[(int) $customer->migrated_from_asteria_id] = $customer;
  494. $byEmail[strtolower((string) $customer->email)] = $customer;
  495. }
  496. }
  497. if ($emails !== []) {
  498. foreach (DB::table('customers')->select($select)->whereIn(DB::raw('LOWER(email)'), $emails)->get() as $customer) {
  499. $byEmail[strtolower((string) $customer->email)] = $byEmail[strtolower((string) $customer->email)] ?? $customer;
  500. if ($customer->migrated_from_asteria_id) {
  501. $byAsteriaId[(int) $customer->migrated_from_asteria_id] = $byAsteriaId[(int) $customer->migrated_from_asteria_id] ?? $customer;
  502. }
  503. }
  504. }
  505. return [$byAsteriaId, $byEmail];
  506. }
  507. /**
  508. * @param array<int, string> $emails
  509. * @return array<string, object>
  510. */
  511. private function loadExistingSubscriberLists(array $emails): array
  512. {
  513. $emails = array_values(array_unique(array_filter($emails)));
  514. if ($emails === []) {
  515. return [];
  516. }
  517. $byEmail = [];
  518. foreach (DB::table('subscribers_list')->whereIn(DB::raw('LOWER(email)'), $emails)->get(['id', 'email', 'customer_id', 'token']) as $row) {
  519. $byEmail[strtolower((string) $row->email)] = $row;
  520. }
  521. return $byEmail;
  522. }
  523. private function mapDateTime(mixed $value): ?string
  524. {
  525. $value = trim((string) $value);
  526. if ($value === '' || str_starts_with($value, '0000-00-00')) {
  527. return null;
  528. }
  529. return $value;
  530. }
  531. /**
  532. * @param array<int, int> $asteriaIds
  533. * @param array<int, string> $emails
  534. * @return array{0: array<int, object>, 1: array<string, object>}
  535. */
  536. private function loadExistingCustomers(array $asteriaIds, array $emails): array
  537. {
  538. $byAsteriaId = [];
  539. $byEmail = [];
  540. $select = ['id', 'email', 'first_name', 'last_name', 'phone', 'migrated_from_asteria_id', 'legacy_password'];
  541. if ($asteriaIds !== []) {
  542. foreach (DB::table('customers')->select($select)->whereIn('migrated_from_asteria_id', $asteriaIds)->get() as $customer) {
  543. $byAsteriaId[(int) $customer->migrated_from_asteria_id] = $customer;
  544. $byEmail[strtolower((string) $customer->email)] = $customer;
  545. }
  546. }
  547. if ($emails !== []) {
  548. foreach (DB::table('customers')->select($select)->whereIn(DB::raw('LOWER(email)'), $emails)->get() as $customer) {
  549. $byEmail[strtolower((string) $customer->email)] = $byEmail[strtolower((string) $customer->email)] ?? $customer;
  550. if ($customer->migrated_from_asteria_id) {
  551. $byAsteriaId[(int) $customer->migrated_from_asteria_id] = $byAsteriaId[(int) $customer->migrated_from_asteria_id] ?? $customer;
  552. }
  553. }
  554. }
  555. return [$byAsteriaId, $byEmail];
  556. }
  557. /**
  558. * @param array<int, int> $customerIds
  559. * @return array<int, array<int, int>>
  560. */
  561. private function loadExistingAddressIds(array $customerIds): array
  562. {
  563. $customerIds = array_values(array_unique(array_filter($customerIds)));
  564. if ($customerIds === []) {
  565. return [];
  566. }
  567. $map = [];
  568. $rows = DB::table('addresses')
  569. ->where('address_type', CustomerAddress::ADDRESS_TYPE)
  570. ->whereIn('customer_id', $customerIds)
  571. ->get(['customer_id', 'additional']);
  572. foreach ($rows as $row) {
  573. $asteriaId = $this->asteriaAddressIdFromAdditional($row->additional);
  574. if ($asteriaId) {
  575. $map[(int) $row->customer_id][] = $asteriaId;
  576. }
  577. }
  578. return $map;
  579. }
  580. /**
  581. * @param Collection<int, array<string, mixed>> $addressRows
  582. * @param array<int, int> $existingIds
  583. * @return array<int, array<string, mixed>>
  584. */
  585. private function buildAddressRows(
  586. int $customerId,
  587. array $customerRow,
  588. $addressRows,
  589. string $email,
  590. string $firstName,
  591. string $lastName,
  592. mixed $phone,
  593. array $existingIds
  594. ): array {
  595. $defaultBilling = (int) ($customerRow['default_billing'] ?? 0);
  596. $defaultShipping = (int) ($customerRow['default_shipping'] ?? 0);
  597. $now = now()->format('Y-m-d H:i:s');
  598. $rows = [];
  599. foreach ($addressRows as $row) {
  600. $asteriaAddressId = (int) $row['entity_id'];
  601. if (in_array($asteriaAddressId, $existingIds, true)) {
  602. continue;
  603. }
  604. $rows[] = [
  605. 'customer_id' => $customerId,
  606. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  607. 'first_name' => $this->requiredName($row['firstname'] ?? null, $firstName),
  608. 'last_name' => trim((string) ($row['lastname'] ?? '')) ?: $lastName,
  609. 'company_name' => $this->nullableString($row['company'] ?? null),
  610. 'address' => $this->mapStreet($row['street'] ?? null) ?: '-',
  611. 'city' => trim((string) ($row['city'] ?? '')) ?: '-',
  612. 'state' => $this->nullableString($row['region'] ?? null),
  613. 'country' => $this->nullableString($row['country_id'] ?? null),
  614. 'postcode' => $this->nullableString($row['postcode'] ?? null),
  615. 'email' => $email,
  616. 'phone' => $this->nullableString($row['telephone'] ?? null) ?? $phone,
  617. 'default_address' => $defaultBilling > 0 && $asteriaAddressId === $defaultBilling ? 1 : 0,
  618. 'use_for_shipping' => $defaultShipping > 0 && $asteriaAddressId === $defaultShipping ? 1 : 0,
  619. 'additional' => json_encode(['asteria_address_id' => $asteriaAddressId]),
  620. 'created_at' => $now,
  621. 'updated_at' => $now,
  622. ];
  623. }
  624. return $rows;
  625. }
  626. /**
  627. * @param array<int, array<string, mixed>> $rows
  628. */
  629. private function insertRows(string $table, array $rows): void
  630. {
  631. foreach (array_chunk($rows, self::INSERT_CHUNK) as $chunk) {
  632. DB::table($table)->insert($chunk);
  633. }
  634. }
  635. /**
  636. * @param array<string, int> $usedPhones
  637. */
  638. private function uniquePhone(string $phone, array &$usedPhones): ?string
  639. {
  640. $phone = trim($phone);
  641. if ($phone === '') {
  642. return null;
  643. }
  644. $key = mb_strtolower($phone);
  645. if (isset($usedPhones[$key])) {
  646. return null;
  647. }
  648. $usedPhones[$key] = 1;
  649. return $phone;
  650. }
  651. private function mapGender(mixed $value): ?string
  652. {
  653. return match ((int) $value) {
  654. 1 => 'Male',
  655. 2 => 'Female',
  656. default => null,
  657. };
  658. }
  659. private function mapCustomerSource(mixed $value): ?int
  660. {
  661. if ($value === null || $value === '') {
  662. return null;
  663. }
  664. if (! is_numeric($value)) {
  665. return null;
  666. }
  667. return (int) $value;
  668. }
  669. private function mapDate(mixed $value): ?string
  670. {
  671. $value = trim((string) $value);
  672. if ($value === '' || str_starts_with($value, '0000-00-00')) {
  673. return null;
  674. }
  675. return substr($value, 0, 10);
  676. }
  677. private function mapStreet(mixed $value): string
  678. {
  679. $value = trim((string) $value);
  680. if ($value === '') {
  681. return '';
  682. }
  683. $lines = preg_split("/\r\n|\n|\r/", $value) ?: [];
  684. return implode(', ', array_filter(array_map('trim', $lines)));
  685. }
  686. private function requiredName(mixed $value, string $fallback): string
  687. {
  688. $value = trim((string) $value);
  689. if ($value !== '') {
  690. return $value;
  691. }
  692. $local = strstr($fallback, '@', true);
  693. return $local !== false && $local !== '' ? $local : 'Customer';
  694. }
  695. private function nullableString(mixed $value): ?string
  696. {
  697. $value = trim((string) $value);
  698. return $value === '' ? null : $value;
  699. }
  700. private function asteriaAddressIdFromAdditional(mixed $additional): ?int
  701. {
  702. if (is_string($additional) && $additional !== '') {
  703. $additional = json_decode($additional, true);
  704. }
  705. if (! is_array($additional)) {
  706. return null;
  707. }
  708. return isset($additional['asteria_address_id'])
  709. ? (int) $additional['asteria_address_id']
  710. : null;
  711. }
  712. }