- ShipmentController: 출하 CRUD 및 상태 관리 API - ShipmentService: 출하 비즈니스 로직 - Shipment, ShipmentItem 모델 - FormRequest 검증 클래스 - Swagger 문서화 - shipments, shipment_items 테이블 마이그레이션 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
211 lines
4.8 KiB
PHP
211 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Tenants;
|
|
|
|
use App\Traits\BelongsToTenant;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Shipment extends Model
|
|
{
|
|
use BelongsToTenant, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'shipment_no',
|
|
'lot_no',
|
|
'order_id',
|
|
'scheduled_date',
|
|
'status',
|
|
'priority',
|
|
'delivery_method',
|
|
// 발주처/배송 정보
|
|
'client_id',
|
|
'customer_name',
|
|
'site_name',
|
|
'delivery_address',
|
|
'receiver',
|
|
'receiver_contact',
|
|
// 상태 플래그
|
|
'can_ship',
|
|
'deposit_confirmed',
|
|
'invoice_issued',
|
|
'customer_grade',
|
|
// 상차 정보
|
|
'loading_manager',
|
|
'loading_completed_at',
|
|
'loading_time',
|
|
// 물류/배차 정보
|
|
'logistics_company',
|
|
'vehicle_tonnage',
|
|
'shipping_cost',
|
|
// 차량/운전자 정보
|
|
'vehicle_no',
|
|
'driver_name',
|
|
'driver_contact',
|
|
'expected_arrival',
|
|
'confirmed_arrival',
|
|
// 기타
|
|
'remarks',
|
|
'created_by',
|
|
'updated_by',
|
|
'deleted_by',
|
|
];
|
|
|
|
protected $casts = [
|
|
'scheduled_date' => '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;
|
|
}
|
|
}
|