Files
sam-api/app/Models/Tenants/Purchase.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

139 lines
3.1 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\SoftDeletes;
class Purchase extends Model
{
use Auditable, BelongsToTenant, SoftDeletes;
protected $fillable = [
'tenant_id',
'purchase_number',
'purchase_date',
'client_id',
'supply_amount',
'tax_amount',
'total_amount',
'description',
'status',
'purchase_type',
'withdrawal_id',
'approval_id',
'tax_invoice_received',
'created_by',
'updated_by',
'deleted_by',
];
/**
* 매입 유형 목록
*/
public const PURCHASE_TYPES = [
'unset' => '미설정',
'raw_material' => '원재료매입',
'subsidiary_material' => '부재료매입',
'product' => '상품매입',
'outsourcing' => '외주가공비',
'consumables' => '소모품비',
'repair' => '수선비',
'transportation' => '운반비',
'office_supplies' => '사무용품비',
'rent' => '임차료',
'utilities' => '수도광열비',
'communication' => '통신비',
'vehicle' => '차량유지비',
'entertainment' => '접대비',
'insurance' => '보험료',
'other_service' => '기타용역비',
];
protected $casts = [
'purchase_date' => 'date',
'supply_amount' => 'decimal:2',
'tax_amount' => 'decimal:2',
'total_amount' => 'decimal:2',
'client_id' => 'integer',
'withdrawal_id' => 'integer',
'approval_id' => 'integer',
'tax_invoice_received' => 'boolean',
];
/**
* 상태 목록
*/
public const STATUSES = [
'draft' => '임시저장',
'confirmed' => '확정',
];
/**
* 거래처 관계
*/
public function client(): BelongsTo
{
return $this->belongsTo(\App\Models\Orders\Client::class);
}
/**
* 출금 관계
*/
public function withdrawal(): BelongsTo
{
return $this->belongsTo(Withdrawal::class);
}
/**
* 연결된 품의서/지출결의서
*/
public function approval(): BelongsTo
{
return $this->belongsTo(Approval::class);
}
/**
* 생성자 관계
*/
public function creator(): BelongsTo
{
return $this->belongsTo(\App\Models\Members\User::class, 'created_by');
}
/**
* 상태 라벨
*/
public function getStatusLabelAttribute(): string
{
return self::STATUSES[$this->status] ?? $this->status;
}
/**
* 확정 가능 여부
*/
public function canConfirm(): bool
{
return $this->status === 'draft';
}
/**
* 수정 가능 여부
*/
public function canEdit(): bool
{
return $this->status === 'draft';
}
/**
* 삭제 가능 여부
*/
public function canDelete(): bool
{
return $this->status === 'draft';
}
}