inquiry_conversion_stats.php 27 KB

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