219 lines
5.7 KiB
PHP
219 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Shop;
|
|
|
|
use App\Models\Botanic\Specie;
|
|
use App\Models\Botanic\Variety;
|
|
use App\Traits\Model\HasComments;
|
|
use App\Traits\Model\Imageable;
|
|
use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Kirschbaum\PowerJoins\PowerJoins;
|
|
use Rinvex\Categories\Traits\Categorizable;
|
|
use Rinvex\Tags\Traits\Taggable;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
|
|
use \Venturecraft\Revisionable\RevisionableTrait;
|
|
use Wildside\Userstamps\Userstamps;
|
|
|
|
class Article extends Model implements HasMedia
|
|
{
|
|
use Categorizable;
|
|
use EloquentJoin;
|
|
use HasComments;
|
|
use HasRelationships;
|
|
use Imageable;
|
|
use Powerjoins;
|
|
use RevisionableTrait;
|
|
use Taggable;
|
|
use SoftDeletes;
|
|
use UserStamps;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected $table = 'shop_articles';
|
|
|
|
protected $revisionEnabled = true;
|
|
|
|
protected $keepRevisionOf = [
|
|
'article_nature_id',
|
|
'product_type',
|
|
'product_id',
|
|
'ref',
|
|
'name',
|
|
'description',
|
|
'visible',
|
|
'homepage',
|
|
];
|
|
|
|
public function article_nature()
|
|
{
|
|
return $this->belongsTo(ArticleNature::class);
|
|
}
|
|
|
|
public function invoiceItems()
|
|
{
|
|
return $this->hasMany(InvoiceItem::class);
|
|
}
|
|
|
|
public function offers()
|
|
{
|
|
return $this->hasMany(Offer::class);
|
|
}
|
|
|
|
public function price_lists()
|
|
{
|
|
return $this->hasManyDeep(
|
|
PriceList::class,
|
|
[Offer::class, Tariff::class],
|
|
['article_id', 'id'],
|
|
['id', 'tariff_id'],
|
|
);
|
|
}
|
|
|
|
public function prices()
|
|
{
|
|
return $this->hasManyDeep(
|
|
PriceListValue::class,
|
|
[Offer::class, Tariff::class, PriceList::class],
|
|
['article_id', 'id', 'tariff_id'],
|
|
['id', 'tariff_id', 'id'],
|
|
);
|
|
}
|
|
|
|
public function product()
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
public function siblings()
|
|
{
|
|
return $this->hasMany(Article::class, 'name', 'name');
|
|
}
|
|
|
|
public function tags()
|
|
{
|
|
return $this->morphToMany(Tag::class, 'taggable');
|
|
}
|
|
|
|
public function tariffs()
|
|
{
|
|
return $this->hasManyThrough(Tariff::class, Offer::class, 'article_id', 'id', 'id', 'tariff_id');
|
|
// return $this->belongsToMany(Tariff::class, Offer::$table, 'id', 'id', 'tariff_id', 'tariff_id');
|
|
}
|
|
|
|
public function scopeByArticle($query, $id)
|
|
{
|
|
return $query->where($this->table.'.id', $id);
|
|
}
|
|
|
|
public function scopeByAutocomplete($query, $str)
|
|
{
|
|
return $query->where($this->table.'.name', 'LIKE', '%'.$str.'%');
|
|
}
|
|
|
|
public function scopeSearch($query, $str)
|
|
{
|
|
return $query->where($this->table.'.name', 'LIKE', '%'.$str.'%');
|
|
}
|
|
|
|
public function scopeByArticleNature($query, $id)
|
|
{
|
|
return $id ? $query->where($this->table.'.article_nature_id', $id) : $query;
|
|
}
|
|
|
|
public function scopeByArticleNatures($query, $ids)
|
|
{
|
|
return $ids ? $query->whereIn($this->table.'.article_nature_id', $ids) : $query;
|
|
}
|
|
|
|
public function scopeByCategories($query, $categoriesId)
|
|
{
|
|
return $categoriesId ? $query->whereHas('categories', function ($query) use ($categoriesId) {
|
|
$query->whereIn('id', $categoriesId);
|
|
}) : $query;
|
|
}
|
|
|
|
public function scopeByCategoryParent($query, $categoryId)
|
|
{
|
|
$category = Category::find($categoryId);
|
|
|
|
return $categoryId ? $query->whereHas('categories', function ($query) use ($category) {
|
|
$query->where('_lft', '>=', $category->_lft)->where('_rgt', '<=', $category->_rgt);
|
|
}) : $query;
|
|
}
|
|
|
|
public function scopeByCategory($query, $categoryId)
|
|
{
|
|
return $categoryId ? $query->whereHas('categories', function ($query) use ($categoryId) {
|
|
$query->where('id', $categoryId);
|
|
}) : $query;
|
|
}
|
|
|
|
public function scopeBotanic($query)
|
|
{
|
|
return $query->whereIn($this->table.'.product_type', [Variety::class, Specie::class]);
|
|
}
|
|
|
|
public function scopeMerchandise($query)
|
|
{
|
|
return $query->byProduct(Merchandise::class);
|
|
}
|
|
|
|
public function scopeByProduct($query, $model)
|
|
{
|
|
return $model ? $query->where($this->table.'.product_type', $model) : $query;
|
|
}
|
|
|
|
public function scopeByProductId($query, $productId)
|
|
{
|
|
return $productId ? $query->where($this->table.'.product_id', $productId) : $query;
|
|
}
|
|
|
|
public function scopeByTag($query, $tagId)
|
|
{
|
|
return $tagId ? $query->whereHas('tags', function ($query) use ($tagId) {
|
|
$query->where('id', $tagId);
|
|
}) : $query;
|
|
}
|
|
|
|
public function scopeByTags($query, $tags)
|
|
{
|
|
return $tags ? $query->whereHas('tags', function ($query) use ($tags) {
|
|
$query->whereIn('id', $tags);
|
|
}) : $query;
|
|
}
|
|
|
|
public function scopeByTagsSelected($query, $tags)
|
|
{
|
|
return $tags ? $query->whereHas('tags', function ($query) use ($tags) {
|
|
foreach ($tags as $tag) {
|
|
$query->where('id', $tag);
|
|
}
|
|
}) : $query;
|
|
}
|
|
|
|
public function scopeWithOffers($query)
|
|
{
|
|
return $query->has('offers');
|
|
}
|
|
|
|
public function scopeWithAvailableOffers($query, $saleChannelId = false)
|
|
{
|
|
return $query->whereHas('offers', function ($query) use ($saleChannelId) {
|
|
$query->active()->byStockAvailable()->bySaleChannel($saleChannelId);
|
|
});
|
|
}
|
|
|
|
public function scopeVisible($query)
|
|
{
|
|
return $query->where($this->table.'.visible', 1);
|
|
}
|
|
|
|
public function scopeHomepage($query)
|
|
{
|
|
return $query->where($this->table.'.homepage', 1);
|
|
}
|
|
}
|