RepairMigratedVariantSkus.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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($planned, 200, true) as $chunk) {
  224. $ids = array_keys($chunk);
  225. DB::table('product_variants')
  226. ->whereIn('id', $ids)
  227. ->update([
  228. 'sku' => DB::raw("CONCAT('__sku_repair_', id)"),
  229. 'updated_at' => $now,
  230. ]);
  231. $case = 'CASE id';
  232. $bindings = [];
  233. foreach ($chunk as $variantId => $newSku) {
  234. $case .= ' WHEN ? THEN ?';
  235. $bindings[] = $variantId;
  236. $bindings[] = $newSku;
  237. $this->rememberExample($variantId, $previous[$variantId], $newSku);
  238. }
  239. $case .= ' END';
  240. $placeholders = implode(',', array_fill(0, count($chunk), '?'));
  241. DB::update(
  242. "UPDATE product_variants SET sku = {$case}, updated_at = ? WHERE id IN ({$placeholders})",
  243. array_merge($bindings, [$now], $ids)
  244. );
  245. }
  246. });
  247. }
  248. private function rememberExample(int $variantId, string $from, string $to): void
  249. {
  250. if (count($this->examples) >= 15) {
  251. return;
  252. }
  253. $this->examples[] = [$variantId, $from, $to];
  254. }
  255. /**
  256. * Point order documents at the variant SKU. Match variant_id first, then the
  257. * option labels stored on the order item.
  258. *
  259. * @param array<int, array{product_id: int, sku: string, pairs: array<int, array{0: string, 1: string}>}> $targets
  260. * @param array<int, int> $productIds
  261. */
  262. private function rewriteOrderSkus(array $targets, array $productIds, bool $dryRun): void
  263. {
  264. if ($targets === [] || $productIds === [] || ! Schema::hasTable('order_items')) {
  265. return;
  266. }
  267. $byVariant = [];
  268. $byProductAndKey = [];
  269. $optionNames = [];
  270. foreach ($targets as $variantId => $target) {
  271. $byVariant[$variantId] = $target['sku'];
  272. $key = $this->labelKey($target['pairs']);
  273. $byProductAndKey[$target['product_id']][$key][] = $variantId;
  274. foreach ($target['pairs'] as [$optionLabel]) {
  275. $optionNames[$target['product_id']][$this->normalize($optionLabel)] = true;
  276. }
  277. }
  278. DB::table('order_items')
  279. ->whereIn('product_id', $productIds)
  280. ->orderBy('id')
  281. ->select('id', 'product_id', 'sku', 'additional')
  282. ->chunkById(500, function (Collection $items) use ($byVariant, $byProductAndKey, $optionNames, $dryRun) {
  283. $skuUpdates = [];
  284. $additionalUpdates = [];
  285. foreach ($items as $item) {
  286. $productId = (int) $item->product_id;
  287. $additional = $this->decodeAdditional($item->additional);
  288. $newSku = $this->resolveOrderSku($productId, $additional, $byVariant, $byProductAndKey, $optionNames);
  289. if ($newSku === null) {
  290. continue;
  291. }
  292. if ((string) $item->sku !== $newSku) {
  293. $skuUpdates[(int) $item->id] = $newSku;
  294. }
  295. if ($this->additionalVariantSku($additional, $newSku)) {
  296. $additionalUpdates[(int) $item->id] = $additional;
  297. }
  298. }
  299. $this->orderItems += count($skuUpdates);
  300. $now = now()->toDateTimeString();
  301. $this->syncRelatedItemSkus($skuUpdates, $dryRun, $now);
  302. if ($dryRun || ($skuUpdates === [] && $additionalUpdates === [])) {
  303. return;
  304. }
  305. $this->writeColumnSkus('order_items', $skuUpdates, $now);
  306. $this->writeAdditional($additionalUpdates, $now);
  307. });
  308. }
  309. /**
  310. * @param array<string, mixed> $additional
  311. * @param array<int, string> $byVariant
  312. * @param array<int, array<string, array<int, int>>> $byProductAndKey
  313. * @param array<int, array<string, true>> $optionNames
  314. */
  315. private function resolveOrderSku(
  316. int $productId,
  317. array $additional,
  318. array $byVariant,
  319. array $byProductAndKey,
  320. array $optionNames
  321. ): ?string {
  322. $variantId = (int) ($additional['variant_id'] ?? 0);
  323. if ($variantId > 0 && isset($byVariant[$variantId])) {
  324. return $byVariant[$variantId];
  325. }
  326. $pairs = $this->orderLabelPairs($additional, $optionNames[$productId] ?? []);
  327. if ($pairs === []) {
  328. return null;
  329. }
  330. $ids = $byProductAndKey[$productId][$this->labelKey($pairs)] ?? [];
  331. if (count($ids) !== 1) {
  332. return null;
  333. }
  334. return $byVariant[$ids[0]] ?? null;
  335. }
  336. /**
  337. * @param array<string, mixed> $additional
  338. * @param array<string, true> $optionNames
  339. * @return array<int, array{0: string, 1: string}>
  340. */
  341. private function orderLabelPairs(array $additional, array $optionNames): array
  342. {
  343. $attributes = $additional['attributes'] ?? null;
  344. if (! is_array($attributes) || $attributes === []) {
  345. return [];
  346. }
  347. $pairs = [];
  348. $list = array_is_list($attributes);
  349. foreach ($attributes as $row) {
  350. if (! is_array($row)) {
  351. continue;
  352. }
  353. if ($list) {
  354. $option = trim((string) ($row['attribute_name'] ?? $row['label'] ?? ''));
  355. $value = trim((string) ($row['option_label'] ?? $row['value_label'] ?? ''));
  356. } else {
  357. $option = trim((string) ($row['attribute_name'] ?? $row['option_label'] ?? ''));
  358. $value = trim((string) ($row['value_label'] ?? ''));
  359. }
  360. if ($option === '' || $value === '' || ! isset($optionNames[$this->normalize($option)])) {
  361. continue;
  362. }
  363. $pairs[] = [$option, $value];
  364. }
  365. return $pairs;
  366. }
  367. /**
  368. * @param array<string, mixed> $additional
  369. */
  370. private function additionalVariantSku(array &$additional, string $newSku): bool
  371. {
  372. $hasVariant = ! empty($additional['variant_id']) || array_key_exists('variant_sku', $additional);
  373. if (! $hasVariant || (string) ($additional['variant_sku'] ?? '') === $newSku) {
  374. return false;
  375. }
  376. $additional['variant_sku'] = $newSku;
  377. return true;
  378. }
  379. /**
  380. * @param array<int, array{0: string, 1: string}> $pairs
  381. */
  382. private function labelKey(array $pairs): string
  383. {
  384. $parts = [];
  385. foreach ($pairs as [$option, $value]) {
  386. $parts[] = $this->normalize($option)."\n".$value;
  387. }
  388. sort($parts);
  389. return implode("\n---\n", $parts);
  390. }
  391. private function normalize(string $value): string
  392. {
  393. return mb_strtolower(trim($value));
  394. }
  395. /**
  396. * @return array<string, mixed>
  397. */
  398. private function decodeAdditional(mixed $value): array
  399. {
  400. if (is_array($value)) {
  401. return $value;
  402. }
  403. if (is_string($value) && $value !== '') {
  404. $decoded = json_decode($value, true);
  405. return is_array($decoded) ? $decoded : [];
  406. }
  407. return [];
  408. }
  409. /**
  410. * @param array<int, string> $skuUpdates
  411. */
  412. private function syncRelatedItemSkus(array $skuUpdates, bool $dryRun, string $now): void
  413. {
  414. if ($skuUpdates === []) {
  415. return;
  416. }
  417. foreach (['invoice_items', 'shipment_items', 'refund_items'] as $table) {
  418. if (! Schema::hasTable($table) || ! Schema::hasColumn($table, 'sku') || ! Schema::hasColumn($table, 'order_item_id')) {
  419. continue;
  420. }
  421. $rows = DB::table($table)
  422. ->whereIn('order_item_id', array_keys($skuUpdates))
  423. ->get(['id', 'order_item_id', 'sku']);
  424. $updates = [];
  425. foreach ($rows as $row) {
  426. $newSku = $skuUpdates[(int) $row->order_item_id] ?? null;
  427. if ($newSku === null || (string) $row->sku === $newSku) {
  428. continue;
  429. }
  430. $updates[(int) $row->id] = $newSku;
  431. }
  432. $this->orderItems += count($updates);
  433. if (! $dryRun && $updates !== []) {
  434. $this->writeColumnSkus($table, $updates, $now);
  435. }
  436. }
  437. }
  438. /**
  439. * @param array<int, string> $updates
  440. */
  441. private function writeColumnSkus(string $table, array $updates, string $now): void
  442. {
  443. foreach (array_chunk($updates, 200, true) as $chunk) {
  444. $case = 'CASE id';
  445. $bindings = [];
  446. foreach ($chunk as $id => $sku) {
  447. $case .= ' WHEN ? THEN ?';
  448. $bindings[] = $id;
  449. $bindings[] = $sku;
  450. }
  451. $case .= ' END';
  452. $placeholders = implode(',', array_fill(0, count($chunk), '?'));
  453. DB::update(
  454. "UPDATE {$table} SET sku = {$case}, updated_at = ? WHERE id IN ({$placeholders})",
  455. array_merge($bindings, [$now], array_keys($chunk))
  456. );
  457. }
  458. }
  459. /**
  460. * @param array<int, array<string, mixed>> $updates
  461. */
  462. private function writeAdditional(array $updates, string $now): void
  463. {
  464. foreach ($updates as $id => $additional) {
  465. $encoded = json_encode($additional, JSON_UNESCAPED_UNICODE);
  466. if ($encoded === false) {
  467. continue;
  468. }
  469. DB::table('order_items')->where('id', $id)->update([
  470. 'additional' => $encoded,
  471. 'updated_at' => $now,
  472. ]);
  473. }
  474. }
  475. }