153 lines
6.5 KiB
PHP
153 lines
6.5 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 = '';
|
|
$msg = '';
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM loans WHERE user_id = ? AND is_active = 1 ORDER BY id DESC");
|
|
$stmt->execute([$uid]);
|
|
$loans = $stmt->fetchAll();
|
|
|
|
$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();
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM categories WHERE user_id = ? AND category_type = 'expense' AND is_active = 1 ORDER BY sort_order, id");
|
|
$stmt->execute([$uid]);
|
|
$categories = $stmt->fetchAll();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
try {
|
|
$loanId = (int)($_POST['loan_id'] ?? 0);
|
|
$accountId = (int)($_POST['account_id'] ?? 0);
|
|
$paymentDate = $_POST['payment_date'] ?? date('Y-m-d');
|
|
$totalAmount = (float)($_POST['total_amount'] ?? 0);
|
|
$principalAmount = (float)($_POST['principal_amount'] ?? 0);
|
|
$interestAmount = (float)($_POST['interest_amount'] ?? 0);
|
|
$feeAmount = (float)($_POST['fee_amount'] ?? 0);
|
|
$categoryId = (int)($_POST['category_id'] ?? 0);
|
|
$description = trim($_POST['description'] ?? '') ?: null;
|
|
$skipDuplicate = isset($_POST['skip_duplicate']);
|
|
|
|
if ($loanId <= 0 || $accountId <= 0 || $categoryId <= 0) {
|
|
throw new RuntimeException('대출, 출금계좌, 카테고리를 선택하세요.');
|
|
}
|
|
if ($totalAmount <= 0) {
|
|
throw new RuntimeException('총 납부금액은 0보다 커야 합니다.');
|
|
}
|
|
if (($principalAmount + $interestAmount + $feeAmount) != $totalAmount) {
|
|
throw new RuntimeException('원금 + 이자 + 수수료 합계가 총 납부금액과 같아야 합니다.');
|
|
}
|
|
|
|
$inserted = create_loan_payment([
|
|
'user_id' => $uid,
|
|
'loan_id' => $loanId,
|
|
'account_id' => $accountId,
|
|
'payment_date' => $paymentDate,
|
|
'total_amount' => $totalAmount,
|
|
'principal_amount' => $principalAmount,
|
|
'interest_amount' => $interestAmount,
|
|
'fee_amount' => $feeAmount,
|
|
'category_id' => $categoryId,
|
|
'description' => $description,
|
|
], $skipDuplicate);
|
|
|
|
$msg = $inserted ? '상환 내역이 저장되었습니다.' : '중복으로 판단되어 건너뛰었습니다.';
|
|
} catch (Throwable $e) {
|
|
$error = $e->getMessage();
|
|
}
|
|
}
|
|
|
|
require __DIR__ . '/../app/views/header.php';
|
|
?>
|
|
|
|
<h2 class="mb-4">대출 상환 등록</h2>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?= h($error) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($msg): ?>
|
|
<div class="alert alert-success"><?= h($msg) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card finance-card">
|
|
<div class="card-body">
|
|
<form method="post" class="row g-3">
|
|
<div class="col-md-4">
|
|
<label class="form-label">대출</label>
|
|
<select name="loan_id" class="form-select" required>
|
|
<option value="">선택하세요</option>
|
|
<?php foreach ($loans as $loan): ?>
|
|
<option value="<?= $loan['id'] ?>"><?= h($loan['loan_name']) ?> / <?= h($loan['institution_name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="col-md-4">
|
|
<label class="form-label">출금 계좌</label>
|
|
<select name="account_id" class="form-select" required>
|
|
<option value="">선택하세요</option>
|
|
<?php foreach ($accounts as $account): ?>
|
|
<option value="<?= $account['id'] ?>"><?= h($account['account_name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="col-md-4">
|
|
<label class="form-label">거래 카테고리</label>
|
|
<select name="category_id" class="form-select" required>
|
|
<?php foreach ($categories as $category): ?>
|
|
<option value="<?= $category['id'] ?>"><?= h($category['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="col-md-3">
|
|
<label class="form-label">상환일</label>
|
|
<input type="date" name="payment_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<label class="form-label">총 납부금액</label>
|
|
<input type="number" name="total_amount" class="form-control" min="1" step="1" required>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label">원금</label>
|
|
<input type="number" name="principal_amount" class="form-control" min="0" step="1" required>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label">이자</label>
|
|
<input type="number" name="interest_amount" class="form-control" min="0" step="1" value="0" required>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label">수수료</label>
|
|
<input type="number" name="fee_amount" class="form-control" min="0" step="1" value="0" required>
|
|
</div>
|
|
|
|
<div class="col-md-8">
|
|
<label class="form-label">메모</label>
|
|
<input type="text" name="description" class="form-control" placeholder="예: 5월 정기상환">
|
|
</div>
|
|
<div class="col-md-4 d-flex align-items-end">
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="checkbox" name="skip_duplicate" id="skip_duplicate" checked>
|
|
<label class="form-check-label" for="skip_duplicate">중복이면 건너뛰기</label>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-12">
|
|
<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'; ?>
|