prepare("UPDATE categories SET nome = :nome, macro_area_id = :macro_area_id WHERE id = :id AND client_id = :client_id"); $stmt->execute(['nome' => $nome, 'macro_area_id' => $macro_area_id, 'id' => $id, 'client_id' => $client_id]); $success = "Categoria atualizada com sucesso!"; } else { // Create $stmt = $pdo->prepare("INSERT INTO categories (nome, macro_area_id, client_id) VALUES (:nome, :macro_area_id, :client_id)"); $stmt->execute(['nome' => $nome, 'macro_area_id' => $macro_area_id, 'client_id' => $client_id]); $success = "Categoria criada com sucesso!"; } } } elseif ($action === 'toggle_archive') { $id = $_POST['id'] ?? null; if ($id) { $stmt = $pdo->prepare("UPDATE categories SET is_archived = 1 - is_archived WHERE id = :id AND client_id = :client_id"); $stmt->execute(['id' => $id, 'client_id' => $client_id]); $success = "Status da categoria alterado com sucesso!"; } } elseif ($action === 'delete') { $id = $_POST['id'] ?? null; if ($id) { // Check for related expenses $stmt = $pdo->prepare("SELECT COUNT(*) FROM expenses WHERE category_id = :category_id AND client_id = :client_id"); $stmt->execute(['category_id' => $id, 'client_id' => $client_id]); if ($stmt->fetchColumn() > 0) { $errors[] = "Não é possível excluir a categoria, pois existem despesas associadas a ela."; } else { $stmt = $pdo->prepare("DELETE FROM categories WHERE id = :id AND client_id = :client_id"); $stmt->execute(['id' => $id, 'client_id' => $client_id]); $success = "Categoria excluída com sucesso!"; } } } } catch (PDOException $e) { if ($e->getCode() == '23000') { // Integrity constraint violation $errors[] = "Erro: Já existe uma categoria com este nome para a macro área selecionada."; } else { $errors[] = "Ocorreu um erro no banco de dados: " . $e->getMessage(); } } } // Fetch data for display $macro_areas = $pdo->prepare("SELECT id, nome FROM macro_areas WHERE client_id = :client_id ORDER BY nome"); $macro_areas->execute(['client_id' => $client_id]); $macro_areas_list = $macro_areas->fetchAll(); $stmt = $pdo->prepare(" SELECT c.id, c.nome, c.is_archived, c.macro_area_id, m.nome as macro_area_nome FROM categories c JOIN macro_areas m ON c.macro_area_id = m.id WHERE c.client_id = :client_id ORDER BY m.nome, c.nome "); $stmt->execute(['client_id' => $client_id]); $categories = $stmt->fetchAll(); $page_title = "Categorias"; include 'includes/header.php'; ?>

Categorias

Nome da Categoria Macro Área Status Ações
Nenhuma categoria encontrada.