Files
opensem/app/Repositories/Shop/Baskets.php

141 lines
4.9 KiB
PHP

<?php
namespace App\Repositories\Shop;
use App\Repositories\Core\User\ShopCart;
class Baskets
{
public static function addBasket($offer_id, $quantity = 1, $update = false)
{
if (ShopCart::has($offer_id) && ! $quantity) {
$ret = ShopCart::remove($offer_id);
}
$data = $quantity ? self::getBasketData($offer_id, $quantity) : 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)
{
$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);
$weight = $offer->weight * $item->quantity;
$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 = DeliveryTypeCalculations::getPriceByDeliveryType($deliveryTypeId, $totalWeight);
$data = [
'detail' => $detail,
'total' => $total,
'taxes' => $totalTaxed - $total,
'total_taxed' => $totalTaxed,
'shipping' => $shipping,
'total_shipped' => $totalTaxed + $shipping,
'count' => ShopCart::count(),
'quantity' => ShopCart::getTotalQuantity(),
'weight' => $totalWeight,
];
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 = Offers::getWithVariationByIds(ShopCart::keys());
$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;
}
public static function getBasket($saleChannelId = false)
{
$saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();
$basket = ShopCart::getContent();
$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);
$data[$article_nature][] = [
'id' => (int) $item->id,
'name' => $item->name,
'quantity' => (int) $item->quantity,
'price' => $item->price,
'variation' => $offer->variation->name,
'image' => Articles::getPreviewSrc(Articles::getFullImageByArticle($offer->article)),
'latin' => $offer->article->product->specie->latin ?? false,
];
}
return $data ?? false;
}
public static function getBasketData($id, $quantity = 1)
{
$offer = Offers::get($id, ['article', 'variation']);
return [
'id' => $id,
'name' => self::getArticleName($offer),
'price' => Offers::getPrice($id, $quantity)->price_taxed,
'quantity' => $quantity,
'attributes' => [],
];
}
public static function getArticleName(&$offer)
{
return $offer->article->name;
// return $offer->article->name . ' (' . $offer->variation->name . ')';
}
public static function getIds()
{
return ShopCart::keys();
}
}