Untitled
public function pneumatiky() { session(['previous_url' => url()->full()]); $widthOptions = []; // Nahraďte reálnymi hodnotami $aspectRatioOptions = []; // Nahraďte reálnymi hodnotami $diameterOptions = []; // Nahraďte reálnymi hodnotami $manufacturerOptions = []; // Nahraďte reálnymi hodnotami $seasonOptions = []; // Nahraďte reálnymi hodnotami $products = []; $widthOptions = DB::table('attribute_values') ->join('attributes', 'attribute_values.attribute_id', '=', 'attributes.id') ->where('attributes.name', 'width') ->pluck('attribute_values.value') ->unique() ->sort(); $aspectRatioOptions = DB::table('attribute_values') ->join('attributes', 'attribute_values.attribute_id', '=', 'attributes.id') ->where('attributes.name', 'aspect_ratio') ->pluck('attribute_values.value') ->unique() ->sort(); $diameterOptions = DB::table('attribute_values') ->join('attributes', 'attribute_values.attribute_id', '=', 'attributes.id') ->where('attributes.name', 'diameter') ->pluck('attribute_values.value') ->unique() ->sort(); $manufacturerOptions = DB::table('products') ->pluck('vyrobca') ->unique() ->sort(); $seasonOptions = DB::table('attribute_values') ->join('attributes', 'attribute_values.attribute_id', '=', 'attributes.id') ->where('attributes.name', 'season') ->pluck('attribute_values.value') ->unique() ->sort(); $liOptions = DB::table('attribute_values') ->join('attributes', 'attribute_values.attribute_id', '=', 'attributes.id') ->where('attributes.name', 'li') ->pluck('attribute_values.value') ->unique() ->sort(); $siOptions = DB::table('attribute_values') ->join('attributes', 'attribute_values.attribute_id', '=', 'attributes.id') ->where('attributes.name', 'si') ->pluck('attribute_values.value') ->unique() ->sort(); return view('layouts.shop', compact('widthOptions', 'aspectRatioOptions', 'diameterOptions', 'manufacturerOptions', 'seasonOptions','liOptions','siOptions', 'products')); } public function pneumatikySearch(Request $request) { session(['previous_url' => url()->full()]); // Načítanie dostupných možností pre filtre $widthOptions = AttributeValue::whereHas('attribute', fn($query) => $query->where('name', 'width'))->pluck('value')->unique()->sort(); $aspectRatioOptions = AttributeValue::whereHas('attribute', fn($query) => $query->where('name', 'aspect_ratio'))->pluck('value')->unique()->sort(); $diameterOptions = AttributeValue::whereHas('attribute', fn($query) => $query->where('name', 'diameter'))->pluck('value')->unique()->sort(); $manufacturerOptions = Product::pluck('vyrobca')->unique()->sort(); $seasonOptions = AttributeValue::whereHas('attribute', fn($query) => $query->where('name', 'season'))->pluck('value')->unique()->sort(); $liOptions = AttributeValue::whereHas('attribute', fn($query) => $query->where('name', 'li'))->pluck('value')->unique()->sort(); $siOptions = AttributeValue::whereHas('attribute', fn($query) => $query->where('name', 'si'))->pluck('value')->unique()->sort(); // Filtrovanie podľa požiadaviek z requestu $widthRequest = $request->input('width'); $aspectRatioRequest = $request->input('aspect_ratio'); $diameterRequest = $request->input('diameter'); $manufacturerRequest = $request->input('manufacturer'); $seasonRequest = $request->input('season'); $rofRequest = $request->input('rof'); $liRequest = $request->input('li'); $siRequest = $request->input('si'); $searchBy = $request->input('searchBy'); $categories = Category::whereNull('parent_id')->select('id', 'name', 'slug')->get(); // Základný dotaz na produkty s načítaním stock a filtrov $productsQuery = Product::with(['stocks', 'attributeValues']) ->when($widthRequest, fn($query) => $query->whereHas('attributeValues', fn($q) => $q->where('value', $widthRequest)->whereHas('attribute', fn($a) => $a->where('name', 'width')))) ->when($aspectRatioRequest, fn($query) => $query->whereHas('attributeValues', fn($q) => $q->where('value', $aspectRatioRequest)->whereHas('attribute', fn($a) => $a->where('name', 'aspect_ratio')))) ->when($diameterRequest, fn($query) => $query->whereHas('attributeValues', fn($q) => $q->where('value', $diameterRequest)->whereHas('attribute', fn($a) => $a->where('name', 'diameter')))) ->when($seasonRequest, fn($query) => $query->whereHas('attributeValues', fn($q) => $q->where('value', $seasonRequest)->whereHas('attribute', fn($a) => $a->where('name', 'season')))) ->when($liRequest, fn($query) => $query->whereHas('attributeValues', fn($q) => $q->where('value', $liRequest)->whereHas('attribute', fn($a) => $a->where('name', 'li')))) ->when($siRequest, fn($query) => $query->whereHas('attributeValues', fn($q) => $q->where('value', $siRequest)->whereHas('attribute', fn($a) => $a->where('name', 'si')))) ->when($rofRequest, fn($query) => $query->whereHas('attributeValues', fn($q) => $q->whereIn('value', ['rof', 'ROF'])->whereHas('attribute', fn($a) => $a->where('name', 'rof')))) ->when($manufacturerRequest, fn($query) => $query->whereIn('vyrobca', is_array($manufacturerRequest) ? $manufacturerRequest : [$manufacturerRequest])); // Načítanie produktov $products = $productsQuery->get(); // Pridanie min_price, min_delivery_date a min_qty do každého produktu foreach ($products as $key => $product) { $stockEntries = $product->stocks; $selectedStock = null; // Determine the selected stock based on search criteria if ($searchBy === 'price') { $selectedStock = $stockEntries->sortBy('price_buy')->first(); } elseif ($searchBy === 'availability') { $selectedStock = $stockEntries->sortBy(fn($stock) => Carbon::parse($stock['delivery_date'])) // Parse the delivery_date as a date ->first(); } else { $selectedStock = $stockEntries->sortBy('price_buy')->first(); } // Set product properties based on selected stock if ($selectedStock) { $product->min_price = $this->getPriceWithSurcharge($selectedStock->warehouse_id, $product->category_id, $selectedStock->price_buy); $product->min_delivery_date = $selectedStock->delivery_date; $product->min_qty = $selectedStock->qty; } else { $product->min_price = 0; $product->min_delivery_date = null; $product->min_qty = 0; } // Calculate total stock across all entries (sum of quantities) $totalStockQty = $stockEntries->sum('qty'); $product->total_qty = $totalStockQty; // Add total stock quantity to product for easier checks // Generate slug for the product $attributes = $product->attributes; $slugParts = array_filter([ $this->sanitizeSlugPart(optional($attributes->where('attribute.name', 'width')->first())->value), $this->sanitizeSlugPart(optional($attributes->where('attribute.name', 'aspect_ratio')->first())->value), $this->sanitizeSlugPart(optional($attributes->where('attribute.name', 'diameter')->first())->value), $this->sanitizeSlugPart(strtolower(str_replace(' ', '-', $product->vyrobca))), $this->sanitizeSlugPart(strtolower(str_replace(' ', '-', $product->model))), ]); $product->slug = implode('-', $slugParts); } // Separate in-stock and out-of-stock products based on total stock quantity $inStockProducts = $products->filter(fn($product) => $product->total_qty > 0); $outOfStockProducts = $products->filter(fn($product) => $product->total_qty == 0); // Sort in-stock products by the selected search criteria (price or availability) if ($searchBy === 'price') { $inStockProducts = $inStockProducts->sortBy('min_price'); // Sort in-stock products by price } elseif ($searchBy === 'availability') { $inStockProducts = $inStockProducts->sortBy(fn($product) => Carbon::parse($product->min_delivery_date)); // Sort in-stock products by delivery date } // Sort out-of-stock products by the selected search criteria, if needed if ($searchBy === 'price') { $outOfStockProducts = $outOfStockProducts->sortBy('min_price'); // Sort out-of-stock products by price } elseif ($searchBy === 'availability') { $outOfStockProducts = $outOfStockProducts->sortBy(fn($product) => Carbon::parse($product->min_delivery_date)); // Sort out-of-stock products by delivery date } // Combine in-stock and out-of-stock products, with in-stock first $sortedProducts = $inStockProducts->merge($outOfStockProducts); // Return to view return view('layouts.shop', [ 'categories' => $categories, 'products' => $sortedProducts->values()->toArray(), // Reset keys after merge 'widthOptions' => $widthOptions, 'aspectRatioOptions' => $aspectRatioOptions, 'diameterOptions' => $diameterOptions, 'manufacturerOptions' => $manufacturerOptions, 'seasonOptions' => $seasonOptions, 'liOptions' => $liOptions, 'siOptions' => $siOptions, 'widthRequest' => $widthRequest, 'aspectRatioRequest' => $aspectRatioRequest, 'diameterRequest' => $diameterRequest, 'manufacturerRequest' => $manufacturerRequest, 'seasonRequest' => $seasonRequest, 'liRequest' => $liRequest, 'siRequest' => $siRequest, ]); }
Leave a Comment