Files
2026-06-07 00:33:58 +09:00

247 lines
9.7 KiB
PHP

<?php
require_once __DIR__ . '/../app/lib/auth.php';
require_once __DIR__ . '/../app/lib/db.php';
require_once __DIR__ . '/../app/lib/helpers.php';
require_once __DIR__ . '/../app/lib/loan_service.php';
check_auth();
$pdo = db();
$uid = user_id();
$error = '';
$stmt = $pdo->prepare("
SELECT * FROM accounts
WHERE user_id = ?
AND is_active = 1
AND account_type IN ('bank','cash','other')
ORDER BY id ASC
");
$stmt->execute([$uid]);
$accounts = $stmt->fetchAll();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$loanName = trim($_POST['loan_name'] ?? '');
$lenderName = trim($_POST['lender_name'] ?? '');
$principalAmount = (float)($_POST['principal_amount'] ?? 0);
$annualInterestRate = (float)($_POST['annual_interest_rate'] ?? 0);
$startDate = $_POST['start_date'] ?? date('Y-m-d');
$graceMonths = (int)($_POST['grace_period_months'] ?? 0);
$repaymentMonths = (int)($_POST['repayment_months'] ?? 0);
$repaymentMethod = $_POST['repayment_method'] ?? '';
$paymentDay = (int)($_POST['payment_day'] ?? 1);
$accountId = !empty($_POST['account_id']) ? (int)$_POST['account_id'] : null;
$description = trim($_POST['description'] ?? '');
$createFullHistory = !empty($_POST['create_full_history']) ? 1 : 0;
if ($loanName === '') {
throw new RuntimeException('대출명을 입력하세요.');
}
if ($principalAmount <= 0) {
throw new RuntimeException('대출원금은 0보다 커야 합니다.');
}
if ($annualInterestRate < 0) {
throw new RuntimeException('연이자율이 올바르지 않습니다.');
}
if ($repaymentMonths <= 0) {
throw new RuntimeException('상환개월 수는 1 이상이어야 합니다.');
}
if ($paymentDay < 1 || $paymentDay > 31) {
throw new RuntimeException('납부일은 1~31 사이여야 합니다.');
}
$allowed = [
'interest_only_then_equal_payment',
'interest_only_then_equal_principal',
'interest_only_then_bullet',
'equal_payment',
'equal_principal',
'bullet',
];
if (!in_array($repaymentMethod, $allowed, true)) {
throw new RuntimeException('상환방식이 올바르지 않습니다.');
}
if ($createFullHistory && !$accountId) {
throw new RuntimeException('완전 반영 방식을 사용할 때는 입출금 계좌를 선택하세요.');
}
create_loan_with_full_backfill([
'user_id' => $uid,
'account_id' => $accountId,
'loan_name' => $loanName,
'lender_name' => $lenderName,
'principal_amount' => $principalAmount,
'annual_interest_rate' => $annualInterestRate,
'start_date' => $startDate,
'maturity_date' => null,
'grace_period_months' => $graceMonths,
'repayment_months' => $repaymentMonths,
'repayment_method' => $repaymentMethod,
'payment_day' => $paymentDay,
'description' => $description,
'create_full_history' => $createFullHistory,
'today_date' => date('Y-m-d'),
]);
redirect('/loans.php');
} catch (Throwable $e) {
$error = $e->getMessage();
}
}
require __DIR__ . '/../app/views/header.php';
?>
<style>
.loan-create-highlight {
border: 1px solid rgba(37, 99, 235, 0.18);
background: linear-gradient(180deg, rgba(37,99,235,0.06), rgba(37,99,235,0.03));
border-radius: 16px;
padding: 18px 18px 16px 18px;
}
.loan-create-highlight .title {
font-weight: 700;
margin-bottom: 8px;
}
.loan-create-highlight .desc {
color: #5b6475;
font-size: 14px;
line-height: 1.6;
}
.loan-create-check {
margin-top: 14px;
padding: 14px 16px;
background: #fff;
border-radius: 14px;
border: 1px solid rgba(37, 99, 235, 0.14);
}
.loan-create-check .form-check-label {
font-weight: 700;
}
.loan-create-check small {
display: block;
color: #6b7280;
margin-top: 6px;
line-height: 1.5;
}
</style>
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="mb-0">대출 등록</h2>
<a href="/loans.php" class="btn btn-outline-secondary">목록</a>
</div>
<?php if ($error): ?>
<div class="alert alert-danger"><?= h($error) ?></div>
<?php endif; ?>
<div class="card finance-card">
<div class="card-body">
<form method="post" class="row g-3">
<div class="col-12 col-md-6">
<label class="form-label">대출명</label>
<input type="text" name="loan_name" class="form-control" required>
</div>
<div class="col-12 col-md-6">
<label class="form-label">대출기관</label>
<input type="text" name="lender_name" class="form-control">
</div>
<div class="col-12 col-md-4">
<label class="form-label">대출원금</label>
<input type="number" name="principal_amount" class="form-control" min="1" step="1" required>
</div>
<div class="col-12 col-md-4">
<label class="form-label">연이자율(%)</label>
<input type="number" name="annual_interest_rate" class="form-control" min="0" step="0.01" required>
</div>
<div class="col-12 col-md-4">
<label class="form-label">시작일</label>
<input type="date" name="start_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
</div>
<div class="col-12 col-md-4">
<label class="form-label">거치기간(개월)</label>
<input type="number" name="grace_period_months" class="form-control" min="0" value="0" required>
</div>
<div class="col-12 col-md-4">
<label class="form-label">상환기간(개월)</label>
<input type="number" name="repayment_months" class="form-control" min="1" value="12" required>
</div>
<div class="col-12 col-md-4">
<label class="form-label">납부일</label>
<input type="number" name="payment_day" class="form-control" min="1" max="31" value="1" required>
</div>
<div class="col-12">
<label class="form-label">상환방식</label>
<select name="repayment_method" class="form-select" required>
<option value="interest_only_then_equal_payment">거치 후 원리금균등</option>
<option value="interest_only_then_equal_principal">거치 후 원금균등</option>
<option value="interest_only_then_bullet">거치 후 만기일시상환</option>
<option value="equal_payment">원리금균등</option>
<option value="equal_principal">원금균등</option>
<option value="bullet">만기일시상환</option>
</select>
</div>
<div class="col-12 col-md-6">
<label class="form-label">입출금 계좌</label>
<select name="account_id" class="form-select">
<option value="">선택안함</option>
<?php foreach ($accounts as $acc): ?>
<option value="<?= $acc['id'] ?>"><?= h($acc['account_name']) ?> (<?= h($acc['account_type']) ?>)</option>
<?php endforeach; ?>
</select>
<div class="form-text">과거 거래까지 자동 생성하려면 계좌를 선택하세요.</div>
</div>
<div class="col-12 col-md-6">
<label class="form-label">메모</label>
<input type="text" name="description" class="form-control">
</div>
<div class="col-12">
<div class="loan-create-highlight">
<div class="title">완전 반영 방식</div>
<div class="desc">
대출 시작일이 과거라면, 대출 실행 거래와 오늘까지의 과거 상환 거래를 자동으로 모두 생성합니다.
미래 회차는 예정 스케줄로 남겨둡니다.
</div>
<div class="loan-create-check">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="create_full_history" id="create_full_history" value="1" checked>
<label class="form-check-label" for="create_full_history">
대출 실행 + 과거 납부분 거래내역까지 전부 자동 생성
</label>
<small>
예: 2024년에 받은 대출을 지금 등록하면<br>
대출 실행일의 유입 거래 1건 + 오늘까지 도래한 상환 거래들이 자동 생성됩니다.
</small>
</div>
</div>
</div>
</div>
<div class="col-12 d-flex gap-2">
<button class="btn btn-primary">저장</button>
<a href="/loans.php" class="btn btn-outline-secondary">목록</a>
</div>
</form>
</div>
</div>
<?php require __DIR__ . '/../app/views/footer.php'; ?>