98 lines
2.4 KiB
PHP
98 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Lists\UserPosition;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use OwenIt\Auditing\Auditable as AuditableTrait;
|
|
use OwenIt\Auditing\Contracts\Auditable;
|
|
|
|
class UserAffiliation extends Model implements Auditable
|
|
{
|
|
use AuditableTrait;
|
|
use HasFactory;
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'institution_id',
|
|
'user_id',
|
|
'user_position_id',
|
|
'start_year',
|
|
'end_year',
|
|
'legacy_user_id',
|
|
'legacy_institution_id',
|
|
];
|
|
|
|
/**
|
|
* Bookkeeping ETL, non rilevanti per l'admin UI.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $hidden = [
|
|
'legacy_user_id',
|
|
'legacy_institution_id',
|
|
];
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'institution_id' => 'integer',
|
|
'user_id' => 'integer',
|
|
'user_position_id' => 'integer',
|
|
'start_year' => 'integer',
|
|
'end_year' => 'integer',
|
|
'is_open' => 'boolean',
|
|
'legacy_user_id' => 'integer',
|
|
'legacy_institution_id' => 'integer',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<Institution, $this>
|
|
*/
|
|
public function institution(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Institution::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<User, $this>
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<UserPosition, $this>
|
|
*/
|
|
public function userPosition(): BelongsTo
|
|
{
|
|
return $this->belongsTo(UserPosition::class);
|
|
}
|
|
|
|
/**
|
|
* Esiste già un'altra affiliazione aperta per lo stesso ente+utente: un
|
|
* restore qui urterebbe l'indice unique su (institution_id, user_id, is_open).
|
|
*/
|
|
public function hasConflictingOpenAffiliation(): bool
|
|
{
|
|
if (filled($this->end_year)) {
|
|
return false;
|
|
}
|
|
|
|
return self::query()
|
|
->where('institution_id', $this->institution_id)
|
|
->where('user_id', $this->user_id)
|
|
->where('id', '!=', $this->id)
|
|
->whereNotNull('is_open')
|
|
->exists();
|
|
}
|
|
}
|