124 lines
3.9 KiB
PHP
124 lines
3.9 KiB
PHP
|
|
<?php
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
require_once 'db/config.php';
|
|
include 'includes/header.php';
|
|
include 'includes/sidebar.php';
|
|
|
|
$error = '';
|
|
$success = '';
|
|
|
|
// Fetch categories for the dropdown
|
|
try {
|
|
$pdo = db();
|
|
$categories = $pdo->query('SELECT * FROM financial_categories ORDER BY name')->fetchAll();
|
|
} catch (PDOException $e) {
|
|
$categories = [];
|
|
$error = 'Erro ao carregar categorias.';
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
$description = $_POST['description'] ?? '';
|
|
$type = $_POST['type'] ?? '';
|
|
$amount = $_POST['amount'] ?? 0;
|
|
$category_id = $_POST['category_id'] ?? null;
|
|
$date = $_POST['date'] ?? '';
|
|
|
|
if (empty($description) || empty($type) || empty($amount) || empty($category_id) || empty($date)) {
|
|
$error = 'Por favor, preencha todos os campos.';
|
|
} elseif ($amount <= 0) {
|
|
$error = 'O valor deve ser maior que zero.';
|
|
} else {
|
|
try {
|
|
$stmt = $pdo->prepare('INSERT INTO financial_transactions (description, type, amount, category_id, user_id, date) VALUES (?, ?, ?, ?, ?, ?)');
|
|
$stmt->execute([$description, $type, $amount, $category_id, $_SESSION['user_id'], $date]);
|
|
$success = 'Transação adicionada com sucesso!';
|
|
} catch (PDOException $e) {
|
|
$error = 'Erro ao adicionar transação: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<header class="top-bar">
|
|
<h1>Adicionar Transação</h1>
|
|
</header>
|
|
|
|
<main class="content">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
Nova Transação Financeira
|
|
</div>
|
|
|
|
<?php if ($error): ?><p class="error"><?php echo $error; ?></p><?php endif; ?>
|
|
<?php if ($success): ?><p class="success"><?php echo $success; ?></p><?php endif; ?>
|
|
|
|
<form action="add_transaction.php" method="POST">
|
|
<div class="form-group">
|
|
<label for="description">Descrição</label>
|
|
<input type="text" id="description" name="description" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="type">Tipo</label>
|
|
<select id="type" name="type" required>
|
|
<option value="despesa">Despesa</option>
|
|
<option value="receita">Receita</option>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="amount">Valor (R$)</label>
|
|
<input type="number" step="0.01" id="amount" name="amount" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="category_id">Categoria</label>
|
|
<select id="category_id" name="category_id" required>
|
|
<option value="">Selecione uma categoria</option>
|
|
<?php foreach ($categories as $category): ?>
|
|
<option value="<?php echo htmlspecialchars($category['id']); ?>">
|
|
<?php echo htmlspecialchars($category['name']); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="date">Data</label>
|
|
<input type="date" id="date" name="date" required>
|
|
</div>
|
|
<button type="submit" class="btn">Adicionar</button>
|
|
</form>
|
|
<br>
|
|
<a href="financial.php">Voltar para o Financeiro</a>
|
|
</div>
|
|
</main>
|
|
|
|
<style>
|
|
.form-group {
|
|
margin-bottom: 15px;
|
|
}
|
|
.form-group label {
|
|
display: block;
|
|
margin-bottom: 5px;
|
|
}
|
|
.form-group input, .form-group select {
|
|
width: 100%;
|
|
padding: 8px;
|
|
box-sizing: border-box;
|
|
}
|
|
.btn {
|
|
background-color: #27ae60;
|
|
color: white;
|
|
padding: 10px 15px;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
}
|
|
.error { color: red; }
|
|
.success { color: green; }
|
|
</style>
|
|
|
|
<?php include 'includes/footer.php'; ?>
|