- Price, PriceRevision 모델 추가 (PriceHistory 대체) - PricingService: CRUD, 원가 조회, 확정 기능 - PricingController: statusCode 파라미터로 201 반환 지원 - NotFoundHttpException(404) 적용 (존재하지 않는 리소스) - FormRequest 분리 (Store, Update, Index, Cost, ByItems) - Swagger 문서 업데이트 - ApiResponse::handle()에 statusCode 옵션 추가 - prices/price_revisions 마이그레이션 및 데이터 이관
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,
|
|
];
|
|
}
|
|
}
|