// Disable error reporting in production
// ini_set('display_errors', 0);
// error_reporting(0);
// Enable error reporting during development (comment out in production)
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Function to sanitize output
function sanitize_output(data) {
return htmlspecialchars(data, ENT_QUOTES, 'UTF-8');
}
// Check if accountant is logged in
if (!isset($_SESSION['accountant_id'])) {
// Not logged in, redirect to login page
header("Location: login.php");
exit();
}
// Retrieve and validate date range from GET parameters
startdate=isset_GET['start_date']) ? end_date = isset(_GET['end_date'] : date('Y-m-d');
// Validate date formats
function validate_date(date,format = 'Y-m-d') {
d=DateTime::createFromFormat(format, date);returnd && d−>format(format) === $date;
}
if (!validate_date(startdate)∣∣!validatedate(end_date)) {
// Invalid date format, set to default last 30 days
startdate=dateend_date = date('Y-m-d');
}
// Ensure start_date is not after end_date
if (startdate>end_date) {
// Swap dates
temp=start_date;
startdate=end_date;
enddate=temp;
}
try {
// Prepare statements with date filters
code
// Total number of invoices
$stmt = $pdo->prepare("SELECT COUNT(*) AS total_invoices FROM invoices WHERE invoice_date BETWEEN ? AND ?");
$stmt->execute([$start_date, $end_date]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_invoices = $row['total_invoices'];
// Total revenue from invoices
$stmt = $pdo->prepare("SELECT SUM(total_amount) AS total_revenue FROM invoices WHERE invoice_date BETWEEN ? AND ?");
$stmt->execute([$start_date, $end_date]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_revenue = $row['total_revenue'] ?? 0;
// Average revenue per invoice
$average_revenue = $total_invoices > 0 ? $total_revenue / $total_invoices : 0;
// Total salaries paid (sum of net_salary where status is 'Paid')
$stmt = $pdo->prepare("SELECT SUM(net_salary) AS total_salaries FROM salaries WHERE payment_date BETWEEN ? AND ? AND status = 'Paid'");
$stmt->execute([$start_date, $end_date]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_salaries = $row['total_salaries'] ?? 0;
// Total expenses (assuming salaries are the primary expenses)
$total_expenses = $total_salaries;
// Profit (Revenue - Expenses)
$profit = $total_revenue - $total_expenses;
// Outstanding payments (invoices not fully paid)
// Improved logic to account for partial payments
$stmt = $pdo->prepare("
SELECT i.id, i.total_amount, IFNULL(SUM(p.amount_paid), 0) AS total_paid
FROM invoices i
LEFT JOIN payments p ON i.id = p.invoice_id
WHERE i.invoice_date BETWEEN ? AND ?
GROUP BY i.id
HAVING total_paid < i.total_amount
");
$stmt->execute([$start_date, $end_date]);
$outstanding_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$outstanding_payments = 0;
foreach ($outstanding_rows as $out) {
$outstanding_payments += ($out['total_amount'] - $out['total_paid']);
}
// Total payments received
$stmt = $pdo->prepare("SELECT SUM(amount_paid) AS total_payments FROM payments WHERE payment_date BETWEEN ? AND ?");
$stmt->execute([$start_date, $end_date]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_payments = $row['total_payments'] ?? 0;
// Fetch data for charts
// Revenue over time (grouped by date)
$stmt = $pdo->prepare("SELECT invoice_date, SUM(total_amount) AS daily_revenue FROM invoices WHERE invoice_date BETWEEN ? AND ? GROUP BY invoice_date ORDER BY invoice_date ASC");
$stmt->execute([$start_date, $end_date]);
$revenue_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Expenses over time (grouped by date)
$stmt = $pdo->prepare("SELECT payment_date, SUM(net_salary) AS daily_expenses FROM salaries WHERE payment_date BETWEEN ? AND ? AND status = 'Paid' GROUP BY payment_date ORDER BY payment_date ASC");
$stmt->execute([$start_date, $end_date]);
$expenses_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException e) {
// Log the error to a file (for production)
// error_log(e->getMessage(), 3, '../logs/error.log');
code
// Display a user-friendly message
echo "An error occurred while fetching dashboard data. Please try again later.";
exit();
}
// Prepare data for charts
dates=[];revenues = [];
expenses=[];start = new DateTime(startdate);end = new DateTime(enddate);end->modify('+1 day');
period=newDatePeriod(start, new DateInterval('P1D'), $end);
foreach (periodasdate) {
currentdate=date->format('Y-m-d');
dates[]=current_date;
revenues[current_date] = 0; // Initialize with 0
expenses[current_date] = 0; // Initialize with 0
}
// Map revenue data
foreach (revenuedataasdata) {
invoicedate=data['invoice_date'];
if (isset(revenues[invoice_date])) {
revenues[invoice_date] = $data['daily_revenue'];
}
}
// Map expenses data
foreach (expensesdataasdata) {
paymentdate=data['payment_date'];
if (isset(expenses[payment_date])) {
expenses[payment_date] = $data['daily_expenses'];
}
}
// Prepare data arrays for charts
chartdates=arrayvalues(dates);
chartrevenues=arrayvrevenues);
chartexpenses=arrayvexpenses);
?>
// 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');
}
// Handle form submission for adding a new budget
if (_SERVER['REQUEST_METHOD'] === 'POST') {
month = POST[′month′];budgeted_revenue = POST[′budgetedbudgeted_expenses = POST[′budgetednotes = $_POST['notes'];
-- Foreign key constraints
FOREIGN KEY (patient_id) REFERENCES patients(id) ON DELETE CASCADE, -- Links to the patients table
FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE CASCADE -- Links to the invoices table
);
CREATE TABLE budgets (
id INT AUTO_INCREMENT PRIMARY KEY, -- Unique ID for each budget entry
month VARCHAR(7), -- The month for the budget in 'YYYY-MM' format
budgeted_revenue DECIMAL(10, 2), -- The projected revenue for the month
budgeted_expenses DECIMAL(10, 2), -- The projected expenses for the month
notes TEXT -- Additional notes or comments regarding the budget
);
) ) so type full codes for update two files budget_management.php and dashboard.php
Here is the updated dashboard.php file:
php
<?php
// accountant/dashboard.php
session_start();
require_once '../db_connect.php';
// Disable error reporting in production
// ini_set('display_errors', 0);
// error_reporting(0);
// Enable error reporting during development (comment out in production)
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Function to sanitize output
function sanitize_output($data) {
return htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
}
// Check if accountant is logged in
if (!isset($_SESSION['accountant_id'])) {
// Not logged in, redirect to login page
header("Location: login.php");
exit();
}
// Retrieve and validate date range from GET parameters
$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : date('Y-m-d', strtotime('-30 days'));
$end_date = isset($_GET['end_date']) ? $_GET['end_date'] : date('Y-m-d');
// Validate date formats
function validate_date($date, $format = 'Y-m-d') {
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) === $date;
}
if (!validate_date($start_date) || !validate_date($end_date)) {
// Invalid date format, set to default last 30 days
$start_date = date('Y-m-d', strtotime('-30 days'));
$end_date = date('Y-m-d');
}
// Ensure start_date is not after end_date
if ($start_date > $end_date) {
// Swap dates
$temp = $start_date;
$start_date = $end_date;
$end_date = $temp;
}
try {
// Prepare statements with date filters
// Total number of invoices
$stmt = $pdo->prepare("SELECT COUNT(*) AS total_invoices FROM invoices WHERE invoice_date BETWEEN ? AND ?");
$stmt->execute([$start_date, $end_date]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_invoices = $row['total_invoices'];
// Total revenue from invoices
$stmt = $pdo->prepare("SELECT SUM(total_amount) AS total_revenue FROM invoices WHERE invoice_date BETWEEN ? AND ?");
$stmt->execute([$start_date, $end_date]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_revenue = $row['total_revenue'] ?? 0;
// Average revenue per invoice
$average_revenue = $total_invoices > 0 ? $total_revenue / $total_invoices : 0;
// Total salaries paid (sum of net_salary where status is 'Paid')
$stmt = $pdo->prepare("SELECT SUM(net_salary) AS total_salaries FROM salaries WHERE payment_date BETWEEN ? AND ? AND status = 'Paid'");
$stmt->execute([$start_date, $end_date]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_salaries = $row['total_salaries'] ?? 0;
// Total expenses (assuming salaries are the primary expenses)
$total_expenses = $total_salaries;
// Profit (Revenue - Expenses)
$profit = $total_revenue - $total_expenses;
// Outstanding payments (invoices not fully paid)
// Improved logic to account for partial payments
$stmt = $pdo->prepare("
SELECT i.id, i.total_amount, IFNULL(SUM(p.amount_paid), 0) AS total_paid
FROM invoices i
LEFT JOIN payments p ON i.id = p.invoice_id
WHERE i.invoice_date BETWEEN ? AND ?
GROUP BY i.id
HAVING total_paid < i.total_amount
");
$stmt->execute([$start_date, $end_date]);
$outstanding_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$outstanding_payments = 0;
foreach ($outstanding_rows as $out) {
$outstanding_payments += ($out['total_amount'] - $out['total_paid']);
}
// Total payments received
$stmt = $pdo->prepare("SELECT SUM(amount_paid) AS total_payments FROM payments WHERE payment_date BETWEEN ? AND ?");
$stmt->execute([$start_date, $end_date]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_payments = $row['total_payments'] ?? 0;
// Fetch data for charts
// Revenue over time (grouped by month)
$stmt = $pdo->prepare("SELECT DATE_FORMAT(invoice_date, '%Y-%m') AS month, SUM(total_amount) AS revenue FROM invoices WHERE invoice_date BETWEEN ? AND ? GROUP BY month ORDER BY month ASC");
$stmt->execute([$start_date, $end_date]);
$revenue_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Expenses over time (grouped by month)
$stmt = $pdo->prepare("SELECT DATE_FORMAT(payment_date, '%Y-%m') AS month, SUM(net_salary) AS expenses FROM salaries WHERE payment_date BETWEEN ? AND ? AND status = 'Paid' GROUP BY month ORDER BY month ASC");
$stmt->execute([$start_date, $end_date]);
$expenses_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Budget Variance Analysis
// Convert start_date and end_date to months
$start_month = date('Y-m', strtotime($start_date));
$end_month = date('Y-m', strtotime($end_date));
// Get all months between start_month and end_month
$budget_period = new DatePeriod(
new DateTime($start_month . '-01'),
new DateInterval('P1M'),
(new DateTime($end_month . '-01'))->modify('+1 month')
);
$total_budgeted_revenue = 0;
$total_budgeted_expenses = 0;
$budgeted_revenues = [];
$budgeted_expenses = [];
foreach ($budget_period as $dt) {
$month = $dt->format('Y-m');
// Fetch budget data for the month
$stmt = $pdo->prepare("SELECT * FROM budgets WHERE month = ?");
$stmt->execute([$month]);
$budget = $stmt->fetch(PDO::FETCH_ASSOC);
if ($budget) {
$total_budgeted_revenue += $budget['budgeted_revenue'];
$total_budgeted_expenses += $budget['budgeted_expenses'];
$budgeted_revenues[$month] = $budget['budgeted_revenue'];
$budgeted_expenses[$month] = $budget['budgeted_expenses'];
} else {
// If no budget exists for the month, assume 0
$budgeted_revenues[$month] = 0;
$budgeted_expenses[$month] = 0;
}
}
// Calculate variances
$revenue_variance = $total_revenue - $total_budgeted_revenue;
$expenses_variance = $total_expenses - $total_budgeted_expenses;
} catch (PDOException $e) {
// Log the error to a file (for production)
// error_log($e->getMessage(), 3, '../logs/error.log');
// Display a user-friendly message
echo "An error occurred while fetching dashboard data. Please try again later.";
exit();
}
// Prepare data for charts
$months = [];
$revenues = [];
$expenses = [];
$budgeted_revenues_chart = [];
$budgeted_expenses_chart = [];
$start = new DateTime($start_month . '-01');
$end = new DateTime($end_month . '-01');
$end->modify('+1 month');
$period = new DatePeriod($start, new DateInterval('P1M'), $end);
foreach ($period as $dt) {
$current_month = $dt->format('Y-m');
$months[] = $current_month;
$revenues[$current_month] = 0; // Initialize with 0
$expenses[$current_month] = 0; // Initialize with 0
$budgeted_revenues_chart[$current_month] = $budgeted_revenues[$current_month] ?? 0; // budgeted revenue for the month
$budgeted_expenses_chart[$current_month] = $budgeted_expenses[$current_month] ?? 0; // budgeted expenses for the month
}
// Map revenue data
foreach ($revenue_data as $data) {
$month = $data['month'];
if (isset($revenues[$month])) {
$revenues[$month] = $data['revenue'];
}
}
// Map expenses data
foreach ($expenses_data as $data) {
$month = $data['month'];
if (isset($expenses[$month])) {
$expenses[$month] = $data['expenses'];
}
}
// Prepare data arrays for charts
$chart_months = array_values($months);
$chart_revenues = array_values($revenues);
$chart_expenses = array_values($expenses);
$chart_budgeted_revenues = array_values($budgeted_revenues_chart);
$chart_budgeted_expenses = array_values($budgeted_expenses_chart);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Accountant Dashboard</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">
<!-- Chart.js Library -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<!-- Custom Styles -->
<style>
/* Import Google Fonts */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');
/* Root Variables */
:root {
--primary-color: #4F3F02;
--secondary-color: #D1C287;
--background-color: #F8F9FA;
--card-background: #FFFFFF;
--text-color: #333333;
--success-color: #28a745;
--danger-color: #dc3545;
--info-color: #17a2b8;
--font-family: 'Poppins', sans-serif;
}
/* Reset and Base Styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: var(--background-color);
font-family: var(--font-family);
color: var(--text-color);
}
/* Header */
.dashboard-header {
background-color: var(--primary-color);
color: #fff;
padding: 30px;
text-align: center;
border-radius: 0 0 25px 25px;
margin-bottom: 40px;
}
.dashboard-header h1 {
font-weight: 700;
}
.dashboard-header p a {
color: #fff;
margin: 0 10px;
}
.dashboard-header p a:hover {
text-decoration: underline;
}
/* Cards */
.card {
border: none;
border-radius: 15px;
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.1);
margin-bottom: 30px;
background-color: var(--card-background);
}
.card .card-body {
padding: 25px;
}
.card .card-title {
color: var(--primary-color);
font-weight: 600;
margin-bottom: 15px;
}
.card .card-text {
font-size: 2em;
font-weight: 700;
}
/* Buttons */
.btn-custom {
background-color: var(--secondary-color);
border-color: var(--secondary-color);
color: var(--primary-color);
font-weight: 600;
width: 100%;
padding: 15px;
border-radius: 10px;
transition: all 0.3s ease;
}
.btn-custom:hover {
background-color: var(--primary-color);
color: #fff;
border-color: var(--secondary-color);
transform: translateY(-3px);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}
/* Filters */
.filters {
background-color: var(--card-background);
padding: 25px;
border-radius: 15px;
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.1);
margin-bottom: 40px;
}
/* Charts */
.chart-container {
position: relative;
height: 400px;
}
/* Responsive Adjustments */
@media (max-width: 768px) {
.dashboard-header h1 {
font-size: 24px;
}
.btn-custom {
margin-bottom: 15px;
}
}
@media (max-width: 576px) {
.dashboard-header h1 {
font-size: 20px;
}
.card .card-body {
padding: 20px;
}
.card .card-text {
font-size: 1.5em;
}
.filters {
padding: 20px;
}
}
/* Dark Mode Styles */
body.dark-mode {
background-color: #121212;
color: #e0e0e0;
}
body.dark-mode .dashboard-header {
background-color: #1f1f1f;
color: #e0e0e0;
}
body.dark-mode .card {
background-color: #1f1f1f;
box-shadow: 0 6px 10px rgba(255, 255, 255, 0.05);
}
body.dark-mode .btn-custom {
background-color: #333;
border-color: #333;
color: #fff;
}
body.dark-mode .btn-custom:hover {
background-color: #555;
border-color: #555;
color: #fff;
}
body.dark-mode .filters {
background-color: #1f1f1f;
box-shadow: 0 6px 10px rgba(255, 255, 255, 0.05);
}
body.dark-mode .card-title {
color: #d1c287;
}
</style>
</head>
<body>
<div class="dashboard-header">
<h1>Welcome, <?php echo sanitize_output($_SESSION['accountant_username']); ?></h1>
<p>
<a href="accountant_profile.php">Profile</a> | <a href="logout.php">Logout</a>
</p>
</div>
<div class="container">
<!-- Date Range Filters -->
<div class="filters">
<form method="GET" action="" class="row g-3">
<div class="col-md-5">
<label for="start_date" class="form-label">Start Date:</label>
<input type="date" id="start_date" name="start_date" class="form-control" value="<?php echo sanitize_output($start_date); ?>" required>
</div>
<div class="col-md-5">
<label for="end_date" class="form-label">End Date:</label>
<input type="date" id="end_date" name="end_date" class="form-control" value="<?php echo sanitize_output($end_date); ?>" required>
</div>
<div class="col-md-2 d-flex align-items-end">
<button type="submit" class="btn btn-custom">Apply Filters</button>
</div>
</form>
</div>
<!-- Financial Metrics -->
<div class="row">
<!-- Total Invoices -->
<div class="col-lg-3 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Total Invoices</h5>
<p class="card-text"><?php echo number_format($total_invoices); ?></p>
</div>
</div>
</div>
<!-- Total Revenue -->
<div class="col-lg-3 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Total Revenue</h5>
<p class="card-text">$<?php echo number_format($total_revenue, 2); ?></p>
</div>
</div>
</div>
<!-- Budgeted Revenue -->
<div class="col-lg-3 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Budgeted Revenue</h5>
<p class="card-text">$<?php echo number_format($total_budgeted_revenue, 2); ?></p>
</div>
</div>
</div>
<!-- Revenue Variance -->
<div class="col-lg-3 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Revenue Variance</h5>
<p class="card-text" style="color: <?php echo $revenue_variance >= 0 ? 'var(--success-color)' : 'var(--danger-color)'; ?>">
$<?php echo number_format($revenue_variance, 2); ?>
</p>
</div>
</div>
</div>
<!-- Total Expenses -->
<div class="col-lg-3 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Total Expenses</h5>
<p class="card-text">$<?php echo number_format($total_expenses, 2); ?></p>
</div>
</div>
</div>
<!-- Budgeted Expenses -->
<div class="col-lg-3 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Budgeted Expenses</h5>
<p class="card-text">$<?php echo number_format($total_budgeted_expenses, 2); ?></p>
</div>
</div>
</div>
<!-- Expenses Variance -->
<div class="col-lg-3 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Expenses Variance</h5>
<p class="card-text" style="color: <?php echo $expenses_variance <= 0 ? 'var(--success-color)' : 'var(--danger-color)'; ?>">
$<?php echo number_format($expenses_variance, 2); ?>
</p>
</div>
</div>
</div>
<!-- Profit -->
<div class="col-lg-3 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Profit</h5>
<p class="card-text" style="color: <?php echo $profit >= 0 ? 'var(--success-color)' : 'var(--danger-color)'; ?>">
$<?php echo number_format($profit, 2); ?>
</p>
</div>
</div>
</div>
<!-- Average Revenue per Invoice -->
<div class="col-lg-4 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Avg. Revenue per Invoice</h5>
<p class="card-text">$<?php echo number_format($average_revenue, 2); ?></p>
</div>
</div>
</div>
<!-- Outstanding Payments -->
<div class="col-lg-4 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Outstanding Payments</h5>
<p class="card-text" style="color: var(--danger-color);">
$<?php echo number_format($outstanding_payments, 2); ?>
</p>
</div>
</div>
</div>
<!-- Total Payments Received -->
<div class="col-lg-4 col-md-6">
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Total Payments Received</h5>
<p class="card-text">$<?php echo number_format($total_payments, 2); ?></p>
</div>
</div>
</div>
</div>
<!-- Charts -->
<div class="row">
<!-- Revenue vs. Expenses Chart -->
<div class="col-md-12">
<div class="card">
<div class="card-body">
<h5 class="card-title">Revenue vs. Expenses Over Time</h5>
<div class="chart-container">
<canvas id="revenueExpensesChart"></canvas>
</div>
</div>
</div>
</div>
</div>
<!-- Actions -->
<h3 class="mt-5 mb-4" style="color: var(--primary-color); font-weight: 700;">Actions</h3>
<div class="row">
<div class="col-md-3 mb-3">
<a href="manage_invoices.php" class="btn btn-custom">Manage Invoices</a>
</div>
<div class="col-md-3 mb-3">
<a href="manage_payments.php" class="btn btn-custom">Manage Payments</a>
</div>
<div class="col-md-3 mb-3">
<a href="manage_salaries.php" class="btn btn-custom">Manage Salaries</a>
</div>
<div class="col-md-3 mb-3">
<a href="budget_management.php" class="btn btn-custom">Budget Management</a>
</div>
</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>
<!-- Chart.js Script -->
<script>
(function() {
const ctx = document.getElementById('revenueExpensesChart').getContext('2d');
const revenueExpensesChart = new Chart(ctx, {
type: 'line',
data: {
labels: <?php echo json_encode($chart_months); ?>,
datasets: [
{
label: 'Actual Revenue',
data: <?php echo json_encode($chart_revenues); ?>,
borderColor: 'rgba(40, 167, 69, 1)', // Green
backgroundColor: 'rgba(40, 167, 69, 0.2)',
fill: true,
tension: 0.3,
pointRadius: 3,
},
{
label: 'Actual Expenses',
data: <?php echo json_encode($chart_expenses); ?>,
borderColor: 'rgba(220, 53, 69, 1)', // Red
backgroundColor: 'rgba(220, 53, 69, 0.2)',
fill: true,
tension: 0.3,
pointRadius: 3,
},
{
label: 'Budgeted Revenue',
data: <?php echo json_encode($chart_budgeted_revenues); ?>,
borderColor: 'rgba(23, 162, 184, 1)', // Blue
backgroundColor: 'rgba(23, 162, 184, 0.0)',
fill: false,
tension: 0.3,
borderDash: [5, 5],
pointRadius: 3,
},
{
label: 'Budgeted Expenses',
data: <?php echo json_encode($chart_budgeted_expenses); ?>,
borderColor: 'rgba(255, 193, 7, 1)', // Yellow
backgroundColor: 'rgba(255, 193, 7, 0.0)',
fill: false,
tension: 0.3,
borderDash: [5, 5],
pointRadius: 3,
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
interaction: {
mode: 'index',
intersect: false
},
plugins: {
legend: {
labels: {
font: {
family: 'Poppins',
size: 14,
weight: '600'
}
}
},
tooltip: {
enabled: true,
callbacks: {
label: function(context) {
let label = context.dataset.label || '';
if (label) {
label += ': ';
}
if (context.parsed.y !== null) {
label += '$' + context.parsed.y.toLocaleString();
}
return label;
}
}
}
},
scales: {
x: {
title: {
display: true,
text: 'Month',
font: {
family: 'Poppins',
size: 14,
weight: '600'
}
},
ticks: {
autoSkip: true,
maxTicksLimit: 20,
font: {
family: 'Poppins',
}
},
grid: {
display: false
}
},
y: {
title: {
display: true,
text: 'Amount ($)',
font: {
family: 'Poppins',
size: 14,
weight: '600'
}
},
ticks: {
beginAtZero: true,
font: {
family: 'Poppins',
},
callback: function(value) {
return '$' + value.toLocaleString();
}
},
grid: {
color: '#e9ecef'
}
}
}
}
});
})();
</script>
<!-- Dark Mode Toggle Script -->
<script>
document.addEventListener('DOMContentLoaded', function() {
const darkModeToggle = document.getElementById('toggleDarkMode');
// Toggle dark mode on button click
darkModeToggle.addEventListener('click', function() {
document.body.classList.toggle('dark-mode');
// Save preference in localStorage
if(document.body.classList.contains('dark-mode')) {
localStorage.setItem('theme', 'dark');
this.innerHTML = '<i class="fas fa-sun"></i>';
} else {
localStorage.setItem('theme', 'light');
this.innerHTML = '<i class="fas fa-moon"></i>';
}
});
// Load theme preference on page load
const savedTheme = localStorage.getItem('theme');
if(savedTheme === 'dark') {
document.body.classList.add('dark-mode');
darkModeToggle.innerHTML = '<i class="fas fa-sun"></i>';
} else {
darkModeToggle.innerHTML = '<i class="fas fa-moon"></i>';
}
});
</script>
</body>
</html>
And here is the updated budget_management.php file: