File: /var/www/ipsremont-demo/app/Models/WorkCompleteCertificateChangeLog.php
<?php
namespace App\Models;
use App\Repository\Wcc\WccRepository;
use App\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property int $id
* @property int $work_complete_certificate_id
* @property int $user_id
* @property string $field
* @property string $old_value
* @property string $new_value
* @property int $type
* @property string $created_at
* @property string $updated_at
*
* @property WorkCompleteCertificate $workCompleteCertificate
* @property User $user
*/
class WorkCompleteCertificateChangeLog extends BaseModel
{
protected $fillable = [
'work_complete_certificate_id',
'user_id',
'field',
'old_value',
'new_value',
'type',
];
const TYPE_CREATE = 1;
const TYPE_UPDATE = 2;
const TYPE_DELETE = 3;
public function getDate(): string
{
$text = $this->created_at->format('d.m.Y') . '</br>';
$text .= $this->created_at->format('H:i:s');
return $text;
}
public static function getLogLabel(string $field): string
{
switch ($field) {
case 'article':
return 'Артикул';
case 'serial_number':
return 'Серийный номер';
case 'amount':
return 'Количество';
case 'name':
return 'Название ТМЦ';
case 'work_description':
return 'Описание работ';
case 'executor':
return 'Исполнитель';
case 'client':
return 'Заказчик';
case 'device_name':
return 'Устройство';
}
return 'Неизвестное поле (' . $field . ')';
}
public function getText(): string
{
$fieldName = self::getLogLabel($this->field);
if (in_array($this->field, ['deleted_item', 'new_item'])) {
$text = 'Была ' . ($this->field === 'deleted_item' ? 'удалена' : 'создана') . ' ТМЦ: ';
$workCompleteCertificateItem = WccRepository::getWorkCompleteCertificateItem($this->new_value);
$text .= 'артикул <strong>' . $workCompleteCertificateItem->article . '</strong>, ';
$text .= 'наименование <strong>' . $workCompleteCertificateItem->name . '</strong>, ';
$text .= 'количество <strong>' . $workCompleteCertificateItem->amount . '</strong>, ';
} elseif (isset($this->old_value, $this->new_value)) {
$text = 'Изменено поле «' . $fieldName . '» с ';
$text .= '<strong>' . $this->old_value . '</strong>';
$text .= ' на ';
$text .= '<strong>' . $this->new_value . '</strong> ';
} elseif (isset($this->new_value)) {
$text = 'В поле «' . $fieldName . '» установлено значение ';
$text .= '<strong>' . $this->new_value . '</strong> ';
} else {
$text = 'В поле «' . $fieldName . '» убрали значение, было ';
$text .= '<strong>' . $this->old_value . '</strong> ';
}
if (isset($this->user_id)) {
$text .= __('users.byUser') . ' <strong>' . $this->user->getName() . '</strong> ';
} else {
$text .= __('users.userNotSpecified');
}
return $text;
}
// MARK: - Relations
public function workCompleteCertificate(): BelongsTo
{
return $this->belongsTo(WorkCompleteCertificate::class)->withTrashed();
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class)->withTrashed();
}
}