prepare("SELECT id FROM companies WHERE user_id = ?"); $stmt->execute([$user_id]); $company = $stmt->fetch(); $company_id = $company ? $company['id'] : null; // Handle POST requests if ($_SERVER['REQUEST_METHOD'] === 'POST' && $company_id) { $action = $_POST['action'] ?? ''; // ADD ACCOUNT if ($action === 'add_account') { $account_code = trim($_POST['account_code'] ?? ''); $account_name = trim($_POST['account_name'] ?? ''); $account_type = trim($_POST['account_type'] ?? ''); $description = trim($_POST['description'] ?? ''); if (empty($account_code) || empty($account_name) || empty($account_type)) { $errors[] = 'Account Code, Name, and Type are required.'; } if (empty($errors)) { $stmt = $pdo->prepare("INSERT INTO chart_of_accounts (company_id, account_code, account_name, account_type, description) VALUES (?, ?, ?, ?, ?)"); if ($stmt->execute([$company_id, $account_code, $account_name, $account_type, $description])) { $success_message = 'Account added successfully!'; } else { $errors[] = 'Failed to add account. The account code may already exist.'; } } } // EDIT ACCOUNT if ($action === 'edit_account') { $account_id = $_POST['account_id'] ?? null; $account_code = trim($_POST['account_code'] ?? ''); $account_name = trim($_POST['account_name'] ?? ''); $account_type = trim($_POST['account_type'] ?? ''); $description = trim($_POST['description'] ?? ''); if (empty($account_id) || empty($account_code) || empty($account_name) || empty($account_type)) { $errors[] = 'All fields are required for editing.'; } if (empty($errors)) { $stmt = $pdo->prepare("UPDATE chart_of_accounts SET account_code = ?, account_name = ?, account_type = ?, description = ? WHERE id = ? AND company_id = ?"); if ($stmt->execute([$account_code, $account_name, $account_type, $description, $account_id, $company_id])) { $success_message = 'Account updated successfully!'; } else { $errors[] = 'Failed to update account. The account code may already exist for another account.'; } } } // DELETE ACCOUNT if ($action === 'delete_account') { $account_id = $_POST['account_id'] ?? null; if (empty($account_id)) { $errors[] = 'Invalid account for deletion.'; } if (empty($errors)) { $stmt = $pdo->prepare("DELETE FROM chart_of_accounts WHERE id = ? AND company_id = ?"); if ($stmt->execute([$account_id, $company_id])) { $success_message = 'Account deleted successfully!'; } else { $errors[] = 'Failed to delete account.'; } } } } // Fetch all accounts for the company $accounts = []; if ($company_id) { $stmt = $pdo->prepare("SELECT * FROM chart_of_accounts WHERE company_id = ? ORDER BY account_code"); $stmt->execute([$company_id]); $accounts = $stmt->fetchAll(); } require_once __DIR__ . '/includes/sidebar.php'; ?>

Chart of Accounts

This is the list of all financial accounts for your company.

Please set up your company before managing accounts.

Code Name Type Description Actions
No accounts found. Please add one.