netgescon-day0/app/Models/FornitoreDipendente.php

101 lines
2.8 KiB
PHP
Executable File

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FornitoreDipendente extends Model
{
use HasFactory;
public const TIPO_INTERNO = 'interno';
public const TIPO_FORNITORE_ESTERNO = 'fornitore_esterno';
protected $table = 'fornitore_dipendenti';
protected $fillable = [
'fornitore_id',
'fornitore_esterno_id',
'rubrica_id',
'user_id',
'tipo_collaboratore',
'nome',
'cognome',
'email',
'telefono',
'attivo',
'ultimo_accesso_at',
'note',
'created_by_user_id',
'updated_by_user_id',
];
protected $casts = [
'attivo' => 'boolean',
'ultimo_accesso_at' => 'datetime',
];
public function fornitore()
{
return $this->belongsTo(Fornitore::class, 'fornitore_id');
}
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function fornitoreEsterno()
{
return $this->belongsTo(Fornitore::class, 'fornitore_esterno_id');
}
public function rubrica()
{
return $this->belongsTo(RubricaUniversale::class, 'rubrica_id');
}
public function creatoDaUser()
{
return $this->belongsTo(User::class, 'created_by_user_id');
}
public function aggiornatoDaUser()
{
return $this->belongsTo(User::class, 'updated_by_user_id');
}
public function getNomeCompletoAttribute(): string
{
$label = trim((string) ($this->nome . ' ' . ($this->cognome ?? '')));
if ($label !== '') {
return $label;
}
$supplierLabel = trim((string) ($this->fornitoreEsterno?->ragione_sociale ?: trim((string) (($this->fornitoreEsterno?->nome ?? '') . ' ' . ($this->fornitoreEsterno?->cognome ?? '')))));
return $supplierLabel !== '' ? $supplierLabel : 'Collaboratore fornitore';
}
public function getTipoLabelAttribute(): string
{
return match ((string) $this->tipo_collaboratore) {
self::TIPO_FORNITORE_ESTERNO => 'Altro fornitore',
default => 'Collaboratore interno',
};
}
public function getRuoloOperativoLabelAttribute(): string
{
if ((string) $this->tipo_collaboratore === self::TIPO_FORNITORE_ESTERNO) {
$supplierLabel = trim((string) ($this->fornitoreEsterno?->ragione_sociale ?: trim((string) (($this->fornitoreEsterno?->nome ?? '') . ' ' . ($this->fornitoreEsterno?->cognome ?? '')))));
return $supplierLabel !== '' ? 'Subfornitore: ' . $supplierLabel : 'Subfornitore esterno';
}
return $this->nome_completo !== '' ? $this->nome_completo : 'Collaboratore interno';
}
}