Files
sam-api/app/Services/ProcessStepService.php
권혁성 1d7ef66d19 feat: 작업일지/중간검사 설정을 ProcessStep → Process 레벨로 이동
- Process 모델에 document_template_id, needs_work_log, work_log_template_id 추가
- ProcessStep에서 해당 필드 제거
- WorkOrderService의 검사 관련 3개 메서드(getInspectionTemplate, resolveInspectionDocument, createInspectionDocument) 공정 레벨 참조로 변경
- ProcessService eager loading에 documentTemplate, workLogTemplateRelation 추가
- FormRequest 검증 규칙 이동 (ProcessStep → Process)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 09:51:35 +09:00

144 lines
3.6 KiB
PHP

<?php
namespace App\Services;
use App\Models\Process;
use App\Models\ProcessStep;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ProcessStepService extends Service
{
/**
* 공정 단계 목록 조회
*/
public function index(int $processId)
{
$process = $this->findProcess($processId);
return $process->steps()
->select('*')
->orderBy('sort_order')
->get();
}
/**
* 공정 단계 상세 조회
*/
public function show(int $processId, int $stepId)
{
$this->findProcess($processId);
$step = ProcessStep::where('process_id', $processId)
->select('*')
->find($stepId);
if (! $step) {
throw new NotFoundHttpException(__('error.not_found'));
}
return $step;
}
/**
* 공정 단계 생성
*/
public function store(int $processId, array $data)
{
$process = $this->findProcess($processId);
$data['process_id'] = $process->id;
$data['step_code'] = $this->generateStepCode($process->id);
$data['sort_order'] = ($process->steps()->max('sort_order') ?? 0) + 1;
$data['is_active'] = $data['is_active'] ?? true;
return ProcessStep::create($data);
}
/**
* 공정 단계 수정
*/
public function update(int $processId, int $stepId, array $data)
{
$this->findProcess($processId);
$step = ProcessStep::where('process_id', $processId)->find($stepId);
if (! $step) {
throw new NotFoundHttpException(__('error.not_found'));
}
$step->update($data);
return $step->fresh();
}
/**
* 공정 단계 삭제
*/
public function destroy(int $processId, int $stepId)
{
$this->findProcess($processId);
$step = ProcessStep::where('process_id', $processId)->find($stepId);
if (! $step) {
throw new NotFoundHttpException(__('error.not_found'));
}
$step->delete();
return true;
}
/**
* 공정 단계 순서 변경
*/
public function reorder(int $processId, array $items)
{
$this->findProcess($processId);
return DB::transaction(function () use ($processId, $items) {
foreach ($items as $item) {
ProcessStep::where('process_id', $processId)
->where('id', $item['id'])
->update(['sort_order' => $item['sort_order']]);
}
return ProcessStep::where('process_id', $processId)
->orderBy('sort_order')
->get();
});
}
/**
* 부모 공정 조회 (tenant scope)
*/
private function findProcess(int $processId): Process
{
$tenantId = $this->tenantId();
$process = Process::where('tenant_id', $tenantId)->find($processId);
if (! $process) {
throw new NotFoundHttpException(__('error.not_found'));
}
return $process;
}
/**
* 단계코드 자동 생성 (STP-001, STP-002, ...)
*/
private function generateStepCode(int $processId): string
{
$lastStep = ProcessStep::where('process_id', $processId)
->orderByRaw('CAST(SUBSTRING(step_code, 5) AS UNSIGNED) DESC')
->first();
if ($lastStep && preg_match('/^STP-(\d+)$/', $lastStep->step_code, $matches)) {
$nextNum = (int) $matches[1] + 1;
} else {
$nextNum = 1;
}
return sprintf('STP-%03d', $nextNum);
}
}