158 lines
4.5 KiB
PHP
158 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Shop;
|
|
|
|
use App\Models\Shop\Offer;
|
|
|
|
class Offers
|
|
{
|
|
public static function count()
|
|
{
|
|
return Offer::count();
|
|
}
|
|
|
|
public static function getWeight($id, $quantity = 1)
|
|
{
|
|
$offer = self::get($id);
|
|
|
|
return $offer ? $offer->weight * $quantity : 0;
|
|
}
|
|
|
|
public static function getFull($id, $saleChannelId = false)
|
|
{
|
|
$saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();
|
|
$offer = Offer::with([
|
|
'article.article_nature',
|
|
'article.product',
|
|
'tariff' => function ($query) use ($saleChannelId) {
|
|
$query->bySaleChannel($saleChannelId);
|
|
},
|
|
'tariff.price_lists' => function ($query) use ($saleChannelId) {
|
|
$query->BySaleChannel($saleChannelId);
|
|
},
|
|
'tariff.price_lists.price_list_values',
|
|
'variation',
|
|
])->find($id);
|
|
$images = Articles::getFullImagesByArticle($offer->article);
|
|
$offer->article->image = Articles::getPreviewSrc($images[0] ?? false);
|
|
|
|
return $offer;
|
|
}
|
|
|
|
public static function getPrice($id, $quantity = 1, $saleChannelId = false)
|
|
{
|
|
$saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();
|
|
$offer = Offer::withPriceBySaleChannelByQuantity($saleChannelId, $quantity)->find($id);
|
|
|
|
return $offer->price_lists->first()->price_list_values->first();
|
|
}
|
|
|
|
public static function getOffersByArticles($article_ids, $saleChannelId = false)
|
|
{
|
|
return self::getOffersBySaleChannelRaw($saleChannelId)->byArticles($article_ids)->get();
|
|
}
|
|
|
|
public static function getOffersByArticle($article_id, $saleChannelId = false)
|
|
{
|
|
return self::getOffersBySaleChannelRaw($saleChannelId)->byArticle($article_id)->get();
|
|
}
|
|
|
|
public static function getOffersBySaleChannel($saleChannelId = false)
|
|
{
|
|
return self::getOffersBySaleChannelRaw($saleChannelId)->get();
|
|
}
|
|
|
|
public static function getOffersBySaleChannelRaw($saleChannelId = false)
|
|
{
|
|
$saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();
|
|
|
|
return Offer::active()->byStockAvailable()
|
|
->with([
|
|
'article_nature',
|
|
'variation',
|
|
'tariff.price_lists' => function ($query) use ($saleChannelId) {
|
|
$query->bySaleChannel($saleChannelId);
|
|
},
|
|
'tariff.price_lists.price_list_values',
|
|
])
|
|
->bySaleChannel($saleChannelId);
|
|
}
|
|
|
|
public static function getThumbSrcById($id)
|
|
{
|
|
return self::getThumbSrc(self::get($id));
|
|
}
|
|
|
|
public static function getThumbSrc(Offer $offer)
|
|
{
|
|
$image = $offer->article ? Articles::getFullImageByArticle($offer->article) : false;
|
|
|
|
return $image ? Articles::getThumbSrc($image) : false;
|
|
}
|
|
|
|
public static function getLast()
|
|
{
|
|
return Offer::with(['article.image'])->active()->orderByDesc('updated_at')->get();
|
|
}
|
|
|
|
public static function getByCategoryWithTags($category_id)
|
|
{
|
|
$category = Categories::get($category_id);
|
|
$tags = Categories::getTagsByCategory($category);
|
|
$offers1 = self::getByCategory($category_id)->toArray();
|
|
$offers2 = self::getByTags($tags)->toArray();
|
|
$data = array_merge($offers1, $offers2);
|
|
|
|
return $data;
|
|
}
|
|
|
|
public static function getByCategory($category_id)
|
|
{
|
|
return Offer::with(['article.image'])->byCategory($category_id)->get();
|
|
}
|
|
|
|
public static function getByTags($tags)
|
|
{
|
|
return Offer::with(['article.tags'])->byTags($tags)->get();
|
|
}
|
|
|
|
public static function getAll()
|
|
{
|
|
return Offer::get();
|
|
}
|
|
|
|
public static function get($id, $relations = false)
|
|
{
|
|
return $relations ? Offer::with($relations)->findOrFail($id) : Offer::findOrFail($id);
|
|
}
|
|
|
|
public static function store($data)
|
|
{
|
|
return ($data['id'] ?? false) ? self::update($data) : self::create($data);
|
|
}
|
|
|
|
public static function create($data)
|
|
{
|
|
return Offer::create($data);
|
|
}
|
|
|
|
public static function update($data, $id = false)
|
|
{
|
|
$id = $id ? $id : $data['id'];
|
|
$item = self::get($id);
|
|
$item->update($data);
|
|
|
|
return $item;
|
|
}
|
|
|
|
public static function destroy($id)
|
|
{
|
|
return Offer::destroy($id);
|
|
}
|
|
|
|
public static function toggle_active($id, $status_id)
|
|
{
|
|
return self::update(['status_id' => $status_id], $id);
|
|
}
|
|
}
|