<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use Sentinel;

class Intervento extends Model
{
    protected $table = 'interventi';
    
    public function setAnnoNumero(){
        
        $anno = date('Y');
        $this->anno = $anno;
        
        //---Cerco Ultimo Numero Inserito di Questo anno
        $last = \App\Intervento::where('anno', $anno)->orderBy('numero', 'desc')->first();
        if($last && $last->numero > 0) {
            $numero = $last->numero + 1;
        } else { 
            $numero = 1;
        }
        
        $this->numero = $numero;
    }
    
    public function elaboraDati(){
        $this->data_inserimento_ticket = $this->data_inserimento_ticket && $this->data_inserimento_ticket != '0000-00-00' ? Carbon::createFromFormat('Y-m-d', $this->data_inserimento_ticket)->format('d/m/Y') : '';
        $this->intervento_data_scadenza = $this->intervento_data_scadenza && $this->intervento_data_scadenza != '0000-00-00' ? Carbon::createFromFormat('Y-m-d', $this->intervento_data_scadenza)->format('d/m/Y') : '';
        $this->intervento_data = $this->intervento_data && $this->intervento_data != '0000-00-00' ? Carbon::createFromFormat('Y-m-d', $this->intervento_data)->format('d/m/Y') : '';
    }

    /**
     * Prende il cliente associato all'intervento
     */    
    public function cliente() {
        //return $this->belongsTo('App\Anagrafica', 'id', 'cliente_id' );
        return $this->hasOne('App\Anagrafica', 'id', 'cliente_id' );
    }
    
    /**
     * Prende il committente associato all'intervento
     */     
    public function committente() {
        return $this->hasOne('App\Anagrafica', 'id', 'committente_id' );
    }

    /**
     * Prende lo stato associato all'intervento
     */     
    public function stato() {
        return $this->belongsTo('App\InterventoStato', 'intervento_stato_id', 'id' );
    } 
    
    /**
     * Prende il tipo associato all'intervento
     */     
    public function tipo() {
        return $this->belongsTo('App\InterventoTipo', 'intervento_tipo_id', 'id' );
    }    
 
    /**
     * Prende i tecnici associati all'intervento
     */
    public function tecnici()
    {
        return $this->belongsToMany('App\Tecnico', 'intervento_tecnico', 'intervento_id', 'tecnico_id');
    }
    
    /**
     * Prende i prodotti associati all'intervento
     */
    public function prodotti()
    {
        return $this->belongsToMany('App\Prodotto', 'intervento_prodotto', 'intervento_id', 'prodotto_id');
    }    
    
//    public function scopeSearch($query, $search, $fields)
//    {
//        $query->where(function($q) use ($search, $fields) {
//            foreach ($fields as $field) {
//                $q->orWhere($field, "LIKE", "%$search%");
//            }
//        }
//    }  
    
    public function canUpdate(){
        if(Sentinel::inRole('admin')) {
            return true;
        } else if(Sentinel::inRole('tecnico')) {
            return in_array($this->intervento_stato_id, array(1,2));
        } else if(Sentinel::inRole('cliente')) {
            return false;
        }         
    }
}
