<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Anagrafica extends Model
{
    protected $table = 'anagrafica';

    /**
     * Get the phone record associated with the user.
     */
    public function tipo()
    {
        //return $this->hasOne('App\AnagraficaTipo', 'id', 'anagrafica_tipo_id');
        return $this->belongsTo('App\AnagraficaTipo', 'anagrafica_tipo_id', 'id');
    }
    
    /**
     * Scope a query to only include Clienti.
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeClienti($query)
    {
        return $query->where('anagrafica_tipo_id', '=', 1);
    }

    /**
     * Scope a query to only include Clienti.
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeFornitori($query)
    {
        return $query->where('anagrafica_tipo_id', '=', 2);
    }  
    
    /**
     * Scope a query to only include Clienti.
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeCommittenti($query)
    {
        return $query->where('anagrafica_tipo_id', '=', 3);
    }   
    
    /**
     * Get the comments for the blog post.
     */
    public function interventiAsCliente()
    {
            return $this->hasMany('App\Intervento', 'cliente_id', 'id');
    }
    
    /**
     * Get the comments for the blog post.
     */
    public function interventiAsCommittente()    
    {
        return $this->hasMany('App\Intervento', 'committente_id', 'id');
    }   
    
    public function scopeOrder($query)
    {
        return $query->orderBy('ragione_sociale', 'asc')->orderBy('nome', 'asc')->orderBy('cognome', 'asc');
    }     
}
