금융 거래 기록 흐름 공통화
This commit is contained in:
@@ -41,55 +41,8 @@ $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,
|
||||
]);
|
||||
$payload = build_transaction_payload_from_input($_POST, $uid);
|
||||
update_transaction($id, $uid, $payload);
|
||||
|
||||
redirect('/transactions.php');
|
||||
} catch (Throwable $e) {
|
||||
|
||||
Reference in New Issue
Block a user