|
2 | 2 |
|
3 | 3 | namespace App\Http\Controllers\Product; |
4 | 4 |
|
5 | | -use App\Models\Product; |
6 | | -use Illuminate\Http\Request; |
7 | 5 | use App\Http\Controllers\Controller; |
8 | 6 | use App\Http\Resources\ProductResource; |
| 7 | +use App\Models\Product; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Illuminate\Database\Eloquent\Builder; |
| 10 | + |
9 | 11 |
|
10 | 12 | class ProductByCategoryController extends Controller |
11 | 13 | { |
12 | 14 | public function index($categoryUrl){ |
13 | | - $products=Product::with('categories')->whereHas('categories',function($query) use ($categoryUrl){ |
| 15 | + $query=Product::with('categories')->whereHas('categories',function($query) use ($categoryUrl){ |
14 | 16 | $query->where('categories.category_url',$categoryUrl); |
15 | | - })->get(); |
16 | | - return response()->json(ProductResource::collection($products)); |
| 17 | + }); |
| 18 | + |
| 19 | + |
| 20 | + if ($request->has('min_price') || $request->has('max_price')) { |
| 21 | + $minPrice = $request->input('min_price') ? floatval($request->input('min_price')) : null; |
| 22 | + $maxPrice = $request->input('max_price') ? floatval($request->input('max_price')) : null; |
| 23 | + |
| 24 | + $query->where(function (Builder $subQuery) use ($minPrice, $maxPrice) { |
| 25 | + |
| 26 | + // Filtre pour les produits simples (prix dans la table 'products') |
| 27 | + $subQuery->where(function ($simpleProductQuery) use ($minPrice, $maxPrice) { |
| 28 | + if ($minPrice !== null) { |
| 29 | + $simpleProductQuery->whereRaw('CAST(product_price AS DECIMAL(10, 2)) >= ?', [$minPrice]); |
| 30 | + } |
| 31 | + if ($maxPrice !== null) { |
| 32 | + $simpleProductQuery->whereRaw('CAST(product_price AS DECIMAL(10, 2)) <= ?', [$maxPrice]); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + // Filtre pour les produits variés (couleur uniquement, prix dans la table 'product_variations') |
| 37 | + $subQuery->orWhereHas('variations', function (Builder $variationQuery) use ($minPrice, $maxPrice) { |
| 38 | + if ($minPrice !== null) { |
| 39 | + $variationQuery->whereRaw('CAST(price AS DECIMAL(10, 2)) >= ?', [$minPrice]); |
| 40 | + } |
| 41 | + if ($maxPrice !== null) { |
| 42 | + $variationQuery->whereRaw('CAST(price AS DECIMAL(10, 2)) <= ?', [$maxPrice]); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + // Filtre pour les produits variés (couleur + attributs, prix dans 'variation_attributes') |
| 47 | + $subQuery->orWhereHas('variations.attributesVariation', function (Builder $attributeQuery) use ($minPrice, $maxPrice) { |
| 48 | + if ($minPrice !== null) { |
| 49 | + $attributeQuery->whereRaw('CAST(price AS DECIMAL(10, 2)) >= ?', [$minPrice]); |
| 50 | + } |
| 51 | + if ($maxPrice !== null) { |
| 52 | + $attributeQuery->whereRaw('CAST(price AS DECIMAL(10, 2)) <= ?', [$maxPrice]); |
| 53 | + } |
| 54 | + }); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + if ($request->has('colors')) { |
| 61 | + $colorsString = $request->input('colors'); |
| 62 | + $colorNames = explode(',', $colorsString); |
| 63 | + |
| 64 | + $query->whereHas('variations', function (Builder $variationQuery) use ($colorNames) { |
| 65 | + $variationQuery->whereHas('color', function (Builder $colorQuery) use ($colorNames) { |
| 66 | + $colorQuery->whereIn('value', $colorNames); |
| 67 | + }); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + if ($request->has('attributes')) { |
| 72 | + $attributesToFilter = $request->input('attributes'); |
| 73 | + |
| 74 | + // Check if the input is a string and handle it as a single attribute |
| 75 | + if (!is_array($attributesToFilter) && is_string($attributesToFilter)) { |
| 76 | + // Assuming a single attribute ID is passed, e.g., ?attributes=5 |
| 77 | + $attributesToFilter = [$attributesToFilter => []]; |
| 78 | + } |
| 79 | + |
| 80 | + foreach ($attributesToFilter as $attributeId => $valueIdsString) { |
| 81 | + $valueIds = explode(',', $valueIdsString); |
| 82 | + |
| 83 | + $query->whereHas('variations.attributesVariation', function (Builder $attributeQuery) use ($attributeId, $valueIds) { |
| 84 | + $attributeQuery->whereHas('attributeValue', function (Builder $attributeValueQuery) use ($attributeId, $valueIds) { |
| 85 | + $attributeValueQuery |
| 86 | + ->whereIn('id', $valueIds); |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if ($request->has('gender')) { |
| 93 | + $genderId = $request->input('gender'); |
| 94 | + $query->where('product_gender', $genderId); |
| 95 | + } |
| 96 | + |
| 97 | + if ($request->has('seller_mode') && $request->input('seller_mode')==true) { |
| 98 | + $query->where('is_wholesale',1); |
| 99 | + |
| 100 | + if ($request->has('bulk_price_range')) { |
| 101 | + list($minBulkPrice, $maxBulkPrice) = explode('-', $request->input('bulk_price_range')); |
| 102 | + |
| 103 | + $minBulkPrice = floatval($minBulkPrice); |
| 104 | + $maxBulkPrice = floatval($maxBulkPrice); |
| 105 | + // On regroupe toutes les conditions de prix de gros |
| 106 | + $query->where(function(Builder $bulkPriceQuery) use ($minBulkPrice, $maxBulkPrice) { |
| 107 | + |
| 108 | + // Cas 1: Prix de gros pour les produits simples (sans variations) |
| 109 | + $bulkPriceQuery->whereHas('wholesalePrices', function (Builder $wholesaleQuery) use ($minBulkPrice, $maxBulkPrice) { |
| 110 | + $wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) >= ?', [$minBulkPrice]); |
| 111 | + $wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) <= ?', [$maxBulkPrice]); |
| 112 | + }); |
| 113 | + |
| 114 | + // Cas 2: Prix de gros pour les produits variés (couleur uniquement) |
| 115 | + $bulkPriceQuery->orWhereHas('variations', function (Builder $variationQuery) use ($minBulkPrice, $maxBulkPrice) { |
| 116 | + $variationQuery->whereHas('wholesalePrices', function (Builder $wholesaleQuery) use ($minBulkPrice, $maxBulkPrice) { |
| 117 | + $wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) >= ?', [$minBulkPrice]); |
| 118 | + $wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) <= ?', [$maxBulkPrice]); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + // Cas 3: Prix de gros pour les produits variés (couleur + attribut) |
| 123 | + $bulkPriceQuery->orWhereHas('variations.attributesVariation', function (Builder $attributeQuery) use ($minBulkPrice, $maxBulkPrice) { |
| 124 | + $attributeQuery->whereHas('wholesalePrices', function (Builder $wholesaleQuery) use ($minBulkPrice, $maxBulkPrice) { |
| 125 | + $wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) >= ?', [$minBulkPrice]); |
| 126 | + $wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) <= ?', [$maxBulkPrice]); |
| 127 | + }); |
| 128 | + }); |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + $products = $query->paginate(6); |
| 135 | + |
| 136 | + return ProductResource::collection($products); |
17 | 137 | } |
18 | 138 | } |
0 commit comments