RepairMigratedVariantSkus.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Schema;
  7. use Webkul\Product\Models\Product;
  8. /**
  9. * Rewrites flexible-variant SKUs that were migrated with option sort_order suffixes.
  10. *
  11. * The new suffix is the option value SKU (`product_option_values.code`), in option position order.
  12. * Variant rows, images, and ids stay in place. Order, invoice, shipment, and
  13. * refund item SKUs that belong to those variants are rewritten to the same SKU.
  14. */
  15. class RepairMigratedVariantSkus extends Command
  16. {
  17. protected $signature = 'products:repair-variant-skus
  18. {--dry-run : Print the SKU changes without writing}
  19. {--sku= : Repair one parent product SKU}';
  20. protected $description = 'Rewrite migrated variant and order item SKUs from option value SKUs';
  21. private int $updated = 0;
  22. private int $unchanged = 0;
  23. private int $skipped = 0;
  24. private int $collisions = 0;
  25. private int $orderItems = 0;
  26. /** @var array<int, array{0: int, 1: string, 2: string}> */
  27. private array $examples = [];
  28. public function handle(): int
  29. {
  30. $dryRun = (bool) $this->option('dry-run');
  31. $sku = trim((string) $this->option('sku'));
  32. $query = Product::query()
  33. ->whereNotNull('migrated_from_asteria_id')
  34. ->where('type', 'flexible_variant')
  35. ->select('id', 'sku')
  36. ->orderBy('id');
  37. if ($sku !== '') {
  38. $query->where('sku', $sku);
  39. }
  40. $query->chunkById(20, function (Collection $products) use ($dryRun) {
  41. $this->repairChunk($products, $dryRun);
  42. });
  43. if ($this->examples !== []) {
  44. $this->table(['variant_id', 'from', 'to'], $this->examples);
  45. }
  46. $this->info(sprintf(
  47. '%s updated=%d unchanged=%d skipped=%d collisions=%d order_skus=%d',
  48. $dryRun ? '[DRY RUN]' : 'Done.',
  49. $this->updated,
  50. $this->unchanged,
  51. $this->skipped,
  52. $this->collisions,
  53. $this->orderItems
  54. ));
  55. return $this->collisions > 0 ? self::FAILURE : self::SUCCESS;
  56. }
  57. private function repairChunk(Collection $products, bool $dryRun): void
  58. {
  59. $productIds = $products->pluck('id')->map(fn ($id) => (int) $id)->all();
  60. $parentSku = $products->mapWithKeys(fn ($product) => [(int) $product->id => trim((string) $product->sku)]);
  61. $optionCounts = DB::table('product_product_options')
  62. ->whereIn('product_id', $productIds)
  63. ->select('product_id', DB::raw('COUNT(*) as aggregate'))
  64. ->groupBy('product_id')
  65. ->pluck('aggregate', 'product_id');
  66. $rows = DB::table('product_variants as v')
  67. ->join('product_variant_option_values as pvov', 'pvov.product_variant_id', '=', 'v.id')
  68. ->join('product_option_values as pov', 'pov.id', '=', 'pvov.product_option_value_id')
  69. ->join('product_product_options as ppo', function ($join) {
  70. $join->on('ppo.product_option_id', '=', 'pov.product_option_id')
  71. ->on('ppo.product_id', '=', 'v.product_id');
  72. })
  73. ->whereIn('v.product_id', $productIds)
  74. ->whereNull('v.deleted_at')
  75. ->orderBy('v.id')
  76. ->orderBy('ppo.position')
  77. ->orderBy('pov.id')
  78. ->join('product_options as po', 'po.id', '=', 'pov.product_option_id')
  79. ->get([
  80. 'v.id as variant_id',
  81. 'v.product_id',
  82. 'v.sku',
  83. 'pov.product_option_id',
  84. 'po.label as option_label',
  85. 'pov.label as value_label',
  86. 'pov.code as value_sku',
  87. ]);
  88. $grouped = [];
  89. foreach ($rows as $row) {
  90. $variantId = (int) $row->variant_id;
  91. $grouped[$variantId]['product_id'] = (int) $row->product_id;
  92. $grouped[$variantId]['sku'] = (string) $row->sku;
  93. $grouped[$variantId]['parts'][] = $row;
  94. }
  95. $liveVariantIds = DB::table('product_variants')
  96. ->whereIn('product_id', $productIds)
  97. ->whereNull('deleted_at')
  98. ->pluck('id');
  99. foreach ($liveVariantIds as $variantId) {
  100. if (! isset($grouped[(int) $variantId])) {
  101. $this->skipped++;
  102. }
  103. }
  104. /** @var array<int, string> $planned */
  105. $planned = [];
  106. /** @var array<int, string> $previous */
  107. $previous = [];
  108. /** @var array<int, array{product_id: int, sku: string, pairs: array<int, array{0: string, 1: string}>}> $targets */
  109. $targets = [];
  110. foreach ($grouped as $variantId => $info) {
  111. $expected = (int) ($optionCounts[$info['product_id']] ?? 0);
  112. $seenOptions = [];
  113. $labels = [];
  114. $invalid = false;
  115. $pairs = [];
  116. foreach ($info['parts'] as $part) {
  117. $optionId = (int) $part->product_option_id;
  118. if (isset($seenOptions[$optionId])) {
  119. $invalid = true;
  120. break;
  121. }
  122. $seenOptions[$optionId] = true;
  123. $optionLabel = trim((string) $part->option_label);
  124. $valueLabel = trim((string) $part->value_label);
  125. $valueSku = trim((string) ($part->value_sku ?? ''));
  126. if ($valueSku === '') {
  127. $valueSku = $valueLabel;
  128. }
  129. if ($optionLabel === '' || $valueLabel === '' || $valueSku === '') {
  130. $invalid = true;
  131. break;
  132. }
  133. $labels[] = $valueSku;
  134. $pairs[] = [$optionLabel, $valueLabel];
  135. }
  136. if ($invalid || $expected === 0 || count($labels) !== $expected) {
  137. $this->skipped++;
  138. continue;
  139. }
  140. $newSku = $parentSku[$info['product_id']].'-'.implode('-', $labels);
  141. if (strlen($newSku) > 255) {
  142. $this->skipped++;
  143. $this->warn("Variant {$variantId} SKU exceeds 255 characters.");
  144. continue;
  145. }
  146. $targets[$variantId] = [
  147. 'product_id' => $info['product_id'],
  148. 'sku' => $newSku,
  149. 'pairs' => $pairs,
  150. ];
  151. if ($newSku === $info['sku']) {
  152. $this->unchanged++;
  153. continue;
  154. }
  155. $planned[$variantId] = $newSku;
  156. $previous[$variantId] = $info['sku'];
  157. }
  158. $this->dropCollisions($planned, $previous);
  159. foreach ($targets as $variantId => $target) {
  160. $current = (string) ($grouped[$variantId]['sku'] ?? '');
  161. if ($target['sku'] !== $current && ! isset($planned[$variantId])) {
  162. unset($targets[$variantId]);
  163. }
  164. }
  165. if ($dryRun) {
  166. foreach ($planned as $variantId => $newSku) {
  167. $this->rememberExample($variantId, $previous[$variantId], $newSku);
  168. $this->updated++;
  169. }
  170. $this->rewriteOrderSkus($targets, $productIds, true);
  171. return;
  172. }
  173. if ($planned !== []) {
  174. $this->writeSkus($planned, $previous);
  175. $this->updated += count($planned);
  176. }
  177. $this->rewriteOrderSkus($targets, $productIds, false);
  178. }
  179. /**
  180. * @param array<int, string> $planned
  181. * @param array<int, string> $previous
  182. */
  183. private function dropCollisions(array &$planned, array &$previous): void
  184. {
  185. if ($planned === []) {
  186. return;
  187. }
  188. $owners = DB::table('product_variants')
  189. ->whereIn('sku', array_values($planned))
  190. ->pluck('id', 'sku');
  191. $blocked = [];
  192. $seen = [];
  193. foreach ($planned as $variantId => $newSku) {
  194. if (isset($seen[$newSku])) {
  195. $blocked[$variantId] = true;
  196. $blocked[$seen[$newSku]] = true;
  197. continue;
  198. }
  199. $seen[$newSku] = $variantId;
  200. $ownerId = isset($owners[$newSku]) ? (int) $owners[$newSku] : null;
  201. if ($ownerId === null || $ownerId === (int) $variantId) {
  202. continue;
  203. }
  204. $ownerNext = $planned[$ownerId] ?? null;
  205. if ($ownerNext === null || $ownerNext === $newSku) {
  206. $blocked[$variantId] = true;
  207. }
  208. }
  209. foreach (array_keys($blocked) as $variantId) {
  210. $this->warn("SKU collision, left variant {$variantId} as {$previous[$variantId]}.");
  211. unset($planned[$variantId], $previous[$variantId]);
  212. $this->collisions++;
  213. }
  214. }
  215. /**
  216. * @param array<int, string> $planned
  217. * @param array<int, string> $previous
  218. */
  219. private function writeSkus(array $planned, array $previous): void
  220. {
  221. $now = now()->toDateTimeString();
  222. DB::transaction(function () use ($planned, $previous, $now) {
  223. foreach (array_chunk(array_keys($planned), 200) as $ids) {
  224. DB::table('product_variants')
  225. ->whereIn('id', $ids)
  226. ->update([
  227. 'sku' => DB::raw("CONCAT('__sku_repair_', id)"),
  228. 'updated_at' => $now,
  229. ]);
  230. }
  231. foreach (array_chunk($planned, 200, true) as $chunk) {
  232. $case = 'CASE id';
  233. $bindings = [];
  234. $ids = array_keys($chunk);
  235. foreach ($chunk as $variantId => $newSku) {
  236. $case .= ' WHEN ? THEN ?';
  237. $bindings[] = $variantId;
  238. $bindings[] = $newSku;
  239. $this->rememberExample($variantId, $previous[$variantId], $newSku);
  240. }
  241. $case .= ' END';
  242. $placeholders = implode(',', array_fill(0, count($chunk), '?'));
  243. DB::update(
  244. "UPDATE product_variants SET sku = {$case}, updated_at = ? WHERE id IN ({$placeholders})",
  245. array_merge($bindings, [$now], $ids)
  246. );
  247. }
  248. });
  249. }
  250. private function rememberExample(int $variantId, string $from, string $to): void
  251. {
  252. if (count($this->examples) >= 15) {
  253. return;
  254. }
  255. $this->examples[] = [$variantId, $from, $to];
  256. }
  257. /**
  258. * Point order documents at the variant SKU. Match variant_id first, then the
  259. * option labels stored on the order item.
  260. *
  261. * @param array<int, array{product_id: int, sku: string, pairs: array<int, array{0: string, 1: string}>}> $targets
  262. * @param array<int, int> $productIds
  263. */
  264. private function rewriteOrderSkus(array $targets, array $productIds, bool $dryRun): void
  265. {
  266. if ($targets === [] || $productIds === [] || ! Schema::hasTable('order_items')) {
  267. return;
  268. }
  269. $byVariant = [];
  270. $byProductAndKey = [];
  271. $optionNames = [];
  272. foreach ($targets as $variantId => $target) {
  273. $byVariant[$variantId] = $target['sku'];
  274. $key = $this->labelKey($target['pairs']);
  275. $byProductAndKey[$target['product_id']][$key][] = $variantId;
  276. foreach ($target['pairs'] as [$optionLabel]) {
  277. $optionNames[$target['product_id']][$this->normalize($optionLabel)] = true;
  278. }
  279. }
  280. DB::table('order_items')
  281. ->whereIn('product_id', $productIds)
  282. ->orderBy('id')
  283. ->select('id', 'product_id', 'sku', 'additional')
  284. ->chunkById(500, function (Collection $items) use ($byVariant, $byProductAndKey, $optionNames, $dryRun) {
  285. $skuUpdates = [];
  286. $additionalUpdates = [];
  287. foreach ($items as $item) {
  288. $productId = (int) $item->product_id;
  289. $additional = $this->decodeAdditional($item->additional);
  290. $newSku = $this->resolveOrderSku($productId, $additional, $byVariant, $byProductAndKey, $optionNames);
  291. if ($newSku === null) {
  292. continue;
  293. }
  294. if ((string) $item->sku !== $newSku) {
  295. $skuUpdates[(int) $item->id] = $newSku;
  296. }
  297. if ($this->additionalVariantSku($additional, $newSku)) {
  298. $additionalUpdates[(int) $item->id] = $additional;
  299. }
  300. }
  301. $this->orderItems += count($skuUpdates);
  302. $now = now()->toDateTimeString();
  303. $this->syncRelatedItemSkus($skuUpdates, $dryRun, $now);
  304. if ($dryRun || ($skuUpdates === [] && $additionalUpdates === [])) {
  305. return;
  306. }
  307. $this->writeColumnSkus('order_items', $skuUpdates, $now);
  308. $this->writeAdditional($additionalUpdates, $now);
  309. });
  310. }
  311. /**
  312. * @param array<string, mixed> $additional
  313. * @param array<int, string> $byVariant
  314. * @param array<int, array<string, array<int, int>>> $byProductAndKey
  315. * @param array<int, array<string, true>> $optionNames
  316. */
  317. private function resolveOrderSku(
  318. int $productId,
  319. array $additional,
  320. array $byVariant,
  321. array $byProductAndKey,
  322. array $optionNames
  323. ): ?string {
  324. $variantId = (int) ($additional['variant_id'] ?? 0);
  325. if ($variantId > 0 && isset($byVariant[$variantId])) {
  326. return $byVariant[$variantId];
  327. }
  328. $pairs = $this->orderLabelPairs($additional, $optionNames[$productId] ?? []);
  329. if ($pairs === []) {
  330. return null;
  331. }
  332. $ids = $byProductAndKey[$productId][$this->labelKey($pairs)] ?? [];
  333. if (count($ids) !== 1) {
  334. return null;
  335. }
  336. return $byVariant[$ids[0]] ?? null;
  337. }
  338. /**
  339. * @param array<string, mixed> $additional
  340. * @param array<string, true> $optionNames
  341. * @return array<int, array{0: string, 1: string}>
  342. */
  343. private function orderLabelPairs(array $additional, array $optionNames): array
  344. {
  345. $attributes = $additional['attributes'] ?? null;
  346. if (! is_array($attributes) || $attributes === []) {
  347. return [];
  348. }
  349. $pairs = [];
  350. $list = array_is_list($attributes);
  351. foreach ($attributes as $row) {
  352. if (! is_array($row)) {
  353. continue;
  354. }
  355. if ($list) {
  356. $option = trim((string) ($row['attribute_name'] ?? $row['label'] ?? ''));
  357. $value = trim((string) ($row['option_label'] ?? $row['value_label'] ?? ''));
  358. } else {
  359. $option = trim((string) ($row['attribute_name'] ?? $row['option_label'] ?? ''));
  360. $value = trim((string) ($row['value_label'] ?? ''));
  361. }
  362. if ($option === '' || $value === '' || ! isset($optionNames[$this->normalize($option)])) {
  363. continue;
  364. }
  365. $pairs[] = [$option, $value];
  366. }
  367. return $pairs;
  368. }
  369. /**
  370. * @param array<string, mixed> $additional
  371. */
  372. private function additionalVariantSku(array &$additional, string $newSku): bool
  373. {
  374. $hasVariant = ! empty($additional['variant_id']) || array_key_exists('variant_sku', $additional);
  375. if (! $hasVariant || (string) ($additional['variant_sku'] ?? '') === $newSku) {
  376. return false;
  377. }
  378. $additional['variant_sku'] = $newSku;
  379. return true;
  380. }
  381. /**
  382. * @param array<int, array{0: string, 1: string}> $pairs
  383. */
  384. private function labelKey(array $pairs): string
  385. {
  386. $parts = [];
  387. foreach ($pairs as [$option, $value]) {
  388. $parts[] = $this->normalize($option)."\n".$value;
  389. }
  390. sort($parts);
  391. return implode("\n---\n", $parts);
  392. }
  393. private function normalize(string $value): string
  394. {
  395. return mb_strtolower(trim($value));
  396. }
  397. /**
  398. * @return array<string, mixed>
  399. */
  400. private function decodeAdditional(mixed $value): array
  401. {
  402. if (is_array($value)) {
  403. return $value;
  404. }
  405. if (is_string($value) && $value !== '') {
  406. $decoded = json_decode($value, true);
  407. return is_array($decoded) ? $decoded : [];
  408. }
  409. return [];
  410. }
  411. /**
  412. * @param array<int, string> $skuUpdates
  413. */
  414. private function syncRelatedItemSkus(array $skuUpdates, bool $dryRun, string $now): void
  415. {
  416. if ($skuUpdates === []) {
  417. return;
  418. }
  419. foreach (['invoice_items', 'shipment_items', 'refund_items'] as $table) {
  420. if (! Schema::hasTable($table) || ! Schema::hasColumn($table, 'sku') || ! Schema::hasColumn($table, 'order_item_id')) {
  421. continue;
  422. }
  423. $rows = DB::table($table)
  424. ->whereIn('order_item_id', array_keys($skuUpdates))
  425. ->get(['id', 'order_item_id', 'sku']);
  426. $updates = [];
  427. foreach ($rows as $row) {
  428. $newSku = $skuUpdates[(int) $row->order_item_id] ?? null;
  429. if ($newSku === null || (string) $row->sku === $newSku) {
  430. continue;
  431. }
  432. $updates[(int) $row->id] = $newSku;
  433. }
  434. $this->orderItems += count($updates);
  435. if (! $dryRun && $updates !== []) {
  436. $this->writeColumnSkus($table, $updates, $now);
  437. }
  438. }
  439. }
  440. /**
  441. * @param array<int, string> $updates
  442. */
  443. private function writeColumnSkus(string $table, array $updates, string $now): void
  444. {
  445. foreach (array_chunk($updates, 200, true) as $chunk) {
  446. $case = 'CASE id';
  447. $bindings = [];
  448. foreach ($chunk as $id => $sku) {
  449. $case .= ' WHEN ? THEN ?';
  450. $bindings[] = $id;
  451. $bindings[] = $sku;
  452. }
  453. $case .= ' END';
  454. $placeholders = implode(',', array_fill(0, count($chunk), '?'));
  455. DB::update(
  456. "UPDATE {$table} SET sku = {$case}, updated_at = ? WHERE id IN ({$placeholders})",
  457. array_merge($bindings, [$now], array_keys($chunk))
  458. );
  459. }
  460. }
  461. /**
  462. * @param array<int, array<string, mixed>> $updates
  463. */
  464. private function writeAdditional(array $updates, string $now): void
  465. {
  466. foreach ($updates as $id => $additional) {
  467. $encoded = json_encode($additional, JSON_UNESCAPED_UNICODE);
  468. if ($encoded === false) {
  469. continue;
  470. }
  471. DB::table('order_items')->where('id', $id)->update([
  472. 'additional' => $encoded,
  473. 'updated_at' => $now,
  474. ]);
  475. }
  476. }
  477. }