inquiry_conversion_stats.php 26 KB

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