183 lines
7.6 KiB
PHP
183 lines
7.6 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../app/lib/auth.php';
|
|
require_once __DIR__ . '/../app/lib/db.php';
|
|
require_once __DIR__ . '/../app/lib/helpers.php';
|
|
require_once __DIR__ . '/../app/lib/recurring_service.php';
|
|
|
|
check_auth();
|
|
|
|
$pdo = db();
|
|
$uid = user_id();
|
|
$error = '';
|
|
$msg = '';
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM accounts WHERE user_id = ? AND is_active = 1 ORDER BY id");
|
|
$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 {
|
|
$mode = $_POST['mode'] ?? 'create';
|
|
|
|
if ($mode === 'apply_month') {
|
|
$ym = $_POST['ym'] ?? date('Y-m');
|
|
$count = apply_recurring_transactions_for_month($uid, $ym, true);
|
|
$msg = $ym . ' 적용 완료: ' . $count . '건 입력';
|
|
} else {
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO recurring_transactions
|
|
(user_id, account_id, category_id, transaction_type, amount, day_of_month, merchant_name, description, related_account_id, is_active)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1)
|
|
");
|
|
$stmt->execute([
|
|
$uid,
|
|
(int)$_POST['account_id'],
|
|
(int)$_POST['category_id'],
|
|
$_POST['transaction_type'],
|
|
(float)$_POST['amount'],
|
|
(int)$_POST['day_of_month'],
|
|
trim($_POST['merchant_name'] ?? '') ?: null,
|
|
trim($_POST['description'] ?? '') ?: null,
|
|
!empty($_POST['related_account_id']) ? (int)$_POST['related_account_id'] : null,
|
|
]);
|
|
|
|
redirect('/recurring.php');
|
|
}
|
|
} catch (Throwable $e) {
|
|
$error = $e->getMessage();
|
|
}
|
|
}
|
|
|
|
$stmt = $pdo->prepare("SELECT rt.*, a.account_name, c.name AS category_name, ra.account_name AS related_name
|
|
FROM recurring_transactions rt
|
|
JOIN accounts a ON rt.account_id = a.id
|
|
JOIN categories c ON rt.category_id = c.id
|
|
LEFT JOIN accounts ra ON rt.related_account_id = ra.id
|
|
WHERE rt.user_id = ?
|
|
ORDER BY rt.day_of_month, rt.id");
|
|
$stmt->execute([$uid]);
|
|
$list = $stmt->fetchAll();
|
|
|
|
require __DIR__ . '/../app/views/header.php';
|
|
?>
|
|
|
|
<div class="page-head">
|
|
<h2>고정지출 / 고정거래</h2>
|
|
</div>
|
|
|
|
<?php if ($error): ?><div class="alert alert-danger"><?= h($error) ?></div><?php endif; ?>
|
|
<?php if ($msg): ?><div class="alert alert-success"><?= h($msg) ?></div><?php endif; ?>
|
|
|
|
<div class="d-flex gap-2 mb-4">
|
|
<form method="post" class="d-inline">
|
|
<input type="hidden" name="mode" value="apply_month">
|
|
<input type="hidden" name="ym" value="<?= date('Y-m') ?>">
|
|
<button class="btn btn-outline-primary" onclick="return confirm('이번 달 고정거래를 자동입력하시겠습니까?');">
|
|
이번 달 자동입력 실행
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="row g-4">
|
|
<div class="col-lg-4">
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-body">
|
|
<form method="post" class="row g-3">
|
|
<input type="hidden" name="mode" value="create">
|
|
<div class="col-12">
|
|
<label class="form-label">유형</label>
|
|
<select name="transaction_type" class="form-select">
|
|
<option value="expense">지출</option>
|
|
<option value="income">수입</option>
|
|
<option value="card_payment">카드대금납부</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">주 계좌</label>
|
|
<select name="account_id" class="form-select">
|
|
<?php foreach ($accounts as $a): ?>
|
|
<option value="<?= $a['id'] ?>"><?= h($a['account_name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">관련 계좌</label>
|
|
<select name="related_account_id" class="form-select">
|
|
<option value="">선택안함</option>
|
|
<?php foreach ($accounts as $a): ?>
|
|
<option value="<?= $a['id'] ?>"><?= h($a['account_name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">카테고리</label>
|
|
<select name="category_id" class="form-select">
|
|
<?php foreach ($categories as $c): ?>
|
|
<option value="<?= $c['id'] ?>">[<?= h($c['category_type']) ?>] <?= h($c['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-6">
|
|
<label class="form-label">금액</label>
|
|
<input type="number" name="amount" class="form-control" required>
|
|
</div>
|
|
<div class="col-6">
|
|
<label class="form-label">매월 일자</label>
|
|
<input type="number" name="day_of_month" min="1" max="31" class="form-control" required>
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">사용처</label>
|
|
<input type="text" name="merchant_name" class="form-control">
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">메모</label>
|
|
<input type="text" name="description" class="form-control">
|
|
</div>
|
|
<div class="col-12">
|
|
<button class="btn btn-primary">등록</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-lg-8">
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-body mobile-scroll">
|
|
<table class="table mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>일자</th>
|
|
<th>유형</th>
|
|
<th>계좌</th>
|
|
<th>관련</th>
|
|
<th>카테고리</th>
|
|
<th>금액</th>
|
|
<th>마지막 적용월</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($list as $row): ?>
|
|
<tr>
|
|
<td><?= $row['day_of_month'] ?>일</td>
|
|
<td><?= h($row['transaction_type']) ?></td>
|
|
<td><?= h($row['account_name']) ?></td>
|
|
<td><?= h($row['related_name'] ?? '-') ?></td>
|
|
<td><?= h($row['category_name']) ?></td>
|
|
<td><?= won($row['amount']) ?></td>
|
|
<td><?= h($row['last_applied_ym'] ?? '-') ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require __DIR__ . '/../app/views/footer.php'; ?>
|