monthly_deal_stats.php 26 KB

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