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();