add shipping to order, methods to calculate, little refactoring

This commit is contained in:
ludo
2023-11-15 23:20:42 +01:00
parent 04df068931
commit 2ebdc5f16b
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\Repositories\Core\User\ShopCart;
use App\Repositories\Shop\Baskets;
use App\Repositories\Shop\Deliveries;
use App\Repositories\Shop\DeliveryTypes;
use App\Repositories\Shop\Offers;
use App\Repositories\Shop\Orders;
@@ -63,15 +64,9 @@ class BasketController extends Controller
return ShopCart::count();
}
public function getBasketTotal($deliveryTypeId = false)
public function getBasketTotal($deliveryId = false, $deliveryTypeId = false)
{
$data = [
'basket' => ShopCart::getSummary(),
];
$weight = Baskets::getWeight();
$data['basket']['shipping'] = DeliveryTypes::getPrice($deliveryTypeId, $weight);
$data['basket'] = Baskets::getBasketTotal($deliveryId, $deliveryTypeId);
return view('Shop.Baskets.partials.basketTotal', $data);
}

View File

@@ -40,15 +40,18 @@ class OrderController extends Controller
{
if (ShopCart::count()) {
$customer = Customers::getWithAddresses();
$data = [
'customer' => $customer ? $customer->toArray() : false,
'basket' => ShopCart::getSummary(),
'basket' => Baskets::getBasketTotal(),
'deliveries' => Deliveries::getAllWithSaleChannel()->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);
} else {
return redirect()->route('home');

View File

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

View File

@@ -25,6 +25,23 @@ class Offers
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)
{
$saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();

View File

@@ -149,6 +149,11 @@ trait Basic
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)
{
return self::getModelRelations($relations, $relationsCount)->get();

View File

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

View File

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

View File

@@ -1,3 +1,28 @@
@foreach ($delivery_types as $delivery_type)
@endforeach
@if ($delivery_types ?? false)
<table class="">
<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::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('getBasketTotal/{deliveryId?}/{deliveryTypeId?}', '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');