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'; ?>

대출 상환 등록

목록