add shipping to order, methods to calculate, little refactoring

This commit is contained in:
ludo
2023-11-15 23:20:42 +01:00
parent 216c408596
commit 174c4ca0e2
10 changed files with 117 additions and 45 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\Deliveries;
use App\Repositories\Shop\DeliveryTypes; 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;
@@ -63,15 +64,9 @@ class BasketController extends Controller
return ShopCart::count(); return ShopCart::count();
} }
public function getBasketTotal($deliveryTypeId = false) public function getBasketTotal($deliveryId = false, $deliveryTypeId = false)
{ {
$data = [ $data['basket'] = Baskets::getBasketTotal($deliveryId, $deliveryTypeId);
'basket' => ShopCart::getSummary(),
];
$weight = Baskets::getWeight();
$data['basket']['shipping'] = DeliveryTypes::getPrice($deliveryTypeId, $weight);
return view('Shop.Baskets.partials.basketTotal', $data); return view('Shop.Baskets.partials.basketTotal', $data);
} }

View File

@@ -40,15 +40,18 @@ class OrderController extends Controller
{ {
if (ShopCart::count()) { if (ShopCart::count()) {
$customer = Customers::getWithAddresses(); $customer = Customers::getWithAddresses();
$data = [ $data = [
'customer' => $customer ? $customer->toArray() : false, 'customer' => $customer ? $customer->toArray() : false,
'basket' => ShopCart::getSummary(), 'basket' => Baskets::getBasketTotal(),
'deliveries' => Deliveries::getAllWithSaleChannel()->toArray(), 'deliveries' => Deliveries::getAllWithSaleChannel()->toArray(),
'sale_channel' => SaleChannels::getDefault()->toArray(), 'sale_channel' => SaleChannels::getDefault()->toArray(),
'delivery_types' => DeliveryTypes::getAll(), 'delivery_types' => DeliveryTypes::getWithPrice(Baskets::getWeight()),
]; ];
// dump($data);
// exit;
return view('Shop.Orders.order', $data); return view('Shop.Orders.order', $data);
} else { } else {
return redirect()->route('home'); return redirect()->route('home');

View File

@@ -2,7 +2,6 @@
namespace App\Repositories\Shop; namespace App\Repositories\Shop;
use App\Models\Shop\Offer;
use App\Repositories\Core\User\ShopCart; use App\Repositories\Core\User\ShopCart;
class Baskets class Baskets
@@ -17,13 +16,21 @@ class Baskets
return $data ? ShopCart::add($data, $update) : false; return $data ? ShopCart::add($data, $update) : false;
} }
public static function getBasketTotal($deliveryId = false, $deliveryTypeId = false)
{
$saleChannelId = $deliveryId ? Deliveries::getField($deliveryId, 'sale_channel_id') : SaleChannels::getDefaultID();
return self::getBasketSummary($saleChannelId, $deliveryTypeId);
}
public static function getBasketSummary($saleChannelId = false, $deliveryTypeId = false) public static function getBasketSummary($saleChannelId = false, $deliveryTypeId = false)
{ {
$basket = ShopCart::getContent();
$offers = Offer::with('variation')->whereIn('id', ShopCart::keys())->get();
$total = 0; $total = 0;
$totalTaxed = 0; $totalTaxed = 0;
$totalWeight = 0; $totalWeight = 0;
$basket = ShopCart::getContent();
$offers = Offers::getWithVariationByIds(self::getIds());
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);
@@ -33,15 +40,18 @@ class Baskets
$totalTaxed += self::getTotal($item->quantity, $prices->price_taxed); $totalTaxed += self::getTotal($item->quantity, $prices->price_taxed);
$totalWeight += $weight; $totalWeight += $weight;
} }
$shipping = 0; $shipping = DeliveryTypeCalculations::getPriceByDeliveryType($deliveryTypeId, $totalWeight);
$data = [ $data = [
'detail' => $detail, 'detail' => $detail,
'total' => $total, 'total' => $total,
'taxes' => $totalTaxed - $total, 'taxes' => $totalTaxed - $total,
'total_taxed' => $totalTaxed, 'total_taxed' => $totalTaxed,
'total_weight' => $totalWeight,
'shipping' => $shipping, 'shipping' => $shipping,
'total_shipped' => $totalTaxed + $shipping, 'total_shipped' => $totalTaxed + $shipping,
'count' => ShopCart::count(),
'quantity' => ShopCart::getTotalQuantity(),
'weight' => $totalWeight,
]; ];
return $data ?? false; return $data ?? false;
@@ -66,7 +76,7 @@ class Baskets
public static function getWeight() public static function getWeight()
{ {
$basket = ShopCart::getContent(); $basket = ShopCart::getContent();
$offers = Offer::with('variation')->byIds(ShopCart::keys())->get(); $offers = Offers::getWithVariationByIds(ShopCart::keys());
$totalWeight = 0; $totalWeight = 0;
foreach ($basket as $item) { foreach ($basket as $item) {
$offer = $offers->where('id', $item->id)->first(); $offer = $offers->where('id', $item->id)->first();
@@ -86,15 +96,7 @@ class Baskets
{ {
$saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID(); $saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();
$basket = ShopCart::getContent(); $basket = ShopCart::getContent();
// dump($basket->toArray()); $offers = Offers::getWithPricesByIds(self::getIds(), $saleChannelId);
$offers = Offer::with([
'variation',
'article.article_nature',
'article.product.Specie',
'article.image',
'price_lists.price_list_values',
])->withPriceListsBySaleChannel($saleChannelId)
->whereIn('id', ShopCart::keys())->get();
foreach ($basket as $item) { foreach ($basket as $item) {
$offer = $offers->where('id', $item->id)->first(); $offer = $offers->where('id', $item->id)->first();
$article_nature = strtolower($offer->article->article_nature->name); $article_nature = strtolower($offer->article->article_nature->name);
@@ -130,4 +132,9 @@ class Baskets
return $offer->article->name; return $offer->article->name;
// return $offer->article->name . ' (' . $offer->variation->name . ')'; // return $offer->article->name . ' (' . $offer->variation->name . ')';
} }
public static function getIds()
{
return ShopCart::keys();
}
} }

View File

@@ -9,6 +9,21 @@ class DeliveryTypes
{ {
use Basic; use Basic;
public static function getWithPrice($weight)
{
$data = [];
$types = self::getAll();
foreach ($types as $type) {
$data[$type->id] = [
'name' => $type->name,
'price' => self::getPrice($type->id, $weight),
];
}
return $data;
}
public static function getPrice($id, $weight) public static function getPrice($id, $weight)
{ {
return DeliveryTypeCalculations::getPriceByDeliveryType($id, $weight); return DeliveryTypeCalculations::getPriceByDeliveryType($id, $weight);

View File

@@ -25,6 +25,23 @@ class Offers
return $offer ? $offer->weight * $quantity : 0; return $offer ? $offer->weight * $quantity : 0;
} }
public static function getWithVariationByIds($ids)
{
return Offer::with('variation')->byIds($ids)->get();
}
public static function getWithPricesByIds($ids, $saleChannelId = false)
{
$saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();
return Offer::with([
'variation',
'article.article_nature',
'article.product.Specie',
'article.image',
'price_lists.price_list_values',
])->withPriceListsBySaleChannel($saleChannelId)->byIds($ids)->get();
}
public static function getFull($id, $saleChannelId = false) public static function getFull($id, $saleChannelId = false)
{ {
$saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID(); $saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();

View File

@@ -149,6 +149,11 @@ trait Basic
return self::getModelRelations($relations, $relationsCount)->withTrashed()->find($id); return self::getModelRelations($relations, $relationsCount)->withTrashed()->find($id);
} }
public static function getAllArray($relations = false, $relationsCount = false)
{
return self::getAll($relations, $relationsCount)->toArray();
}
public static function getAll($relations = false, $relationsCount = false) public static function getAll($relations = false, $relationsCount = false)
{ {
return self::getModelRelations($relations, $relationsCount)->get(); return self::getModelRelations($relations, $relationsCount)->get();

View File

@@ -13,27 +13,32 @@
<span id="basket-count">{{ $basket['quantity'] ?? 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_taxed'] ?? 0 }}
</span>
</div> </div>
</div> </div>
<!-- @if ($basket['shipping'])
<div class="row mb-3"> <div class="row mb-3">
<div class="col-6"> <div class="col-6">
LIVRAISON LIVRAISON
</div>
<div class="col-6 text-right font-weight-bold">
<span id="shipping">
{{ $basket['shipping'] }}
</span>
</div>
</div> </div>
<div class="col-6 text-right font-weight-bold"> <hr>
<span id="shipping">5</span> @endif
</div>
</div>
<hr>
-->
<div class="row mb-3 font-weight-bold" style="font-size: 1.6em;"> <div class="row mb-3 font-weight-bold" style="font-size: 1.6em;">
<div class="col-6"> <div class="col-6">
TOTAL TTC TOTAL TTC
</div> </div>
<div class="col-6 text-right"> <div class="col-6 text-right">
<span <span id="basket-total-shipped">
id="basket-total-shipped">{{ App\Repositories\Core\User\ShopCart::fixDecimal($basket['total'] ?? 0) }}</span> {{ $basket['total_shipped'] ?? 0 }}
</span>
</div> </div>
</div> </div>

View File

@@ -29,7 +29,7 @@
@if ($basket['count']) @if ($basket['count'])
<div class="col-4"> <div class="col-4">
<x-card class='shadow'> <x-card class='shadow'>
@include('Shop.Baskets.partials.basketTotal') @include('Shop.Baskets.partials.basketTotal', ['basket' => $basket])
</x-card> </x-card>
</div> </div>
@endif @endif

View File

@@ -1,3 +1,28 @@
@foreach ($delivery_types as $delivery_type) @if ($delivery_types ?? false)
<table class="">
@endforeach <thead>
<tr>
<th colspan="3">
Choix du transporteur
</th>
</tr>
</thead>
@foreach ($delivery_types as $delivery_type_id => $delivery_type)
<tr>
<td>
@include('components.form.radios.icheck', [
'name' => 'delivery_type_id',
'val' => $delivery_type_id,
'check' => false,
])
</td>
<td>
{{ $delivery_type['name'] }}
</td>
<td class="text-right">
{{ $delivery_type['price'] }}
</td>
</tr>
@endforeach
</table>
@endif

View File

@@ -5,7 +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('getBasketTotal/{deliveryId?}/{deliveryTypeId?}', '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');