option('dry-run'); $variants = $shopify->getAllVariants(); $added = 0; $removed = 0; $inSync = 0; DB::transaction(function () use ($variants, $dryRun, &$added, &$removed, &$inSync) { foreach ($variants as $variant) { $size = ProductSize::where('shopify_variant_id', $variant['id'])->first(); if (!$size) { continue; // not mapped yet — run shopify:map first } $shopifyQty = $variant['inventory_quantity']; $localQty = ProductInventory::where('size_id', $size->id) ->whereNull('out_at') ->count(); if ($localQty === $shopifyQty) { $inSync++; continue; } $diff = $shopifyQty - $localQty; if ($diff > 0) { // Shopify hat mehr → Einträge hinzufügen $this->line(" +{$diff} {$variant['product_title']} (SKU {$variant['sku']})"); if (!$dryRun) { for ($i = 0; $i < $diff; $i++) { ProductInventory::create([ 'product_id' => $size->product_id, 'size_id' => $size->id, ]); } } $added += $diff; } else { // Shopify hat weniger → älteste Einträge als ausgebucht markieren $excess = abs($diff); $this->line(" -{$excess} {$variant['product_title']} (SKU {$variant['sku']})"); if (!$dryRun) { ProductInventory::where('size_id', $size->id) ->whereNull('out_at') ->oldest() ->limit($excess) ->update(['out_at' => now()]); } $removed += $excess; } } }); $this->info("✓ Sync abgeschlossen" . ($dryRun ? ' (dry-run)' : '')); $this->info(" In Sync: {$inSync} | Hinzugefügt: {$added} | Ausgebucht: {$removed}"); return self::SUCCESS; } }