<?php

namespace App\Models\Shop;

use Illuminate\Database\Eloquent\Model;
use Znck\Eloquent\Traits\BelongsToThrough;

class ArticlePrice extends Model
{
    use BelongsToThrough;

    protected $guarded = ['id'];
    protected $table = 'shop_article_prices';

    public function price()
    {
        return $this->hasOne(Price::class);
    }

    public function article_family()
    {
        return $this->belongsTo(ArticleNature::class);
    }

    public function scopeByQuantity($query, $quantity)
    {
        return $query->orderBy('quantity', 'desc')->where('quantity', '<', $quantity)->first();
    }

    public function scopeByFamily($query, $id)
    {
        return $query->where($this->table . '.article_family_value_id', $id);
    }
}
