MigrateAsteriaCustomers.php 32 KB

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