sales_stats.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. /**
  3. * 销售统计分析展示页面
  4. */
  5. require_once 'conn.php';
  6. require_once 'statistics_utils.php';
  7. require_once 'statistics_sales.php';
  8. // 检查登录状态
  9. if (!isset($_SESSION['employee_id'])) {
  10. checkLogin();
  11. }
  12. // 获取日期范围参数
  13. $date_params = getDateRangeParams();
  14. $start_date = $date_params['start_date_sql'];
  15. $end_date = $date_params['end_date_sql'];
  16. $date_range = $date_params['date_range'];
  17. $period = $date_params['period'];
  18. // 获取选中的业务员ID
  19. $selected_employee = isset($_GET['employee_id']) ? intval($_GET['employee_id']) : 0;
  20. // 获取所有业务员列表
  21. $employees = getAllEmployees($conn);
  22. // 页面头部
  23. include('statistics_header.php');
  24. ?>
  25. <style>
  26. .container {
  27. max-width: 1200px;
  28. margin: 0 auto;
  29. padding: 20px;
  30. }
  31. .page-header {
  32. margin-bottom: 30px;
  33. }
  34. .page-title {
  35. font-size: 24px;
  36. color: #333;
  37. margin: 0;
  38. }
  39. .filter-form {
  40. background: #f5f5f5;
  41. padding: 20px;
  42. border-radius: 8px;
  43. margin-bottom: 30px;
  44. }
  45. .filter-form-inline {
  46. display: flex;
  47. flex-wrap: wrap;
  48. gap: 15px;
  49. align-items: flex-end;
  50. }
  51. .form-group {
  52. margin-bottom: 10px;
  53. }
  54. .form-group label {
  55. display: block;
  56. margin-bottom: 5px;
  57. color: #666;
  58. }
  59. .form-control {
  60. padding: 8px 12px;
  61. border: 1px solid #ddd;
  62. border-radius: 4px;
  63. font-size: 14px;
  64. }
  65. .btn {
  66. background: #4a90e2;
  67. color: white;
  68. padding: 8px 16px;
  69. border: none;
  70. border-radius: 4px;
  71. cursor: pointer;
  72. }
  73. .btn:hover {
  74. background: #357abd;
  75. }
  76. .chart-container {
  77. background: white;
  78. border-radius: 8px;
  79. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  80. padding: 20px;
  81. margin-bottom: 30px;
  82. }
  83. .chart-header {
  84. margin-bottom: 20px;
  85. }
  86. .chart-title {
  87. font-size: 18px;
  88. color: #333;
  89. margin: 0;
  90. }
  91. .stats-grid {
  92. display: grid;
  93. grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  94. gap: 20px;
  95. margin-bottom: 30px;
  96. }
  97. .stat-card {
  98. background: white;
  99. padding: 20px;
  100. border-radius: 8px;
  101. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  102. text-align: center;
  103. }
  104. .stat-card h3 {
  105. font-size: 16px;
  106. color: #666;
  107. margin: 0 0 10px 0;
  108. }
  109. .stat-value {
  110. font-size: 24px;
  111. color: #4a90e2;
  112. font-weight: bold;
  113. }
  114. .analysis-grid {
  115. display: grid;
  116. grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  117. gap: 20px;
  118. margin-bottom: 30px;
  119. }
  120. .table-responsive {
  121. overflow-x: auto;
  122. }
  123. .data-table {
  124. width: 100%;
  125. border-collapse: collapse;
  126. }
  127. .data-table th,
  128. .data-table td {
  129. padding: 12px;
  130. text-align: left;
  131. border-bottom: 1px solid #ddd;
  132. }
  133. .data-table th {
  134. background: #f5f5f5;
  135. font-weight: bold;
  136. }
  137. .data-table tr:hover {
  138. background: #f9f9f9;
  139. }
  140. .employee-stats {
  141. background: #fff;
  142. border-radius: 8px;
  143. padding: 20px;
  144. margin-bottom: 30px;
  145. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  146. }
  147. .employee-header {
  148. display: flex;
  149. align-items: center;
  150. margin-bottom: 20px;
  151. padding-bottom: 15px;
  152. border-bottom: 1px solid #eee;
  153. }
  154. .employee-avatar {
  155. width: 64px;
  156. height: 64px;
  157. border-radius: 50%;
  158. background: #f0f0f0;
  159. margin-right: 15px;
  160. display: flex;
  161. align-items: center;
  162. justify-content: center;
  163. font-size: 24px;
  164. color: #666;
  165. }
  166. .employee-info {
  167. flex: 1;
  168. }
  169. .employee-name {
  170. font-size: 20px;
  171. font-weight: bold;
  172. margin: 0 0 5px;
  173. }
  174. .employee-contact {
  175. color: #666;
  176. font-size: 14px;
  177. }
  178. .performance-grid {
  179. display: grid;
  180. grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  181. gap: 15px;
  182. margin-bottom: 20px;
  183. }
  184. .performance-card {
  185. background: #f8f9fa;
  186. padding: 15px;
  187. border-radius: 6px;
  188. text-align: center;
  189. }
  190. .performance-label {
  191. font-size: 14px;
  192. color: #666;
  193. margin-bottom: 5px;
  194. }
  195. .performance-value {
  196. font-size: 20px;
  197. font-weight: bold;
  198. color: #4a90e2;
  199. }
  200. .trend-chart {
  201. margin-top: 20px;
  202. }
  203. </style>
  204. <div class="container">
  205. <div class="page-header">
  206. <h1 class="page-title">销售统计分析</h1>
  207. </div>
  208. <!-- 日期和业务员筛选 -->
  209. <div class="filter-form">
  210. <form method="get" class="filter-form-inline">
  211. <div class="form-group">
  212. <label for="employee_id">选择业务员</label>
  213. <select class="form-control" id="employee_id" name="employee_id">
  214. <option value="0">全部业务员</option>
  215. <?php foreach ($employees as $emp): ?>
  216. <option value="<?php echo $emp['id']; ?>" <?php echo $selected_employee == $emp['id'] ? 'selected' : ''; ?>>
  217. <?php echo htmlspecialchars($emp['em_user']); ?>
  218. </option>
  219. <?php endforeach; ?>
  220. </select>
  221. </div>
  222. <div class="form-group">
  223. <label for="date_range">选择日期范围</label>
  224. <select class="form-control" id="date_range" name="date_range" onchange="toggleCustomDates()">
  225. <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
  226. <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
  227. <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
  228. <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>最近30天</option>
  229. <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>最近90天</option>
  230. <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
  231. </select>
  232. </div>
  233. <div class="form-group custom-date-inputs" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  234. <label for="start_date">开始日期</label>
  235. <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
  236. </div>
  237. <div class="form-group custom-date-inputs" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  238. <label for="end_date">结束日期</label>
  239. <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
  240. </div>
  241. <div class="form-group">
  242. <label for="period">时间粒度</label>
  243. <select class="form-control" id="period" name="period">
  244. <option value="day" <?php echo $period == 'day' ? 'selected' : ''; ?>>日</option>
  245. <option value="week" <?php echo $period == 'week' ? 'selected' : ''; ?>>周</option>
  246. <option value="month" <?php echo $period == 'month' ? 'selected' : ''; ?>>月</option>
  247. </select>
  248. </div>
  249. <div class="form-group">
  250. <button type="submit" class="btn">应用筛选</button>
  251. </div>
  252. </form>
  253. </div>
  254. <?php if ($selected_employee > 0): ?>
  255. <!-- 单个业务员详细分析 -->
  256. <?php
  257. $employee_detail = getEmployeeDetail($conn, $selected_employee);
  258. $employee_stats = getEmployeeStats($conn, $selected_employee, $start_date, $end_date);
  259. ?>
  260. <div class="employee-stats">
  261. <div class="employee-header">
  262. <div class="employee-avatar">
  263. <?php echo strtoupper(substr($employee_detail['em_user'], 0, 1)); ?>
  264. </div>
  265. <div class="employee-info">
  266. <h2 class="employee-name"><?php echo htmlspecialchars($employee_detail['em_user']); ?></h2>
  267. <div class="employee-contact">
  268. <?php if ($employee_detail['em_email']): ?>
  269. <span>邮箱: <?php echo htmlspecialchars($employee_detail['em_email']); ?></span>
  270. <?php endif; ?>
  271. <?php if ($employee_detail['em_tel']): ?>
  272. <span>电话: <?php echo htmlspecialchars($employee_detail['em_tel']); ?></span>
  273. <?php endif; ?>
  274. </div>
  275. </div>
  276. </div>
  277. <div class="performance-grid">
  278. <div class="performance-card">
  279. <div class="performance-label">总订单数</div>
  280. <div class="performance-value"><?php echo number_format($employee_stats['total_orders']); ?></div>
  281. </div>
  282. <div class="performance-card">
  283. <div class="performance-label">总销售额</div>
  284. <div class="performance-value">¥<?php echo number_format($employee_stats['total_revenue'], 2); ?></div>
  285. </div>
  286. <div class="performance-card">
  287. <div class="performance-label">客户数量</div>
  288. <div class="performance-value"><?php echo number_format($employee_stats['customer_count']); ?></div>
  289. </div>
  290. <div class="performance-card">
  291. <div class="performance-label">平均订单金额</div>
  292. <div class="performance-value">¥<?php echo number_format($employee_stats['avg_order_value'], 2); ?></div>
  293. </div>
  294. <div class="performance-card">
  295. <div class="performance-label">订单完成率</div>
  296. <div class="performance-value"><?php echo formatNumber(($employee_stats['completion_rate'] / 100) * 100, 1); ?>%</div>
  297. </div>
  298. </div>
  299. <!-- 业务员销售趋势 -->
  300. <div class="trend-chart">
  301. <?php renderEmployeeSalesTrend($conn, $selected_employee, $start_date, $end_date); ?>
  302. </div>
  303. <!-- 业务员客户分布 -->
  304. <div class="trend-chart">
  305. <?php renderEmployeeCustomerDistribution($conn, $selected_employee, $start_date, $end_date); ?>
  306. </div>
  307. <!-- 业务员产品销售分析 -->
  308. <div class="trend-chart">
  309. <?php renderEmployeeProductAnalysis($conn, $selected_employee, $start_date, $end_date); ?>
  310. </div>
  311. </div>
  312. <?php endif; ?>
  313. <!-- 销售概览 -->
  314. <div class="chart-container">
  315. <div class="chart-header">
  316. <h2 class="chart-title">销售概览</h2>
  317. </div>
  318. <div class="card-body">
  319. <?php
  320. $sales_overview = getSalesOverview($conn, $start_date, $end_date);
  321. renderSalesOverviewCards($sales_overview);
  322. ?>
  323. </div>
  324. </div>
  325. <!-- 销售转化率分析 -->
  326. <div class="chart-container">
  327. <div class="chart-header">
  328. <h2 class="chart-title">销售转化率分析</h2>
  329. </div>
  330. <?php
  331. $conversion_stats = getOrderConversionStats($conn, $start_date, $end_date);
  332. renderConversionAnalysis($conversion_stats);
  333. ?>
  334. </div>
  335. <!-- 产品类别销售分析 -->
  336. <div class="chart-container">
  337. <div class="chart-header">
  338. <h2 class="chart-title">产品类别销售分析</h2>
  339. </div>
  340. <?php
  341. $category_sales = getProductCategorySales($conn, $start_date, $end_date);
  342. renderCategorySalesChart($category_sales);
  343. ?>
  344. </div>
  345. <!-- 客户地区分布 -->
  346. <div class="chart-container">
  347. <div class="chart-header">
  348. <h2 class="chart-title">客户地区分布</h2>
  349. </div>
  350. <?php
  351. $customer_distribution = getCustomerDistribution($conn, $start_date, $end_date);
  352. renderCustomerDistributionChart($customer_distribution);
  353. ?>
  354. </div>
  355. <!-- 销售员业绩排行 -->
  356. <div class="chart-container">
  357. <div class="chart-header">
  358. <h2 class="chart-title">销售员业绩排行</h2>
  359. </div>
  360. <?php
  361. $employee_performance = getEmployeePerformance($conn, $start_date, $end_date);
  362. renderEmployeePerformanceTable($employee_performance);
  363. ?>
  364. </div>
  365. <!-- 月度销售趋势 -->
  366. <div class="chart-container">
  367. <?php
  368. $monthly_sales = getMonthlySalesTrend($conn, $start_date, $end_date);
  369. $monthly_labels = [];
  370. $monthly_orders = [];
  371. $monthly_revenue = [];
  372. while ($row = $monthly_sales->fetch_assoc()) {
  373. $monthly_labels[] = $row['month'];
  374. $monthly_orders[] = $row['orders'];
  375. $monthly_revenue[] = $row['revenue'];
  376. }
  377. renderMonthlySalesTrendChart($monthly_labels, $monthly_orders, $monthly_revenue);
  378. ?>
  379. </div>
  380. <!-- 支付状态分析 -->
  381. <div class="chart-container">
  382. <div class="chart-header">
  383. <h2 class="chart-title">支付状态分析</h2>
  384. </div>
  385. <?php
  386. $payment_stats = getPaymentStatusStats($conn, $start_date, $end_date);
  387. renderPaymentStatusChart($payment_stats);
  388. ?>
  389. </div>
  390. <!-- 详细订单趋势 -->
  391. <div class="chart-container">
  392. <?php
  393. $detailed_trends = getDetailedOrderTrend($conn, $start_date, $end_date, $period);
  394. $time_labels = [];
  395. $time_orders = [];
  396. $time_quantities = [];
  397. while ($row = $detailed_trends->fetch_assoc()) {
  398. $time_labels[] = $row['time_period'];
  399. $time_orders[] = $row['order_count'];
  400. $time_quantities[] = $row['total_quantity'];
  401. }
  402. renderDetailedOrderTrendChart($time_labels, $time_orders, $time_quantities, $period);
  403. ?>
  404. </div>
  405. </div>
  406. <script>
  407. function toggleCustomDates() {
  408. const dateRange = document.getElementById('date_range').value;
  409. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  410. customDateInputs.forEach(el => {
  411. el.style.display = dateRange === 'custom' ? 'inline-block' : 'none';
  412. });
  413. }
  414. </script>
  415. <?php
  416. include('statistics_footer.php');
  417. ?>