'date', 'can_ship' => 'boolean', 'deposit_confirmed' => 'boolean', 'invoice_issued' => 'boolean', 'loading_completed_at' => 'datetime', 'loading_time' => 'datetime', 'expected_arrival' => 'datetime', 'confirmed_arrival' => 'datetime', 'shipping_cost' => 'decimal:0', 'order_id' => 'integer', 'client_id' => 'integer', ]; /** * 출하 상태 목록 */ public const STATUSES = [ 'scheduled' => '출고예정', 'ready' => '출하대기', 'shipping' => '배송중', 'completed' => '배송완료', ]; /** * 우선순위 목록 */ public const PRIORITIES = [ 'urgent' => '긴급', 'normal' => '보통', 'low' => '낮음', ]; /** * 배송방식 목록 */ public const DELIVERY_METHODS = [ 'pickup' => '상차', 'direct' => '직접배차', 'logistics' => '물류사', ]; /** * 출하 품목 관계 */ public function items(): HasMany { return $this->hasMany(ShipmentItem::class)->orderBy('seq'); } /** * 거래처 관계 */ public function client(): BelongsTo { return $this->belongsTo(\App\Models\Clients\Client::class); } /** * 생성자 관계 */ public function creator(): BelongsTo { return $this->belongsTo(\App\Models\Members\User::class, 'created_by'); } /** * 수정자 관계 */ public function updater(): BelongsTo { return $this->belongsTo(\App\Models\Members\User::class, 'updated_by'); } /** * 상태 라벨 */ public function getStatusLabelAttribute(): string { return self::STATUSES[$this->status] ?? $this->status; } /** * 우선순위 라벨 */ public function getPriorityLabelAttribute(): string { return self::PRIORITIES[$this->priority] ?? $this->priority; } /** * 배송방식 라벨 */ public function getDeliveryMethodLabelAttribute(): string { return self::DELIVERY_METHODS[$this->delivery_method] ?? $this->delivery_method; } /** * 총 품목 수량 */ public function getTotalQuantityAttribute(): float { return $this->items->sum('quantity'); } /** * 품목 수 */ public function getItemCountAttribute(): int { return $this->items->count(); } /** * 긴급 여부 */ public function getIsUrgentAttribute(): bool { return $this->priority === 'urgent'; } /** * 출하 가능 여부 확인 */ public function canProceedToShip(): bool { return $this->can_ship && $this->deposit_confirmed; } /** * 새 출하번호 생성 */ public static function generateShipmentNo(int $tenantId): string { $today = now()->format('Ymd'); $prefix = 'SHP-'.$today.'-'; $lastShipment = static::withoutGlobalScopes() ->where('tenant_id', $tenantId) ->where('shipment_no', 'like', $prefix.'%') ->orderByDesc('shipment_no') ->first(); if ($lastShipment) { $lastSeq = (int) substr($lastShipment->shipment_no, -4); $newSeq = str_pad($lastSeq + 1, 4, '0', STR_PAD_LEFT); } else { $newSeq = '0001'; } return $prefix.$newSeq; } }