Files
sam-api/app/Models/Tenants/Stock.php
권혁성 189b38c936 feat: Auditable 트레이트 구현 및 97개 모델 적용
- Auditable 트레이트 신규 생성 (bootAuditable 패턴)
  - creating: created_by/updated_by 자동 채우기
  - updating: updated_by 자동 채우기
  - deleting: deleted_by 채우기 + saveQuietly()
  - created/updated/deleted: audit_logs 자동 기록
- 기존 AuditLogger 패턴과 동일한 try/catch 조용한 실패
- 변경된 필드만 before/after 기록 (updated 이벤트)
- auditExclude 프로퍼티로 모델별 제외 필드 설정 가능
- 제외 대상: Attendance, StockTransaction, TodayIssue 등 고빈도/시스템 모델

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 15:33:54 +09:00

166 lines
3.8 KiB
PHP

<?php
namespace App\Models\Tenants;
use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Stock extends Model
{
use Auditable, BelongsToTenant, SoftDeletes;
protected $fillable = [
'tenant_id',
'item_id',
'item_code',
'item_name',
'item_type',
'specification',
'unit',
'stock_qty',
'safety_stock',
'reserved_qty',
'available_qty',
'lot_count',
'oldest_lot_date',
'location',
'status',
'last_receipt_date',
'last_issue_date',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'stock_qty' => 'decimal:3',
'safety_stock' => 'decimal:3',
'reserved_qty' => 'decimal:3',
'available_qty' => 'decimal:3',
'lot_count' => 'integer',
'oldest_lot_date' => 'date',
'last_receipt_date' => 'date',
'last_issue_date' => 'date',
];
/**
* 품목 유형 목록
*/
public const ITEM_TYPES = [
'raw_material' => '원자재',
'bent_part' => '절곡부품',
'purchased_part' => '구매부품',
'sub_material' => '부자재',
'consumable' => '소모품',
];
/**
* 재고 상태 목록
*/
public const STATUSES = [
'normal' => '정상',
'low' => '부족',
'out' => '없음',
];
/**
* 품목 관계
*/
public function item(): BelongsTo
{
return $this->belongsTo(\App\Models\Items\Item::class);
}
/**
* LOT 관계
*/
public function lots(): HasMany
{
return $this->hasMany(StockLot::class)->orderBy('fifo_order');
}
/**
* 거래 이력 관계
*/
public function transactions(): HasMany
{
return $this->hasMany(StockTransaction::class)->orderByDesc('created_at');
}
/**
* 생성자 관계
*/
public function creator(): BelongsTo
{
return $this->belongsTo(\App\Models\Members\User::class, 'created_by');
}
/**
* 품목유형 라벨
*/
public function getItemTypeLabelAttribute(): string
{
return self::ITEM_TYPES[$this->item_type] ?? $this->item_type;
}
/**
* 상태 라벨
*/
public function getStatusLabelAttribute(): string
{
return self::STATUSES[$this->status] ?? $this->status;
}
/**
* 경과일 계산 (가장 오래된 LOT 기준)
*/
public function getDaysElapsedAttribute(): int
{
if (! $this->oldest_lot_date) {
return 0;
}
return $this->oldest_lot_date->diffInDays(now());
}
/**
* 재고 상태 계산
*/
public function calculateStatus(): string
{
if ($this->stock_qty <= 0) {
return 'out';
}
if ($this->stock_qty < $this->safety_stock) {
return 'low';
}
return 'normal';
}
/**
* 재고 정보 업데이트 (LOT 기반)
*/
public function refreshFromLots(): void
{
$lots = $this->lots()->where('status', '!=', 'used')->get();
$this->lot_count = $lots->count();
$this->stock_qty = $lots->sum('qty');
$this->reserved_qty = $lots->sum('reserved_qty');
$this->available_qty = $lots->sum('available_qty');
$oldestLot = $lots->sortBy('receipt_date')->first();
$this->oldest_lot_date = $oldestLot?->receipt_date;
$this->last_receipt_date = $lots->max('receipt_date');
$this->status = $this->calculateStatus();
$this->save();
}
}