Files
opensem/app/Repositories/Shop/Baskets.php
Ludovic CANDELLIER ebe7ba5f6c restart
2022-11-11 13:05:40 +01:00

106 lines
3.9 KiB
PHP

<?php
namespace App\Repositories\Shop;
use App\Models\Shop\Offer;
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 getBasketSummary($sale_channel_id = false)
{
$basket = ShopCart::getContent();
$offers = Offer::with('variation')->whereIn('id', ShopCart::keys())->get();
$total = 0;
$total_taxed = 0;
$shipping = 5;
foreach ($basket as $item) {
$offer = $offers->where('id', $item->id)->first();
$prices = Offers::getPrice($item->id, $item->quantity, $sale_channel_id);
$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),
];
$total += self::getTotal($item->quantity, $prices->price);
$total_taxed += self::getTotal($item->quantity, $prices->price_taxed);
}
$data = [
'detail' => $detail,
'total' => $total,
'taxes' => $total_taxed - $total,
'total_taxed' => $total_taxed,
'shipping' => $shipping,
'total_shipped' => $total_taxed + $shipping,
];
return $data ?? false;
}
public static function getTotal($quantity, $price)
{
return $quantity * $price;
}
public static function getBasket($sale_channel_id = false)
{
$sale_channel_id = $sale_channel_id ? $sale_channel_id : 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($sale_channel_id)
->whereIn('id', ShopCart::keys())->get();
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 . ')';
}
}