[WIP] Working on orders & invoices
This commit is contained in:
100
app/Repositories/Shop/Baskets.php
Normal file
100
app/Repositories/Shop/Baskets.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?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()
|
||||
{
|
||||
$basket = ShopCart::getContent();
|
||||
$offers = Offer::with('variation')->whereIn('id', ShopCart::keys())->get();
|
||||
$total = 0;
|
||||
$total_taxed = 0;
|
||||
foreach ($basket as $item) {
|
||||
$offer = $offers->where('id', $item->id)->first();
|
||||
$detail[] = [
|
||||
'id' => (int) $item->id,
|
||||
'name' => $offer->article->name . ' (' . $offer->variation->name . ')',
|
||||
'quantity' => (int) $item->quantity,
|
||||
'price' => $item->price,
|
||||
'tax' => '',
|
||||
'price_taxed' => $item->price,
|
||||
'total' => self::getTotal($item->quantity, $item->price),
|
||||
'total_taxed' => self::getTotal($item->quantity, $item->price),
|
||||
];
|
||||
$total += self::getTotal($item->quantity, $item->price);
|
||||
$total_taxed += self::getTotal($item->quantity, $item->price);
|
||||
}
|
||||
$data = [
|
||||
'detail' => $detail,
|
||||
'total' => $total,
|
||||
'taxes' => $total_taxed - $total,
|
||||
'total_taxed' => $total_taxed,
|
||||
];
|
||||
return $data ?? false;
|
||||
}
|
||||
|
||||
public static function getTotal($quantity, $price)
|
||||
{
|
||||
return (int) $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 . ')';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user