fix: MES 모델 및 서비스 개선
- WorkOrderItem 모델 관계 정의 추가 - InspectionService, WorkResultService 로직 개선 - ItemReceipt, Inspection 모델 수정 - work_order_items 테이블에 options 컬럼 추가 마이그레이션 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Models\Items;
|
||||
|
||||
use App\Models\Scopes\BelongsToTenant;
|
||||
use App\Models\Tenants\User;
|
||||
use App\Models\Members\User;
|
||||
use App\Traits\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
*/
|
||||
class ItemReceipt extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
use BelongsToTenant, SoftDeletes;
|
||||
|
||||
protected $table = 'item_receipts';
|
||||
|
||||
@@ -61,11 +61,6 @@ class ItemReceipt extends Model
|
||||
'weight_kg' => 'decimal:2',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::addGlobalScope(new BelongsToTenant);
|
||||
}
|
||||
|
||||
// ===== Relationships =====
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,6 +26,7 @@ class WorkOrderItem extends Model
|
||||
'unit',
|
||||
'sort_order',
|
||||
'status',
|
||||
'options',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -44,6 +45,7 @@ class WorkOrderItem extends Model
|
||||
protected $casts = [
|
||||
'quantity' => 'decimal:2',
|
||||
'sort_order' => 'integer',
|
||||
'options' => 'array',
|
||||
];
|
||||
|
||||
// ──────────────────────────────────────────────────────────────
|
||||
@@ -77,4 +79,70 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
namespace App\Models\Qualitys;
|
||||
|
||||
use App\Models\Items\Item;
|
||||
use App\Models\Scopes\BelongsToTenant;
|
||||
use App\Models\Tenants\User;
|
||||
use App\Models\Members\User;
|
||||
use App\Traits\BelongsToTenant;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
@@ -32,7 +32,7 @@
|
||||
*/
|
||||
class Inspection extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
use BelongsToTenant, SoftDeletes;
|
||||
|
||||
protected $table = 'inspections';
|
||||
|
||||
@@ -89,11 +89,6 @@ class Inspection extends Model
|
||||
|
||||
public const RESULT_FAIL = 'fail';
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::addGlobalScope(new BelongsToTenant);
|
||||
}
|
||||
|
||||
// ===== Relationships =====
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user