78 lines
1.8 KiB
PHP
78 lines
1.8 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Model;
|
||
|
||
class ArchiveHistory extends Model
|
||
{
|
||
protected $connection = 'pgsql';
|
||
|
||
protected static function booted()
|
||
{
|
||
static::created(function ($archiveHistory) {
|
||
$archiveHistory->updateArchiveInfoStatus();
|
||
});
|
||
|
||
static::updated(function ($archiveHistory) {
|
||
$archiveHistory->updateArchiveInfoStatus();
|
||
});
|
||
}
|
||
|
||
public function updateArchiveInfoStatus()
|
||
{
|
||
// Получаем связанную модель через морф
|
||
$historyable = $this->historyable;
|
||
|
||
if (!$historyable) {
|
||
return;
|
||
}
|
||
|
||
// Проверяем, есть ли у модели архивная информация
|
||
if (method_exists($historyable, 'archiveInfo') && $historyable->archiveInfo) {
|
||
$historyable->archiveInfo->update([
|
||
'status_id' => $this->determineStatusId()
|
||
]);
|
||
}
|
||
}
|
||
|
||
public function determineStatusId()
|
||
{
|
||
if ($this->has_lost) {
|
||
return 4; // Утерян
|
||
}
|
||
|
||
if ($this->issue_at && !$this->return_at) {
|
||
return 3; // Выдан
|
||
}
|
||
|
||
if ($this->return_at) {
|
||
return 2; // В архиве
|
||
}
|
||
|
||
return 2; // По умолчанию
|
||
}
|
||
|
||
protected $fillable = [
|
||
'historyable_type',
|
||
'historyable_id',
|
||
'issue_at',
|
||
'return_at',
|
||
'comment',
|
||
'org_id',
|
||
'employee_name',
|
||
'employee_post',
|
||
'has_lost',
|
||
];
|
||
|
||
public function historyable()
|
||
{
|
||
return $this->morphTo();
|
||
}
|
||
|
||
public function org(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||
{
|
||
return $this->belongsTo(Org::class);
|
||
}
|
||
}
|