Files
financial/public/loan_edit.php
T
2026-06-07 00:33:58 +09:00

223 lines
9.6 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();
$id = (int)($_GET['id'] ?? $_POST['id'] ?? 0);
$error = '';
$stmt = $pdo->prepare("
SELECT *
FROM loans
WHERE id = ? AND user_id = ?
LIMIT 1
");
$stmt->execute([$id, $uid]);
$loan = $stmt->fetch();
if (!$loan) {
exit('대출 정보를 찾을 수 없습니다.');
}
$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('상환방식이 올바르지 않습니다.');
}
update_loan_and_rebuild_full_history($uid, $id, [
'account_id' => $accountId,
'loan_name' => $loanName,
'lender_name' => $lenderName,
'principal_amount' => $principalAmount,
'annual_interest_rate' => $annualInterestRate,
'start_date' => $startDate,
'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'),
]);
header('Location: /loans.php');
exit;
} catch (Throwable $e) {
$error = $e->getMessage();
}
}
require __DIR__ . '/../app/views/header.php';
?>
<div class="page-head">
<h2>대출 수정</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">
<input type="hidden" name="id" value="<?= $loan['id'] ?>">
<div class="col-12 col-md-6">
<label class="form-label">대출명</label>
<input type="text" name="loan_name" class="form-control" value="<?= h($loan['loan_name']) ?>" required>
</div>
<div class="col-12 col-md-6">
<label class="form-label">대출기관</label>
<input type="text" name="lender_name" class="form-control" value="<?= h($loan['lender_name'] ?? '') ?>">
</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" value="<?= h((string)$loan['principal_amount']) ?>" 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" value="<?= h((string)$loan['annual_interest_rate']) ?>" required>
</div>
<div class="col-12 col-md-4">
<label class="form-label">시작일</label>
<input type="date" name="start_date" class="form-control" value="<?= h($loan['start_date']) ?>" 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="<?= h((string)$loan['grace_period_months']) ?>" 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="<?= h((string)$loan['repayment_months']) ?>" 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="<?= h((string)$loan['payment_day']) ?>" 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" <?= $loan['repayment_method'] === 'interest_only_then_equal_payment' ? 'selected' : '' ?>>거치 후 원리금균등</option>
<option value="interest_only_then_equal_principal" <?= $loan['repayment_method'] === 'interest_only_then_equal_principal' ? 'selected' : '' ?>>거치 후 원금균등</option>
<option value="interest_only_then_bullet" <?= $loan['repayment_method'] === 'interest_only_then_bullet' ? 'selected' : '' ?>>거치 후 만기일시상환</option>
<option value="equal_payment" <?= $loan['repayment_method'] === 'equal_payment' ? 'selected' : '' ?>>원리금균등</option>
<option value="equal_principal" <?= $loan['repayment_method'] === 'equal_principal' ? 'selected' : '' ?>>원금균등</option>
<option value="bullet" <?= $loan['repayment_method'] === 'bullet' ? 'selected' : '' ?>>만기일시상환</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'] ?>" <?= (int)$loan['account_id'] === (int)$acc['id'] ? 'selected' : '' ?>>
<?= h($acc['account_name']) ?> (<?= h($acc['account_type']) ?>)
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-12 col-md-6">
<label class="form-label">메모</label>
<input type="text" name="description" class="form-control" value="<?= h($loan['description'] ?? '') ?>">
</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>
과거 회차는 자동 반영, 미래 회차는 예정 상태로 다시 생성합니다.
</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'; ?>