region_stats.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. // 修改getRegionTotalSales和getActiveCountries函数调用,传入国家筛选条件
  143. $total_sales = getRegionTotalSales($conn, $start_date, $end_date, $employee_filter, $country_filter);
  144. echo "<div class='stat-value'>¥" . number_format($total_sales['total_amount'], 2) . "</div>";
  145. echo "<div class='stat-trend " . ($total_sales['growth'] >= 0 ? 'positive' : 'negative') . "'>";
  146. echo ($total_sales['growth'] >= 0 ? '+' : '') . number_format($total_sales['growth'], 2) . "%</div>";
  147. ?>
  148. </div>
  149. <div class="stat-card">
  150. <h3>活跃国家数</h3>
  151. <?php
  152. $active_countries = getActiveCountries($conn, $start_date, $end_date, $employee_filter, $country_filter);
  153. echo "<div class='stat-value'>" . $active_countries['count'] . "</div>";
  154. ?>
  155. </div>
  156. </div>
  157. </div>
  158. <!-- 热门地区 -->
  159. <div class="chart-container">
  160. <div class="section-title">
  161. <h2><?php echo $country_filter > 0 ? '地区数据详情' : '热门地区 TOP-10'; ?></h2>
  162. </div>
  163. <?php
  164. $region_orders = getOrdersByRegion($conn, $start_date, $end_date, $employee_filter, $country_filter);
  165. $region_labels = [];
  166. $region_order_counts = [];
  167. $region_amounts = [];
  168. while ($row = $region_orders->fetch_assoc()) {
  169. $region_labels[] = $row['countryName'];
  170. $region_order_counts[] = $row['order_count'];
  171. $region_amounts[] = $row['total_amount'];
  172. }
  173. renderTopRegionsTable($region_labels, $region_order_counts, $region_amounts);
  174. ?>
  175. </div>
  176. <!-- 客户国家分布 -->
  177. <div class="chart-container">
  178. <div class="section-title">
  179. <h2><?php echo $country_filter > 0 ? '客户国家分布' : '客户国家分布 TOP-10'; ?></h2>
  180. </div>
  181. <?php
  182. $country_distribution = getCustomerCountryDistribution($conn, $employee_filter);
  183. $country_labels = [];
  184. $country_data = [];
  185. while ($row = $country_distribution->fetch_assoc()) {
  186. $country_labels[] = $row['countryName'];
  187. $country_data[] = $row['customer_count'];
  188. }
  189. renderCustomerCountryDistributionChart($country_labels, $country_data);
  190. ?>
  191. </div>
  192. <!-- 地区销售趋势 -->
  193. <div class="chart-container">
  194. <div class="section-title">
  195. <h2><?php echo $country_filter > 0 ? '地区销售趋势' : '地区销售趋势 TOP-10'; ?></h2>
  196. </div>
  197. <?php
  198. $growth_trends = getRegionGrowthTrends($conn, $start_date, $end_date, $period, $employee_filter, $country_filter);
  199. renderRegionGrowthTrendsChart($growth_trends);
  200. ?>
  201. </div>
  202. <!-- 地区订单分析 -->
  203. <div class="chart-container">
  204. <div class="section-title">
  205. <h2><?php echo $country_filter > 0 ? '地区订单分析' : '地区订单分析 TOP-10'; ?></h2>
  206. </div>
  207. <?php
  208. renderRegionOrdersChart($region_labels, $region_order_counts);
  209. ?>
  210. </div>
  211. <!-- 地区销售同比分析 -->
  212. <div class="chart-container">
  213. <div class="section-title">
  214. <h2><?php echo $country_filter > 0 ? '地区销售同比分析' : '地区销售同比分析 TOP-10'; ?></h2>
  215. </div>
  216. <?php
  217. $comparison_data = getRegionSalesComparison($conn, $start_date, $end_date, $employee_filter, $country_filter);
  218. renderRegionSalesComparisonTable($comparison_data);
  219. ?>
  220. </div>
  221. <!-- 地区季节性分析 -->
  222. <div class="chart-container">
  223. <div class="section-title">
  224. <h2><?php echo $country_filter > 0 ? '地区季节性分析' : '地区季节性分析 TOP-10'; ?></h2>
  225. </div>
  226. <?php
  227. $seasonal_analysis = getRegionSeasonalAnalysis($conn, $employee_filter, $country_filter);
  228. renderRegionSeasonalAnalysisChart($seasonal_analysis);
  229. ?>
  230. </div>
  231. <!-- 地区销售预测 -->
  232. <div class="chart-container">
  233. <div class="section-title">
  234. <h2><?php echo $country_filter > 0 ? '地区销售预测 (未来6个月)' : '地区销售预测 (未来6个月) TOP-10'; ?></h2>
  235. </div>
  236. <?php
  237. $forecast_data = getRegionSalesForecast($conn, $start_date, $end_date, $employee_filter, $country_filter);
  238. renderRegionSalesForecastChart($forecast_data);
  239. ?>
  240. </div>
  241. <style>
  242. .positive {
  243. color: #28a745;
  244. font-weight: bold;
  245. }
  246. .negative {
  247. color: #dc3545;
  248. font-weight: bold;
  249. }
  250. .key-metrics-section {
  251. margin-bottom: 30px;
  252. }
  253. .section-title {
  254. margin-bottom: 20px;
  255. border-bottom: 1px solid #eee;
  256. padding-bottom: 10px;
  257. }
  258. .section-title h2 {
  259. font-size: 20px;
  260. margin: 0;
  261. color: #333;
  262. }
  263. .stats-row {
  264. display: flex;
  265. flex-wrap: nowrap;
  266. gap: 20px;
  267. justify-content: space-between;
  268. width: 100%;
  269. margin-bottom: 20px;
  270. }
  271. .stat-card {
  272. background-color: #f8f9fa;
  273. border-radius: 8px;
  274. padding: 20px;
  275. margin-bottom: 0;
  276. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  277. height: 100%;
  278. transition: all 0.3s ease;
  279. flex: 1;
  280. min-width: 0; /* Prevents flex items from overflowing */
  281. }
  282. .stat-card:hover {
  283. transform: translateY(-5px);
  284. box-shadow: 0 5px 15px rgba(0,0,0,0.1);
  285. }
  286. .stat-value {
  287. font-size: 24px;
  288. font-weight: bold;
  289. margin: 10px 0;
  290. }
  291. .stat-trend {
  292. font-size: 16px;
  293. }
  294. .chart-container {
  295. background-color: #fff;
  296. border-radius: 8px;
  297. padding: 20px;
  298. margin-bottom: 30px;
  299. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  300. }
  301. .chart-title {
  302. margin-top: 0;
  303. margin-bottom: 20px;
  304. font-size: 18px;
  305. font-weight: bold;
  306. }
  307. .filter-form {
  308. background-color: #f8f9fa;
  309. border-radius: 8px;
  310. padding: 15px;
  311. margin-bottom: 30px;
  312. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  313. }
  314. .filter-form-inline {
  315. display: flex;
  316. flex-wrap: wrap;
  317. align-items: end;
  318. gap: 15px;
  319. }
  320. .form-group {
  321. margin-bottom: 10px;
  322. }
  323. .data-table {
  324. width: 100%;
  325. border-collapse: collapse;
  326. margin-top: 15px;
  327. }
  328. .data-table th, .data-table td {
  329. padding: 12px 15px;
  330. text-align: left;
  331. border-bottom: 1px solid #ddd;
  332. }
  333. .data-table th {
  334. background-color: #f8f9fa;
  335. font-weight: bold;
  336. color: #333;
  337. }
  338. .data-table tr:hover {
  339. background-color: #f5f5f5;
  340. }
  341. .subchart-container {
  342. margin-bottom: 20px;
  343. }
  344. .subchart-title {
  345. font-size: 16px;
  346. margin-bottom: 15px;
  347. color: #555;
  348. }
  349. .grid-row {
  350. display: flex;
  351. flex-wrap: wrap;
  352. margin: -10px; /* Negative margin to offset the padding in grid-column */
  353. }
  354. .grid-column {
  355. flex: 0 0 50%; /* Two columns per row */
  356. padding: 10px;
  357. box-sizing: border-box;
  358. }
  359. @media (max-width: 768px) {
  360. .grid-column {
  361. flex: 0 0 100%; /* Full width on small screens */
  362. }
  363. .stats-row {
  364. flex-direction: column;
  365. }
  366. }
  367. </style>
  368. <script>
  369. function toggleCustomDates() {
  370. const dateRange = document.getElementById('date_range').value;
  371. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  372. if (dateRange === 'custom') {
  373. customDateInputs.forEach(el => el.style.display = 'inline-block');
  374. } else {
  375. customDateInputs.forEach(el => el.style.display = 'none');
  376. }
  377. }
  378. // 初始化所有图表的配置
  379. Chart.defaults.font.family = "'Arial', sans-serif";
  380. Chart.defaults.color = '#666';
  381. Chart.defaults.plugins.tooltip.backgroundColor = 'rgba(0, 0, 0, 0.8)';
  382. Chart.defaults.plugins.legend.position = 'bottom';
  383. </script>
  384. <?php
  385. // 页面底部
  386. include('statistics_footer.php');
  387. ?>