feat: 견적확정 밸리데이션, 작업지시 통계 공정별 카운트, 입고/재고 개선

- 견적확정 시 업체명/현장명/담당자/연락처 필수 검증 추가 (QuoteService)
- 작업지시 stats API에 by_process 공정별 카운트 반환 추가
- 작업지시 목록/상세 쿼리에 수주 개소(rootNodes) 연관 로딩
- 작업지시 품목에 sourceOrderItem.node 관계 추가
- 입고관리 완료건 수정 허용 및 재고 차이 조정
- work_order_step_progress 테이블 마이그레이션
- receivings 테이블 options 컬럼 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 03:27:07 +09:00
parent 6b3e5c3e87
commit 487e651845
22 changed files with 1422 additions and 72 deletions

View File

@@ -16,7 +16,7 @@ public function index(array $params): LengthAwarePaginator
$tenantId = $this->tenantId();
$query = Receiving::query()
->with('creator:id,name')
->with(['creator:id,name', 'item:id,item_type,code,name'])
->where('tenant_id', $tenantId);
// 검색어 필터
@@ -57,8 +57,67 @@ public function index(array $params): LengthAwarePaginator
// 페이지네이션
$perPage = $params['per_page'] ?? 20;
$paginator = $query->paginate($perPage);
return $query->paginate($perPage);
// 수입검사 템플릿 연결 여부 계산
$itemIds = $paginator->pluck('item_id')->filter()->unique()->values()->toArray();
$itemsWithInspection = $this->getItemsWithInspectionTemplate($itemIds);
// has_inspection_template 필드 추가
$paginator->getCollection()->transform(function ($receiving) use ($itemsWithInspection) {
$receiving->has_inspection_template = $receiving->item_id
? in_array($receiving->item_id, $itemsWithInspection)
: false;
return $receiving;
});
return $paginator;
}
/**
* 수입검사 템플릿에 연결된 품목 ID 조회
*
* DocumentService::resolve()와 동일한 조건 사용:
* - category: 영문 코드('incoming_inspection'), 한글('수입검사'), 부분 매칭 모두 지원
* - linked_item_ids: int/string 타입 모두 매칭
*/
private function getItemsWithInspectionTemplate(array $itemIds): array
{
if (empty($itemIds)) {
return [];
}
$tenantId = $this->tenantId();
// DocumentService::resolve()와 동일한 category 매칭 조건
$categoryCode = 'incoming_inspection';
$categoryName = '수입검사';
$templates = DB::table('document_templates')
->where('tenant_id', $tenantId)
->where('is_active', true)
->whereNotNull('linked_item_ids')
->where(function ($q) use ($categoryCode, $categoryName) {
$q->where('category', $categoryCode)
->orWhere('category', $categoryName)
->orWhere('category', 'LIKE', "%{$categoryName}%");
})
->get(['linked_item_ids']);
$linkedItemIds = [];
foreach ($templates as $template) {
$ids = json_decode($template->linked_item_ids, true) ?? [];
// int/string 타입 모두 매칭되도록 정수로 통일
foreach ($ids as $id) {
$linkedItemIds[] = (int) $id;
}
}
// 요청된 품목 ID도 정수로 통일하여 교집합
$intItemIds = array_map('intval', $itemIds);
return array_values(array_intersect($intItemIds, array_unique($linkedItemIds)));
}
/**
@@ -103,7 +162,7 @@ public function show(int $id): Receiving
return Receiving::query()
->where('tenant_id', $tenantId)
->with(['creator:id,name'])
->with(['creator:id,name', 'item:id,item_type,code,name'])
->findOrFail($id);
}
@@ -119,12 +178,18 @@ public function store(array $data): Receiving
// 입고번호 자동 생성
$receivingNumber = $this->generateReceivingNumber($tenantId);
// item_id 조회 (전달되지 않은 경우 item_code로 조회)
$itemId = $data['item_id'] ?? null;
if (! $itemId && ! empty($data['item_code'])) {
$itemId = $this->findItemIdByCode($tenantId, $data['item_code']);
}
$receiving = new Receiving;
$receiving->tenant_id = $tenantId;
$receiving->receiving_number = $receivingNumber;
$receiving->order_no = $data['order_no'] ?? null;
$receiving->order_date = $data['order_date'] ?? null;
$receiving->item_id = $data['item_id'] ?? null;
$receiving->item_id = $itemId;
$receiving->item_code = $data['item_code'];
$receiving->item_name = $data['item_name'];
$receiving->specification = $data['specification'] ?? null;
@@ -134,6 +199,10 @@ public function store(array $data): Receiving
$receiving->due_date = $data['due_date'] ?? null;
$receiving->status = $data['status'] ?? 'order_completed';
$receiving->remark = $data['remark'] ?? null;
// options 필드 처리 (제조사, 수입검사 등 확장 필드)
$receiving->options = $this->buildOptions($data);
$receiving->created_by = $userId;
$receiving->updated_by = $userId;
$receiving->save();
@@ -167,6 +236,13 @@ public function update(int $id, array $data): Receiving
}
if (isset($data['item_code'])) {
$receiving->item_code = $data['item_code'];
// item_code 변경 시 item_id도 업데이트
if (! isset($data['item_id'])) {
$receiving->item_id = $this->findItemIdByCode($tenantId, $data['item_code']);
}
}
if (isset($data['item_id'])) {
$receiving->item_id = $data['item_id'];
}
if (isset($data['item_name'])) {
$receiving->item_name = $data['item_name'];
@@ -190,10 +266,13 @@ public function update(int $id, array $data): Receiving
$receiving->remark = $data['remark'];
}
// 입고완료(completed) 상태 변경 시 입고처리 로직 실행
$isCompletingReceiving = isset($data['status'])
&& $data['status'] === 'completed'
&& $receiving->status !== 'completed';
// 상태 변경 감지
$oldStatus = $receiving->status;
$newStatus = $data['status'] ?? $oldStatus;
$wasCompleted = $oldStatus === 'completed';
// 입고완료(completed) 상태로 신규 전환
$isCompletingReceiving = $newStatus === 'completed' && ! $wasCompleted;
if ($isCompletingReceiving) {
// 입고수량 설정 (없으면 발주수량 사용)
@@ -201,16 +280,44 @@ public function update(int $id, array $data): Receiving
$receiving->receiving_date = $data['receiving_date'] ?? now()->toDateString();
$receiving->lot_no = $data['lot_no'] ?? $this->generateLotNo();
$receiving->status = 'completed';
} elseif (isset($data['status'])) {
$receiving->status = $data['status'];
} else {
// 일반 필드 업데이트
if (isset($data['receiving_qty'])) {
$receiving->receiving_qty = $data['receiving_qty'];
}
if (isset($data['receiving_date'])) {
$receiving->receiving_date = $data['receiving_date'];
}
if (isset($data['lot_no'])) {
$receiving->lot_no = $data['lot_no'];
}
if (isset($data['status'])) {
$receiving->status = $data['status'];
}
}
// options 필드 업데이트 (제조사, 수입검사 등 확장 필드)
$receiving->options = $this->mergeOptions($receiving->options, $data);
$receiving->updated_by = $userId;
$receiving->save();
// 입고완료 시 재고 연동
if ($isCompletingReceiving && $receiving->item_id) {
app(StockService::class)->increaseFromReceiving($receiving);
// 재고 연동
if ($receiving->item_id) {
$stockService = app(StockService::class);
if ($isCompletingReceiving) {
// 대기 → 완료: 전량 재고 증가
$stockService->increaseFromReceiving($receiving);
} elseif ($wasCompleted) {
// 기존 완료 상태에서 수정: 차이만큼 조정
// 완료→완료(수량변경): newQty = 변경된 수량
// 완료→대기: newQty = 0 (전량 차감)
$newQty = $newStatus === 'completed'
? (float) $receiving->receiving_qty
: 0;
$stockService->adjustFromReceiving($receiving, $newQty);
}
}
return $receiving->fresh();
@@ -318,4 +425,110 @@ private function generateLotNo(): string
return "{$year}{$month}{$day}-{$seq}";
}
/**
* 품목코드로 품목 ID 조회
*/
private function findItemIdByCode(int $tenantId, string $itemCode): ?int
{
$item = DB::table('items')
->where('tenant_id', $tenantId)
->where('code', $itemCode)
->whereNull('deleted_at')
->first(['id']);
return $item?->id;
}
/**
* options 필드 빌드 (등록 시)
*/
private function buildOptions(array $data): ?array
{
$options = [];
// 제조사
if (isset($data['manufacturer'])) {
$options[Receiving::OPTION_MANUFACTURER] = $data['manufacturer'];
}
// 거래처 자재번호
if (isset($data['material_no'])) {
$options[Receiving::OPTION_MATERIAL_NO] = $data['material_no'];
}
// 수입검사 상태 (적/부적/-)
if (isset($data['inspection_status'])) {
$options[Receiving::OPTION_INSPECTION_STATUS] = $data['inspection_status'];
}
// 검사일
if (isset($data['inspection_date'])) {
$options[Receiving::OPTION_INSPECTION_DATE] = $data['inspection_date'];
}
// 검사결과 (합격/불합격)
if (isset($data['inspection_result'])) {
$options[Receiving::OPTION_INSPECTION_RESULT] = $data['inspection_result'];
}
// 추가 확장 필드가 있으면 여기에 계속 추가 가능
return ! empty($options) ? $options : null;
}
/**
* options 필드 병합 (수정 시)
*/
private function mergeOptions(?array $existing, array $data): ?array
{
$options = $existing ?? [];
// 제조사
if (array_key_exists('manufacturer', $data)) {
if ($data['manufacturer'] === null || $data['manufacturer'] === '') {
unset($options[Receiving::OPTION_MANUFACTURER]);
} else {
$options[Receiving::OPTION_MANUFACTURER] = $data['manufacturer'];
}
}
// 거래처 자재번호
if (array_key_exists('material_no', $data)) {
if ($data['material_no'] === null || $data['material_no'] === '') {
unset($options[Receiving::OPTION_MATERIAL_NO]);
} else {
$options[Receiving::OPTION_MATERIAL_NO] = $data['material_no'];
}
}
// 수입검사 상태
if (array_key_exists('inspection_status', $data)) {
if ($data['inspection_status'] === null || $data['inspection_status'] === '') {
unset($options[Receiving::OPTION_INSPECTION_STATUS]);
} else {
$options[Receiving::OPTION_INSPECTION_STATUS] = $data['inspection_status'];
}
}
// 검사일
if (array_key_exists('inspection_date', $data)) {
if ($data['inspection_date'] === null || $data['inspection_date'] === '') {
unset($options[Receiving::OPTION_INSPECTION_DATE]);
} else {
$options[Receiving::OPTION_INSPECTION_DATE] = $data['inspection_date'];
}
}
// 검사결과
if (array_key_exists('inspection_result', $data)) {
if ($data['inspection_result'] === null || $data['inspection_result'] === '') {
unset($options[Receiving::OPTION_INSPECTION_RESULT]);
} else {
$options[Receiving::OPTION_INSPECTION_RESULT] = $data['inspection_result'];
}
}
return ! empty($options) ? $options : null;
}
}