- BusinessIncomePayment 모델 (소득세3%/지방소득세0.3% 자동계산) - BusinessIncomePaymentService (일괄저장/통계/CSV내보내기) - 웹/API 컨트롤러 (ALLOWED_PAYROLL_USERS 접근 제한) - 스프레드시트 UI (인라인 편집, 실시간 세금 계산) - HTMX 연월 변경 갱신, CSV 내보내기
146 lines
4.9 KiB
PHP
146 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\HR;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\HR\BusinessIncomePayment;
|
|
use App\Services\HR\BusinessIncomePaymentService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class BusinessIncomePaymentController extends Controller
|
|
{
|
|
private const ALLOWED_PAYROLL_USERS = ['이경호', '전진선', '김보곤'];
|
|
|
|
public function __construct(
|
|
private BusinessIncomePaymentService $service
|
|
) {}
|
|
|
|
private function checkPayrollAccess(): ?JsonResponse
|
|
{
|
|
if (! in_array(auth()->user()->name, self::ALLOWED_PAYROLL_USERS)) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '급여관리는 관계자만 볼 수 있습니다.',
|
|
], 403);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 사업소득 지급 목록 (HTMX → 스프레드시트 파셜)
|
|
*/
|
|
public function index(Request $request): JsonResponse|Response
|
|
{
|
|
if ($denied = $this->checkPayrollAccess()) {
|
|
return $denied;
|
|
}
|
|
|
|
$year = $request->integer('year') ?: now()->year;
|
|
$month = $request->integer('month') ?: now()->month;
|
|
|
|
$earners = $this->service->getActiveEarners();
|
|
$payments = $this->service->getPayments($year, $month);
|
|
$paymentsByUser = $payments->keyBy('user_id');
|
|
$stats = $this->service->getMonthlyStats($year, $month);
|
|
|
|
if ($request->header('HX-Request')) {
|
|
return response(
|
|
view('hr.business-income-payments.partials.stats', compact('stats')).
|
|
'<!-- SPLIT -->'.
|
|
view('hr.business-income-payments.partials.spreadsheet', compact('earners', 'paymentsByUser', 'year', 'month'))
|
|
);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => $payments,
|
|
'stats' => $stats,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 일괄 저장
|
|
*/
|
|
public function bulkSave(Request $request): JsonResponse
|
|
{
|
|
if ($denied = $this->checkPayrollAccess()) {
|
|
return $denied;
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'year' => 'required|integer|min:2020|max:2100',
|
|
'month' => 'required|integer|min:1|max:12',
|
|
'items' => 'required|array',
|
|
'items.*.user_id' => 'required|integer',
|
|
'items.*.gross_amount' => 'required|numeric|min:0',
|
|
'items.*.service_content' => 'nullable|string|max:200',
|
|
'items.*.payment_date' => 'nullable|date',
|
|
'items.*.note' => 'nullable|string|max:500',
|
|
]);
|
|
|
|
$result = $this->service->bulkSave(
|
|
$validated['year'],
|
|
$validated['month'],
|
|
$validated['items']
|
|
);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => "저장 {$result['saved']}건, 삭제 {$result['deleted']}건, 건너뜀 {$result['skipped']}건",
|
|
'data' => $result,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* CSV 내보내기
|
|
*/
|
|
public function export(Request $request): StreamedResponse|JsonResponse
|
|
{
|
|
if ($denied = $this->checkPayrollAccess()) {
|
|
return $denied;
|
|
}
|
|
|
|
$year = $request->integer('year') ?: now()->year;
|
|
$month = $request->integer('month') ?: now()->month;
|
|
|
|
$payments = $this->service->getExportData($year, $month);
|
|
$filename = "사업소득자임금대장_{$year}년{$month}월_".now()->format('Ymd').'.csv';
|
|
|
|
return response()->streamDownload(function () use ($payments) {
|
|
$file = fopen('php://output', 'w');
|
|
fwrite($file, "\xEF\xBB\xBF"); // UTF-8 BOM
|
|
|
|
fputcsv($file, [
|
|
'번호', '상호/성명', '사업자등록번호', '용역내용',
|
|
'지급총액', '소득세(3%)', '지방소득세(0.3%)', '공제합계',
|
|
'실지급액', '지급일자', '비고', '상태',
|
|
]);
|
|
|
|
foreach ($payments as $idx => $payment) {
|
|
$earnerName = $payment->user?->name ?? '-';
|
|
|
|
fputcsv($file, [
|
|
$idx + 1,
|
|
$earnerName,
|
|
'', // 사업자등록번호는 별도 조회 필요
|
|
$payment->service_content ?? '',
|
|
$payment->gross_amount,
|
|
$payment->income_tax,
|
|
$payment->local_income_tax,
|
|
$payment->total_deductions,
|
|
$payment->net_amount,
|
|
$payment->payment_date?->format('Y-m-d') ?? '',
|
|
$payment->note ?? '',
|
|
BusinessIncomePayment::STATUS_MAP[$payment->status] ?? $payment->status,
|
|
]);
|
|
}
|
|
|
|
fclose($file);
|
|
}, $filename, ['Content-Type' => 'text/csv; charset=UTF-8']);
|
|
}
|
|
}
|