| Linux premium274.web-hosting.com 4.18.0-553.45.1.lve.el8.x86_64 #1 SMP Wed Mar 26 12:08:09 UTC 2025 x86_64 Path : /home/whagcoha/ledger.mwagalwaservices.com/ |
| Current File : /home/whagcoha/ledger.mwagalwaservices.com/summary.php |
<?php
if ((!isset($_SESSION['gen256'])) ) {
echo "<script>
window.open('login.php','_self');
</script>";
}
?>
<?php
// Assume user is logged in
$user_id = $_SESSION['gen256']['id'];
// Get selected month/year or default to current
$month = isset($_GET['month']) ? $_GET['month'] : date('m');
$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
// Prepare SQL for income
$sql_income = "SELECT SUM(amount) as total_income
FROM income
WHERE user_id = ?
AND MONTH(date) = ?
AND YEAR(date) = ?";
$stmt_income = $conn->prepare($sql_income);
$stmt_income->bind_param("iii", $user_id, $month, $year);
$stmt_income->execute();
$result_income = $stmt_income->get_result()->fetch_assoc();
$total_income = $result_income['total_income'] ?? 0;
// Prepare SQL for expenses
$sql_expense = "SELECT SUM(amount) as total_expense
FROM expenses
WHERE user_id = ?
AND MONTH(date) = ?
AND YEAR(date) = ?";
$stmt_expense = $conn->prepare($sql_expense);
$stmt_expense->bind_param("iii", $user_id, $month, $year);
$stmt_expense->execute();
$result_expense = $stmt_expense->get_result()->fetch_assoc();
$total_expense = $result_expense['total_expense'] ?? 0;
$net_total = $total_income - $total_expense;
?>
<div class="container mt-4">
<div class="card shadow">
<div class="card-header bg-primary text-white">
<h5 class="mb-0">Monthly Summary</h5>
</div>
<div class="card-body">
<!-- Month Selector -->
<form method="get" action="index.php?p=summary" class="row g-3 mb-4">
<input type="hidden" name="p" value="summary">
<div class="col-md-5">
<select name="month" class="form-select">
<?php
for ($m = 1; $m <= 12; $m++) {
$selected = ($m == $month) ? "selected" : "";
echo "<option value='$m' $selected>" . date("F", mktime(0, 0, 0, $m, 10)) . "</option>";
}
?>
</select>
</div>
<div class="col-md-5">
<select name="year" class="form-select">
<?php
for ($y = date("Y"); $y >= date("Y") - 5; $y--) {
$selected = ($y == $year) ? "selected" : "";
echo "<option value='$y' $selected>$y</option>";
}
?>
</select>
</div>
<div class="col-md-2">
<button type="submit" class="btn btn-success w-100">View</button>
</div>
</form>
<!-- Summary Display -->
<div class="row text-center">
<div class="col-md-4">
<div class="p-3 border rounded bg-light">
<h6>Income</h6>
<h4 class="text-success">Shs <?= number_format($total_income, 2) ?></h4>
</div>
</div>
<div class="col-md-4">
<div class="p-3 border rounded bg-light">
<h6>Expenses</h6>
<h4 class="text-danger">Shs <?= number_format($total_expense, 2) ?></h4>
</div>
</div>
<div class="col-md-4">
<div class="p-3 border rounded bg-light">
<h6>Net Total</h6>
<h4 class="<?= ($net_total >= 0) ? 'text-success' : 'text-danger' ?>">
Shs <?= number_format($net_total, 2) ?>
</h4>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
// include "monthlychart.php";
?>