ok, budget_management.php (<?php
// accountant/budget_management.php

session_start();
require_once '../db_connect.php';

// Check if accountant is logged in
if (!isset($_SESSION['accountant_id'])) {
    header("Location: login.php");
    exit();
}

// Function to sanitize output
function sanitize_output($data) {
    return htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}

// Initialize variables for form feedback
$success_message = '';
$error_message = '';

// Handle form submission for adding a new budget
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $month = $_POST['month'];
    $budgeted_revenue = $_POST['budgeted_revenue'];
    $budgeted_expenses = $_POST['budgeted_expenses'];
    $notes = $_POST['notes'];

    // Validate inputs
    if (empty($month) || empty($budgeted_revenue) || empty($budgeted_expenses)) {
        $error_message = 'Please fill in all required fields.';
    } else {
        // Validate month format
        if (!preg_match('/^\d{4}-\d{2}$/', $month)) {
            $error_message = 'Invalid month format. Please use YYYY-MM.';
        } else {
            try {
                // Check if budget for the month already exists
                $stmt = $pdo->prepare("SELECT COUNT(*) FROM budgets WHERE month = ?");
                $stmt->execute([$month]);
                $count = $stmt->fetchColumn();
                if ($count > 0) {
                    $error_message = 'Budget for this month already exists.';
                } else {
                    $stmt = $pdo->prepare("INSERT INTO budgets (month, budgeted_revenue, budgeted_expenses, notes) VALUES (?, ?, ?, ?)");
                    $stmt->execute([$month, $budgeted_revenue, $budgeted_expenses, $notes]);
                    $success_message = 'Budget added successfully.';
                }
            } catch (PDOException $e) {
                $error_message = 'An error occurred while adding the budget.';
            }
        }
    }
}

// Fetch budgets
try {
    $stmt = $pdo->prepare("SELECT * FROM budgets ORDER BY month DESC");
    $stmt->execute();
    $budgets = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Fetch actual revenues and expenses for variance analysis
    foreach ($budgets as &$budget) {
        $month_start = $budget['month'] . '-01';
        $month_end = date('Y-m-t', strtotime($month_start));

        // Actual revenue
        $stmt = $pdo->prepare("
            SELECT SUM(total_amount) AS actual_revenue
            FROM invoices
            WHERE invoice_date BETWEEN ? AND ?
        ");
        $stmt->execute([$month_start, $month_end]);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $budget['actual_revenue'] = $row['actual_revenue'] ?? 0;

        // Actual expenses
        $stmt = $pdo->prepare("
            SELECT SUM(net_salary) AS actual_expenses
            FROM salaries
            WHERE payment_date BETWEEN ? AND ?
            AND status = 'Paid'
        ");
        $stmt->execute([$month_start, $month_end]);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $budget['actual_expenses'] = $row['actual_expenses'] ?? 0;

        // Variance calculation
        $budget['revenue_variance'] = $budget['actual_revenue'] - $budget['budgeted_revenue'];
        $budget['expenses_variance'] = $budget['actual_expenses'] - $budget['budgeted_expenses'];
    }

} catch (PDOException $e) {
    echo "An error occurred: " . sanitize_output($e->getMessage());
    exit();
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Budget Management</title>
    <!-- Meta tags for responsiveness -->
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom Styles -->
    <style>
        /* Custom styles */
        body {
            font-family: Arial, sans-serif;
            background-color: #f8f9fa;
        }
        h1, h2 {
            margin-top: 20px;
            margin-bottom: 20px;
            color: #4F3F02;
        }
        .container {
            margin-top: 30px;
        }
        .card {
            margin-bottom: 20px;
        }
        .btn-custom {
            background-color: #D1C287;
            border-color: #D1C287;
            color: #4F3F02;
            font-weight: bold;
        }
        .btn-custom:hover {
            background-color: #4F3F02;
            border-color: #4F3F02;
            color: #fff;
        }
        .table thead th {
            background-color: #D1C287;
            color: #4F3F02;
        }
        .alert {
            margin-top: 20px;
        }
        .nav-link {
            color: #4F3F02;
            font-weight: bold;
        }
        .nav-link:hover {
            color: #D1C287;
        }
        .navbar {
            margin-bottom: 40px;
            background-color: #f8f9fa;
        }
    </style>
</head>
<body>

<nav class="navbar navbar-expand-lg">
  <div class="container">
    <a class="navbar-brand" href="dashboard.php">Accountant Dashboard</a>
    <div class="collapse navbar-collapse">
      <ul class="navbar-nav ms-auto">
        <li class="nav-item">
          <a class="nav-link" href="budget_management.php">Budget Management</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="accountant_profile.php">Profile</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="logout.php">Logout</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

<div class="container">
    <h1>Budget Management</h1>

    <?php if ($success_message): ?>
        <div class="alert alert-success" role="alert">
            <?php echo sanitize_output($success_message); ?>
        </div>
    <?php endif; ?>
    <?php if ($error_message): ?>
        <div class="alert alert-danger" role="alert">
            <?php echo sanitize_output($error_message); ?>
        </div>
    <?php endif; ?>

    <div class="card">
        <div class="card-header">
            Add New Budget
        </div>
        <div class="card-body">
            <form method="POST" action="">
                <div class="mb-3">
                    <label for="month" class="form-label">Month (YYYY-MM):</label>
                    <input type="text" id="month" name="month" class="form-control" placeholder="e.g., 2023-09" required>
                </div>
                <div class="mb-3">
                    <label for="budgeted_revenue" class="form-label">Budgeted Revenue:</label>
                    <input type="number" step="0.01" id="budgeted_revenue" name="budgeted_revenue" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label for="budgeted_expenses" class="form-label">Budgeted Expenses:</label>
                    <input type="number" step="0.01" id="budgeted_expenses" name="budgeted_expenses" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label for="notes" class="form-label">Notes:</label>
                    <textarea id="notes" name="notes" class="form-control"></textarea>
                </div>
                <button type="submit" class="btn btn-custom">Add Budget</button>
            </form>
        </div>
    </div>

    <h2>Budget Variance Analysis</h2>
    <div class="table-responsive">
    <table class="table table-bordered table-hover align-middle">
        <thead>
            <tr>
                <th>Month</th>
                <th>Budgeted Revenue</th>
                <th>Actual Revenue</th>
                <th>Revenue Variance</th>
                <th>Budgeted Expenses</th>
                <th>Actual Expenses</th>
                <th>Expenses Variance</th>
                <th>Notes</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($budgets as $budget): ?>
            <tr>
                <td><?php echo sanitize_output($budget['month']); ?></td>
                <td>$<?php echo number_format($budget['budgeted_revenue'], 2); ?></td>
                <td>$<?php echo number_format($budget['actual_revenue'], 2); ?></td>
                <td style="color: <?php echo $budget['revenue_variance'] >= 0 ? '#28a745' : '#dc3545'; ?>">
                    $<?php echo number_format($budget['revenue_variance'], 2); ?>
                </td>
                <td>$<?php echo number_format($budget['budgeted_expenses'], 2); ?></td>
                <td>$<?php echo number_format($budget['actual_expenses'], 2); ?></td>
                <td style="color: <?php echo $budget['expenses_variance'] <= 0 ? '#28a745' : '#dc3545'; ?>">
                    $<?php echo number_format($budget['expenses_variance'], 2); ?>
                </td>
                <td><?php echo sanitize_output($budget['notes']); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    </div>
</div>

<!-- Bootstrap JS and dependencies (optional but recommended) -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Popper.js (for Bootstrap 5) -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>) make it cool and wihtout errors  and make it oranzige and type full codes