prepare("SELECT * FROM transactions WHERE id = ? AND user_id = ?"); $stmt->execute([$id, $uid]); $item = $stmt->fetch(); if (!$item) { exit('거래를 찾을 수 없습니다.'); } $stmt = $pdo->prepare(" SELECT * FROM accounts WHERE user_id = ? AND is_active = 1 ORDER BY FIELD(account_type, 'bank', 'card', 'cash', 'other'), id ASC "); $stmt->execute([$uid]); $accounts = $stmt->fetchAll(); $stmt = $pdo->prepare(" SELECT * FROM categories WHERE user_id = ? AND is_active = 1 ORDER BY category_type, sort_order, id "); $stmt->execute([$uid]); $categories = $stmt->fetchAll(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { $transactionType = $_POST['transaction_type'] ?? ''; $accountId = (int)($_POST['account_id'] ?? 0); $categoryId = (int)($_POST['category_id'] ?? 0); $amount = (float)str_replace(',', '', (string)($_POST['amount'] ?? 0)); $transactionDate = $_POST['transaction_date'] ?? date('Y-m-d'); $merchantName = trim($_POST['merchant_name'] ?? ''); $description = trim($_POST['description'] ?? ''); $relatedAccountId = !empty($_POST['related_account_id']) ? (int)$_POST['related_account_id'] : null; $isInstallment = (int)($_POST['is_installment'] ?? 0); $installmentMonths = !empty($_POST['installment_months']) ? (int)$_POST['installment_months'] : null; $installmentInterestRate = !empty($_POST['installment_interest_rate']) ? (float)$_POST['installment_interest_rate'] : 0.0; $installmentInterestTotal = isset($_POST['installment_interest_total']) && $_POST['installment_interest_total'] !== '' ? (float)str_replace(',', '', (string)$_POST['installment_interest_total']) : null; $installmentTotalBilled = isset($_POST['installment_total_billed']) && $_POST['installment_total_billed'] !== '' ? (float)str_replace(',', '', (string)$_POST['installment_total_billed']) : null; if ($amount <= 0) throw new RuntimeException('금액은 0보다 커야 합니다.'); if (!in_array($transactionType, ['income', 'expense', 'transfer', 'card_payment'], true)) throw new RuntimeException('거래 유형이 올바르지 않습니다.'); if ($accountId <= 0) throw new RuntimeException('주 계좌/카드를 선택하세요.'); if ($categoryId <= 0) throw new RuntimeException('카테고리를 선택하세요.'); if (in_array($transactionType, ['transfer', 'card_payment'], true) && !$relatedAccountId) throw new RuntimeException('관련 계좌를 선택해야 합니다.'); if ($relatedAccountId && $relatedAccountId === $accountId) throw new RuntimeException('주 계좌와 관련 계좌는 같을 수 없습니다.'); if ($transactionType !== 'expense') { $isInstallment = 0; $installmentMonths = null; $installmentInterestRate = 0.0; $installmentInterestTotal = null; $installmentTotalBilled = null; } update_transaction($id, $uid, [ 'account_id' => $accountId, 'category_id' => $categoryId, 'transaction_type' => $transactionType, 'amount' => $amount, 'transaction_date' => $transactionDate, 'merchant_name' => $merchantName !== '' ? $merchantName : null, 'description' => $description !== '' ? $description : null, 'related_account_id' => $relatedAccountId, 'is_installment' => $isInstallment, 'installment_months' => $installmentMonths, 'installment_interest_rate' => $installmentInterestRate, 'installment_interest_total' => $installmentInterestTotal, 'installment_total_billed' => $installmentTotalBilled, ]); redirect('/transactions.php'); } catch (Throwable $e) { $error = $e->getMessage(); } } $accountsJson = []; foreach ($accounts as $acc) { $accountsJson[] = [ 'id' => (int)$acc['id'], 'name' => $acc['account_name'], 'type' => $acc['account_type'], 'card_kind' => $acc['card_kind'] ?? null, 'billing_day' => $acc['billing_day'] ?? null, 'payment_day' => $acc['payment_day'] ?? null, 'use_credit_grace_period' => !empty($acc['use_credit_grace_period']) ? 1 : 0, ]; } require __DIR__ . '/../app/views/header.php'; ?>

거래 수정

목록
현재 청구월:
현재 개월 / 0) ? '연 ' . numf($item['installment_interest_rate'],2) . '%' : '무이자' ?>
이자 포함 실제 카드 청구 총액
상호명 입력 시 카테고리를 자동 추천합니다.
목록