feat: 가지급금 관리 API 구현

- loans 테이블 마이그레이션 추가
- Loan 모델 (인정이자 계산, 세금 계산 로직)
- LoanService (CRUD, 정산, 인정이자 계산/리포트)
- LoanController, FormRequest 5개
- 9개 API 라우트 등록
- i18n 키 추가 (validation)
This commit is contained in:
2025-12-18 14:27:10 +09:00
parent 8b30a555d2
commit af833194ea
11 changed files with 1065 additions and 0 deletions

View File

@@ -0,0 +1,115 @@
<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use App\Http\Requests\Loan\LoanCalculateInterestRequest;
use App\Http\Requests\Loan\LoanIndexRequest;
use App\Http\Requests\Loan\LoanSettleRequest;
use App\Http\Requests\Loan\LoanStoreRequest;
use App\Http\Requests\Loan\LoanUpdateRequest;
use App\Http\Response\ApiResponse;
use App\Services\LoanService;
use Illuminate\Http\JsonResponse;
class LoanController extends Controller
{
public function __construct(
private readonly LoanService $loanService
) {}
/**
* 가지급금 목록
*/
public function index(LoanIndexRequest $request): JsonResponse
{
$result = $this->loanService->index($request->validated());
return ApiResponse::handle('message.fetched', $result);
}
/**
* 가지급금 요약
*/
public function summary(LoanIndexRequest $request): JsonResponse
{
$userId = $request->validated()['user_id'] ?? null;
$result = $this->loanService->summary($userId);
return ApiResponse::handle('message.fetched', $result);
}
/**
* 가지급금 등록
*/
public function store(LoanStoreRequest $request): JsonResponse
{
$result = $this->loanService->store($request->validated());
return ApiResponse::handle('message.created', $result, 201);
}
/**
* 가지급금 상세
*/
public function show(int $id): JsonResponse
{
$result = $this->loanService->show($id);
return ApiResponse::handle('message.fetched', $result);
}
/**
* 가지급금 수정
*/
public function update(LoanUpdateRequest $request, int $id): JsonResponse
{
$result = $this->loanService->update($id, $request->validated());
return ApiResponse::handle('message.updated', $result);
}
/**
* 가지급금 삭제
*/
public function destroy(int $id): JsonResponse
{
$this->loanService->destroy($id);
return ApiResponse::handle('message.deleted');
}
/**
* 가지급금 정산
*/
public function settle(LoanSettleRequest $request, int $id): JsonResponse
{
$result = $this->loanService->settle($id, $request->validated());
return ApiResponse::handle('message.loan.settled', $result);
}
/**
* 인정이자 계산
*/
public function calculateInterest(LoanCalculateInterestRequest $request): JsonResponse
{
$validated = $request->validated();
$result = $this->loanService->calculateInterest(
$validated['year'],
$validated['user_id'] ?? null
);
return ApiResponse::handle('message.fetched', $result);
}
/**
* 인정이자 리포트
*/
public function interestReport(int $year): JsonResponse
{
$result = $this->loanService->interestReport($year);
return ApiResponse::handle('message.fetched', $result);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Http\Requests\Loan;
use Illuminate\Foundation\Http\FormRequest;
class LoanCalculateInterestRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'year' => ['required', 'integer', 'min:2000', 'max:2100'],
'user_id' => ['nullable', 'integer', 'exists:users,id'],
];
}
/**
* Get the validation attribute names.
*
* @return array<string, string>
*/
public function attributes(): array
{
return [
'year' => __('validation.attributes.year'),
'user_id' => __('validation.attributes.user_id'),
];
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Http\Requests\Loan;
use App\Models\Tenants\Loan;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class LoanIndexRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'user_id' => ['nullable', 'integer', 'exists:users,id'],
'status' => ['nullable', 'string', Rule::in(Loan::STATUSES)],
'start_date' => ['nullable', 'date', 'date_format:Y-m-d'],
'end_date' => ['nullable', 'date', 'date_format:Y-m-d', 'after_or_equal:start_date'],
'search' => ['nullable', 'string', 'max:100'],
'sort_by' => ['nullable', 'string', Rule::in(['loan_date', 'amount', 'status', 'created_at'])],
'sort_dir' => ['nullable', 'string', Rule::in(['asc', 'desc'])],
'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
];
}
/**
* Get the validation attribute names.
*
* @return array<string, string>
*/
public function attributes(): array
{
return [
'user_id' => __('validation.attributes.user_id'),
'status' => __('validation.attributes.status'),
'start_date' => __('validation.attributes.start_date'),
'end_date' => __('validation.attributes.end_date'),
'search' => __('validation.attributes.search'),
'sort_by' => __('validation.attributes.sort_by'),
'sort_dir' => __('validation.attributes.sort_dir'),
'per_page' => __('validation.attributes.per_page'),
];
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Http\Requests\Loan;
use Illuminate\Foundation\Http\FormRequest;
class LoanSettleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'settlement_date' => ['required', 'date', 'date_format:Y-m-d'],
'settlement_amount' => ['required', 'numeric', 'min:0.01', 'max:999999999999.99'],
];
}
/**
* Get the validation attribute names.
*
* @return array<string, string>
*/
public function attributes(): array
{
return [
'settlement_date' => __('validation.attributes.settlement_date'),
'settlement_amount' => __('validation.attributes.settlement_amount'),
];
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Http\Requests\Loan;
use Illuminate\Foundation\Http\FormRequest;
class LoanStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'user_id' => ['required', 'integer', 'exists:users,id'],
'loan_date' => ['required', 'date', 'date_format:Y-m-d'],
'amount' => ['required', 'numeric', 'min:0', 'max:999999999999.99'],
'purpose' => ['nullable', 'string', 'max:1000'],
'withdrawal_id' => ['nullable', 'integer', 'exists:withdrawals,id'],
];
}
/**
* Get the validation attribute names.
*
* @return array<string, string>
*/
public function attributes(): array
{
return [
'user_id' => __('validation.attributes.user_id'),
'loan_date' => __('validation.attributes.loan_date'),
'amount' => __('validation.attributes.amount'),
'purpose' => __('validation.attributes.purpose'),
'withdrawal_id' => __('validation.attributes.withdrawal_id'),
];
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Http\Requests\Loan;
use Illuminate\Foundation\Http\FormRequest;
class LoanUpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'user_id' => ['sometimes', 'integer', 'exists:users,id'],
'loan_date' => ['sometimes', 'date', 'date_format:Y-m-d'],
'amount' => ['sometimes', 'numeric', 'min:0', 'max:999999999999.99'],
'purpose' => ['nullable', 'string', 'max:1000'],
'withdrawal_id' => ['nullable', 'integer', 'exists:withdrawals,id'],
];
}
/**
* Get the validation attribute names.
*
* @return array<string, string>
*/
public function attributes(): array
{
return [
'user_id' => __('validation.attributes.user_id'),
'loan_date' => __('validation.attributes.loan_date'),
'amount' => __('validation.attributes.amount'),
'purpose' => __('validation.attributes.purpose'),
'withdrawal_id' => __('validation.attributes.withdrawal_id'),
];
}
}