prepare("SELECT * FROM accounts WHERE id = ? AND user_id = ?"); $stmt->execute([$id, $uid]); $account = $stmt->fetch(); if (!$account) { exit('계좌를 찾을 수 없습니다.'); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { $accountType = $_POST['account_type'] ?? ''; $institutionName = trim($_POST['institution_name'] ?? ''); $accountName = trim($_POST['account_name'] ?? ''); $openingBalance = (float)str_replace(',', '', (string)($_POST['opening_balance'] ?? 0)); $isActive = isset($_POST['is_active']) ? 1 : 0; $cardKind = $_POST['card_kind'] ?? null; $billingDay = !empty($_POST['billing_day']) ? (int)$_POST['billing_day'] : null; $paymentDay = !empty($_POST['payment_day']) ? (int)$_POST['payment_day'] : null; $useCreditGracePeriod = !empty($_POST['use_credit_grace_period']) ? 1 : 0; $billingCycleMemo = trim($_POST['billing_cycle_memo'] ?? ''); $statementStartMonthOffset = isset($_POST['statement_start_month_offset']) && $_POST['statement_start_month_offset'] !== '' ? (int)$_POST['statement_start_month_offset'] : null; $statementStartDay = !empty($_POST['statement_start_day']) ? (int)$_POST['statement_start_day'] : null; $statementEndMonthOffset = isset($_POST['statement_end_month_offset']) && $_POST['statement_end_month_offset'] !== '' ? (int)$_POST['statement_end_month_offset'] : null; $statementEndDay = !empty($_POST['statement_end_day']) ? (int)$_POST['statement_end_day'] : null; if (!in_array($accountType, ['bank', 'card', 'cash', 'other'], true)) { throw new RuntimeException('계정 유형이 올바르지 않습니다.'); } if ($institutionName === '' || $accountName === '') { throw new RuntimeException('기관명과 계좌명을 입력하세요.'); } if ($accountType !== 'card') { $cardKind = null; $billingDay = null; $paymentDay = null; $useCreditGracePeriod = 0; $billingCycleMemo = null; $statementStartMonthOffset = null; $statementStartDay = null; $statementEndMonthOffset = null; $statementEndDay = null; } else { if (!in_array($cardKind, ['credit', 'check'], true)) { throw new RuntimeException('카드 종류를 선택하세요.'); } if ($cardKind === 'check') { $billingDay = null; $paymentDay = null; $useCreditGracePeriod = 0; $billingCycleMemo = '즉시출금'; $statementStartMonthOffset = null; $statementStartDay = null; $statementEndMonthOffset = null; $statementEndDay = null; } if ($cardKind === 'credit') { if ($paymentDay === null || $paymentDay < 1 || $paymentDay > 31) { throw new RuntimeException('납부일은 1~31 사이여야 합니다.'); } if ($useCreditGracePeriod) { if ($statementStartMonthOffset === null || $statementEndMonthOffset === null) { throw new RuntimeException('사용기간의 시작/종료 월 기준을 선택하세요.'); } if ($statementStartDay === null || $statementStartDay < 1 || $statementStartDay > 31) { throw new RuntimeException('사용기간 시작일은 1~31 사이여야 합니다.'); } if ($statementEndDay === null || $statementEndDay < 1 || $statementEndDay > 31) { throw new RuntimeException('사용기간 종료일은 1~31 사이여야 합니다.'); } } else { $statementStartMonthOffset = null; $statementStartDay = null; $statementEndMonthOffset = null; $statementEndDay = null; } if ($billingCycleMemo === '') { $billingCycleMemo = null; } if ($billingDay !== null && ($billingDay < 1 || $billingDay > 31)) { throw new RuntimeException('구형 결제기준일은 1~31 사이여야 합니다.'); } } } $stmt = $pdo->prepare(" UPDATE accounts SET account_type = ?, institution_name = ?, account_name = ?, opening_balance = ?, is_active = ?, card_kind = ?, billing_day = ?, payment_day = ?, use_credit_grace_period = ?, statement_start_month_offset = ?, statement_start_day = ?, statement_end_month_offset = ?, statement_end_day = ?, billing_cycle_memo = ? WHERE id = ? AND user_id = ? "); $stmt->execute([ $accountType, $institutionName, $accountName, $openingBalance, $isActive, $cardKind, $billingDay, $paymentDay, $useCreditGracePeriod, $statementStartMonthOffset, $statementStartDay, $statementEndMonthOffset, $statementEndDay, $billingCycleMemo, $id, $uid ]); recalculate_account_balance($id); redirect('/accounts.php'); } catch (Throwable $e) { $error = $e->getMessage(); } } require __DIR__ . '/../app/views/header.php'; ?>