Erro de Permissão de Arquivo
Não foi possível criar/abrir o arquivo do banco de dados.
Garanta que a pasta do seu servidor web tem permissão de escrita (CHMOD 755 ou 777).
");
}
// Criação da Estrutura do Banco de Dados
$db->exec("CREATE TABLE IF NOT EXISTS categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
type TEXT
)");
$db->exec("CREATE TABLE IF NOT EXISTS subcategories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
category_id INTEGER,
name TEXT,
FOREIGN KEY(category_id) REFERENCES categories(id)
)");
$db->exec("CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type TEXT,
description TEXT,
amount REAL,
category_id INTEGER,
subcategory_id INTEGER,
date TEXT,
status TEXT,
notes TEXT,
FOREIGN KEY(category_id) REFERENCES categories(id),
FOREIGN KEY(subcategory_id) REFERENCES subcategories(id)
)");
// Inserir categorias padrão se o banco estiver vazio
$cat_check = $db->querySingle("SELECT COUNT(*) FROM categories");
if ($cat_check == 0) {
$db->exec("INSERT INTO categories (name, type) VALUES ('Salário', 'receita'), ('Alimentação', 'despesa'), ('Moradia', 'despesa')");
}
// ==========================================
// PROCESSAMENTO DE FORMULÁRIOS (AÇÕES)
// ==========================================
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'add_transaction') {
$stmt = $db->prepare("INSERT INTO transactions (type, description, amount, category_id, subcategory_id, date, status, notes) VALUES (:type, :desc, :amount, :cat, :subcat, :date, :status, :notes)");
$stmt->bindValue(':type', $_POST['type']);
$stmt->bindValue(':desc', $_POST['description']);
$stmt->bindValue(':amount', floatval(str_replace(['.', ','], ['', '.'], $_POST['amount'])));
$stmt->bindValue(':cat', $_POST['category_id']);
$stmt->bindValue(':subcat', !empty($_POST['subcategory_id']) ? $_POST['subcategory_id'] : null);
$stmt->bindValue(':date', $_POST['date']);
$stmt->bindValue(':status', $_POST['status']);
$stmt->bindValue(':notes', $_POST['notes']);
$stmt->execute();
header("Location: ?page=transactions"); exit;
}
if ($_POST['action'] === 'add_category') {
$stmt = $db->prepare("INSERT INTO categories (name, type) VALUES (:name, :type)");
$stmt->bindValue(':name', $_POST['name']);
$stmt->bindValue(':type', $_POST['type']);
$stmt->execute();
header("Location: ?page=categories"); exit;
}
if ($_POST['action'] === 'add_subcategory') {
$stmt = $db->prepare("INSERT INTO subcategories (category_id, name) VALUES (:cat_id, :name)");
$stmt->bindValue(':cat_id', $_POST['category_id']);
$stmt->bindValue(':name', $_POST['name']);
$stmt->execute();
header("Location: ?page=categories"); exit;
}
}
// Exclusão
if (isset($_GET['delete_tx'])) {
$db->exec("DELETE FROM transactions WHERE id = " . intval($_GET['delete_tx']));
header("Location: ?page=transactions"); exit;
}
// Roteamento de Páginas
$page = isset($_GET['page']) ? $_GET['page'] : 'dashboard';
// Carregar Categorias e Subcategorias para uso global
$categories = [];
$res_cat = $db->query("SELECT * FROM categories ORDER BY name");
while ($row = $res_cat->fetchArray(SQLITE3_ASSOC)) { $categories[] = $row; }
$subcategories = [];
$res_sub = $db->query("SELECT * FROM subcategories ORDER BY name");
while ($row = $res_sub->fetchArray(SQLITE3_ASSOC)) { $subcategories[] = $row; }
?>
querySingle("SELECT SUM(amount) FROM transactions WHERE type='receita' AND date LIKE '$mes_atual%'") ?: 0;
$tot_des = $db->querySingle("SELECT SUM(amount) FROM transactions WHERE type='despesa' AND date LIKE '$mes_atual%'") ?: 0;
$saldo = $tot_rec - $tot_des;
?>
Saldo Mensal
R$ = number_format($saldo, 2, ',', '.') ?>
Receitas (Mês)
R$ = number_format($tot_rec, 2, ',', '.') ?>
Despesas (Mês)
R$ = number_format($tot_des, 2, ',', '.') ?>
Últimos 5 Lançamentos
| Data | Descrição | Categoria | Valor | Status |
query("SELECT t.*, c.name as cat_name FROM transactions t LEFT JOIN categories c ON t.category_id = c.id ORDER BY t.date DESC, t.id DESC LIMIT 5");
while ($r = $recent->fetchArray(SQLITE3_ASSOC)): ?>
| = date('d/m/Y', strtotime($r['date'])) ?> |
= htmlspecialchars($r['description']) ?> |
= htmlspecialchars($r['cat_name']) ?> |
= $r['type'] == 'receita' ? '+' : '-' ?> R$ = number_format($r['amount'], 2, ',', '.') ?>
|
= $r['status'] ?> |
Novo Lançamento (Entrada/Saída)
query($query);
?>
query("SELECT c.name, c.type, SUM(t.amount) as total FROM transactions t JOIN categories c ON t.category_id = c.id WHERE t.date LIKE '$mes_rel%' GROUP BY c.id ORDER BY total DESC");
?>