- 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>
153 lines
4.5 KiB
PHP
153 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Production;
|
|
|
|
use App\Models\Items\Item;
|
|
use App\Traits\Auditable;
|
|
use App\Traits\BelongsToTenant;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* 작업지시 품목 모델
|
|
*/
|
|
class WorkOrderItem extends Model
|
|
{
|
|
use Auditable, BelongsToTenant;
|
|
|
|
protected $table = 'work_order_items';
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'work_order_id',
|
|
'source_order_item_id', // 원본 수주 품목 추적용
|
|
'item_id',
|
|
'item_name',
|
|
'specification',
|
|
'quantity',
|
|
'unit',
|
|
'sort_order',
|
|
'status',
|
|
'options',
|
|
];
|
|
|
|
/**
|
|
* 품목 상태 상수
|
|
*/
|
|
public const STATUS_WAITING = 'waiting';
|
|
|
|
public const STATUS_IN_PROGRESS = 'in_progress';
|
|
|
|
public const STATUS_COMPLETED = 'completed';
|
|
|
|
public const STATUSES = [
|
|
self::STATUS_WAITING,
|
|
self::STATUS_IN_PROGRESS,
|
|
self::STATUS_COMPLETED,
|
|
];
|
|
|
|
protected $casts = [
|
|
'quantity' => 'decimal:2',
|
|
'sort_order' => 'integer',
|
|
'options' => 'array',
|
|
];
|
|
|
|
// ──────────────────────────────────────────────────────────────
|
|
// 관계
|
|
// ──────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* 작업지시
|
|
*/
|
|
public function workOrder(): BelongsTo
|
|
{
|
|
return $this->belongsTo(WorkOrder::class);
|
|
}
|
|
|
|
/**
|
|
* 품목
|
|
*/
|
|
public function item(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Item::class);
|
|
}
|
|
|
|
// ──────────────────────────────────────────────────────────────
|
|
// 스코프
|
|
// ──────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* 정렬 순서
|
|
*/
|
|
public function scopeOrdered($query)
|
|
{
|
|
return $query->orderBy('sort_order');
|
|
}
|
|
|
|
/**
|
|
* 완료된 품목만 필터
|
|
*/
|
|
public function scopeCompleted($query)
|
|
{
|
|
return $query->where('status', self::STATUS_COMPLETED);
|
|
}
|
|
|
|
/**
|
|
* 작업 결과가 있는 품목만 필터
|
|
*/
|
|
public function scopeHasResult($query)
|
|
{
|
|
return $query->whereNotNull('options->result');
|
|
}
|
|
|
|
// ──────────────────────────────────────────────────────────────
|
|
// 헬퍼 메서드
|
|
// ──────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* 작업 결과 데이터 가져오기
|
|
*/
|
|
public function getResult(): ?array
|
|
{
|
|
return $this->options['result'] ?? null;
|
|
}
|
|
|
|
/**
|
|
* 작업 결과 데이터 설정
|
|
*/
|
|
public function setResult(array $result): void
|
|
{
|
|
$options = $this->options ?? [];
|
|
$options['result'] = array_merge($options['result'] ?? [], $result);
|
|
$this->options = $options;
|
|
}
|
|
|
|
/**
|
|
* 작업 완료 처리 (결과 데이터 저장)
|
|
*/
|
|
public function completeWithResult(array $resultData = []): void
|
|
{
|
|
$this->status = self::STATUS_COMPLETED;
|
|
|
|
$result = [
|
|
'completed_at' => now()->toDateTimeString(),
|
|
'good_qty' => $resultData['good_qty'] ?? $this->quantity,
|
|
'defect_qty' => $resultData['defect_qty'] ?? 0,
|
|
'lot_no' => $resultData['lot_no'] ?? null,
|
|
'is_inspected' => $resultData['is_inspected'] ?? false,
|
|
'is_packaged' => $resultData['is_packaged'] ?? false,
|
|
'worker_id' => $resultData['worker_id'] ?? null,
|
|
'memo' => $resultData['memo'] ?? null,
|
|
];
|
|
|
|
// 불량률 자동 계산
|
|
$totalQty = $result['good_qty'] + $result['defect_qty'];
|
|
$result['defect_rate'] = $totalQty > 0
|
|
? round(($result['defect_qty'] / $totalQty) * 100, 2)
|
|
: 0;
|
|
|
|
$this->setResult($result);
|
|
$this->save();
|
|
}
|
|
}
|