2026-01-27 11:31:02 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\DocumentTemplate;
|
2026-01-28 17:47:14 +09:00
|
|
|
use App\Models\Tenants\Tenant;
|
2026-01-27 11:31:02 +09:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\View\View;
|
|
|
|
|
|
|
|
|
|
class DocumentTemplateController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 문서양식 목록 페이지
|
|
|
|
|
*/
|
|
|
|
|
public function index(Request $request): View
|
|
|
|
|
{
|
|
|
|
|
return view('document-templates.index', [
|
2026-01-28 17:47:14 +09:00
|
|
|
'categories' => $this->getCategories(),
|
2026-01-27 11:31:02 +09:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 문서양식 생성 페이지
|
|
|
|
|
*/
|
|
|
|
|
public function create(): View
|
|
|
|
|
{
|
|
|
|
|
return view('document-templates.edit', [
|
|
|
|
|
'template' => null,
|
|
|
|
|
'templateData' => null,
|
|
|
|
|
'isCreate' => true,
|
2026-01-28 17:47:14 +09:00
|
|
|
'categories' => $this->getCategories(),
|
|
|
|
|
'tenant' => $this->getCurrentTenant(),
|
2026-01-27 11:31:02 +09:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 문서양식 수정 페이지
|
|
|
|
|
*/
|
|
|
|
|
public function edit(int $id): View
|
|
|
|
|
{
|
|
|
|
|
$template = DocumentTemplate::with([
|
|
|
|
|
'approvalLines',
|
|
|
|
|
'basicFields',
|
|
|
|
|
'sections.items',
|
|
|
|
|
'columns',
|
|
|
|
|
])->findOrFail($id);
|
|
|
|
|
|
|
|
|
|
// JavaScript용 데이터 변환
|
|
|
|
|
$templateData = $this->prepareTemplateData($template);
|
|
|
|
|
|
|
|
|
|
return view('document-templates.edit', [
|
|
|
|
|
'template' => $template,
|
|
|
|
|
'templateData' => $templateData,
|
|
|
|
|
'isCreate' => false,
|
2026-01-28 17:47:14 +09:00
|
|
|
'categories' => $this->getCategories(),
|
|
|
|
|
'tenant' => $this->getCurrentTenant(),
|
2026-01-27 11:31:02 +09:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-28 17:47:14 +09:00
|
|
|
* 현재 선택된 테넌트 조회
|
2026-01-27 11:31:02 +09:00
|
|
|
*/
|
2026-01-28 17:47:14 +09:00
|
|
|
private function getCurrentTenant(): ?Tenant
|
2026-01-27 11:31:02 +09:00
|
|
|
{
|
|
|
|
|
$tenantId = session('selected_tenant_id');
|
|
|
|
|
|
2026-01-28 17:47:14 +09:00
|
|
|
return $tenantId ? Tenant::find($tenantId) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 문서분류 목록 조회 (글로벌 + 테넌트, 기존 데이터에서 group by)
|
|
|
|
|
*/
|
|
|
|
|
private function getCategories(): array
|
|
|
|
|
{
|
|
|
|
|
$tenantId = session('selected_tenant_id');
|
|
|
|
|
|
|
|
|
|
return DocumentTemplate::query()
|
2026-01-27 11:31:02 +09:00
|
|
|
->where(function ($query) use ($tenantId) {
|
|
|
|
|
$query->whereNull('tenant_id');
|
|
|
|
|
if ($tenantId) {
|
|
|
|
|
$query->orWhere('tenant_id', $tenantId);
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-01-28 17:47:14 +09:00
|
|
|
->whereNotNull('category')
|
|
|
|
|
->where('category', '!=', '')
|
|
|
|
|
->distinct()
|
|
|
|
|
->orderBy('category')
|
|
|
|
|
->pluck('category')
|
2026-01-27 11:31:02 +09:00
|
|
|
->toArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* JavaScript용 템플릿 데이터 준비
|
|
|
|
|
*/
|
|
|
|
|
private function prepareTemplateData(DocumentTemplate $template): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'name' => $template->name,
|
|
|
|
|
'category' => $template->category,
|
|
|
|
|
'title' => $template->title,
|
2026-02-01 20:37:01 +09:00
|
|
|
'company_name' => $template->company_name,
|
2026-01-27 11:31:02 +09:00
|
|
|
'footer_remark_label' => $template->footer_remark_label,
|
|
|
|
|
'footer_judgement_label' => $template->footer_judgement_label,
|
|
|
|
|
'footer_judgement_options' => $template->footer_judgement_options,
|
|
|
|
|
'is_active' => $template->is_active,
|
|
|
|
|
'approval_lines' => $template->approvalLines->map(function ($l) {
|
|
|
|
|
return [
|
|
|
|
|
'id' => $l->id,
|
|
|
|
|
'name' => $l->name,
|
|
|
|
|
'dept' => $l->dept,
|
|
|
|
|
'role' => $l->role,
|
|
|
|
|
];
|
|
|
|
|
})->toArray(),
|
2026-02-01 20:37:01 +09:00
|
|
|
'basic_fields' => $template->basicFields->map(function ($f) {
|
|
|
|
|
return [
|
|
|
|
|
'id' => $f->id,
|
|
|
|
|
'label' => $f->label,
|
|
|
|
|
'field_type' => $f->field_type,
|
|
|
|
|
'default_value' => $f->default_value,
|
|
|
|
|
];
|
|
|
|
|
})->toArray(),
|
2026-01-27 11:31:02 +09:00
|
|
|
'sections' => $template->sections->map(function ($s) {
|
|
|
|
|
return [
|
|
|
|
|
'id' => $s->id,
|
|
|
|
|
'title' => $s->title,
|
|
|
|
|
'image_path' => $s->image_path,
|
|
|
|
|
'items' => $s->items->map(function ($i) {
|
|
|
|
|
return [
|
|
|
|
|
'id' => $i->id,
|
|
|
|
|
'category' => $i->category,
|
|
|
|
|
'item' => $i->item,
|
|
|
|
|
'standard' => $i->standard,
|
2026-02-02 16:29:45 +09:00
|
|
|
'tolerance' => $i->tolerance,
|
2026-02-02 20:37:06 +09:00
|
|
|
'standard_criteria' => $i->standard_criteria,
|
2026-01-27 11:31:02 +09:00
|
|
|
'method' => $i->method,
|
2026-02-02 16:29:45 +09:00
|
|
|
'measurement_type' => $i->measurement_type,
|
2026-02-02 20:37:06 +09:00
|
|
|
'frequency_n' => $i->frequency_n,
|
|
|
|
|
'frequency_c' => $i->frequency_c,
|
2026-01-27 11:31:02 +09:00
|
|
|
'frequency' => $i->frequency,
|
|
|
|
|
'regulation' => $i->regulation,
|
|
|
|
|
];
|
|
|
|
|
})->toArray(),
|
|
|
|
|
];
|
|
|
|
|
})->toArray(),
|
|
|
|
|
'columns' => $template->columns->map(function ($c) {
|
|
|
|
|
return [
|
|
|
|
|
'id' => $c->id,
|
|
|
|
|
'label' => $c->label,
|
|
|
|
|
'width' => $c->width,
|
|
|
|
|
'column_type' => $c->column_type,
|
|
|
|
|
'group_name' => $c->group_name,
|
|
|
|
|
'sub_labels' => $c->sub_labels,
|
|
|
|
|
];
|
|
|
|
|
})->toArray(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|