fix on weight

This commit is contained in:
ludo
2023-11-13 00:40:41 +01:00
parent 741f389620
commit 7ec1d3e89b
7 changed files with 74 additions and 18 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Shop;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Repositories\Core\User\ShopCart; use App\Repositories\Core\User\ShopCart;
use App\Repositories\Shop\Baskets; use App\Repositories\Shop\Baskets;
use App\Repositories\Shop\DeliveryTypes;
use App\Repositories\Shop\Offers; use App\Repositories\Shop\Offers;
use App\Repositories\Shop\Orders; use App\Repositories\Shop\Orders;
use App\Repositories\Users; use App\Repositories\Users;
@@ -62,6 +63,19 @@ class BasketController extends Controller
return ShopCart::count(); 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() public function getSummary()
{ {
$data = ShopCart::getSummary(); $data = ShopCart::getSummary();

View File

@@ -113,6 +113,11 @@ class Offer extends Model
return $query->where($this->table.'.stock_current', '>', 0); 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) public function scopeByArticleNature($query, $article_nature_id)
{ {
return $query->whereHas('article.article_nature', function ($query) use ($article_nature_id) { return $query->whereHas('article.article_nature', function ($query) use ($article_nature_id) {

View File

@@ -17,18 +17,39 @@ class Baskets
return $data ? ShopCart::add($data, $update) : false; return $data ? ShopCart::add($data, $update) : false;
} }
public static function getBasketSummary($saleChannelId = false) public static function getBasketSummary($saleChannelId = false, $deliveryTypeId = false)
{ {
$basket = ShopCart::getContent(); $basket = ShopCart::getContent();
$offers = Offer::with('variation')->whereIn('id', ShopCart::keys())->get(); $offers = Offer::with('variation')->whereIn('id', ShopCart::keys())->get();
$total = 0; $total = 0;
$totalTaxed = 0; $totalTaxed = 0;
$shipping = 5; $totalWeight = 0;
foreach ($basket as $item) { foreach ($basket as $item) {
$offer = $offers->where('id', $item->id)->first(); $offer = $offers->where('id', $item->id)->first();
$prices = Offers::getPrice($item->id, $item->quantity, $saleChannelId); $prices = Offers::getPrice($item->id, $item->quantity, $saleChannelId);
$weight = $offer->weight * $item->quantity; $weight = $offer->weight * $item->quantity;
$detail[] = [ $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,
];
return $data ?? false;
}
public static function getRowDetail($item, $offer, $prices, $weight)
{
return [
'offer_id' => (int) $item->id, 'offer_id' => (int) $item->id,
'name' => $offer->article->name.' ('.$offer->variation->name.')', 'name' => $offer->article->name.' ('.$offer->variation->name.')',
'quantity' => (int) $item->quantity, 'quantity' => (int) $item->quantity,
@@ -40,19 +61,20 @@ class Baskets
'total_taxed' => self::getTotal($item->quantity, $prices->price_taxed), 'total_taxed' => self::getTotal($item->quantity, $prices->price_taxed),
'weight' => $weight, 'weight' => $weight,
]; ];
$total += self::getTotal($item->quantity, $prices->price);
$totalTaxed += self::getTotal($item->quantity, $prices->price_taxed);
} }
$data = [
'detail' => $detail,
'total' => $total,
'taxes' => $totalTaxed - $total,
'total_taxed' => $totalTaxed,
'shipping' => $shipping,
'total_shipped' => $totalTaxed + $shipping,
];
return $data ?? false; 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) public static function getTotal($quantity, $price)

View File

@@ -9,6 +9,13 @@ class DeliveryTypeCalculations
{ {
use Basic; use Basic;
public static function getPriceByDeliveryType($deliveryTypeId, $weight)
{
$price = DeliveryTypeCalculation::byDeliveryType($deliveryTypeId)->byWeight($weight)->first();
return $price ? $price->price : false;
}
public static function getModel() public static function getModel()
{ {
return DeliveryTypeCalculation::query(); return DeliveryTypeCalculation::query();

View File

@@ -9,6 +9,11 @@ class DeliveryTypes
{ {
use Basic; use Basic;
public static function getPrice($id, $weight)
{
return DeliveryTypeCalculations::getPriceByDeliveryType($id, $weight);
}
public static function getModel() public static function getModel()
{ {
return DeliveryType::query(); return DeliveryType::query();

View File

@@ -10,7 +10,7 @@
</div> </div>
<div class="row mb-3"> <div class="row mb-3">
<div class="col-6"> <div class="col-6">
<span id="basket-count">{{ $basket['count'] ?? 0 }}</span> ARTICLES <span id="basket-count">{{ $basket['quantity'] ?? 0 }}</span> ARTICLES
</div> </div>
<div class="col-6 text-right font-weight-bold"> <div class="col-6 text-right font-weight-bold">
<span id="basket-total">{{ $basket['total'] ?? 0 }}</span> <span id="basket-total">{{ $basket['total'] ?? 0 }}</span>
@@ -32,6 +32,8 @@
TOTAL TTC TOTAL TTC
</div> </div>
<div class="col-6 text-right"> <div class="col-6 text-right">
<span id="basket-total-shipped">{{ App\Repositories\Core\User\ShopCart::fixDecimal(($basket['total'] ?? 0)) }}</span> <span
id="basket-total-shipped">{{ App\Repositories\Core\User\ShopCart::fixDecimal($basket['total'] ?? 0) }}</span>
</div> </div>
</div> </div>

View File

@@ -5,6 +5,7 @@ Route::prefix('Panier')->name('Basket.')->group(function () {
Route::post('addBasket', 'BasketController@addBasket')->name('addBasket'); Route::post('addBasket', 'BasketController@addBasket')->name('addBasket');
Route::get('modalBasket/{offer_id?}/{quantity?}', 'BasketController@modalBasket')->name('modalBasket'); Route::get('modalBasket/{offer_id?}/{quantity?}', 'BasketController@modalBasket')->name('modalBasket');
Route::get('getBasket', 'BasketController@getBasket')->name('getBasket'); Route::get('getBasket', 'BasketController@getBasket')->name('getBasket');
Route::get('getBasketTotal', 'BasketController@getBasketTotal')->name('getBasketTotal');
Route::get('getSummary', 'BasketController@getSummary')->name('getSummary'); Route::get('getSummary', 'BasketController@getSummary')->name('getSummary');
Route::get('countBasket', 'BasketController@countBasket')->name('countBasket'); Route::get('countBasket', 'BasketController@countBasket')->name('countBasket');
Route::get('clearBasket', 'BasketController@clearBasket')->name('clearBasket'); Route::get('clearBasket', 'BasketController@clearBasket')->name('clearBasket');