@extends('layouts.app') @section('title', '문서 인쇄 - ' . $document->title) @section('content')
{{-- 상단 버튼 --}}

{{ $document->title }}

{{ $document->document_no }}

{{-- 성적서 본문 --}}
{{-- HTML 스냅샷 우선 출력 (React에서 저장한 rendered_html) --}} @if($document->rendered_html)
{!! $document->rendered_html !!}
@else {{-- 레거시: 템플릿 기반 동적 렌더링 --}} @php $template = $document->template; $hasComplexCol = $template->columns->contains(fn($c) => $c->column_type === 'complex' && $c->sub_labels); // 정규화 데이터 조회 헬퍼 $getData = function($sectionId, $colId, $rowIdx, $fieldKey) use ($document) { return $document->data ->where('section_id', $sectionId) ->where('column_id', $colId) ->where('row_index', $rowIdx) ->where('field_key', $fieldKey) ->first()?->field_value ?? ''; }; // 레거시 fallback 헬퍼 $getLegacyData = function($fieldKey) use ($document) { return $document->data->where('field_key', $fieldKey)->first()?->field_value ?? ''; }; // 컬럼 → 섹션 아이템 매핑 $normalizeLabel = fn($label) => preg_replace('/[①②③④⑤⑥⑦⑧⑨⑩\s]/u', '', trim($label)); @endphp {{-- 섹션별 검사 테이블 --}} @if($template->sections && $template->sections->count() > 0) @foreach($template->sections as $sectionIndex => $section) {{-- 섹션 제목 --}} @if($template->sections->count() > 1)

{{ $section->title }}

