From 23ac0cedad2ee82880558fcfc33ac39761022ba7 Mon Sep 17 00:00:00 2001 From: ludo Date: Mon, 13 Nov 2023 00:40:41 +0100 Subject: [PATCH] fix on weight --- .../Controllers/Shop/BasketController.php | 14 ++++++ app/Models/Shop/Offer.php | 5 ++ app/Repositories/Shop/Baskets.php | 50 +++++++++++++------ .../Shop/DeliveryTypeCalculations.php | 7 +++ app/Repositories/Shop/DeliveryTypes.php | 5 ++ .../Baskets/partials/basketTotal.blade.php | 10 ++-- routes/Shop/Baskets.php | 1 + 7 files changed, 74 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/Shop/BasketController.php b/app/Http/Controllers/Shop/BasketController.php index f01f3b6f..83f317fa 100644 --- a/app/Http/Controllers/Shop/BasketController.php +++ b/app/Http/Controllers/Shop/BasketController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Shop; use App\Http\Controllers\Controller; use App\Repositories\Core\User\ShopCart; use App\Repositories\Shop\Baskets; +use App\Repositories\Shop\DeliveryTypes; use App\Repositories\Shop\Offers; use App\Repositories\Shop\Orders; use App\Repositories\Users; @@ -62,6 +63,19 @@ class BasketController extends Controller return ShopCart::count(); } + public function getBasketTotal($deliveryTypeId = false) + { + $data = [ + 'basket' => ShopCart::getSummary(), + ]; + + $weight = Baskets::getWeight(); + + $data['basket']['shipping'] = DeliveryTypes::getPrice($deliveryTypeId, $weight); + + return view('Shop.Baskets.partials.basketTotal', $data); + } + public function getSummary() { $data = ShopCart::getSummary(); diff --git a/app/Models/Shop/Offer.php b/app/Models/Shop/Offer.php index 627a55ac..8424a3cb 100644 --- a/app/Models/Shop/Offer.php +++ b/app/Models/Shop/Offer.php @@ -113,6 +113,11 @@ class Offer extends Model return $query->where($this->table.'.stock_current', '>', 0); } + public function scopeByIds($query, $ids) + { + return $query->whereIn('id', $ids); + } + public function scopeByArticleNature($query, $article_nature_id) { return $query->whereHas('article.article_nature', function ($query) use ($article_nature_id) { diff --git a/app/Repositories/Shop/Baskets.php b/app/Repositories/Shop/Baskets.php index ad843b04..c6aa3d44 100644 --- a/app/Repositories/Shop/Baskets.php +++ b/app/Repositories/Shop/Baskets.php @@ -17,37 +17,29 @@ class Baskets return $data ? ShopCart::add($data, $update) : false; } - public static function getBasketSummary($saleChannelId = false) + public static function getBasketSummary($saleChannelId = false, $deliveryTypeId = false) { $basket = ShopCart::getContent(); $offers = Offer::with('variation')->whereIn('id', ShopCart::keys())->get(); $total = 0; $totalTaxed = 0; - $shipping = 5; + $totalWeight = 0; foreach ($basket as $item) { $offer = $offers->where('id', $item->id)->first(); $prices = Offers::getPrice($item->id, $item->quantity, $saleChannelId); $weight = $offer->weight * $item->quantity; - $detail[] = [ - 'offer_id' => (int) $item->id, - 'name' => $offer->article->name.' ('.$offer->variation->name.')', - 'quantity' => (int) $item->quantity, - 'price' => (float) $prices->price, - 'tax' => $prices->price_taxed - $prices->price, - 'price_taxed' => (float) $prices->price_taxed, - 'total' => self::getTotal($item->quantity, $prices->price), - 'total_tax' => self::getTotal($item->quantity, $prices->price_taxed - $prices->price), - 'total_taxed' => self::getTotal($item->quantity, $prices->price_taxed), - 'weight' => $weight, - ]; + $detail[] = self::getRowDetail($item, $offer, $prices, $weight); $total += self::getTotal($item->quantity, $prices->price); $totalTaxed += self::getTotal($item->quantity, $prices->price_taxed); + $totalWeight += $weight; } + $shipping = 0; $data = [ 'detail' => $detail, 'total' => $total, 'taxes' => $totalTaxed - $total, 'total_taxed' => $totalTaxed, + 'total_weight' => $totalWeight, 'shipping' => $shipping, 'total_shipped' => $totalTaxed + $shipping, ]; @@ -55,6 +47,36 @@ class Baskets return $data ?? false; } + public static function getRowDetail($item, $offer, $prices, $weight) + { + return [ + 'offer_id' => (int) $item->id, + 'name' => $offer->article->name.' ('.$offer->variation->name.')', + 'quantity' => (int) $item->quantity, + 'price' => (float) $prices->price, + 'tax' => $prices->price_taxed - $prices->price, + 'price_taxed' => (float) $prices->price_taxed, + 'total' => self::getTotal($item->quantity, $prices->price), + 'total_tax' => self::getTotal($item->quantity, $prices->price_taxed - $prices->price), + 'total_taxed' => self::getTotal($item->quantity, $prices->price_taxed), + 'weight' => $weight, + ]; + } + + public static function getWeight() + { + $basket = ShopCart::getContent(); + $offers = Offer::with('variation')->byIds(ShopCart::keys())->get(); + $totalWeight = 0; + foreach ($basket as $item) { + $offer = $offers->where('id', $item->id)->first(); + $weight = $offer->weight * $item->quantity; + $totalWeight += $weight; + } + + return $totalWeight; + } + public static function getTotal($quantity, $price) { return $quantity * $price; diff --git a/app/Repositories/Shop/DeliveryTypeCalculations.php b/app/Repositories/Shop/DeliveryTypeCalculations.php index 875a0fa0..9a366a4e 100644 --- a/app/Repositories/Shop/DeliveryTypeCalculations.php +++ b/app/Repositories/Shop/DeliveryTypeCalculations.php @@ -9,6 +9,13 @@ class DeliveryTypeCalculations { use Basic; + public static function getPriceByDeliveryType($deliveryTypeId, $weight) + { + $price = DeliveryTypeCalculation::byDeliveryType($deliveryTypeId)->byWeight($weight)->first(); + + return $price ? $price->price : false; + } + public static function getModel() { return DeliveryTypeCalculation::query(); diff --git a/app/Repositories/Shop/DeliveryTypes.php b/app/Repositories/Shop/DeliveryTypes.php index 300d8de0..dd873d7b 100644 --- a/app/Repositories/Shop/DeliveryTypes.php +++ b/app/Repositories/Shop/DeliveryTypes.php @@ -9,6 +9,11 @@ class DeliveryTypes { use Basic; + public static function getPrice($id, $weight) + { + return DeliveryTypeCalculations::getPriceByDeliveryType($id, $weight); + } + public static function getModel() { return DeliveryType::query(); diff --git a/resources/views/Shop/Baskets/partials/basketTotal.blade.php b/resources/views/Shop/Baskets/partials/basketTotal.blade.php index 7dfd274a..b88d3036 100644 --- a/resources/views/Shop/Baskets/partials/basketTotal.blade.php +++ b/resources/views/Shop/Baskets/partials/basketTotal.blade.php @@ -1,6 +1,6 @@
- Tarif appliqué : + Tarif appliqué :
@@ -10,7 +10,7 @@
- {{ $basket['count'] ?? 0 }} ARTICLES + {{ $basket['quantity'] ?? 0 }} ARTICLES
{{ $basket['total'] ?? 0 }} € @@ -32,6 +32,8 @@ TOTAL TTC
- {{ App\Repositories\Core\User\ShopCart::fixDecimal(($basket['total'] ?? 0)) }} € + {{ App\Repositories\Core\User\ShopCart::fixDecimal($basket['total'] ?? 0) }} + €
-
\ No newline at end of file +
diff --git a/routes/Shop/Baskets.php b/routes/Shop/Baskets.php index b144d2ca..dfa5ce5d 100644 --- a/routes/Shop/Baskets.php +++ b/routes/Shop/Baskets.php @@ -5,6 +5,7 @@ Route::prefix('Panier')->name('Basket.')->group(function () { Route::post('addBasket', 'BasketController@addBasket')->name('addBasket'); Route::get('modalBasket/{offer_id?}/{quantity?}', 'BasketController@modalBasket')->name('modalBasket'); Route::get('getBasket', 'BasketController@getBasket')->name('getBasket'); + Route::get('getBasketTotal', 'BasketController@getBasketTotal')->name('getBasketTotal'); Route::get('getSummary', 'BasketController@getSummary')->name('getSummary'); Route::get('countBasket', 'BasketController@countBasket')->name('countBasket'); Route::get('clearBasket', 'BasketController@clearBasket')->name('clearBasket');