'shop_code' => 'required|exists:shops,code', 'product_id' => 'nullable|exists:products,id', 'units_sold' => 'required|integer|min:1', 'cash_collected' => 'required|numeric|min:0', 'refill_quantity' => 'required|integer|min:0', 'payment_method' => 'required|in:cash,credit', 'add_bonus_product' => 'nullable|boolean', ]); // Find shop by code and verify it belongs to this agent $shop = Shop::where('code', $validated['shop_code']) ->where('agent_id', $agentId) ->firstOrFail(); // Use provided product or get first active product $productId = $validated['product_id'] ?? null; if ($productId) { $product = Product::findOrFail($productId); } else { $product = Product::active()->first(); if (!$product) { return redirect()->back()->with('error', 'No active products found in the system.'); } }