region_stats.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. /**
  3. * 地区统计分析展示页面
  4. */
  5. require_once 'conn.php';
  6. require_once 'statistics_utils.php';
  7. require_once 'statistics_region.php';
  8. // 检查登录状态
  9. if (!isset($_SESSION['employee_id'])) {
  10. checkLogin();
  11. }
  12. // 获取当前登录用户信息
  13. $current_employee_id = $_SESSION['employee_id'];
  14. $current_permission_role = 0;
  15. // 获取当前用户权限角色
  16. $current_employee_id = intval($current_employee_id); // 确保是整数
  17. $query = "SELECT em_permission_role_id FROM employee WHERE id = $current_employee_id";
  18. $result = $conn->query($query);
  19. if ($result && $row = $result->fetch_assoc()) {
  20. $current_permission_role = $row['em_permission_role_id'];
  21. }
  22. // 获取日期范围参数
  23. $date_params = getDateRangeParams();
  24. $start_date = $date_params['start_date_sql'];
  25. $end_date = $date_params['end_date_sql'];
  26. $date_range = $date_params['date_range'];
  27. $period = $date_params['period'];
  28. // 特定国家过滤
  29. $country_id = isset($_GET['country_id']) ? intval($_GET['country_id']) : 0;
  30. // 页面头部
  31. include('statistics_header.php');
  32. ?>
  33. <div class="page-header">
  34. <h1 class="page-title">地区统计分析</h1>
  35. </div>
  36. <!-- 日期筛选 -->
  37. <div class="filter-form">
  38. <form method="get" class="filter-form-inline">
  39. <div class="form-group">
  40. <label for="date_range">选择日期范围</label>
  41. <select class="form-control" id="date_range" name="date_range" onchange="toggleCustomDates()">
  42. <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
  43. <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
  44. <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
  45. <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>最近30天</option>
  46. <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>最近90天</option>
  47. <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
  48. </select>
  49. </div>
  50. <div class="form-group custom-date-inputs" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  51. <label for="start_date">开始日期</label>
  52. <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
  53. </div>
  54. <div class="form-group custom-date-inputs" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  55. <label for="end_date">结束日期</label>
  56. <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
  57. </div>
  58. <!-- 业务员选择 -->
  59. <div class="form-group">
  60. <label for="selected_employee">选择业务员</label>
  61. <select class="form-control" id="selected_employee" name="selected_employee">
  62. <option value="all">所有业务员</option>
  63. <?php
  64. // 获取当前用户可见的业务员列表
  65. $visible_employees_query = "";
  66. if ($current_permission_role == 1) {
  67. // 管理员可以看到所有业务员
  68. $visible_employees_query = "SELECT id, em_user FROM employee WHERE em_role IS NOT NULL ORDER BY em_user";
  69. } elseif ($current_permission_role == 2) {
  70. // 组长可以看到自己和组员
  71. $visible_employees_query = "SELECT id, em_user FROM employee WHERE id = $current_employee_id OR em_role = $current_employee_id ORDER BY em_user";
  72. } else {
  73. // 组员只能看到自己
  74. $visible_employees_query = "SELECT id, em_user FROM employee WHERE id = $current_employee_id";
  75. }
  76. $visible_employees_result = $conn->query($visible_employees_query);
  77. $selected_employee = isset($_GET['selected_employee']) ? $_GET['selected_employee'] : 'all';
  78. while ($emp = $visible_employees_result->fetch_assoc()) {
  79. $selected = ($selected_employee == $emp['id']) ? 'selected' : '';
  80. echo "<option value='".$emp['id']."' $selected>".$emp['em_user']."</option>";
  81. }
  82. ?>
  83. </select>
  84. </div>
  85. <div class="form-group">
  86. <label for="country_id">国家/地区</label>
  87. <select class="form-control" id="country_id" name="country_id">
  88. <option value="0">全部地区</option>
  89. <?php
  90. $country_filter = isset($_GET['country_id']) ? intval($_GET['country_id']) : 0;
  91. $sql = "SELECT id, countryName FROM country ORDER BY countryName";
  92. $countries = $conn->query($sql);
  93. while($country = $countries->fetch_assoc()) {
  94. $selected = ($country_filter == $country['id']) ? 'selected' : '';
  95. echo "<option value='{$country['id']}' {$selected}>{$country['countryName']}</option>";
  96. }
  97. ?>
  98. </select>
  99. </div>
  100. <div class="form-group">
  101. <button type="submit" class="btn">应用筛选</button>
  102. </div>
  103. </form>
  104. </div>
  105. <!-- 地区销售数据概览 -->
  106. <div class="key-metrics-section">
  107. <div class="section-title">
  108. <h2>地区销售概览</h2>
  109. </div>
  110. <div class="stats-row">
  111. <div class="stat-card">
  112. <h3>总销售额</h3>
  113. <?php
  114. // 获取选择的业务员
  115. $selected_employee = isset($_GET['selected_employee']) ? $_GET['selected_employee'] : 'all';
  116. // 确定要显示哪些业务员的数据
  117. $employee_filter = null;
  118. if ($selected_employee != 'all') {
  119. // 如果选择了特定业务员,则只显示该业务员的数据
  120. $employee_filter = intval($selected_employee);
  121. } else {
  122. // 否则按权限显示相应的业务员数据
  123. if ($current_permission_role == 1) {
  124. // 管理员可以看到所有业务员
  125. $employee_filter = null;
  126. } elseif ($current_permission_role == 2) {
  127. // 组长可以看到自己和组员
  128. $visible_employees = [];
  129. $query = "SELECT id FROM employee WHERE id = " . intval($current_employee_id) . " OR em_role = " . intval($current_employee_id);
  130. $result = $conn->query($query);
  131. if ($result) {
  132. while ($row = $result->fetch_assoc()) {
  133. $visible_employees[] = intval($row['id']);
  134. }
  135. }
  136. $employee_filter = $visible_employees;
  137. } else {
  138. // 组员只能看到自己
  139. $employee_filter = intval($current_employee_id);
  140. }
  141. }
  142. $total_sales = getRegionTotalSales($conn, $start_date, $end_date, $employee_filter);
  143. echo "<div class='stat-value'>¥" . number_format($total_sales['total_amount'], 2) . "</div>";
  144. echo "<div class='stat-trend " . ($total_sales['growth'] >= 0 ? 'positive' : 'negative') . "'>";
  145. echo ($total_sales['growth'] >= 0 ? '+' : '') . number_format($total_sales['growth'], 2) . "%</div>";
  146. ?>
  147. </div>
  148. <div class="stat-card">
  149. <h3>活跃国家数</h3>
  150. <?php
  151. $active_countries = getActiveCountries($conn, $start_date, $end_date, $employee_filter);
  152. echo "<div class='stat-value'>" . $active_countries['count'] . "</div>";
  153. ?>
  154. </div>
  155. <div class="stat-card">
  156. <h3>平均订单金额</h3>
  157. <?php
  158. $avg_order = getAverageOrderByRegion($conn, $start_date, $end_date, $employee_filter);
  159. echo "<div class='stat-value'>¥" . number_format($avg_order['global_avg'], 2) . "</div>";
  160. ?>
  161. </div>
  162. </div>
  163. </div>
  164. <!-- 热门地区 -->
  165. <div class="chart-container">
  166. <div class="section-title">
  167. <h2>热门地区</h2>
  168. </div>
  169. <?php
  170. $region_orders = getOrdersByRegion($conn, $start_date, $end_date, $employee_filter);
  171. $region_labels = [];
  172. $region_order_counts = [];
  173. $region_quantities = [];
  174. $region_amounts = [];
  175. while ($row = $region_orders->fetch_assoc()) {
  176. $region_labels[] = $row['countryName'];
  177. $region_order_counts[] = $row['order_count'];
  178. $region_quantities[] = $row['total_quantity'];
  179. $region_amounts[] = $row['total_amount'];
  180. }
  181. renderTopRegionsTable($region_labels, $region_order_counts, $region_quantities, $region_amounts);
  182. ?>
  183. </div>
  184. <!-- 客户国家分布 -->
  185. <div class="chart-container">
  186. <div class="section-title">
  187. <h2>客户国家分布</h2>
  188. </div>
  189. <?php
  190. $country_distribution = getCustomerCountryDistribution($conn, $employee_filter);
  191. $country_labels = [];
  192. $country_data = [];
  193. while ($row = $country_distribution->fetch_assoc()) {
  194. $country_labels[] = $row['countryName'];
  195. $country_data[] = $row['customer_count'];
  196. }
  197. renderCustomerCountryDistributionChart($country_labels, $country_data);
  198. ?>
  199. </div>
  200. <!-- 地区销售趋势 -->
  201. <div class="chart-container">
  202. <div class="section-title">
  203. <h2>地区销售趋势</h2>
  204. </div>
  205. <?php
  206. $growth_trends = getRegionGrowthTrends($conn, $start_date, $end_date, $period, $employee_filter);
  207. renderRegionGrowthTrendsChart($growth_trends);
  208. ?>
  209. </div>
  210. <!-- 地区订单分析 -->
  211. <div class="chart-container">
  212. <div class="section-title">
  213. <h2>地区订单分析</h2>
  214. </div>
  215. <?php
  216. renderRegionOrdersChart($region_labels, $region_order_counts, $region_quantities);
  217. ?>
  218. </div>
  219. <!-- 地区平均订单金额分析 -->
  220. <div class="chart-container">
  221. <div class="section-title">
  222. <h2>地区平均订单金额分析</h2>
  223. </div>
  224. <?php
  225. $avg_order_data = getAverageOrderByRegion($conn, $start_date, $end_date, $employee_filter);
  226. renderAverageOrderByRegionChart($avg_order_data['regions']);
  227. ?>
  228. </div>
  229. <!-- 各地区产品类别偏好 -->
  230. <div class="chart-container">
  231. <div class="section-title">
  232. <h2>各地区产品类别偏好</h2>
  233. </div>
  234. <?php
  235. $category_preferences = getRegionCategoryPreferences($conn, $start_date, $end_date, $employee_filter);
  236. renderRegionCategoryPreferencesChart($category_preferences);
  237. ?>
  238. </div>
  239. <!-- 地区销售同比分析 -->
  240. <div class="chart-container">
  241. <div class="section-title">
  242. <h2>地区销售同比分析</h2>
  243. </div>
  244. <?php
  245. $comparison_data = getRegionSalesComparison($conn, $start_date, $end_date, $employee_filter);
  246. renderRegionSalesComparisonTable($comparison_data);
  247. ?>
  248. </div>
  249. <!-- 地区季节性分析 -->
  250. <div class="chart-container">
  251. <div class="section-title">
  252. <h2>地区季节性分析</h2>
  253. </div>
  254. <?php
  255. $seasonal_analysis = getRegionSeasonalAnalysis($conn, $employee_filter);
  256. renderRegionSeasonalAnalysisChart($seasonal_analysis);
  257. ?>
  258. </div>
  259. <!-- 地区销售预测 -->
  260. <div class="chart-container">
  261. <div class="section-title">
  262. <h2>地区销售预测</h2>
  263. </div>
  264. <?php
  265. $forecast_data = getRegionSalesForecast($conn, $start_date, $end_date, $employee_filter);
  266. renderRegionSalesForecastChart($forecast_data);
  267. ?>
  268. </div>
  269. <style>
  270. .positive {
  271. color: #28a745;
  272. font-weight: bold;
  273. }
  274. .negative {
  275. color: #dc3545;
  276. font-weight: bold;
  277. }
  278. .key-metrics-section {
  279. margin-bottom: 30px;
  280. }
  281. .section-title {
  282. margin-bottom: 20px;
  283. border-bottom: 1px solid #eee;
  284. padding-bottom: 10px;
  285. }
  286. .section-title h2 {
  287. font-size: 20px;
  288. margin: 0;
  289. color: #333;
  290. }
  291. .stats-row {
  292. display: flex;
  293. flex-wrap: nowrap;
  294. gap: 20px;
  295. justify-content: space-between;
  296. width: 100%;
  297. margin-bottom: 20px;
  298. }
  299. .stat-card {
  300. background-color: #f8f9fa;
  301. border-radius: 8px;
  302. padding: 20px;
  303. margin-bottom: 0;
  304. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  305. height: 100%;
  306. transition: all 0.3s ease;
  307. flex: 1;
  308. min-width: 0; /* Prevents flex items from overflowing */
  309. }
  310. .stat-card:hover {
  311. transform: translateY(-5px);
  312. box-shadow: 0 5px 15px rgba(0,0,0,0.1);
  313. }
  314. .stat-value {
  315. font-size: 24px;
  316. font-weight: bold;
  317. margin: 10px 0;
  318. }
  319. .stat-trend {
  320. font-size: 16px;
  321. }
  322. .chart-container {
  323. background-color: #fff;
  324. border-radius: 8px;
  325. padding: 20px;
  326. margin-bottom: 30px;
  327. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  328. }
  329. .chart-title {
  330. margin-top: 0;
  331. margin-bottom: 20px;
  332. font-size: 18px;
  333. font-weight: bold;
  334. }
  335. .filter-form {
  336. background-color: #f8f9fa;
  337. border-radius: 8px;
  338. padding: 15px;
  339. margin-bottom: 30px;
  340. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  341. }
  342. .filter-form-inline {
  343. display: flex;
  344. flex-wrap: wrap;
  345. align-items: end;
  346. gap: 15px;
  347. }
  348. .form-group {
  349. margin-bottom: 10px;
  350. }
  351. .data-table {
  352. width: 100%;
  353. border-collapse: collapse;
  354. margin-top: 15px;
  355. }
  356. .data-table th, .data-table td {
  357. padding: 12px 15px;
  358. text-align: left;
  359. border-bottom: 1px solid #ddd;
  360. }
  361. .data-table th {
  362. background-color: #f8f9fa;
  363. font-weight: bold;
  364. color: #333;
  365. }
  366. .data-table tr:hover {
  367. background-color: #f5f5f5;
  368. }
  369. .subchart-container {
  370. margin-bottom: 20px;
  371. }
  372. .subchart-title {
  373. font-size: 16px;
  374. margin-bottom: 15px;
  375. color: #555;
  376. }
  377. .grid-row {
  378. display: flex;
  379. flex-wrap: wrap;
  380. margin: -10px; /* Negative margin to offset the padding in grid-column */
  381. }
  382. .grid-column {
  383. flex: 0 0 50%; /* Two columns per row */
  384. padding: 10px;
  385. box-sizing: border-box;
  386. }
  387. @media (max-width: 768px) {
  388. .grid-column {
  389. flex: 0 0 100%; /* Full width on small screens */
  390. }
  391. .stats-row {
  392. flex-direction: column;
  393. }
  394. }
  395. </style>
  396. <script>
  397. function toggleCustomDates() {
  398. const dateRange = document.getElementById('date_range').value;
  399. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  400. if (dateRange === 'custom') {
  401. customDateInputs.forEach(el => el.style.display = 'inline-block');
  402. } else {
  403. customDateInputs.forEach(el => el.style.display = 'none');
  404. }
  405. }
  406. // 初始化所有图表的配置
  407. Chart.defaults.font.family = "'Arial', sans-serif";
  408. Chart.defaults.color = '#666';
  409. Chart.defaults.plugins.tooltip.backgroundColor = 'rgba(0, 0, 0, 0.8)';
  410. Chart.defaults.plugins.legend.position = 'bottom';
  411. </script>
  412. <?php
  413. // 页面底部
  414. include('statistics_footer.php');
  415. ?>