- Order 모델에 workOrders 관계 추가 - OrderService에 for_work_order 파라미터 처리 추가 - DRAFT 상태 & 작업지시 미생성 수주만 조회 Co-Authored-By: Claude <noreply@anthropic.com>
182 lines
4.4 KiB
PHP
182 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Orders;
|
|
|
|
use App\Models\Items\Item;
|
|
use App\Models\Production\WorkOrder;
|
|
use App\Models\Quote\Quote;
|
|
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;
|
|
|
|
/**
|
|
* 수주 마스터 (Orders)
|
|
*
|
|
* @mixin IdeHelperOrder
|
|
*/
|
|
class Order extends Model
|
|
{
|
|
use BelongsToTenant, SoftDeletes;
|
|
|
|
// 상태 코드
|
|
public const STATUS_DRAFT = 'DRAFT';
|
|
|
|
public const STATUS_CONFIRMED = 'CONFIRMED';
|
|
|
|
public const STATUS_IN_PROGRESS = 'IN_PROGRESS';
|
|
|
|
public const STATUS_COMPLETED = 'COMPLETED';
|
|
|
|
public const STATUS_CANCELLED = 'CANCELLED';
|
|
|
|
// 주문 유형
|
|
public const TYPE_ORDER = 'ORDER'; // 수주
|
|
|
|
public const TYPE_PURCHASE = 'PURCHASE'; // 발주
|
|
|
|
protected $table = 'orders';
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'quote_id',
|
|
'order_no',
|
|
'order_type_code',
|
|
'status_code',
|
|
'category_code',
|
|
'item_id',
|
|
'received_at',
|
|
'writer_id',
|
|
'client_id',
|
|
'client_name',
|
|
'client_contact',
|
|
'site_name',
|
|
'quantity',
|
|
// 금액 정보
|
|
'supply_amount',
|
|
'tax_amount',
|
|
'total_amount',
|
|
'discount_rate',
|
|
'discount_amount',
|
|
// 기타
|
|
'delivery_date',
|
|
'delivery_method_code',
|
|
'memo',
|
|
'remarks',
|
|
'note',
|
|
// 감사
|
|
'created_by',
|
|
'updated_by',
|
|
'deleted_by',
|
|
];
|
|
|
|
protected $casts = [
|
|
'quantity' => 'decimal:4',
|
|
'supply_amount' => 'decimal:2',
|
|
'tax_amount' => 'decimal:2',
|
|
'total_amount' => 'decimal:2',
|
|
'discount_rate' => 'decimal:2',
|
|
'discount_amount' => 'decimal:2',
|
|
'received_at' => 'datetime',
|
|
'delivery_date' => 'date',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
'deleted_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* 수주 상세 품목
|
|
*/
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(OrderItem::class)->orderBy('sort_order');
|
|
}
|
|
|
|
/**
|
|
* 수주 이력
|
|
*/
|
|
public function histories(): HasMany
|
|
{
|
|
return $this->hasMany(OrderHistory::class);
|
|
}
|
|
|
|
/**
|
|
* 수주 버전
|
|
*/
|
|
public function versions(): HasMany
|
|
{
|
|
return $this->hasMany(OrderVersion::class);
|
|
}
|
|
|
|
/**
|
|
* 원본 견적
|
|
*/
|
|
public function quote(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Quote::class);
|
|
}
|
|
|
|
/**
|
|
* 거래처
|
|
*/
|
|
public function client(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Client::class);
|
|
}
|
|
|
|
/**
|
|
* 품목 (통합 items 테이블)
|
|
*/
|
|
public function item(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Item::class, 'item_id');
|
|
}
|
|
|
|
/**
|
|
* 작업지시 목록
|
|
*/
|
|
public function workOrders(): HasMany
|
|
{
|
|
return $this->hasMany(WorkOrder::class, 'sales_order_id');
|
|
}
|
|
|
|
/**
|
|
* 품목들로부터 금액 합계 재계산
|
|
*/
|
|
public function recalculateTotals(): self
|
|
{
|
|
$this->supply_amount = $this->items->sum('supply_amount');
|
|
$this->tax_amount = $this->items->sum('tax_amount');
|
|
$this->total_amount = $this->items->sum('total_amount');
|
|
$this->quantity = $this->items->sum('quantity');
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* 견적에서 수주 생성
|
|
*/
|
|
public static function createFromQuote(Quote $quote, string $orderNo): self
|
|
{
|
|
return new self([
|
|
'tenant_id' => $quote->tenant_id,
|
|
'quote_id' => $quote->id,
|
|
'order_no' => $orderNo,
|
|
'order_type_code' => self::TYPE_ORDER,
|
|
'status_code' => self::STATUS_DRAFT,
|
|
'client_id' => $quote->client_id,
|
|
'client_name' => $quote->client?->name,
|
|
'client_contact' => $quote->contact_person,
|
|
'site_name' => $quote->site_name,
|
|
'quantity' => $quote->items->sum('calculated_quantity'),
|
|
'supply_amount' => $quote->total_amount,
|
|
'tax_amount' => round($quote->total_amount * 0.1, 2),
|
|
'total_amount' => round($quote->total_amount * 1.1, 2),
|
|
'delivery_date' => $quote->delivery_date,
|
|
'memo' => $quote->remarks,
|
|
'remarks' => $quote->internal_notes,
|
|
]);
|
|
}
|
|
}
|