region_performance_stats.php 18 KB

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