customer_composition_stats.php 29 KB

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