Skip to content

Commit a4a1502

Browse files
committed
Refactor bulk price filtering in allProducts method of ProductListController to use raw SQL casting
This commit updates the allProducts method to improve the bulk price filtering logic by replacing the whereBetween conditions with whereRaw statements that cast wholesale prices to decimal. This change enhances the accuracy of price comparisons and ensures proper filtering for both simple products and variations.
1 parent 3ec5f88 commit a4a1502

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

app/Http/Controllers/Product/ProductListController.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Models\Product;
88
use Illuminate\Http\Request;
99
use Illuminate\Database\Eloquent\Builder;
10+
use Illuminate\Support\Facades\Log;
1011

1112
class ProductListController extends Controller
1213
{
@@ -116,26 +117,29 @@ public function allProducts(Request $request){
116117

117118
$minBulkPrice = floatval($minBulkPrice);
118119
$maxBulkPrice = floatval($maxBulkPrice);
119-
120+
Log::info('Bulk price range', ['minBulkPrice' => gettype($minBulkPrice), 'maxBulkPrice' => $maxBulkPrice]);
120121
// On regroupe toutes les conditions de prix de gros
121122
$query->where(function(Builder $bulkPriceQuery) use ($minBulkPrice, $maxBulkPrice) {
122123

123124
// Cas 1: Prix de gros pour les produits simples (sans variations)
124125
$bulkPriceQuery->whereHas('wholesalePrices', function (Builder $wholesaleQuery) use ($minBulkPrice, $maxBulkPrice) {
125-
$wholesaleQuery->whereBetween('wholesale_price', [$minBulkPrice, $maxBulkPrice]);
126+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) >= ?', [$minBulkPrice]);
127+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) <= ?', [$maxBulkPrice]);
126128
});
127129

128130
// Cas 2: Prix de gros pour les produits variés (couleur uniquement)
129131
$bulkPriceQuery->orWhereHas('variations', function (Builder $variationQuery) use ($minBulkPrice, $maxBulkPrice) {
130132
$variationQuery->whereHas('wholesalePrices', function (Builder $wholesaleQuery) use ($minBulkPrice, $maxBulkPrice) {
131-
$wholesaleQuery->whereBetween('wholesale_price', [$minBulkPrice, $maxBulkPrice]);
133+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) >= ?', [$minBulkPrice]);
134+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) <= ?', [$maxBulkPrice]);
132135
});
133136
});
134137

135138
// Cas 3: Prix de gros pour les produits variés (couleur + attribut)
136139
$bulkPriceQuery->orWhereHas('variations.attributesVariation', function (Builder $attributeQuery) use ($minBulkPrice, $maxBulkPrice) {
137140
$attributeQuery->whereHas('wholesalePrices', function (Builder $wholesaleQuery) use ($minBulkPrice, $maxBulkPrice) {
138-
$wholesaleQuery->whereBetween('wholesale_price', [$minBulkPrice, $maxBulkPrice]);
141+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) >= ?', [$minBulkPrice]);
142+
$wholesaleQuery->whereRaw('CAST(wholesale_price AS DECIMAL(10, 2)) <= ?', [$maxBulkPrice]);
139143
});
140144
});
141145
});

0 commit comments

Comments
 (0)