customer_composition_stats.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. function formatPercentage($value, $total) {
  17. if ($total == 0) return '0%';
  18. return round(($value / $total) * 100, 2) . '%';
  19. }
  20. // 获取当前登录用户信息
  21. $current_employee_id = $_SESSION['employee_id'];
  22. $current_permission_role = 0;
  23. // 获取当前用户权限角色
  24. $current_employee_id = intval($current_employee_id); // 确保是整数
  25. $query = "SELECT em_permission_role_id FROM employee WHERE id = $current_employee_id";
  26. $result = $conn->query($query);
  27. if ($result && $row = $result->fetch_assoc()) {
  28. $current_permission_role = $row['em_permission_role_id'];
  29. }
  30. // 检查是否为导出请求
  31. $is_export = isset($_GET['export']) && $_GET['export'] == 'excel';
  32. // 如果是导出请求但当前用户不是管理员,则拒绝导出
  33. if ($is_export && $current_permission_role != 1) {
  34. // 不允许导出,重定向回当前页面(不带export参数)
  35. $redirect_url = strtok($_SERVER['REQUEST_URI'], '?') . '?' . http_build_query(array_diff_key($_GET, ['export' => '', 'type' => '']));
  36. echo "<script>alert('只有管理员才有权限导出数据'); window.location.href='$redirect_url';</script>";
  37. exit;
  38. }
  39. // 获取日期范围参数
  40. $date_params = getDateRangeParams();
  41. $start_date = $date_params['start_date_sql'];
  42. $end_date = $date_params['end_date_sql'];
  43. $date_range = $date_params['date_range'];
  44. $period = $date_params['period'];
  45. // 如果不是导出操作,则包含页面头部
  46. if (!$is_export) {
  47. include('statistics_header.php');
  48. }
  49. /**
  50. * 获取业务员业绩的客户构成
  51. */
  52. function getCustomerCompositionStats($conn, $start_date, $end_date, $employee_filter = null) {
  53. global $current_permission_role;
  54. // 查询业务员的新客户业绩(客户编码以0开头的)
  55. $new_customer_sql = "SELECT
  56. e.id AS employee_id,
  57. e.em_user AS employee_name,
  58. COUNT(DISTINCT c.id) AS customer_count,
  59. SUM(o.total_amount) AS total_amount
  60. FROM orders o
  61. JOIN customer c ON o.customer_id = c.id
  62. JOIN employee e ON c.cs_belong = e.id
  63. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  64. AND c.cs_deal = 3
  65. AND c.cs_code LIKE '%.0%'";
  66. // 查询业务员的老客户业绩(客户编码以3开头且不包含斜杠的)
  67. $old_customer_sql = "SELECT
  68. e.id AS employee_id,
  69. e.em_user AS employee_name,
  70. COUNT(DISTINCT c.id) AS customer_count,
  71. SUM(o.total_amount) AS total_amount
  72. FROM orders o
  73. JOIN customer c ON o.customer_id = c.id
  74. JOIN employee e ON c.cs_belong = e.id
  75. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  76. AND c.cs_deal = 3
  77. AND c.cs_code LIKE '%.3%'
  78. AND c.cs_code NOT LIKE '%/%'";
  79. // 查询业务员的分配客户业绩(客户编码包含斜杠的)
  80. $assigned_customer_sql = "SELECT
  81. e.id AS employee_id,
  82. e.em_user AS employee_name,
  83. COUNT(DISTINCT c.id) AS customer_count,
  84. SUM(o.total_amount) AS total_amount
  85. FROM orders o
  86. JOIN customer c ON o.customer_id = c.id
  87. JOIN employee e ON c.cs_belong = e.id
  88. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  89. AND c.cs_deal = 3
  90. AND c.cs_code LIKE '%/%'";
  91. // 查询业务员的总业绩
  92. $total_sql = "SELECT
  93. e.id AS employee_id,
  94. e.em_user AS employee_name,
  95. COUNT(DISTINCT c.id) AS customer_count,
  96. SUM(o.total_amount) AS total_amount
  97. FROM orders o
  98. JOIN customer c ON o.customer_id = c.id
  99. JOIN employee e ON c.cs_belong = e.id
  100. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  101. AND c.cs_deal = 3";
  102. // 根据员工过滤条件添加WHERE子句
  103. if ($employee_filter !== null) {
  104. if (is_array($employee_filter)) {
  105. if (!empty($employee_filter)) {
  106. $employee_ids = implode(',', array_map('intval', $employee_filter));
  107. $new_customer_sql .= " AND c.cs_belong IN ($employee_ids)";
  108. $old_customer_sql .= " AND c.cs_belong IN ($employee_ids)";
  109. $assigned_customer_sql .= " AND c.cs_belong IN ($employee_ids)";
  110. $total_sql .= " AND c.cs_belong IN ($employee_ids)";
  111. }
  112. } else {
  113. $employee_id = intval($employee_filter);
  114. $new_customer_sql .= " AND c.cs_belong = $employee_id";
  115. $old_customer_sql .= " AND c.cs_belong = $employee_id";
  116. $assigned_customer_sql .= " AND c.cs_belong = $employee_id";
  117. $total_sql .= " AND c.cs_belong = $employee_id";
  118. }
  119. }
  120. $new_customer_sql .= " GROUP BY e.id ORDER BY total_amount DESC";
  121. $old_customer_sql .= " GROUP BY e.id ORDER BY total_amount DESC";
  122. $assigned_customer_sql .= " GROUP BY e.id ORDER BY total_amount DESC";
  123. $total_sql .= " GROUP BY e.id ORDER BY total_amount DESC";
  124. $new_result = $conn->query($new_customer_sql);
  125. $old_result = $conn->query($old_customer_sql);
  126. $assigned_result = $conn->query($assigned_customer_sql);
  127. $total_result = $conn->query($total_sql);
  128. $new_data = [];
  129. $old_data = [];
  130. $assigned_data = [];
  131. $total_data = [];
  132. if ($new_result) {
  133. while ($row = $new_result->fetch_assoc()) {
  134. $new_data[$row['employee_id']] = $row;
  135. }
  136. }
  137. if ($old_result) {
  138. while ($row = $old_result->fetch_assoc()) {
  139. $old_data[$row['employee_id']] = $row;
  140. }
  141. }
  142. if ($assigned_result) {
  143. while ($row = $assigned_result->fetch_assoc()) {
  144. $assigned_data[$row['employee_id']] = $row;
  145. }
  146. }
  147. if ($total_result) {
  148. while ($row = $total_result->fetch_assoc()) {
  149. $total_data[$row['employee_id']] = $row;
  150. }
  151. }
  152. // 合并数据
  153. $combined_data = [];
  154. foreach ($total_data as $employee_id => $total) {
  155. $combined_data[$employee_id] = [
  156. 'employee_id' => $employee_id,
  157. 'employee_name' => $total['employee_name'],
  158. 'total_customer_count' => $total['customer_count'],
  159. 'total_amount' => $total['total_amount'],
  160. 'new_customer_count' => $new_data[$employee_id]['customer_count'] ?? 0,
  161. 'new_customer_amount' => $new_data[$employee_id]['total_amount'] ?? 0,
  162. 'old_customer_count' => $old_data[$employee_id]['customer_count'] ?? 0,
  163. 'old_customer_amount' => $old_data[$employee_id]['total_amount'] ?? 0,
  164. 'assigned_customer_count' => $assigned_data[$employee_id]['customer_count'] ?? 0,
  165. 'assigned_customer_amount' => $assigned_data[$employee_id]['total_amount'] ?? 0
  166. ];
  167. }
  168. // 按总业绩降序排序
  169. usort($combined_data, function($a, $b) {
  170. return $b['total_amount'] - $a['total_amount'];
  171. });
  172. return $combined_data;
  173. }
  174. /**
  175. * 获取按月统计的客户构成业绩
  176. */
  177. function getMonthlyCustomerComposition($conn, $start_date, $end_date, $employee_filter = null) {
  178. global $current_permission_role;
  179. // 查询每月新客户业绩(客户编码以0开头的)
  180. $new_customer_sql = "SELECT
  181. DATE_FORMAT(o.order_date, '%Y-%m') AS month,
  182. COUNT(DISTINCT c.id) AS customer_count,
  183. SUM(o.total_amount) AS total_amount
  184. FROM orders o
  185. JOIN customer c ON o.customer_id = c.id
  186. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  187. AND c.cs_deal = 3
  188. AND c.cs_code LIKE '%.0%'";
  189. // 查询每月老客户业绩(客户编码以3开头且不包含斜杠的)
  190. $old_customer_sql = "SELECT
  191. DATE_FORMAT(o.order_date, '%Y-%m') AS month,
  192. COUNT(DISTINCT c.id) AS customer_count,
  193. SUM(o.total_amount) AS total_amount
  194. FROM orders o
  195. JOIN customer c ON o.customer_id = c.id
  196. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  197. AND c.cs_deal = 3
  198. AND c.cs_code LIKE '%.3%'
  199. AND c.cs_code NOT LIKE '%/%'";
  200. // 查询每月分配客户业绩(客户编码包含斜杠的)
  201. $assigned_customer_sql = "SELECT
  202. DATE_FORMAT(o.order_date, '%Y-%m') AS month,
  203. COUNT(DISTINCT c.id) AS customer_count,
  204. SUM(o.total_amount) AS total_amount
  205. FROM orders o
  206. JOIN customer c ON o.customer_id = c.id
  207. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  208. AND c.cs_deal = 3
  209. AND c.cs_code LIKE '%/%'";
  210. // 查询每月总业绩
  211. $total_sql = "SELECT
  212. DATE_FORMAT(o.order_date, '%Y-%m') AS month,
  213. COUNT(DISTINCT c.id) AS customer_count,
  214. SUM(o.total_amount) AS total_amount
  215. FROM orders o
  216. JOIN customer c ON o.customer_id = c.id
  217. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  218. AND c.cs_deal = 3";
  219. // 根据员工过滤条件添加WHERE子句
  220. if ($employee_filter !== null) {
  221. if (is_array($employee_filter)) {
  222. if (!empty($employee_filter)) {
  223. $employee_ids = implode(',', array_map('intval', $employee_filter));
  224. $new_customer_sql .= " AND c.cs_belong IN ($employee_ids)";
  225. $old_customer_sql .= " AND c.cs_belong IN ($employee_ids)";
  226. $assigned_customer_sql .= " AND c.cs_belong IN ($employee_ids)";
  227. $total_sql .= " AND c.cs_belong IN ($employee_ids)";
  228. }
  229. } else {
  230. $employee_id = intval($employee_filter);
  231. $new_customer_sql .= " AND c.cs_belong = $employee_id";
  232. $old_customer_sql .= " AND c.cs_belong = $employee_id";
  233. $assigned_customer_sql .= " AND c.cs_belong = $employee_id";
  234. $total_sql .= " AND c.cs_belong = $employee_id";
  235. }
  236. }
  237. $new_customer_sql .= " GROUP BY DATE_FORMAT(o.order_date, '%Y-%m') ORDER BY month";
  238. $old_customer_sql .= " GROUP BY DATE_FORMAT(o.order_date, '%Y-%m') ORDER BY month";
  239. $assigned_customer_sql .= " GROUP BY DATE_FORMAT(o.order_date, '%Y-%m') ORDER BY month";
  240. $total_sql .= " GROUP BY DATE_FORMAT(o.order_date, '%Y-%m') ORDER BY month";
  241. $new_result = $conn->query($new_customer_sql);
  242. $old_result = $conn->query($old_customer_sql);
  243. $assigned_result = $conn->query($assigned_customer_sql);
  244. $total_result = $conn->query($total_sql);
  245. $new_data = [];
  246. $old_data = [];
  247. $assigned_data = [];
  248. $total_data = [];
  249. if ($new_result) {
  250. while ($row = $new_result->fetch_assoc()) {
  251. $new_data[$row['month']] = $row;
  252. }
  253. }
  254. if ($old_result) {
  255. while ($row = $old_result->fetch_assoc()) {
  256. $old_data[$row['month']] = $row;
  257. }
  258. }
  259. if ($assigned_result) {
  260. while ($row = $assigned_result->fetch_assoc()) {
  261. $assigned_data[$row['month']] = $row;
  262. }
  263. }
  264. if ($total_result) {
  265. while ($row = $total_result->fetch_assoc()) {
  266. $total_data[$row['month']] = $row;
  267. }
  268. }
  269. // 合并数据
  270. $combined_data = [];
  271. foreach ($total_data as $month => $total) {
  272. $combined_data[$month] = [
  273. 'month' => $month,
  274. 'total_customer_count' => $total['customer_count'],
  275. 'total_amount' => $total['total_amount'],
  276. 'new_customer_count' => $new_data[$month]['customer_count'] ?? 0,
  277. 'new_customer_amount' => $new_data[$month]['total_amount'] ?? 0,
  278. 'old_customer_count' => $old_data[$month]['customer_count'] ?? 0,
  279. 'old_customer_amount' => $old_data[$month]['total_amount'] ?? 0,
  280. 'assigned_customer_count' => $assigned_data[$month]['customer_count'] ?? 0,
  281. 'assigned_customer_amount' => $assigned_data[$month]['total_amount'] ?? 0
  282. ];
  283. }
  284. // 按月份排序
  285. ksort($combined_data);
  286. return array_values($combined_data);
  287. }
  288. /**
  289. * 导出数据为CSV
  290. */
  291. function exportToCSV($data, $columns, $filename) {
  292. global $current_permission_role;
  293. // 设置头信息
  294. header('Content-Type: text/csv; charset=utf-8');
  295. header('Content-Disposition: attachment;filename="' . $filename . '.csv"');
  296. header('Cache-Control: max-age=0');
  297. // 创建输出流
  298. $output = fopen('php://output', 'w');
  299. // 添加UTF-8 BOM以确保Excel正确显示中文
  300. fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));
  301. // 输出列头
  302. fputcsv($output, $columns);
  303. // 输出数据行
  304. foreach ($data as $row) {
  305. // 确保所有数据都是数组格式
  306. $rowData = array_values($row);
  307. fputcsv($output, $rowData);
  308. }
  309. fclose($output);
  310. exit;
  311. }
  312. /**
  313. * 渲染业务员客户构成统计表格
  314. */
  315. function renderCustomerCompositionTable($data, $is_export = false) {
  316. if (empty($data)) {
  317. if (!$is_export) {
  318. echo '<div class="alert alert-info">当前选择范围内没有业绩数据</div>';
  319. }
  320. return;
  321. }
  322. // 准备数据
  323. $table_data = [];
  324. foreach ($data as $item) {
  325. $table_data[] = [
  326. '业务员' => $item['employee_name'],
  327. '总业绩' => $is_export ? $item['total_amount'] : formatCurrency($item['total_amount']),
  328. '新客户业绩' => $is_export ? $item['new_customer_amount'] : formatCurrency($item['new_customer_amount']),
  329. '新客户占比' => $is_export ? ($item['total_amount'] > 0 ? ($item['new_customer_amount'] / $item['total_amount']) * 100 : 0) : formatPercentage($item['new_customer_amount'], $item['total_amount']),
  330. '老客户业绩' => $is_export ? $item['old_customer_amount'] : formatCurrency($item['old_customer_amount']),
  331. '老客户占比' => $is_export ? ($item['total_amount'] > 0 ? ($item['old_customer_amount'] / $item['total_amount']) * 100 : 0) : formatPercentage($item['old_customer_amount'], $item['total_amount']),
  332. '分配客户业绩' => $is_export ? $item['assigned_customer_amount'] : formatCurrency($item['assigned_customer_amount']),
  333. '分配客户占比' => $is_export ? ($item['total_amount'] > 0 ? ($item['assigned_customer_amount'] / $item['total_amount']) * 100 : 0) : formatPercentage($item['assigned_customer_amount'], $item['total_amount'])
  334. ];
  335. }
  336. // 如果是导出请求,则导出数据
  337. if ($is_export) {
  338. exportToCSV(
  339. $table_data,
  340. ['业务员', '总业绩', '新客户业绩', '新客户占比(%)', '老客户业绩', '老客户占比(%)', '分配客户业绩', '分配客户占比(%)'],
  341. '业务员客户构成统计_' . date('Ymd')
  342. );
  343. return;
  344. }
  345. // 渲染表格
  346. echo '<div class="row mt-5 mb-5">';
  347. echo '<div class="col-md-12">';
  348. echo '<div class="card">';
  349. echo '<div class="card-header d-flex justify-content-between align-items-center">';
  350. echo '<span>业务员客户构成统计</span>';
  351. // 只有管理员才显示导出按钮
  352. if ($GLOBALS['current_permission_role'] == 1) {
  353. echo '<a href="' . $_SERVER['REQUEST_URI'] . '&export=excel&type=employee" class="btn btn-sm btn-success ml-3">导出CSV</a>';
  354. }
  355. echo '</div>';
  356. echo '<div class="card-body">';
  357. echo '<div class="table-responsive">';
  358. echo '<table class="table table-bordered table-striped">';
  359. echo '<thead class="thead-light">';
  360. echo '<tr>';
  361. echo '<th style="width: 12%; text-align: left;">业务员</th>';
  362. echo '<th style="width: 12%; text-align: left;">总业绩</th>';
  363. echo '<th style="width: 12%; text-align: left;">新客户业绩</th>';
  364. echo '<th style="width: 12%; text-align: left;">新客户占比</th>';
  365. echo '<th style="width: 12%; text-align: left;">老客户业绩</th>';
  366. echo '<th style="width: 12%; text-align: left;">老客户占比</th>';
  367. echo '<th style="width: 12%; text-align: left;">分配客户业绩</th>';
  368. echo '<th style="width: 12%; text-align: left;">分配客户占比</th>';
  369. echo '</tr>';
  370. echo '</thead>';
  371. echo '<tbody>';
  372. foreach ($data as $item) {
  373. echo '<tr>';
  374. echo '<td>' . $item['employee_name'] . '</td>';
  375. echo '<td>' . formatCurrency($item['total_amount']) . '</td>';
  376. echo '<td>' . formatCurrency($item['new_customer_amount']) . '</td>';
  377. echo '<td>' . formatPercentage($item['new_customer_amount'], $item['total_amount']) . '</td>';
  378. echo '<td>' . formatCurrency($item['old_customer_amount']) . '</td>';
  379. echo '<td>' . formatPercentage($item['old_customer_amount'], $item['total_amount']) . '</td>';
  380. echo '<td>' . formatCurrency($item['assigned_customer_amount']) . '</td>';
  381. echo '<td>' . formatPercentage($item['assigned_customer_amount'], $item['total_amount']) . '</td>';
  382. echo '</tr>';
  383. }
  384. echo '</tbody>';
  385. echo '</table>';
  386. echo '</div>';
  387. echo '</div>';
  388. echo '</div>';
  389. echo '</div>';
  390. echo '</div>';
  391. }
  392. /**
  393. * 渲染每月客户构成统计表格
  394. */
  395. function renderMonthlyCustomerCompositionTable($data, $is_export = false) {
  396. if (empty($data)) {
  397. if (!$is_export) {
  398. echo '<div class="alert alert-info">当前选择范围内没有月度业绩数据</div>';
  399. }
  400. return;
  401. }
  402. // 准备数据
  403. $table_data = [];
  404. foreach ($data as $item) {
  405. $table_data[] = [
  406. '月份' => $item['month'],
  407. '总业绩' => $is_export ? $item['total_amount'] : formatCurrency($item['total_amount']),
  408. '新客户业绩' => $is_export ? $item['new_customer_amount'] : formatCurrency($item['new_customer_amount']),
  409. '新客户占比' => $is_export ? ($item['total_amount'] > 0 ? ($item['new_customer_amount'] / $item['total_amount']) * 100 : 0) : formatPercentage($item['new_customer_amount'], $item['total_amount']),
  410. '老客户业绩' => $is_export ? $item['old_customer_amount'] : formatCurrency($item['old_customer_amount']),
  411. '老客户占比' => $is_export ? ($item['total_amount'] > 0 ? ($item['old_customer_amount'] / $item['total_amount']) * 100 : 0) : formatPercentage($item['old_customer_amount'], $item['total_amount']),
  412. '分配客户业绩' => $is_export ? $item['assigned_customer_amount'] : formatCurrency($item['assigned_customer_amount']),
  413. '分配客户占比' => $is_export ? ($item['total_amount'] > 0 ? ($item['assigned_customer_amount'] / $item['total_amount']) * 100 : 0) : formatPercentage($item['assigned_customer_amount'], $item['total_amount'])
  414. ];
  415. }
  416. // 如果是导出请求,则导出数据
  417. if ($is_export) {
  418. exportToCSV(
  419. $table_data,
  420. ['月份', '总业绩', '新客户业绩', '新客户占比(%)', '老客户业绩', '老客户占比(%)', '分配客户业绩', '分配客户占比(%)'],
  421. '每月客户构成统计_' . date('Ymd')
  422. );
  423. return;
  424. }
  425. // 渲染表格
  426. echo '<div class="row mt-5 mb-5">';
  427. echo '<div class="col-md-12">';
  428. echo '<div class="card">';
  429. echo '<div class="card-header d-flex justify-content-between align-items-center">';
  430. echo '<span>每月客户构成统计</span>';
  431. // 只有管理员才显示导出按钮
  432. if ($GLOBALS['current_permission_role'] == 1) {
  433. echo '<a href="' . $_SERVER['REQUEST_URI'] . '&export=excel&type=monthly" class="btn btn-sm btn-success ml-3">导出CSV</a>';
  434. }
  435. echo '</div>';
  436. echo '<div class="card-body">';
  437. echo '<div class="table-responsive">';
  438. echo '<table class="table table-bordered table-striped">';
  439. echo '<thead class="thead-light">';
  440. echo '<tr>';
  441. echo '<th style="width: 12%; text-align: left;">月份</th>';
  442. echo '<th style="width: 12%; text-align: left;">总业绩</th>';
  443. echo '<th style="width: 12%; text-align: left;">新客户业绩</th>';
  444. echo '<th style="width: 12%; text-align: left;">新客户占比</th>';
  445. echo '<th style="width: 12%; text-align: left;">老客户业绩</th>';
  446. echo '<th style="width: 12%; text-align: left;">老客户占比</th>';
  447. echo '<th style="width: 12%; text-align: left;">分配客户业绩</th>';
  448. echo '<th style="width: 12%; text-align: left;">分配客户占比</th>';
  449. echo '</tr>';
  450. echo '</thead>';
  451. echo '<tbody>';
  452. foreach ($data as $item) {
  453. echo '<tr>';
  454. echo '<td>' . $item['month'] . '</td>';
  455. echo '<td>' . formatCurrency($item['total_amount']) . '</td>';
  456. echo '<td>' . formatCurrency($item['new_customer_amount']) . '</td>';
  457. echo '<td>' . formatPercentage($item['new_customer_amount'], $item['total_amount']) . '</td>';
  458. echo '<td>' . formatCurrency($item['old_customer_amount']) . '</td>';
  459. echo '<td>' . formatPercentage($item['old_customer_amount'], $item['total_amount']) . '</td>';
  460. echo '<td>' . formatCurrency($item['assigned_customer_amount']) . '</td>';
  461. echo '<td>' . formatPercentage($item['assigned_customer_amount'], $item['total_amount']) . '</td>';
  462. echo '</tr>';
  463. }
  464. echo '</tbody>';
  465. echo '</table>';
  466. echo '</div>';
  467. echo '</div>';
  468. echo '</div>';
  469. echo '</div>';
  470. echo '</div>';
  471. }
  472. // 获取选择的业务员
  473. $selected_employee = isset($_GET['selected_employee']) ? $_GET['selected_employee'] : 'all';
  474. // 确定要显示哪些业务员的数据
  475. $employee_filter = null;
  476. if ($selected_employee != 'all') {
  477. // 如果选择了特定业务员,则只显示该业务员的数据
  478. $employee_filter = intval($selected_employee);
  479. } else {
  480. // 否则按权限显示相应的业务员数据
  481. if ($current_permission_role == 1) {
  482. // 管理员可以看到所有业务员
  483. $employee_filter = null;
  484. } elseif ($current_permission_role == 2) {
  485. // 组长可以看到自己和组员
  486. $visible_employees = [];
  487. $query = "SELECT id FROM employee WHERE id = " . intval($current_employee_id) . " OR em_role = " . intval($current_employee_id);
  488. $result = $conn->query($query);
  489. if ($result) {
  490. while ($row = $result->fetch_assoc()) {
  491. $visible_employees[] = $row['id'];
  492. }
  493. }
  494. $employee_filter = $visible_employees;
  495. } else {
  496. // 组员只能看到自己
  497. $employee_filter = intval($current_employee_id);
  498. }
  499. }
  500. // 获取业务员客户构成统计数据
  501. $customer_composition_stats = getCustomerCompositionStats($conn, $start_date, $end_date, $employee_filter);
  502. // 获取每月客户构成统计数据
  503. $monthly_customer_composition = getMonthlyCustomerComposition($conn, $start_date, $end_date, $employee_filter);
  504. // 处理导出请求
  505. if ($is_export) {
  506. $export_type = isset($_GET['type']) ? $_GET['type'] : '';
  507. switch ($export_type) {
  508. case 'employee':
  509. renderCustomerCompositionTable($customer_composition_stats, true);
  510. break;
  511. case 'monthly':
  512. renderMonthlyCustomerCompositionTable($monthly_customer_composition, true);
  513. break;
  514. }
  515. exit; // 确保导出后停止执行
  516. }
  517. ?>
  518. <div class="container">
  519. <div class="page-header">
  520. <h1 class="page-title">业绩客户构成统计</h1>
  521. </div>
  522. <!-- 日期筛选 -->
  523. <div class="filter-form mb-5">
  524. <form method="get" class="filter-form-inline">
  525. <div class="form-group mr-3">
  526. <label for="date_range" class="mr-2">选择日期范围</label>
  527. <select class="form-control" id="date_range" name="date_range" onchange="toggleCustomDates()">
  528. <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
  529. <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
  530. <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
  531. <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>最近30天</option>
  532. <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>最近90天</option>
  533. <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
  534. </select>
  535. </div>
  536. <div class="form-group custom-date-inputs mr-3" id="custom_start_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  537. <label for="start_date" class="mr-2">开始日期</label>
  538. <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
  539. </div>
  540. <div class="form-group custom-date-inputs mr-3" id="custom_end_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  541. <label for="end_date" class="mr-2">结束日期</label>
  542. <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
  543. </div>
  544. <!-- 业务员选择 -->
  545. <div class="form-group mr-3">
  546. <label for="selected_employee" class="mr-2">选择业务员</label>
  547. <select class="form-control" id="selected_employee" name="selected_employee">
  548. <option value="all">所有业务员</option>
  549. <?php
  550. // 获取当前用户可见的业务员列表
  551. $visible_employees_query = "";
  552. if ($current_permission_role == 1) {
  553. // 管理员可以看到所有业务员
  554. $visible_employees_query = "SELECT id, em_user FROM employee WHERE em_role IS NOT NULL ORDER BY em_user";
  555. } elseif ($current_permission_role == 2) {
  556. // 组长可以看到自己和组员
  557. $visible_employees_query = "SELECT id, em_user FROM employee WHERE id = $current_employee_id OR em_role = $current_employee_id ORDER BY em_user";
  558. } else {
  559. // 组员只能看到自己
  560. $visible_employees_query = "SELECT id, em_user FROM employee WHERE id = $current_employee_id";
  561. }
  562. $visible_employees_result = $conn->query($visible_employees_query);
  563. $selected_employee = isset($_GET['selected_employee']) ? $_GET['selected_employee'] : 'all';
  564. while ($emp = $visible_employees_result->fetch_assoc()) {
  565. $selected = ($selected_employee == $emp['id']) ? 'selected' : '';
  566. echo "<option value='".$emp['id']."' $selected>".$emp['em_user']."</option>";
  567. }
  568. ?>
  569. </select>
  570. </div>
  571. <div class="form-group">
  572. <button type="submit" class="btn btn-primary">应用筛选</button>
  573. </div>
  574. </form>
  575. </div>
  576. <!-- 统计内容区域 -->
  577. <div class="stats-content">
  578. <?php
  579. // 渲染表格
  580. renderCustomerCompositionTable($customer_composition_stats);
  581. renderMonthlyCustomerCompositionTable($monthly_customer_composition);
  582. ?>
  583. </div>
  584. </div>
  585. <style>
  586. /* 添加一些额外的样式以改善表格显示 */
  587. .filter-form {
  588. background-color: #f8f9fa;
  589. padding: 20px;
  590. border-radius: 5px;
  591. margin-bottom: 30px;
  592. }
  593. .filter-form-inline {
  594. display: flex;
  595. flex-wrap: wrap;
  596. align-items: flex-end;
  597. }
  598. .filter-form .form-group {
  599. margin-bottom: 10px;
  600. margin-right: 15px;
  601. }
  602. /* 表格样式 */
  603. .table {
  604. width: 100%;
  605. margin-bottom: 1rem;
  606. }
  607. .table th, .table td {
  608. padding: 12px;
  609. vertical-align: middle;
  610. }
  611. /* 确保表头也是左对齐 */
  612. .thead-light th {
  613. background-color: #f8f9fa;
  614. border-color: #dee2e6;
  615. text-align: left;
  616. }
  617. .table-striped tbody tr:nth-of-type(odd) {
  618. background-color: rgba(0, 0, 0, 0.03);
  619. }
  620. .card {
  621. box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
  622. }
  623. .card-header {
  624. background-color: #f8f9fa;
  625. padding: 15px;
  626. }
  627. .card-body {
  628. padding: 1.5rem;
  629. }
  630. /* 添加按钮左边距 */
  631. .ml-3 {
  632. margin-left: 15px !important;
  633. }
  634. /* 确保卡片头部布局正确 */
  635. .card-header.d-flex {
  636. display: flex !important;
  637. justify-content: space-between !important;
  638. align-items: center !important;
  639. }
  640. </style>
  641. <script>
  642. function toggleCustomDates() {
  643. const dateRange = document.getElementById('date_range').value;
  644. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  645. if (dateRange === 'custom') {
  646. customDateInputs.forEach(el => el.style.display = 'inline-block');
  647. } else {
  648. customDateInputs.forEach(el => el.style.display = 'none');
  649. }
  650. }
  651. </script>
  652. <?php
  653. // 页面底部
  654. include('statistics_footer.php');
  655. ?>