customer_composition_stats.php 29 KB

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