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

View File

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

View File

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

View File

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

View File

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