<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class BuyBill extends Model
{
    protected $table = "buy_bills";
    protected $fillable = [
        'company_id','client_id','supplier_id','buy_bill_number','date','time','notes'
        ,'final_total','status','paid','rest','deleted'
    ];
    public function elements(){
        return $this->hasMany('\App\Models\BuyBillElement','buy_bill_id','id');
    }
    public function extras(){
        return $this->hasMany('\App\Models\BuyBillExtra','buy_bill_id','id');
    }
    public function company(){
        return $this->belongsTo('\App\Models\Company','company_id','id');
    }
    public function supplier(){
        return $this->belongsTo('\App\Models\Supplier','supplier_id','id');
    }
    public function client(){
        return $this->belongsTo('\App\Models\Client','client_id','id');
    }
    public function extra(){
        return $this->hasOne('\App\Models\BuyBillExtra','buy_bill_id','id')->where('action','extra');
    }
    public function discount(){
        return $this->hasOne('\App\Models\BuyBillExtra','buy_bill_id','id')->where('action','discount');
    }

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

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

    public function scopeDone($query){
        return $query->where('status','done');
    }
}
