region_performance_stats.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /**
  3. * 每月新区域客户总业绩统计分析
  4. */
  5. require_once 'conn.php';
  6. require_once 'statistics_utils.php';
  7. require_once 'statistics_customers.php';
  8. require_once 'statistics_sales.php';
  9. // 检查登录状态
  10. if (!isset($_SESSION['employee_id'])) {
  11. checkLogin();
  12. }
  13. function formatCurrency($value) {
  14. return '¥' . number_format($value ?? 0, 2);
  15. }
  16. // 获取当前登录用户信息
  17. $current_employee_id = $_SESSION['employee_id'];
  18. $current_permission_role = 0;
  19. // 获取当前用户权限角色
  20. $current_employee_id = intval($current_employee_id); // 确保是整数
  21. $query = "SELECT em_permission_role_id FROM employee WHERE id = $current_employee_id";
  22. $result = $conn->query($query);
  23. if ($result && $row = $result->fetch_assoc()) {
  24. $current_permission_role = $row['em_permission_role_id'];
  25. }
  26. // 检查是否为导出请求
  27. $is_export = isset($_GET['export']) && $_GET['export'] == 'excel';
  28. // 如果是导出请求但当前用户不是管理员,则拒绝导出
  29. if ($is_export && $current_permission_role != 1) {
  30. // 不允许导出,重定向回当前页面(不带export参数)
  31. $redirect_url = strtok($_SERVER['REQUEST_URI'], '?') . '?' . http_build_query(array_diff_key($_GET, ['export' => '', 'type' => '']));
  32. echo "<script>alert('只有管理员才有权限导出数据'); window.location.href='$redirect_url';</script>";
  33. exit;
  34. }
  35. // 获取日期范围参数
  36. $date_params = getDateRangeParams();
  37. $start_date = $date_params['start_date_sql'];
  38. $end_date = $date_params['end_date_sql'];
  39. $date_range = $date_params['date_range'];
  40. $period = $date_params['period'];
  41. // 如果不是导出操作,则包含页面头部
  42. if (!$is_export) {
  43. include('statistics_header.php');
  44. }
  45. /**
  46. * 获取每月各区域新增客户业绩数据
  47. */
  48. function getMonthlyRegionPerformance($conn, $start_date, $end_date, $employee_filter = null) {
  49. $sql = "SELECT
  50. DATE_FORMAT(o.order_date, '%Y-%m') AS month,
  51. co.countryName AS region,
  52. COUNT(DISTINCT c.id) AS customer_count,
  53. SUM(o.total_amount) AS total_amount
  54. FROM orders o
  55. JOIN customer c ON o.customer_id = c.id
  56. JOIN country co ON c.cs_country = co.id
  57. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  58. AND c.cs_deal = 3
  59. AND c.cs_dealdate BETWEEN '$start_date' AND '$end_date'";
  60. // 根据员工过滤条件添加WHERE子句
  61. if ($employee_filter !== null) {
  62. if (is_array($employee_filter)) {
  63. if (!empty($employee_filter)) {
  64. $employee_ids = implode(',', array_map('intval', $employee_filter));
  65. $sql .= " AND c.cs_belong IN ($employee_ids)";
  66. }
  67. } else {
  68. $sql .= " AND c.cs_belong = " . intval($employee_filter);
  69. }
  70. }
  71. $sql .= " GROUP BY DATE_FORMAT(o.order_date, '%Y-%m'), co.id
  72. ORDER BY month, total_amount DESC";
  73. $result = $conn->query($sql);
  74. $data = [];
  75. if ($result) {
  76. while ($row = $result->fetch_assoc()) {
  77. $data[] = $row;
  78. }
  79. }
  80. return $data;
  81. }
  82. /**
  83. * 获取区域业绩汇总数据
  84. */
  85. function getRegionPerformanceSummary($conn, $start_date, $end_date, $employee_filter = null) {
  86. $sql = "SELECT
  87. co.countryName AS region,
  88. COUNT(DISTINCT c.id) AS customer_count,
  89. SUM(o.total_amount) AS total_amount
  90. FROM orders o
  91. JOIN customer c ON o.customer_id = c.id
  92. JOIN country co ON c.cs_country = co.id
  93. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  94. AND c.cs_deal = 3
  95. AND c.cs_dealdate BETWEEN '$start_date' AND '$end_date'";
  96. // 根据员工过滤条件添加WHERE子句
  97. if ($employee_filter !== null) {
  98. if (is_array($employee_filter)) {
  99. if (!empty($employee_filter)) {
  100. $employee_ids = implode(',', array_map('intval', $employee_filter));
  101. $sql .= " AND c.cs_belong IN ($employee_ids)";
  102. }
  103. } else {
  104. $sql .= " AND c.cs_belong = " . intval($employee_filter);
  105. }
  106. }
  107. $sql .= " GROUP BY co.id
  108. ORDER BY total_amount DESC";
  109. $result = $conn->query($sql);
  110. $data = [];
  111. if ($result) {
  112. while ($row = $result->fetch_assoc()) {
  113. $data[] = $row;
  114. }
  115. }
  116. return $data;
  117. }
  118. /**
  119. * 导出数据为CSV
  120. */
  121. function exportToCSV($data, $columns, $filename) {
  122. // 设置头信息
  123. header('Content-Type: text/csv; charset=utf-8');
  124. header('Content-Disposition: attachment;filename="' . $filename . '.csv"');
  125. header('Cache-Control: max-age=0');
  126. // 创建输出流
  127. $output = fopen('php://output', 'w');
  128. // 添加UTF-8 BOM以确保Excel正确显示中文
  129. fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));
  130. // 输出列头
  131. fputcsv($output, $columns);
  132. // 输出数据行
  133. foreach ($data as $row) {
  134. // 确保所有数据都是数组格式
  135. $rowData = array_values($row);
  136. fputcsv($output, $rowData);
  137. }
  138. fclose($output);
  139. exit;
  140. }
  141. /**
  142. * 渲染每月区域业绩表格
  143. */
  144. function renderMonthlyRegionPerformanceTable($data, $is_export = false) {
  145. if (empty($data)) {
  146. if (!$is_export) {
  147. echo '<div class="alert alert-info">当前选择范围内没有区域业绩数据</div>';
  148. }
  149. return;
  150. }
  151. // 准备数据
  152. $table_data = [];
  153. $total_amount = 0;
  154. $total_customers = 0;
  155. // 按月份分组整理数据
  156. $months_data = [];
  157. foreach ($data as $item) {
  158. $months_data[$item['month']][] = $item;
  159. $total_amount += floatval($item['total_amount']);
  160. $total_customers += intval($item['customer_count']);
  161. }
  162. foreach ($months_data as $month => $regions) {
  163. foreach ($regions as $region) {
  164. $table_data[] = [
  165. '月份' => $month,
  166. '区域' => $region['region'],
  167. '新成交客户数' => $region['customer_count'],
  168. '成交金额' => $is_export ? $region['total_amount'] : formatCurrency($region['total_amount'])
  169. ];
  170. }
  171. }
  172. // 如果是导出请求,则导出数据
  173. if ($is_export) {
  174. exportToCSV(
  175. $table_data,
  176. ['月份', '区域', '新成交客户数', '成交金额'],
  177. '每月区域新客户业绩_' . date('Ymd')
  178. );
  179. return;
  180. }
  181. // 渲染表格
  182. echo '<div class="row mt-5 mb-5">';
  183. echo '<div class="col-md-12">';
  184. echo '<div class="card">';
  185. echo '<div class="card-header d-flex justify-content-between align-items-center">';
  186. echo '<span>每月区域新客户业绩明细 (总计: '.formatCurrency($total_amount).', 共'.$total_customers.'个新客户)</span>';
  187. // 只有管理员才显示导出按钮
  188. if ($GLOBALS['current_permission_role'] == 1) {
  189. echo '<a href="' . $_SERVER['REQUEST_URI'] . '&export=excel&type=monthly" class="btn btn-sm btn-success ml-3">导出CSV</a>';
  190. }
  191. echo '</div>';
  192. echo '<div class="card-body">';
  193. echo '<div class="table-responsive">';
  194. echo '<table class="table table-bordered table-striped">';
  195. echo '<thead class="thead-light">';
  196. echo '<tr>';
  197. echo '<th style="width: 15%; text-align: left;">月份</th>';
  198. echo '<th style="width: 30%; text-align: left;">区域</th>';
  199. echo '<th style="width: 25%; text-align: left;">新成交客户数</th>';
  200. echo '<th style="width: 30%; text-align: left;">成交金额</th>';
  201. echo '</tr>';
  202. echo '</thead>';
  203. echo '<tbody>';
  204. $current_month = '';
  205. foreach ($months_data as $month => $regions) {
  206. foreach ($regions as $index => $region) {
  207. echo '<tr>';
  208. // 只在每个月份的第一行显示月份
  209. if ($index === 0) {
  210. echo '<td rowspan="' . count($regions) . '">' . $month . '</td>';
  211. }
  212. echo '<td>' . $region['region'] . '</td>';
  213. echo '<td>' . $region['customer_count'] . '</td>';
  214. echo '<td>' . formatCurrency($region['total_amount']) . '</td>';
  215. echo '</tr>';
  216. }
  217. }
  218. echo '</tbody>';
  219. echo '</table>';
  220. echo '</div>';
  221. echo '</div>';
  222. echo '</div>';
  223. echo '</div>';
  224. echo '</div>';
  225. }
  226. /**
  227. * 渲染区域业绩汇总表格
  228. */
  229. function renderRegionPerformanceSummaryTable($data, $is_export = false) {
  230. if (empty($data)) {
  231. if (!$is_export) {
  232. echo '<div class="alert alert-info">当前选择范围内没有区域业绩汇总数据</div>';
  233. }
  234. return;
  235. }
  236. // 准备数据
  237. $table_data = [];
  238. $total_amount = 0;
  239. $total_customers = 0;
  240. foreach ($data as $item) {
  241. $table_data[] = [
  242. '区域' => $item['region'],
  243. '新成交客户数' => $item['customer_count'],
  244. '成交金额' => $is_export ? $item['total_amount'] : formatCurrency($item['total_amount'])
  245. ];
  246. $total_amount += floatval($item['total_amount']);
  247. $total_customers += intval($item['customer_count']);
  248. }
  249. // 如果是导出请求,则导出数据
  250. if ($is_export) {
  251. exportToCSV(
  252. $table_data,
  253. ['区域', '新成交客户数', '成交金额'],
  254. '区域新客户业绩汇总_' . date('Ymd')
  255. );
  256. return;
  257. }
  258. // 渲染表格
  259. echo '<div class="row mt-5">';
  260. echo '<div class="col-md-12">';
  261. echo '<div class="card">';
  262. echo '<div class="card-header d-flex justify-content-between align-items-center">';
  263. echo '<span>区域新客户业绩汇总 (总计: '.formatCurrency($total_amount).', 共'.$total_customers.'个新客户)</span>';
  264. // 只有管理员才显示导出按钮
  265. if ($GLOBALS['current_permission_role'] == 1) {
  266. echo '<a href="' . $_SERVER['REQUEST_URI'] . '&export=excel&type=summary" class="btn btn-sm btn-success ml-3">导出CSV</a>';
  267. }
  268. echo '</div>';
  269. echo '<div class="card-body">';
  270. echo '<div class="table-responsive">';
  271. echo '<table class="table table-bordered table-striped">';
  272. echo '<thead class="thead-light">';
  273. echo '<tr>';
  274. echo '<th style="width: 40%; text-align: left;">区域</th>';
  275. echo '<th style="width: 30%; text-align: left;">新成交客户数</th>';
  276. echo '<th style="width: 30%; text-align: left;">成交金额</th>';
  277. echo '</tr>';
  278. echo '</thead>';
  279. echo '<tbody>';
  280. foreach ($data as $item) {
  281. echo '<tr>';
  282. echo '<td>' . $item['region'] . '</td>';
  283. echo '<td>' . $item['customer_count'] . '</td>';
  284. echo '<td>' . formatCurrency($item['total_amount']) . '</td>';
  285. echo '</tr>';
  286. }
  287. echo '</tbody>';
  288. echo '</table>';
  289. echo '</div>';
  290. echo '</div>';
  291. echo '</div>';
  292. echo '</div>';
  293. echo '</div>';
  294. }
  295. // 获取选择的业务员
  296. $selected_employee = isset($_GET['selected_employee']) ? $_GET['selected_employee'] : 'all';
  297. // 确定要显示哪些业务员的数据
  298. $employee_filter = null;
  299. if ($selected_employee != 'all') {
  300. // 如果选择了特定业务员,则只显示该业务员的数据
  301. $employee_filter = intval($selected_employee);
  302. } else {
  303. // 否则按权限显示相应的业务员数据
  304. if ($current_permission_role == 1) {
  305. // 管理员可以看到所有业务员
  306. $employee_filter = null;
  307. } elseif ($current_permission_role == 2) {
  308. // 组长可以看到自己和组员
  309. $visible_employees = [];
  310. $query = "SELECT id FROM employee WHERE id = " . intval($current_employee_id) . " OR em_role = " . intval($current_employee_id);
  311. $result = $conn->query($query);
  312. if ($result) {
  313. while ($row = $result->fetch_assoc()) {
  314. $visible_employees[] = $row['id'];
  315. }
  316. }
  317. $employee_filter = $visible_employees;
  318. } else {
  319. // 组员只能看到自己
  320. $employee_filter = intval($current_employee_id);
  321. }
  322. }
  323. // 获取每月区域业绩数据
  324. $monthly_region_performance = getMonthlyRegionPerformance($conn, $start_date, $end_date, $employee_filter);
  325. // 获取区域业绩汇总数据
  326. $region_performance_summary = getRegionPerformanceSummary($conn, $start_date, $end_date, $employee_filter);
  327. // 处理导出请求
  328. if ($is_export) {
  329. $export_type = isset($_GET['type']) ? $_GET['type'] : '';
  330. switch ($export_type) {
  331. case 'monthly':
  332. renderMonthlyRegionPerformanceTable($monthly_region_performance, true);
  333. break;
  334. case 'summary':
  335. renderRegionPerformanceSummaryTable($region_performance_summary, true);
  336. break;
  337. }
  338. exit; // 确保导出后停止执行
  339. }
  340. ?>
  341. <div class="container">
  342. <div class="page-header">
  343. <h1 class="page-title">每月区域新客户总业绩统计</h1>
  344. </div>
  345. <!-- 日期筛选 -->
  346. <div class="filter-form mb-5">
  347. <form method="get" class="filter-form-inline">
  348. <div class="form-group mr-3">
  349. <label for="date_range" class="mr-2">选择日期范围</label>
  350. <select class="form-control" id="date_range" name="date_range" onchange="toggleCustomDates()">
  351. <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
  352. <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
  353. <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
  354. <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>最近30天</option>
  355. <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>最近90天</option>
  356. <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
  357. </select>
  358. </div>
  359. <div class="form-group custom-date-inputs mr-3" id="custom_start_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  360. <label for="start_date" class="mr-2">开始日期</label>
  361. <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
  362. </div>
  363. <div class="form-group custom-date-inputs mr-3" id="custom_end_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  364. <label for="end_date" class="mr-2">结束日期</label>
  365. <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
  366. </div>
  367. <!-- 业务员选择 -->
  368. <div class="form-group mr-3">
  369. <label for="selected_employee" class="mr-2">选择业务员</label>
  370. <select class="form-control" id="selected_employee" name="selected_employee">
  371. <option value="all">所有业务员</option>
  372. <?php
  373. // 获取当前用户可见的业务员列表
  374. $visible_employees_query = "";
  375. if ($current_permission_role == 1) {
  376. // 管理员可以看到所有业务员
  377. $visible_employees_query = "SELECT id, em_user FROM employee WHERE em_role IS NOT NULL ORDER BY em_user";
  378. } elseif ($current_permission_role == 2) {
  379. // 组长可以看到自己和组员
  380. $visible_employees_query = "SELECT id, em_user FROM employee WHERE id = $current_employee_id OR em_role = $current_employee_id ORDER BY em_user";
  381. } else {
  382. // 组员只能看到自己
  383. $visible_employees_query = "SELECT id, em_user FROM employee WHERE id = $current_employee_id";
  384. }
  385. $visible_employees_result = $conn->query($visible_employees_query);
  386. $selected_employee = isset($_GET['selected_employee']) ? $_GET['selected_employee'] : 'all';
  387. while ($emp = $visible_employees_result->fetch_assoc()) {
  388. $selected = ($selected_employee == $emp['id']) ? 'selected' : '';
  389. echo "<option value='".$emp['id']."' $selected>".$emp['em_user']."</option>";
  390. }
  391. ?>
  392. </select>
  393. </div>
  394. <div class="form-group">
  395. <button type="submit" class="btn btn-primary">应用筛选</button>
  396. </div>
  397. </form>
  398. </div>
  399. <!-- 统计内容区域 -->
  400. <div class="stats-content">
  401. <?php
  402. // 渲染表格
  403. renderRegionPerformanceSummaryTable($region_performance_summary);
  404. renderMonthlyRegionPerformanceTable($monthly_region_performance);
  405. ?>
  406. </div>
  407. </div>
  408. <style>
  409. /* 添加一些额外的样式以改善表格显示 */
  410. .filter-form {
  411. background-color: #f8f9fa;
  412. padding: 20px;
  413. border-radius: 5px;
  414. margin-bottom: 30px;
  415. }
  416. .filter-form-inline {
  417. display: flex;
  418. flex-wrap: wrap;
  419. align-items: flex-end;
  420. }
  421. .filter-form .form-group {
  422. margin-bottom: 10px;
  423. margin-right: 15px;
  424. }
  425. /* 表格样式 */
  426. .table {
  427. width: 100%;
  428. margin-bottom: 1rem;
  429. }
  430. .table th, .table td {
  431. padding: 12px;
  432. vertical-align: middle;
  433. }
  434. /* 确保表头也是左对齐 */
  435. .thead-light th {
  436. background-color: #f8f9fa;
  437. border-color: #dee2e6;
  438. text-align: left;
  439. }
  440. .table-striped tbody tr:nth-of-type(odd) {
  441. background-color: rgba(0, 0, 0, 0.03);
  442. }
  443. .card {
  444. box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
  445. }
  446. .card-header {
  447. background-color: #f8f9fa;
  448. padding: 15px;
  449. }
  450. .card-body {
  451. padding: 1.5rem;
  452. }
  453. /* 添加按钮左边距 */
  454. .ml-3 {
  455. margin-left: 15px !important;
  456. }
  457. /* 确保卡片头部布局正确 */
  458. .card-header.d-flex {
  459. display: flex !important;
  460. justify-content: space-between !important;
  461. align-items: center !important;
  462. }
  463. </style>
  464. <script>
  465. function toggleCustomDates() {
  466. const dateRange = document.getElementById('date_range').value;
  467. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  468. if (dateRange === 'custom') {
  469. customDateInputs.forEach(el => el.style.display = 'inline-block');
  470. } else {
  471. customDateInputs.forEach(el => el.style.display = 'none');
  472. }
  473. }
  474. </script>
  475. <?php
  476. // 页面底部
  477. include('statistics_footer.php');
  478. ?>