<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: index.php');
exit;
}
include 'includes/db_connect.php';
// 获取报表数据
$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : date('Y-m-01');
$end_date = isset($_GET['end_date']) ? $_GET['end_date'] : date('Y-m-t');
// 收入统计
$income_stmt = $pdo->prepare("SELECT SUM(amount) as total FROM income WHERE date BETWEEN ? AND ?");
$income_stmt->execute([$start_date, $end_date]);
$total_income = $income_stmt->fetch()['total'];
// 支出统计
$expense_stmt = $pdo->prepare("SELECT SUM(amount) as total FROM expenses WHERE date BETWEEN ? AND ?");
$expense_stmt->execute([$start_date, $end_date]);
$total_expense = $expense_stmt->fetch()['total'];
// 按项目统计收入
$project_income = $pdo->prepare("
SELECT p.name, SUM(i.amount) as total
FROM income i
JOIN projects p ON i.project_id = p.id
WHERE i.date BETWEEN ? AND ?
GROUP BY p.name
");
$project_income->execute([$start_date, $end_date]);
$project_income_data = $project_income->fetchAll();
// 按类别统计支出
$category_expense = $pdo->prepare("
SELECT category, SUM(amount) as total
FROM expenses
WHERE date BETWEEN ? AND ?
GROUP BY category
");
$category_expense->execute([$start_date, $end_date]);
$category_expense_data = $category_expense->fetchAll();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>工地记账系统 - 报表统计</title>
<link rel="stylesheet" href="css/style.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<header>
<div class="container">
<div id="branding">
<h1>工地记账系统</h1>
</div>
<nav>
<ul>
<li><a href="dashboard.php">控制面板</a></li>
<li><a href="income.php">收入</a></li>
<li><a href="expense.php">支出</a></li>
<li><a href="workers.php">工人</a></li>
<li><a href="projects.php">项目</a></li>
<li class="current"><a href="reports.php">报表</a></li>
<li><a href="logout.php">退出</a></li>
</ul>
</nav>
</div>
</header>
<div class="container">
<h2>报表统计</h2>
<form method="get" action="reports.php">
<label for="start_date">开始日期</label>
<input type="date" id="start_date" name="start_date" value="<?php echo $start_date; ?>">
<label for="end_date">结束日期</label>
<input type="date" id="end_date" name="end_date" value="<?php echo $end_date; ?>">
<input type="submit" value="筛选">
</form>
<div style="display: flex; flex-wrap: wrap; justify-content: space-between; margin-top: 20px;">
<div class="dashboard-card" style="flex: 1; min-width: 300px;">
<h3>总收入</h3>
<div class="amount">¥<?php echo number_format($total_income ? $total_income : 0, 2); ?></div>
</div>
<div class="dashboard-card" style="flex: 1; min-width: 300px;">
<h3>总支出</h3>
<div class="amount">¥<?php echo number_format($total_expense ? $total_expense : 0, 2); ?></div>
</div>
<div class="dashboard-card" style="flex: 1; min-width: 300px;">
<h3>净余额</h3>
<div class="amount">¥<?php echo number_format(($total_income ? $total_income : 0) - ($total_expense ? $total_expense : 0), 2); ?></div>
</div>
</div>
<div style="display: flex; flex-wrap: wrap; margin-top: 30px;">
<div style="flex: 1; min-width: 500px; padding: 10px;">
<h3>项目收入分布</h3>
<canvas id="projectIncomeChart" height="300"></canvas>
</div>
<div style="flex: 1; min-width: 500px; padding: 10px;">
<h3>支出类别分布</h3>
<canvas id="expenseCategoryChart" height="300"></canvas>
</div>
</div>
<h3>详细数据</h3>
<div style="display: flex; flex-wrap: wrap;">
<div style="flex: 1; min-width: 500px; padding: 10px;">
<h4>项目收入</h4>
<table>
<tr>
<th>项目名称</th>
<th>收入金额</th>
</tr>
<?php foreach ($project_income_data as $item): ?>
<tr>
<td><?php echo $item['name']; ?></td>
<td>¥<?php echo number_format($item['total'], 2); ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
<div style="flex: 1; min-width: 500px; padding: 10px;">
<h4>类别支出</h4>
<table>
<tr>
<th>类别</th>
<th>支出金额</th>
</tr>
<?php foreach ($category_expense_data as $item): ?>
<tr>
<td><?php echo $item['category']; ?></td>
<td>¥<?php echo number_format($item['total'], 2); ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
</div>
<script>
// 项目收入图表
const projectIncomeCtx = document.getElementById('projectIncomeChart').getContext('2d');
const projectIncomeChart = new Chart(projectIncomeCtx, {
type: 'bar',
data: {
labels: [<?php echo implode(',', array_map(function($item) { return "'" . addslashes($item['name']) . "'"; }, $project_income_data)); ?>],
datasets: [{
label: '项目收入 (¥)',
data: [<?php echo implode(',', array_map(function($item) { return $item['total']; }, $project_income_data)); ?>],
backgroundColor: 'rgba(54, 162, 235, 0.5)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
// 支出类别图表
const expenseCategoryCtx = document.getElementById('expenseCategoryChart').getContext('2d');
const expenseCategoryChart = new Chart(expenseCategoryCtx, {
type: 'pie',
data: {
labels: [<?php echo implode(',', array_map(function($item) { return "'" . addslashes($item['category']) . "'"; }, $category_expense_data)); ?>],
datasets: [{
label: '支出金额 (¥)',
data: [<?php echo implode(',', array_map(function($item) { return $item['total']; }, $category_expense_data)); ?>],
backgroundColor: [
'rgba(255, 99, 132, 0.5)',
'rgba(54, 162, 235, 0.5)',
'rgba(255, 206, 86, 0.5)',
'rgba(75, 192, 192, 0.5)',
'rgba(153, 102, 255, 0.5)',
'rgba(255, 159, 64, 0.5)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',