Files
sam-manage/app/Services/EmploymentCertService.php
김보곤 7f73c054d5 refactor: [approval] 재직증명서 DOCX 생성을 제거하고 content JSON 저장 + PDF 다운로드 방식으로 변경
- 상신 시 DOCX 생성 API 호출 제거, content JSON만 저장
- show 페이지에 PDF 다운로드 버튼 추가
- TCPDF 기반 PDF 생성 (기존 Pretendard 한글 폰트 활용)
- EmploymentCertService에서 generateDocx/createFileRecord 제거
2026-03-05 19:29:38 +09:00

224 lines
7.1 KiB
PHP

<?php
namespace App\Services;
use App\Models\HR\Employee;
use App\Models\Tenants\Tenant;
use Illuminate\Support\Facades\Log;
class EmploymentCertService
{
private ?string $koreanFontName = null;
public function __construct()
{
if (! defined('K_PATH_FONTS')) {
$tcpdfFontsDir = dirname(__DIR__, 2).'/storage/fonts/tcpdf/';
if (is_dir($tcpdfFontsDir)) {
define('K_PATH_FONTS', $tcpdfFontsDir);
}
}
}
/**
* 사원의 재직증명서 정보 조회
*/
public function getCertInfo(int $userId, int $tenantId): array
{
$employee = Employee::withoutGlobalScopes()
->where('tenant_id', $tenantId)
->where('user_id', $userId)
->with(['user', 'department'])
->firstOrFail();
$tenant = Tenant::findOrFail($tenantId);
$residentNumber = $employee->resident_number;
$maskedResident = $residentNumber
? substr($residentNumber, 0, 8).'******'
: '';
return [
'name' => $employee->user->name ?? $employee->display_name ?? '',
'resident_number' => $maskedResident,
'resident_number_full' => $residentNumber ?? '',
'address' => $employee->address ?? '',
'department' => $employee->department?->name ?? '',
'position' => $employee->position_label ?? '',
'hire_date' => $employee->hire_date ?? '',
'company_name' => $tenant->company_name ?? '',
'business_num' => $tenant->business_num ?? '',
'ceo_name' => $tenant->ceo_name ?? '',
'phone' => $tenant->phone ?? '',
'company_address' => $tenant->address ?? '',
];
}
/**
* content JSON 기반 PDF Response 생성
*/
public function generatePdfResponse(array $content): \Illuminate\Http\Response
{
$pdf = new \TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetCreator('SAM');
$pdf->SetAuthor($content['company_name'] ?? 'SAM');
$pdf->SetTitle('재직증명서');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(20, 20, 20);
$pdf->SetAutoPageBreak(true, 20);
$font = $this->getKoreanFont();
$pdf->AddPage();
// 제목
$pdf->SetFont($font, 'B', 22);
$pdf->Cell(0, 20, '재 직 증 명 서', 0, 1, 'C');
$pdf->Ln(8);
// === 1. 인적사항 ===
$pdf->SetFont($font, 'B', 12);
$pdf->Cell(0, 8, '1. 인적사항', 0, 1, 'L');
$pdf->Ln(2);
$this->addTableRow($pdf, $font, [
['성 명', $content['name'] ?? '-', 40],
['주민등록번호', $content['resident_number'] ?? '-', 40],
]);
$this->addTableRow($pdf, $font, [
['주 소', $content['address'] ?? '-', 0],
]);
$pdf->Ln(6);
// === 2. 재직사항 ===
$pdf->SetFont($font, 'B', 12);
$pdf->Cell(0, 8, '2. 재직사항', 0, 1, 'L');
$pdf->Ln(2);
$this->addTableRow($pdf, $font, [
['회 사 명', $content['company_name'] ?? '-', 0],
]);
$this->addTableRow($pdf, $font, [
['사업자번호', $content['business_num'] ?? '-', 0],
]);
$this->addTableRow($pdf, $font, [
['근무부서', $content['department'] ?? '-', 40],
['직 급', $content['position'] ?? '-', 40],
]);
$hireDate = $content['hire_date'] ?? '';
$hireDateDisplay = $hireDate ? $hireDate.' ~ 현재' : '-';
$this->addTableRow($pdf, $font, [
['재직기간', $hireDateDisplay, 0],
]);
$pdf->Ln(6);
// === 3. 발급정보 ===
$pdf->SetFont($font, 'B', 12);
$pdf->Cell(0, 8, '3. 발급정보', 0, 1, 'L');
$pdf->Ln(2);
$this->addTableRow($pdf, $font, [
['사용용도', $content['purpose'] ?? '-', 0],
]);
$pdf->Ln(12);
// 증명 문구
$pdf->SetFont($font, '', 12);
$pdf->Cell(0, 10, '위 사항을 증명합니다.', 0, 1, 'C');
$pdf->Ln(6);
// 발급일
$issueDate = $content['issue_date'] ?? date('Y-m-d');
$issueDateFormatted = $this->formatDate($issueDate);
$pdf->SetFont($font, 'B', 12);
$pdf->Cell(0, 10, $issueDateFormatted, 0, 1, 'C');
$pdf->Ln(12);
// 회사명 + 대표이사
$pdf->SetFont($font, 'B', 14);
$pdf->Cell(0, 10, $content['company_name'] ?? '', 0, 1, 'C');
$pdf->SetFont($font, '', 12);
$pdf->Cell(0, 10, '대표이사 (인)', 0, 1, 'C');
$pdfContent = $pdf->Output('', 'S');
$fileName = '재직증명서_'.($content['name'] ?? '').'.pdf';
return response($pdfContent, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$fileName.'"',
]);
}
/**
* 테이블 행 추가 (TCPDF)
*/
private function addTableRow(\TCPDF $pdf, string $font, array $cells): void
{
$pageWidth = $pdf->getPageWidth() - 40; // margins
$rowHeight = 8;
$thWidth = 30;
if (count($cells) === 1) {
// 단일 셀: th + td (전체 너비)
$pdf->SetFont($font, 'B', 10);
$pdf->SetFillColor(248, 249, 250);
$pdf->Cell($thWidth, $rowHeight, $cells[0][0], 1, 0, 'L', true);
$pdf->SetFont($font, '', 10);
$pdf->Cell($pageWidth - $thWidth, $rowHeight, $cells[0][1], 1, 1, 'L');
} else {
// 복수 셀: th+td + th+td
$tdWidth = ($pageWidth - $thWidth * 2) / 2;
foreach ($cells as $cell) {
$pdf->SetFont($font, 'B', 10);
$pdf->SetFillColor(248, 249, 250);
$pdf->Cell($thWidth, $rowHeight, $cell[0], 1, 0, 'L', true);
$pdf->SetFont($font, '', 10);
$pdf->Cell($tdWidth, $rowHeight, $cell[1], 1, 0, 'L');
}
$pdf->Ln();
}
}
/**
* 날짜 포맷
*/
private function formatDate(string $date): string
{
try {
return date('Y년 m월 d일', strtotime($date));
} catch (\Throwable) {
return $date;
}
}
/**
* 한글 폰트 로드
*/
private function getKoreanFont(): string
{
if ($this->koreanFontName) {
return $this->koreanFontName;
}
if (defined('K_PATH_FONTS') && file_exists(K_PATH_FONTS.'pretendard.php')) {
$this->koreanFontName = 'pretendard';
return $this->koreanFontName;
}
$fontPath = storage_path('fonts/Pretendard-Regular.ttf');
if (file_exists($fontPath)) {
try {
$this->koreanFontName = \TCPDF_FONTS::addTTFfont($fontPath, 'TrueTypeUnicode', '', 96);
} catch (\Throwable $e) {
Log::warning('TCPDF 한글 폰트 등록 실패', ['error' => $e->getMessage()]);
}
}
return $this->koreanFontName ?: 'helvetica';
}
}