금융 거래 기록 흐름 공통화

This commit is contained in:
seo
2026-07-20 18:27:41 +09:00
parent 8a64d993c3
commit c86e5a1481
7 changed files with 383 additions and 413 deletions
+24 -123
View File
@@ -1,56 +1,14 @@
<?php
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/account_service.php';
require_once __DIR__ . '/installment_service.php';
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/card_billing_service.php';
function build_transaction_fingerprint(
int $userId,
int $accountId,
?int $relatedAccountId,
int $categoryId,
string $transactionType,
float $amount,
string $transactionDate,
?string $merchantName,
?string $description
): string {
$raw = implode('|', [
$userId,
$accountId,
$relatedAccountId ?? 0,
$categoryId,
$transactionType,
number_format($amount, 2, '.', ''),
$transactionDate,
trim((string)$merchantName),
trim((string)$description),
]);
return hash('sha256', $raw);
}
function get_account_for_transaction(int $userId, int $accountId): ?array
{
$pdo = db();
$stmt = $pdo->prepare("
SELECT *
FROM accounts
WHERE id = ?
AND user_id = ?
LIMIT 1
");
$stmt->execute([$accountId, $userId]);
$row = $stmt->fetch();
return $row ?: null;
}
function create_transaction(array $data, bool $skipIfDuplicate = false): bool
{
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/account_service.php';
require_once __DIR__ . '/installment_service.php';
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/card_billing_service.php';
require_once __DIR__ . '/transaction_record_service.php';
function create_transaction(array $data, bool $skipIfDuplicate = false): bool
{
$pdo = db();
$pdo->beginTransaction();
@@ -83,20 +41,7 @@ function create_transaction(array $data, bool $skipIfDuplicate = false): bool
}
}
$account = get_account_for_transaction(
(int)$data['user_id'],
(int)$data['account_id']
);
$billingYearMonth = null;
if ($account) {
$billingYearMonth = get_card_billing_year_month(
$account,
(string)$data['transaction_date']
);
}
$isInstallment = !empty($data['is_installment']) ? 1 : 0;
$isInstallment = !empty($data['is_installment']) ? 1 : 0;
$installmentMonths = !empty($data['installment_months']) ? (int)$data['installment_months'] : null;
$installmentInterestRate = !empty($data['installment_interest_rate']) ? (float)$data['installment_interest_rate'] : 0.0;
@@ -146,51 +91,14 @@ function create_transaction(array $data, bool $skipIfDuplicate = false): bool
$installmentTotalBilled = null;
}
$stmt = $pdo->prepare("
INSERT INTO transactions
(
user_id,
account_id,
category_id,
transaction_type,
amount,
is_installment,
installment_months,
installment_interest_rate,
installment_interest_total,
installment_total_billed,
installment_prepay_amount,
transaction_date,
billing_year_month,
merchant_name,
description,
related_account_id,
fingerprint
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$data['user_id'],
$data['account_id'],
$data['category_id'],
$data['transaction_type'],
$data['amount'],
$isInstallment,
$installmentMonths,
$installmentInterestRate,
$installmentInterestTotal,
$installmentTotalBilled,
$data['transaction_date'],
$billingYearMonth,
$data['merchant_name'],
$data['description'],
$data['related_account_id'],
$fingerprint,
]);
$transactionId = (int)$pdo->lastInsertId();
$pdo->commit();
$transactionId = insert_transaction_record(array_merge($data, [
'is_installment' => $isInstallment,
'installment_months' => $installmentMonths,
'installment_interest_rate' => $installmentInterestRate,
'installment_interest_total' => $installmentInterestTotal,
'installment_total_billed' => $installmentTotalBilled,
]), false);
$pdo->commit();
if (
$data['transaction_type'] === 'expense' &&
@@ -281,18 +189,11 @@ function update_transaction(int $transactionId, int $userId, array $data): void
$data['description'] ?? null
);
$account = get_account_for_transaction(
$userId,
(int)$data['account_id']
);
$billingYearMonth = null;
if ($account) {
$billingYearMonth = get_card_billing_year_month(
$account,
(string)$data['transaction_date']
);
}
$billingYearMonth = get_transaction_billing_year_month(
$userId,
(int)$data['account_id'],
(string)$data['transaction_date']
);
$isInstallment = !empty($data['is_installment']) ? 1 : 0;
$installmentMonths = !empty($data['installment_months']) ? (int)$data['installment_months'] : null;
@@ -438,4 +339,4 @@ function delete_transaction(int $transactionId, int $userId): void
}
throw $e;
}
}
}