497 lines
17 KiB
PHP
497 lines
17 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Shop;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use App\Repositories\Core\Tag;
|
|
use App\Repositories\Core\Medias;
|
|
use App\Repositories\Core\Comments;
|
|
use App\Repositories\Botanic\Species;
|
|
use App\Repositories\Botanic\Varieties;
|
|
use App\Models\Shop\Article;
|
|
|
|
use App\Traits\Repository\Imageable;
|
|
|
|
class Articles
|
|
{
|
|
use Imageable;
|
|
|
|
public static function autocomplete($str)
|
|
{
|
|
$data = Article::byAutocomplete($str)->orderBy('name')->limit(30)->pluck('name', 'id');
|
|
$export = [];
|
|
foreach ($data as $key => $name) {
|
|
$export[] = ['value' => $key, 'text' => $name];
|
|
}
|
|
return $export;
|
|
}
|
|
|
|
public static function getOffersGroupedByNature($id, $sale_channel_id = false)
|
|
{
|
|
$article_ids = self::getSiblingsIds($id);
|
|
$offers = Offers::getOffersByArticles($article_ids, $sale_channel_id);
|
|
foreach ($offers as $offer) {
|
|
$data[strtolower($offer->article_nature->name)][] = [
|
|
'id' => $offer->id,
|
|
'name' => $offer->variation->name,
|
|
'prices' => $offer->tariff->price_lists->first()->price_list_values->toArray(),
|
|
];
|
|
}
|
|
return $data ?? false;
|
|
}
|
|
|
|
public static function getSiblingsIDs($id)
|
|
{
|
|
return self::getSiblings($id)->pluck('id')->toArray();
|
|
}
|
|
|
|
public static function getSiblings($id)
|
|
{
|
|
return Article::with('siblings')->find($id)->siblings;
|
|
}
|
|
|
|
public static function getOffersById($id)
|
|
{
|
|
return Offers::getOffersByArticle($id);
|
|
}
|
|
|
|
public static function getOptions()
|
|
{
|
|
return Article::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
|
}
|
|
|
|
public static function getOptionsWithNature()
|
|
{
|
|
$articles = Article::with(['article_nature'])->get();
|
|
foreach ($articles as $article) {
|
|
$data[$article->id] = ($article->article_nature->name ?? null) . ' - ' . $article->name;
|
|
}
|
|
asort($data, SORT_NATURAL);
|
|
return $data;
|
|
}
|
|
|
|
public static function getAll()
|
|
{
|
|
return Article::orderBy('name', 'asc')->get();
|
|
}
|
|
|
|
public static function getArticleToSell($id, $sale_channel_id = false)
|
|
{
|
|
$article = self::get($id);
|
|
$data = $article->toArray();
|
|
$parents = self::getInheritedByProduct($article->product_id, $article->product_type);
|
|
$data['description'] = self::getFullDescriptionByArticle($article);
|
|
$image = self::getFullImageByArticle($article);
|
|
$data['image'] = self::getPreview($image);
|
|
$data['image_big'] = self::getImage($image);
|
|
$data['inherited'] = self::getInherited($id);
|
|
$data['categories'] = self::getCategoriesNameByArticle($article);
|
|
$data['tags'] = self::getTagsSlugByArticle($article);
|
|
$data['offers'] = self::getOffersGroupedByNature($id, $sale_channel_id);
|
|
return $data;
|
|
}
|
|
|
|
public static function getFullDescriptionByArticle($article)
|
|
{
|
|
switch ($article->product_type) {
|
|
case 'App\Models\Botanic\Variety':
|
|
$variety = $article->product;
|
|
$specie = $variety->specie;
|
|
$description = empty($specie->description) ? '' : $specie->description . '<br>';
|
|
$description .= empty($variety->description) ? '' : $variety->description . '<br>';
|
|
break;
|
|
case 'App\Models\Botanic\Specie':
|
|
$specie = $article->product;
|
|
$description = empty($specie->description) ? '' : $specie->description . '<br>';
|
|
break;
|
|
default:
|
|
$description = '';
|
|
}
|
|
$description .= $article->description;
|
|
return $description;
|
|
}
|
|
|
|
public static function getArticle($id)
|
|
{
|
|
$article = self::get($id);
|
|
$data = $article->toArray();
|
|
$data['description'] = (!empty($article->description)) ? $article->description : $article->product->description;
|
|
$data['image'] = Articles::getPreview($article->image);
|
|
$data['image_big'] = Articles::getImage($article->image);
|
|
$data['inherited'] = self::getInherited($id);
|
|
$data['categories'] = self::getCategoriesNameByArticle($article);
|
|
$data['tags'] = self::getTagsSlugByArticle($article);
|
|
$data['comments'] = Comments::getByModel($article);
|
|
return $data;
|
|
}
|
|
|
|
public static function getArticlesByHomepage()
|
|
{
|
|
$shelves = Categories::getByHomepage();
|
|
foreach ($shelves as $shelve) {
|
|
$data[] = [
|
|
'id' => $shelve->id,
|
|
'name' => $shelve->name,
|
|
'articles' => self::getArticlesToSell([
|
|
'category_id' => $shelve->id,
|
|
'homepage' => true,
|
|
]),
|
|
];
|
|
}
|
|
return $data ?? [];
|
|
}
|
|
|
|
public static function getArticlesToSell($options)
|
|
{
|
|
$articles = self::getArticlesWithOffers($options);
|
|
// dump($options);
|
|
// dump($articles->toArray());
|
|
// exit;
|
|
/*
|
|
foreach ($articles as $article) {
|
|
$price_lists = $article->offers[0]->tariff->price_lists->toArray();
|
|
dump($price_lists);
|
|
if (count($price_lists)) {
|
|
if (!is_array($data[$article->name] ?? false)) {
|
|
$data[$article->name] = self::getDataForSale($article);
|
|
}
|
|
$prices = $price_lists[0]['price_list_values'][0];
|
|
$article_nature_name = strtolower($article->article_nature->name);
|
|
$data[$article->name][$article_nature_name] = self::getDataPriceForSale($article, $prices);
|
|
}
|
|
}
|
|
*/
|
|
return $articles ?? false;
|
|
}
|
|
|
|
public static function getDataForSale($article)
|
|
{
|
|
return [
|
|
'id' => $article->id,
|
|
'description' => (!empty($article->description)) ? $article->description : $article->product->description,
|
|
'image' => self::getFullImageByArticle($article),
|
|
'product_type' => $article->product_type,
|
|
'product_id' => $article->product_id,
|
|
'product_name' => $article->product->name,
|
|
'parent_name' => trim(str_replace($article->product->name, '', $article->name)),
|
|
];
|
|
}
|
|
|
|
public static function getDataPriceForSale($article, $prices)
|
|
{
|
|
return [
|
|
'article_id' => $article->id,
|
|
'offer_id' => $article->offers[0]->id,
|
|
'quantity' => $prices['quantity'],
|
|
'price' => $prices['price_taxed'],
|
|
'variation' => $article->offers[0]->variation->name,
|
|
];
|
|
}
|
|
|
|
public static function getArticlesWithOffers($options = false)
|
|
{
|
|
$category_id = $options['category_id'] ?? false;
|
|
$sale_channel_id = $options['sale_channel_id'] ?? SaleChannels::getDefaultID();
|
|
$tags = $options['tags'] ?? false;
|
|
$model = ($options['homepage'] ?? false) ? Article::homepage()->visible() : Article::visible();
|
|
|
|
// exit;
|
|
$data = $model->byCategory($category_id)->byTags($tags)->withAvailableOffers($sale_channel_id)->with([
|
|
'image',
|
|
'product',
|
|
'article_nature',
|
|
'offers' => function ($query) use ($sale_channel_id) {
|
|
$query->bySaleChannel($sale_channel_id);
|
|
},
|
|
'offers.tariff' => function ($query) use ($sale_channel_id) {
|
|
$query->bySaleChannel($sale_channel_id);
|
|
},
|
|
'offers.tariff.price_lists' => function ($query) use ($sale_channel_id) {
|
|
$query->where('sale_channel_id', $sale_channel_id);
|
|
},
|
|
'offers.tariff.price_lists.price_list_values',
|
|
'offers.variation.package',
|
|
])->get();
|
|
return $data;
|
|
}
|
|
|
|
|
|
public static function getFull($id)
|
|
{
|
|
$data['article'] = self::getArticleEdit($id);
|
|
self::getMeta($data);
|
|
return $data;
|
|
}
|
|
|
|
public static function getArticleEdit($id)
|
|
{
|
|
$article = self::get($id);
|
|
$data = $article->toArray();
|
|
$data['inherited'] = self::getInherited($id);
|
|
$data['categories'] = self::getCategoriesByArticle($article);
|
|
$data['tags'] = self::getTagsByArticle($article);
|
|
$data['comments'] = Comments::getByModel($article);
|
|
return $data;
|
|
}
|
|
|
|
public static function getInherited($id)
|
|
{
|
|
$article = Article::with('product.tags.group')->findOrFail($id);
|
|
$product_type = $article->product_type;
|
|
switch ($product_type) {
|
|
case 'App\Models\Botanic\Variety':
|
|
$data[] = [
|
|
'name' => 'Espèces',
|
|
'description' => Species::getDescription($article->product->specie_id),
|
|
'tags' => Species::getTags($article->product->specie_id),
|
|
];
|
|
$data[] = [
|
|
'name' => 'Variétés',
|
|
'description' => $article->product->description,
|
|
'tags' => $article->product->tags->toArray()
|
|
];
|
|
break;
|
|
case 'App\Models\Botanic\Specie':
|
|
$data[] = [
|
|
'name' => 'Espèces',
|
|
'description' => $article->product->description,
|
|
'tags' => $article->product->tags->toArray()
|
|
];
|
|
break;
|
|
case 'App\Models\Shop\Merchandise':
|
|
$data[] = [
|
|
'name' => 'Marchandise',
|
|
'description' => $article->product->description,
|
|
'tags' => $article->product->tags->toArray(),
|
|
];
|
|
break;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public static function getInheritedByProduct($product_id, $product_type)
|
|
{
|
|
switch ($product_type) {
|
|
case 'App\Models\Botanic\Variety':
|
|
$product = Varieties::get($product_id);
|
|
$data[] = [
|
|
'name' => 'Espèces',
|
|
'description' => Species::getDescription($product->specie_id),
|
|
'tags' => Species::getTags($product->specie_id),
|
|
];
|
|
$data[] = [
|
|
'name' => 'Variétés',
|
|
'description' => $product->description,
|
|
'tags' => $product->tags->toArray(),
|
|
];
|
|
break;
|
|
case 'App\Models\Botanic\Specie':
|
|
$product = Species::get($product_id);
|
|
$data[] = [
|
|
'name' => 'Espèces',
|
|
'description' => $product->description,
|
|
'tags' => $product->tags->toArray(),
|
|
];
|
|
break;
|
|
case 'App\Models\Shop\Merchandise':
|
|
$product = Merchandises::get($product_id);
|
|
$data[] = [
|
|
'name' => 'Marchandise',
|
|
'description' => $product->description,
|
|
'tags' => $product->tags->toArray()
|
|
];
|
|
break;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public static function getInheritedImagesByProduct($product_id, $product_type)
|
|
{
|
|
switch ($product_type) {
|
|
case 'App\Models\Botanic\Variety':
|
|
$data['images'] = Varieties::getImages($product_id);
|
|
break;
|
|
case 'App\Models\Botanic\Specie':
|
|
$data['images'] = Species::getImages($product_id);
|
|
break;
|
|
case 'App\Models\Shop\Merchandise':
|
|
$data['images'] = Merchandises::getImages($product_id);
|
|
break;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public static function getMeta(&$data = [])
|
|
{
|
|
switch ($data['article']['product_type'] ?? false) {
|
|
case 'App\Models\Botanic\Variety':
|
|
$data['products'] = Varieties::getOptionsWithSpecie();
|
|
break;
|
|
case 'App\Models\Botanic\Variety':
|
|
$data['products'] = Species::getOptions();
|
|
break;
|
|
case 'App\Models\Shop\Merchandise':
|
|
$data['products'] = Merchandises::getOptions();
|
|
break;
|
|
default:
|
|
$data['products'] = [];
|
|
}
|
|
|
|
$data['categories_options'] = Categories::getOptions();
|
|
$data['natures_options'] = ArticleNatures::getOptions();
|
|
// $data['packages'] = ($data['article']['article_family_id'] ?? false) ? Packages::getSelectByFamily($data['article']['article_family_id']) : [];
|
|
$data['tags_list'] = TagGroups::getTreeTags();
|
|
$data['models_options'] = [
|
|
'App\Models\Botanic\Specie' => 'Espèces',
|
|
'App\Models\Botanic\Variety' => 'Variétés',
|
|
'App\Models\Shop\Merchandise' => 'Marchandise',
|
|
'App\Models\Shop\Merchandise' => 'Autres',
|
|
];
|
|
return $data;
|
|
}
|
|
|
|
public static function getByCategory($category_id)
|
|
{
|
|
return Article::byCategory($category_id)->with(['prices', 'product', 'image'])->get();
|
|
}
|
|
|
|
public static function getCategoriesByArticle($article)
|
|
{
|
|
return $article->categories->pluck('id')->toArray();
|
|
}
|
|
|
|
public static function getCategoriesNameByArticle($article)
|
|
{
|
|
return $article->categories->pluck('name', 'id')->toArray();
|
|
}
|
|
|
|
public static function getTagsByArticle($article)
|
|
{
|
|
return $article->tags->pluck('id')->toArray();
|
|
}
|
|
|
|
public static function getTagsNameByArticle($article)
|
|
{
|
|
return $article->tags->pluck('name', 'id')->toArray();
|
|
}
|
|
|
|
public static function getTagsSlugByArticle($article)
|
|
{
|
|
return $article->tags->pluck('slug', 'id')->toArray();
|
|
}
|
|
|
|
public static function getPricesByArticle($article)
|
|
{
|
|
return Prices::getByArticle($article->id);
|
|
}
|
|
|
|
public static function get($id)
|
|
{
|
|
return Article::find($id);
|
|
}
|
|
|
|
public static function getFullImageById($id)
|
|
{
|
|
$article = self::get($id);
|
|
return self::getFullImageByImage($article);
|
|
}
|
|
|
|
public static function getFullImageByArticle($article)
|
|
{
|
|
$image = $article->image;
|
|
if (!$image) {
|
|
switch ($article->product_type) {
|
|
case 'App\Models\Botanic\Variety':
|
|
$variety = $article->product;
|
|
$image = $variety->image;
|
|
if (!$image) {
|
|
$specie = $variety->specie;
|
|
$image = $specie->image;
|
|
}
|
|
break;
|
|
case 'App\Models\Botanic\Specie':
|
|
$specie = $article->product;
|
|
$image = $specie->image;
|
|
break;
|
|
}
|
|
}
|
|
return $image;
|
|
}
|
|
|
|
public static function storeFull($data)
|
|
{
|
|
$images = isset($data['images']) ? $data['images'] : false;
|
|
unset($data['images']);
|
|
|
|
$categories = isset($data['categories']) ? $data['categories'] : false;
|
|
unset($data['categories']);
|
|
|
|
$tags = isset($data['tags']) ? $data['tags'] : false;
|
|
unset($data['tags']);
|
|
|
|
$prices = isset($data['prices']) ? $data['prices'] : false;
|
|
unset($data['prices']);
|
|
|
|
$article = self::store($data);
|
|
self::storeImages($article, $images);
|
|
self::storeCategories($article, $categories);
|
|
self::storeTags($article, $tags);
|
|
return $article->id;
|
|
}
|
|
|
|
public static function store($data)
|
|
{
|
|
$data['visible'] = $data['visible'] ?? false;
|
|
return ($data['id'] ?? false) ? self::update($data) : self::create($data);
|
|
}
|
|
|
|
public static function create($data)
|
|
{
|
|
return Article::create($data);
|
|
}
|
|
|
|
public static function update($data, $id = false)
|
|
{
|
|
$id = $id ? $id : $data['id'];
|
|
$article = self::get($id);
|
|
$ret = $article->update($data);
|
|
return $article;
|
|
}
|
|
|
|
public static function destroy($id)
|
|
{
|
|
return Article::destroy($id);
|
|
}
|
|
|
|
public static function storeCategories($article, $categories)
|
|
{
|
|
if ($categories) {
|
|
$categories = collect($categories)->transform(
|
|
function ($item, $key) {
|
|
return (int) $item;
|
|
}
|
|
)->toArray();
|
|
return $article->syncCategories($categories, true);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function storeTags($article, $tags)
|
|
{
|
|
return Tag::storeTags($article, $tags);
|
|
}
|
|
|
|
public static function toggleVisible($id, $visible)
|
|
{
|
|
return self::update(['visible' => $visible], $id);
|
|
}
|
|
|
|
public static function toggleHomepage($id, $homepage)
|
|
{
|
|
return self::update(['homepage' => $homepage], $id);
|
|
}
|
|
|
|
}
|