@endif {{-- 검사 기준 이미지 --}} @if($section->image_path) @php $sectionImgUrl = preg_match('/^\d+\//', $section->image_path) ? rtrim(config('app.api_url', 'http://api.sam.kr'), '/') . '/storage/tenants/' . $section->image_path : asset('storage/' . $section->image_path); @endphp
{{ $section->title }}
@endif {{-- 검사 데이터 테이블 --}} @if($section->items->count() > 0 && $template->columns->count() > 0) @php // 컬럼 → 섹션 아이템 매핑 + 기준치 해석 $allItems = $section->items; $columnItemMap = []; foreach ($template->columns as $col) { $colKey = $normalizeLabel($col->label); foreach ($allItems as $sItem) { $itemKey = $normalizeLabel($sItem->item ?? $sItem->category ?? ''); if ($itemKey === $colKey) { $columnItemMap[$col->id] = $sItem; break; } } } $resolveStandard = function($colId, $rowIndex) use ($columnItemMap, &$workOrderItems) { $sItem = $columnItemMap[$colId] ?? null; if (!$sItem) return ''; $woItem = $workOrderItems[$rowIndex] ?? null; if ($woItem) { $fv = $sItem->field_values; $refAttr = is_array($fv) ? ($fv['reference_attribute'] ?? null) : null; if ($refAttr) { $dimKey = $refAttr === 'length' ? 'width' : $refAttr; $dimVal = $woItem->options[$dimKey] ?? null; if ($dimVal) return (string) $dimVal; } } $sc = $sItem->standard_criteria; if ($sc) { if (is_array($sc)) { if (isset($sc['nominal'])) return (string) $sc['nominal']; if (isset($sc['min'], $sc['max'])) return $sc['min'] . ' ~ ' . $sc['max']; if (isset($sc['max'])) return '≤ ' . $sc['max']; if (isset($sc['min'])) return '≥ ' . $sc['min']; } return (string) $sc; } return $sItem->standard ?? ''; }; @endphp {{-- 테이블 헤더 --}} @foreach($template->columns as $col) @if($col->column_type === 'complex' && $col->sub_labels) @else @endif @endforeach {{-- 서브 라벨 행 (complex 컬럼) --}} @if($hasComplexCol) @foreach($template->columns as $col) @if($col->column_type === 'complex' && $col->sub_labels) @foreach($col->sub_labels as $subLabel) @endforeach @endif @endforeach @endif {{-- 테이블 바디 (정규화 형식: section_id + column_id + row_index + field_key) --}} @php $rowCount = $workOrderItems->isNotEmpty() ? $workOrderItems->count() : max(1, ($document->data->where('section_id', $section->id)->max('row_index') ?? 0) + 1); @endphp @for($rowIndex = 0; $rowIndex < $rowCount; $rowIndex++) @foreach($template->columns as $col) @if($col->column_type === 'complex' && $col->sub_labels) @php $inputIdx = 0; $mappedItem = $columnItemMap[$col->id] ?? null; $isOkng = $mappedItem?->measurement_type === 'checkbox'; @endphp @foreach($col->sub_labels as $subIndex => $subLabel) @php $sl = strtolower($subLabel); $isStandardSub = str_contains($sl, '도면') || str_contains($sl, '기준'); $isOkNgSub = str_contains($sl, 'ok') || str_contains($sl, 'ng'); @endphp @if($isStandardSub) @php $standardVal = $getData($section->id, $col->id, $rowIndex, 'standard'); if (!$standardVal) $standardVal = $resolveStandard($col->id, $rowIndex); @endphp @elseif($isOkNgSub) @php $n = $inputIdx + 1; $okVal = $getData($section->id, $col->id, $rowIndex, "n{$n}_ok"); $ngVal = $getData($section->id, $col->id, $rowIndex, "n{$n}_ng"); $savedVal = $okVal === 'OK' ? 'OK' : ($ngVal === 'NG' ? 'NG' : ''); if (!$savedVal) { $valFallback = $getData($section->id, $col->id, $rowIndex, 'value'); if (in_array(strtolower($valFallback), ['ok', 'pass', '적합', '합격'])) $savedVal = 'OK'; elseif (in_array(strtolower($valFallback), ['ng', 'fail', '부적합'])) $savedVal = 'NG'; } $inputIdx++; @endphp @else @php $n = $inputIdx + 1; if ($isOkng) { $okVal = $getData($section->id, $col->id, $rowIndex, "n{$n}_ok"); $ngVal = $getData($section->id, $col->id, $rowIndex, "n{$n}_ng"); $savedVal = $okVal === 'OK' ? 'OK' : ($ngVal === 'NG' ? 'NG' : ''); if (!$savedVal) { $valFallback = $getData($section->id, $col->id, $rowIndex, 'value'); if (in_array(strtolower($valFallback), ['ok', 'pass', '적합', '합격'])) $savedVal = 'OK'; elseif (in_array(strtolower($valFallback), ['ng', 'fail', '부적합'])) $savedVal = 'NG'; } } else { $savedVal = $getData($section->id, $col->id, $rowIndex, "n{$n}"); if (!$savedVal) $savedVal = $getData($section->id, $col->id, $rowIndex, 'value'); } $inputIdx++; @endphp @endif @endforeach @elseif($col->column_type === 'select') @php $savedVal = $getData($section->id, $col->id, $rowIndex, 'value'); if (!$savedVal) { $rowJudgment = $document->data->where('field_key', 'row_judgment')->first()?->field_value ?? ''; if (in_array(strtolower($rowJudgment), ['pass', 'ok', '적합'])) $savedVal = 'OK'; elseif (in_array(strtolower($rowJudgment), ['fail', 'ng', '부적합'])) $savedVal = 'NG'; } @endphp @elseif($col->column_type === 'check') @php $savedVal = $getData($section->id, $col->id, $rowIndex, 'value'); @endphp @elseif($col->column_type === 'measurement') @php $savedVal = $getData($section->id, $col->id, $rowIndex, 'n1'); if (!$savedVal) $savedVal = $getData($section->id, $col->id, $rowIndex, 'value'); @endphp @else @php $label = trim($col->label); $isNoCol = (str_contains(strtolower($label), 'no') && strlen($label) <= 4) || in_array($label, ['일렬번호', '일련번호', '번호', '순번']); $isJudgeCol = str_contains($label, '판정'); @endphp @if($isNoCol) @elseif($isJudgeCol) @php $savedVal = $getData($section->id, $col->id, $rowIndex, 'value'); if (!$savedVal) { $rowJudgment = $document->data->where('field_key', 'row_judgment')->first()?->field_value ?? ''; if (in_array(strtolower($rowJudgment), ['pass', 'ok', '적합'])) $savedVal = 'OK'; elseif (in_array(strtolower($rowJudgment), ['fail', 'ng', '부적합'])) $savedVal = 'NG'; } @endphp @else @php $savedVal = $getData($section->id, $col->id, $rowIndex, 'value'); if (!$savedVal) $savedVal = $getData($section->id, $col->id, $rowIndex, 'n1'); @endphp @endif @endif @endforeach @endfor
{{ $col->label }} {{ $col->label }}
{{ $subLabel }}
{{ $standardVal ?: '-' }} @if($savedVal) {{ strtoupper($savedVal) }} @else - @endif @if($isOkng && $savedVal) {{ strtoupper($savedVal) }} @else {{ $savedVal ?: '-' }} @endif @if($savedVal) {{ in_array(strtolower($savedVal), ['ok', 'pass', '적합', '합격', '적']) ? '적합' : '부적합' }} @else - @endif @if(in_array(strtolower($savedVal), ['ok', 'pass', '적합', '합격'])) OK @elseif($savedVal) NG @else - @endif {{ $savedVal ?: '-' }} {{ $rowIndex + 1 }} @if(in_array(strtolower($savedVal ?? ''), ['ok', 'pass', '적합', '합격'])) OK @elseif($savedVal) NG @else - @endif {{ $savedVal ?: '-' }}
@endif @endforeach @endif {{-- 종합판정 --}} @if($template->footer_judgement_label)
@php $remarkVal = $document->data->where('field_key', 'remark')->first()?->field_value ?? $document->data->where('field_key', 'footer_remark')->first()?->field_value ?? ''; @endphp @if($remarkVal) @endif
{{ $template->footer_judgement_label ?? '종합판정' }} @php $judgementVal = $document->data->where('field_key', 'overall_result')->first()?->field_value ?? $document->data->where('field_key', 'footer_judgement')->first()?->field_value ?? ''; // fallback: overall_result 없으면 row_judgment에서 계산 if (!$judgementVal) { $rowJudgments = $document->data->where('field_key', 'row_judgment')->pluck('field_value'); if ($rowJudgments->isNotEmpty()) { $hasFail = $rowJudgments->contains(fn($v) => in_array(strtolower($v), ['fail', '부', '부적합', '불합격'])); $allPass = $rowJudgments->every(fn($v) => in_array(strtolower($v), ['pass', '적', '적합', '합격'])); $judgementVal = $hasFail ? '불합격' : ($allPass ? '합격' : ''); } } @endphp @if($judgementVal) {{ $judgementVal }} @else 미완료 @endif
{{ $template->footer_remark_label ?? '비고' }} {{ $remarkVal }}
@endif @endif {{-- 스냅샷 vs 레거시 분기 끝 --}}
@endsection