feat: 수주 관리 Phase 3 - 고급 기능 API 구현
- 견적→수주 변환 API (POST /orders/from-quote/{quoteId})
- 생산지시 생성 API (POST /orders/{id}/production-order)
- FormRequest 검증 클래스 추가
- 중복 생성 방지 및 상태 검증 로직
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Orders\Order;
|
||||
use App\Models\Production\WorkOrder;
|
||||
use App\Models\Quote\Quote;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
@@ -319,4 +321,160 @@ private function generateOrderNo(int $tenantId): string
|
||||
|
||||
return sprintf('%s%s%04d', $prefix, $date, $seq);
|
||||
}
|
||||
|
||||
/**
|
||||
* 견적에서 수주 생성
|
||||
*/
|
||||
public function createFromQuote(int $quoteId, array $data = [])
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
// 견적 조회
|
||||
$quote = Quote::where('tenant_id', $tenantId)
|
||||
->with(['items', 'client'])
|
||||
->find($quoteId);
|
||||
|
||||
if (! $quote) {
|
||||
throw new NotFoundHttpException(__('error.quote.not_found'));
|
||||
}
|
||||
|
||||
// 이미 수주가 생성된 견적인지 확인
|
||||
$existingOrder = Order::where('tenant_id', $tenantId)
|
||||
->where('quote_id', $quoteId)
|
||||
->first();
|
||||
|
||||
if ($existingOrder) {
|
||||
throw new BadRequestHttpException(__('error.order.already_created_from_quote'));
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($quote, $data, $tenantId, $userId) {
|
||||
// 수주번호 생성
|
||||
$orderNo = $this->generateOrderNo($tenantId);
|
||||
|
||||
// Order 모델의 createFromQuote 사용
|
||||
$order = Order::createFromQuote($quote, $orderNo);
|
||||
$order->created_by = $userId;
|
||||
$order->updated_by = $userId;
|
||||
|
||||
// 추가 데이터 병합 (납품일, 메모 등)
|
||||
if (! empty($data['delivery_date'])) {
|
||||
$order->delivery_date = $data['delivery_date'];
|
||||
}
|
||||
if (! empty($data['memo'])) {
|
||||
$order->memo = $data['memo'];
|
||||
}
|
||||
|
||||
$order->save();
|
||||
|
||||
// 견적 품목을 수주 품목으로 변환
|
||||
foreach ($quote->items as $index => $quoteItem) {
|
||||
$order->items()->create([
|
||||
'item_id' => $quoteItem->item_id,
|
||||
'item_name' => $quoteItem->item_name,
|
||||
'specification' => $quoteItem->specification,
|
||||
'quantity' => $quoteItem->calculated_quantity,
|
||||
'unit' => $quoteItem->unit,
|
||||
'unit_price' => $quoteItem->unit_price,
|
||||
'supply_amount' => $quoteItem->total_price,
|
||||
'tax_amount' => round($quoteItem->total_price * 0.1, 2),
|
||||
'total_amount' => round($quoteItem->total_price * 1.1, 2),
|
||||
'sort_order' => $index,
|
||||
]);
|
||||
}
|
||||
|
||||
// 합계 재계산
|
||||
$order->refresh();
|
||||
$order->recalculateTotals()->save();
|
||||
|
||||
return $order->load(['client:id,name', 'items', 'quote:id,quote_number']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 생산지시 생성
|
||||
*/
|
||||
public function createProductionOrder(int $orderId, array $data)
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
// 수주 조회
|
||||
$order = Order::where('tenant_id', $tenantId)
|
||||
->with('items')
|
||||
->find($orderId);
|
||||
|
||||
if (! $order) {
|
||||
throw new NotFoundHttpException(__('error.not_found'));
|
||||
}
|
||||
|
||||
// 상태 확인 (CONFIRMED 상태에서만 생산지시 가능)
|
||||
if ($order->status_code !== Order::STATUS_CONFIRMED) {
|
||||
throw new BadRequestHttpException(__('error.order.must_be_confirmed_for_production'));
|
||||
}
|
||||
|
||||
// 이미 생산지시가 존재하는지 확인
|
||||
$existingWorkOrder = WorkOrder::where('tenant_id', $tenantId)
|
||||
->where('sales_order_id', $orderId)
|
||||
->first();
|
||||
|
||||
if ($existingWorkOrder) {
|
||||
throw new BadRequestHttpException(__('error.order.production_order_already_exists'));
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($order, $data, $tenantId, $userId) {
|
||||
// 작업지시번호 생성
|
||||
$workOrderNo = $this->generateWorkOrderNo($tenantId);
|
||||
|
||||
// 작업지시 생성
|
||||
$workOrder = WorkOrder::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'work_order_no' => $workOrderNo,
|
||||
'sales_order_id' => $order->id,
|
||||
'project_name' => $order->site_name ?? $order->client_name,
|
||||
'process_type' => $data['process_type'] ?? WorkOrder::PROCESS_SCREEN,
|
||||
'status' => WorkOrder::STATUS_PENDING,
|
||||
'assignee_id' => $data['assignee_id'] ?? null,
|
||||
'team_id' => $data['team_id'] ?? null,
|
||||
'scheduled_date' => $data['scheduled_date'] ?? $order->delivery_date,
|
||||
'memo' => $data['memo'] ?? null,
|
||||
'is_active' => true,
|
||||
'created_by' => $userId,
|
||||
'updated_by' => $userId,
|
||||
]);
|
||||
|
||||
// 수주 상태를 IN_PROGRESS로 변경
|
||||
$order->status_code = Order::STATUS_IN_PROGRESS;
|
||||
$order->updated_by = $userId;
|
||||
$order->save();
|
||||
|
||||
return [
|
||||
'work_order' => $workOrder->load(['assignee:id,name', 'team:id,name']),
|
||||
'order' => $order->load(['client:id,name', 'items']),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 작업지시번호 자동 생성
|
||||
*/
|
||||
private function generateWorkOrderNo(int $tenantId): string
|
||||
{
|
||||
$prefix = 'WO';
|
||||
$date = now()->format('Ymd');
|
||||
|
||||
$lastNo = WorkOrder::withoutGlobalScopes()
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('work_order_no', 'like', "{$prefix}{$date}%")
|
||||
->orderByDesc('work_order_no')
|
||||
->value('work_order_no');
|
||||
|
||||
if ($lastNo) {
|
||||
$seq = (int) substr($lastNo, -4) + 1;
|
||||
} else {
|
||||
$seq = 1;
|
||||
}
|
||||
|
||||
return sprintf('%s%s%04d', $prefix, $date, $seq);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user