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