<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = "products";
    protected $fillable = [
        'company_id','store_id','category_id','product_name','wholesale_price','sector_price','balance','purchasing_price',
        'product_model','product_pic','description','min_balance','unit_id','balance_start','is_product'
    ];

    public function scopeProduct($query){
        return $query->where('is_product',1);
    }

    public function scopeService($query){
        return $query->where('is_product',0);
    }

    public function company(){
        return $this->belongsTo('\App\Models\Company','company_id','id');
    }

    public function unit(){
        return $this->belongsTo('\App\Models\Unit','unit_id','id');
    }
    public function store(){
        return $this->belongsTo('\App\Models\Store','store_id','id');
    }

    public function category(){
        return $this->belongsTo('\App\Models\Category','category_id','id');
    }

    public function sizes(){
        return $this->belongsToMany('\App\Models\Size','product_size','product_id','size_id','id','id');
    }

    public function colors(){
        return $this->belongsToMany('\App\Models\Color','product_color','product_id','color_id','id','id');
    }

    public function buy_bill_elements(){
        return $this->hasMany('\App\Models\BuyBillElement','product_id','id');
    }

    public function buy_bill_return(){
        return $this->hasMany('\App\Models\BuyBillReturn','product_id','id');
    }

    public function sale_bill_elements(){
        return $this->hasMany('\App\Models\SaleBillElement','product_id','id');
    }

    public function sale_bill_return(){
        return $this->hasMany('\App\Models\SaleBillReturn','product_id','id');
    }

    public function gifts(){
        return $this->hasMany('\App\Models\Gift','product_id','id');
    }

    public function quotation_elements(){
        return $this->hasMany('\App\Models\QuotationElement','product_id','id');
    }

    public function product_units(){
        return $this->hasMany(Product_unit::class,'product_id','id');
    }

    public function main_unit(){
        return $this->belongsTo(Product_unit::class,'unit_id','secondary_unit_id')->where('product_id',$this->id);
    }

    public function main_unit_quantity(){
        return $this->hasOne(Product_unit::class,'secondary_unit_id','unit_id','product_id','id');
    }
    public function getproductPicAttribute($value){
        if(!empty($value)){
            return asset($value);
        }
        return asset('app-assets/images/default.png');
    }


}
