json()->all(); $orderId = (string) ($order['id'] ?? ''); // Idempotency: ignore already-processed orders if (ProductSale::where('shopify_order_id', $orderId)->exists()) { return response('', 200); } DB::transaction(function () use ($order, $orderId) { foreach ($order['line_items'] ?? [] as $item) { $variantId = (string) ($item['variant_id'] ?? ''); $quantity = (int) ($item['quantity'] ?? 0); if (!$variantId || $quantity <= 0) { continue; } $size = ProductSize::where('shopify_variant_id', $variantId)->first(); if (!$size) { Log::warning('Shopify webhook: unbekannte Variant-ID', [ 'variant_id' => $variantId, 'order_id' => $orderId, ]); continue; } // FIFO: älteste verfügbare Einheiten ausbuchen ProductInventory::where('size_id', $size->id) ->whereNull('out_at') ->oldest() ->limit($quantity) ->update(['out_at' => now()]); ProductSale::create([ 'product_id' => $size->product_id, 'size_id' => $size->id, 'amount' => $quantity, 'sold_at' => now(), 'shopify_order_id' => $orderId, ]); } }); return response('', 200); } }