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

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