93 lines
2.1 KiB
PHP
93 lines
2.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models\Products;
|
||
|
|
|
||
|
|
use App\Traits\BelongsToTenant;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 단가 변경 이력 모델
|
||
|
|
*
|
||
|
|
* @property int $id
|
||
|
|
* @property int $tenant_id
|
||
|
|
* @property int $price_id
|
||
|
|
* @property int $revision_number
|
||
|
|
* @property \Carbon\Carbon $changed_at
|
||
|
|
* @property int $changed_by
|
||
|
|
* @property string|null $change_reason
|
||
|
|
* @property array|null $before_snapshot
|
||
|
|
* @property array $after_snapshot
|
||
|
|
*/
|
||
|
|
class PriceRevision extends Model
|
||
|
|
{
|
||
|
|
use BelongsToTenant;
|
||
|
|
|
||
|
|
public $timestamps = false;
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'tenant_id',
|
||
|
|
'price_id',
|
||
|
|
'revision_number',
|
||
|
|
'changed_at',
|
||
|
|
'changed_by',
|
||
|
|
'change_reason',
|
||
|
|
'before_snapshot',
|
||
|
|
'after_snapshot',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'revision_number' => 'integer',
|
||
|
|
'changed_at' => 'datetime',
|
||
|
|
'before_snapshot' => 'array',
|
||
|
|
'after_snapshot' => 'array',
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 단가 관계
|
||
|
|
*/
|
||
|
|
public function price(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Price::class, 'price_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 변경자 관계
|
||
|
|
*/
|
||
|
|
public function changedByUser(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(\App\Models\Members\User::class, 'changed_by');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 변경된 필드 목록 추출
|
||
|
|
*/
|
||
|
|
public function getChangedFields(): array
|
||
|
|
{
|
||
|
|
if (! $this->before_snapshot) {
|
||
|
|
return array_keys($this->after_snapshot ?? []);
|
||
|
|
}
|
||
|
|
|
||
|
|
$changed = [];
|
||
|
|
foreach ($this->after_snapshot as $key => $newValue) {
|
||
|
|
$oldValue = $this->before_snapshot[$key] ?? null;
|
||
|
|
if ($oldValue !== $newValue) {
|
||
|
|
$changed[] = $key;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $changed;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 특정 필드의 이전/이후 값
|
||
|
|
*/
|
||
|
|
public function getFieldChange(string $field): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'before' => $this->before_snapshot[$field] ?? null,
|
||
|
|
'after' => $this->after_snapshot[$field] ?? null,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|