'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', 'work_order_id' => 'integer', 'client_id' => 'integer', ]; /** * JSON 응답에 자동 포함할 accessor */ protected $appends = [ 'order_info', 'delivery_method_label', ]; /** * 출하 상태 목록 */ public const STATUSES = [ 'scheduled' => '출고예정', 'ready' => '출하대기', 'shipping' => '배송중', 'completed' => '배송완료', ]; /** * 우선순위 목록 */ public const PRIORITIES = [ 'urgent' => '긴급', 'normal' => '보통', 'low' => '낮음', ]; /** * 배송방식 목록 */ public const DELIVERY_METHODS = [ 'pickup' => '상차', 'direct' => '직접배차', 'logistics' => '물류사', ]; /** * 수주 관계 */ public function order(): BelongsTo { return $this->belongsTo(Order::class); } /** * 작업지시 관계 */ public function workOrder(): BelongsTo { return $this->belongsTo(WorkOrder::class); } /** * 출하 품목 관계 */ public function items(): HasMany { return $this->hasMany(ShipmentItem::class)->orderBy('seq'); } /** * 배차정보 관계 */ public function vehicleDispatches(): HasMany { return $this->hasMany(ShipmentVehicleDispatch::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; } /** * 배송방식 라벨 (common_codes 테이블에서 조회) */ public function getDeliveryMethodLabelAttribute(): string { return CommonCode::getLabel('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; } // ============================================================ // 수주 연동 정보 Accessor (수주에서 읽기 전용) // 배송지 정보의 원본은 수주(Order)에 저장 // 출하에서 수정 시 updateOrderDeliveryInfo() 메서드로 수주 업데이트 // ============================================================ /** * 거래처 ID (수주에서 참조) */ public function getOrderClientIdAttribute(): ?int { return $this->order?->client_id; } /** * 고객명 (수주.client_name → 수주.거래처.name 순으로 참조) */ public function getOrderCustomerNameAttribute(): ?string { if ($this->order) { return $this->order->client_name ?? $this->order->client?->name; } return null; } /** * 현장명 (수주에서 참조) */ public function getOrderSiteNameAttribute(): ?string { return $this->order?->site_name; } /** * 배송주소 (수주.거래처.address 참조) */ public function getOrderDeliveryAddressAttribute(): ?string { return $this->order?->client?->address; } /** * 연락처 (수주.client_contact → 수주.거래처.phone 순으로 참조) */ public function getOrderContactAttribute(): ?string { if ($this->order) { return $this->order->client_contact ?? $this->order->client?->phone; } return null; } /** * 수주 연동 정보 일괄 조회 (API 응답용) * * @return array 수주에서 참조한 발주처 정보 */ public function getOrderInfoAttribute(): array { return [ 'order_id' => $this->order_id, 'order_no' => $this->order?->order_no, 'order_status' => $this->order?->status_code, 'client_id' => $this->order_client_id, 'customer_name' => $this->order_customer_name, 'site_name' => $this->order_site_name, 'delivery_address' => $this->order_delivery_address, 'contact' => $this->order_contact, // 추가 정보 'delivery_date' => $this->order?->delivery_date?->format('Y-m-d'), 'writer_id' => $this->order?->writer_id, 'writer_name' => $this->order?->writer?->name, ]; } /** * 출하에서 배송 정보 수정 시 수주(Order) 업데이트 * * 출하 화면에서 배송 정보를 수정하면 수주의 데이터를 변경합니다. * 데이터의 원본은 항상 수주(Order)에 저장됩니다. * * @param array $data 수정할 배송 정보 (client_name, client_contact, site_name) * @return bool 업데이트 성공 여부 */ public function updateOrderDeliveryInfo(array $data): bool { if (! $this->order) { return false; } $allowedFields = ['client_id', 'client_name', 'client_contact', 'site_name']; $updateData = array_intersect_key($data, array_flip($allowedFields)); if (empty($updateData)) { return false; } return $this->order->update($updateData); } /** * 새 출하번호 생성 */ 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; } }