monthly_deal_stats.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. $date_params = getDateRangeParams();
  30. $start_date = $date_params['start_date_sql'];
  31. $end_date = $date_params['end_date_sql'];
  32. $date_range = $date_params['date_range'];
  33. $period = $date_params['period'];
  34. // 如果不是导出操作,则包含页面头部
  35. if (!$is_export) {
  36. include('statistics_header.php');
  37. }
  38. /**
  39. * 获取每月新增成交客户数量
  40. */
  41. function getMonthlyDealCustomers($conn, $start_date, $end_date, $employee_filter = null) {
  42. $sql = "SELECT
  43. DATE_FORMAT(cs_dealdate, '%Y-%m') AS month,
  44. COUNT(*) AS customer_count
  45. FROM customer
  46. WHERE cs_dealdate BETWEEN '$start_date' AND '$end_date'
  47. AND cs_deal = 3";
  48. // 根据员工过滤条件添加WHERE子句
  49. if ($employee_filter !== null) {
  50. if (is_array($employee_filter)) {
  51. if (!empty($employee_filter)) {
  52. $employee_ids = implode(',', array_map('intval', $employee_filter));
  53. $sql .= " AND cs_belong IN ($employee_ids)";
  54. }
  55. } else {
  56. $sql .= " AND cs_belong = " . intval($employee_filter);
  57. }
  58. }
  59. $sql .= " GROUP BY DATE_FORMAT(cs_dealdate, '%Y-%m')
  60. ORDER BY month";
  61. $result = $conn->query($sql);
  62. $data = [];
  63. if ($result) {
  64. while ($row = $result->fetch_assoc()) {
  65. $data[] = $row;
  66. }
  67. }
  68. return $data;
  69. }
  70. /**
  71. * 获取按业务员统计的成交客户数量和金额
  72. */
  73. function getDealStatsByEmployee($conn, $start_date, $end_date, $employee_filter = null) {
  74. $sql = "SELECT
  75. e.id AS employee_id,
  76. e.em_user AS employee_name,
  77. COUNT(DISTINCT c.id) AS customer_count,
  78. SUM(o.total_amount) AS total_amount
  79. FROM orders o
  80. JOIN customer c ON o.customer_id = c.id
  81. JOIN employee e ON c.cs_belong = e.id
  82. WHERE o.order_date BETWEEN '$start_date' AND '$end_date'
  83. AND c.cs_deal = 3
  84. AND c.cs_dealdate BETWEEN '$start_date' AND '$end_date'";
  85. // 根据员工过滤条件添加WHERE子句
  86. if ($employee_filter !== null) {
  87. if (is_array($employee_filter)) {
  88. if (!empty($employee_filter)) {
  89. $employee_ids = implode(',', array_map('intval', $employee_filter));
  90. $sql .= " AND c.cs_belong IN ($employee_ids)";
  91. }
  92. } else {
  93. $sql .= " AND c.cs_belong = " . intval($employee_filter);
  94. }
  95. }
  96. $sql .= " GROUP BY e.id
  97. ORDER BY total_amount DESC";
  98. $result = $conn->query($sql);
  99. $data = [];
  100. if ($result) {
  101. while ($row = $result->fetch_assoc()) {
  102. $data[] = $row;
  103. }
  104. }
  105. return $data;
  106. }
  107. /**
  108. * 导出数据为CSV
  109. */
  110. function exportToCSV($data, $columns, $filename) {
  111. // 设置头信息
  112. header('Content-Type: text/csv; charset=utf-8');
  113. header('Content-Disposition: attachment;filename="' . $filename . '.csv"');
  114. header('Cache-Control: max-age=0');
  115. // 创建输出流
  116. $output = fopen('php://output', 'w');
  117. // 添加UTF-8 BOM以确保Excel正确显示中文
  118. fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));
  119. // 输出列头
  120. fputcsv($output, $columns);
  121. // 输出数据行
  122. foreach ($data as $row) {
  123. // 确保所有数据都是数组格式
  124. $rowData = array_values($row);
  125. fputcsv($output, $rowData);
  126. }
  127. fclose($output);
  128. exit;
  129. }
  130. /**
  131. * 渲染每月成交客户数量表格
  132. */
  133. function renderMonthlyDealCustomersTable($data, $is_export = false) {
  134. if (empty($data)) {
  135. if (!$is_export) {
  136. echo '<div class="alert alert-info">当前选择范围内没有成交客户数据</div>';
  137. }
  138. return;
  139. }
  140. // 准备数据
  141. $table_data = [];
  142. $total_customers = 0;
  143. foreach ($data as $item) {
  144. $table_data[] = [
  145. '月份' => $item['month'],
  146. '新增成交客户数量' => $item['customer_count']
  147. ];
  148. $total_customers += intval($item['customer_count']);
  149. }
  150. // 如果是导出请求,则导出数据
  151. if ($is_export) {
  152. exportToCSV(
  153. $table_data,
  154. ['月份', '新增成交客户数量'],
  155. '每月新增成交客户数量_' . date('Ymd')
  156. );
  157. return;
  158. }
  159. // 渲染表格
  160. echo '<div class="row mt-5 mb-5">';
  161. echo '<div class="col-md-12">';
  162. echo '<div class="card">';
  163. echo '<div class="card-header d-flex justify-content-between align-items-center">';
  164. echo '<span>每月新增成交客户数量明细 (总计: '.$total_customers.' 客户)</span>';
  165. echo '<a href="' . $_SERVER['REQUEST_URI'] . '&export=excel&type=customers" class="btn btn-sm btn-success ml-3">导出CSV</a>';
  166. echo '</div>';
  167. echo '<div class="card-body">';
  168. echo '<div class="table-responsive">';
  169. echo '<table class="table table-bordered table-striped">';
  170. echo '<thead class="thead-light">';
  171. echo '<tr>';
  172. echo '<th style="width: 50%; text-align: left;">月份</th>';
  173. echo '<th style="width: 50%; text-align: left;">新增成交客户数量</th>';
  174. echo '</tr>';
  175. echo '</thead>';
  176. echo '<tbody>';
  177. foreach ($data as $item) {
  178. echo '<tr>';
  179. echo '<td>'.$item['month'].'</td>';
  180. echo '<td>'.$item['customer_count'].'</td>';
  181. echo '</tr>';
  182. }
  183. echo '</tbody>';
  184. echo '</table>';
  185. echo '</div>';
  186. echo '</div>';
  187. echo '</div>';
  188. echo '</div>';
  189. echo '</div>';
  190. }
  191. /**
  192. * 渲染业务员成交统计表格
  193. */
  194. function renderDealStatsByEmployeeTable($data, $is_export = false) {
  195. if (empty($data)) {
  196. if (!$is_export) {
  197. echo '<div class="alert alert-info">当前选择范围内没有业务员成交数据</div>';
  198. }
  199. return;
  200. }
  201. // 准备数据
  202. $table_data = [];
  203. foreach ($data as $item) {
  204. $avg_customer_value = $item['customer_count'] > 0 ? $item['total_amount'] / $item['customer_count'] : 0;
  205. $table_data[] = [
  206. '业务员' => $item['employee_name'],
  207. '成交客户数' => $item['customer_count'],
  208. '成交金额' => $is_export ? $item['total_amount'] : formatCurrency($item['total_amount']),
  209. '客单价' => $is_export ? $avg_customer_value : formatCurrency($avg_customer_value)
  210. ];
  211. }
  212. // 如果是导出请求,则导出数据
  213. if ($is_export) {
  214. exportToCSV(
  215. $table_data,
  216. ['业务员', '成交客户数', '成交金额', '客单价'],
  217. '业务员成交统计_' . date('Ymd')
  218. );
  219. return;
  220. }
  221. // 渲染表格
  222. echo '<div class="row mt-5">';
  223. echo '<div class="col-md-12">';
  224. echo '<div class="card">';
  225. echo '<div class="card-header d-flex justify-content-between align-items-center">';
  226. echo '<span>业务员成交统计明细</span>';
  227. echo '<a href="' . $_SERVER['REQUEST_URI'] . '&export=excel&type=employee" class="btn btn-sm btn-success ml-3">导出CSV</a>';
  228. echo '</div>';
  229. echo '<div class="card-body">';
  230. echo '<div class="table-responsive">';
  231. echo '<table class="table table-bordered table-striped">';
  232. echo '<thead class="thead-light">';
  233. echo '<tr>';
  234. echo '<th style="width: 25%; text-align: left;">业务员</th>';
  235. echo '<th style="width: 25%; text-align: left;">成交客户数</th>';
  236. echo '<th style="width: 25%; text-align: left;">成交金额</th>';
  237. echo '<th style="width: 25%; text-align: left;">客单价</th>';
  238. echo '</tr>';
  239. echo '</thead>';
  240. echo '<tbody>';
  241. foreach ($data as $item) {
  242. $avg_customer_value = $item['customer_count'] > 0 ? $item['total_amount'] / $item['customer_count'] : 0;
  243. echo '<tr>';
  244. echo '<td>'.$item['employee_name'].'</td>';
  245. echo '<td>'.$item['customer_count'].'</td>';
  246. echo '<td>'.formatCurrency($item['total_amount']).'</td>';
  247. echo '<td>'.formatCurrency($avg_customer_value).'</td>';
  248. echo '</tr>';
  249. }
  250. echo '</tbody>';
  251. echo '</table>';
  252. echo '</div>';
  253. echo '</div>';
  254. echo '</div>';
  255. echo '</div>';
  256. echo '</div>';
  257. }
  258. // 获取选择的业务员
  259. $selected_employee = isset($_GET['selected_employee']) ? $_GET['selected_employee'] : 'all';
  260. // 确定要显示哪些业务员的数据
  261. $employee_filter = null;
  262. if ($selected_employee != 'all') {
  263. // 如果选择了特定业务员,则只显示该业务员的数据
  264. $employee_filter = intval($selected_employee);
  265. } else {
  266. // 否则按权限显示相应的业务员数据
  267. if ($current_permission_role == 1) {
  268. // 管理员可以看到所有业务员
  269. $employee_filter = null;
  270. } elseif ($current_permission_role == 2) {
  271. // 组长可以看到自己和组员
  272. $visible_employees = [];
  273. $query = "SELECT id FROM employee WHERE id = " . intval($current_employee_id) . " OR em_role = " . intval($current_employee_id);
  274. $result = $conn->query($query);
  275. if ($result) {
  276. while ($row = $result->fetch_assoc()) {
  277. $visible_employees[] = $row['id'];
  278. }
  279. }
  280. $employee_filter = $visible_employees;
  281. } else {
  282. // 组员只能看到自己
  283. $employee_filter = intval($current_employee_id);
  284. }
  285. }
  286. // 获取每月新增成交客户数量数据
  287. $monthly_deal_customers = getMonthlyDealCustomers($conn, $start_date, $end_date, $employee_filter);
  288. // 获取业务员成交统计数据
  289. $deal_stats_by_employee = getDealStatsByEmployee($conn, $start_date, $end_date, $employee_filter);
  290. // 处理导出请求
  291. if ($is_export) {
  292. $export_type = isset($_GET['type']) ? $_GET['type'] : '';
  293. switch ($export_type) {
  294. case 'customers':
  295. renderMonthlyDealCustomersTable($monthly_deal_customers, true);
  296. break;
  297. case 'employee':
  298. renderDealStatsByEmployeeTable($deal_stats_by_employee, true);
  299. break;
  300. }
  301. exit; // 确保导出后停止执行
  302. }
  303. ?>
  304. <div class="container">
  305. <div class="page-header">
  306. <h1 class="page-title">每月新增成交客户统计</h1>
  307. </div>
  308. <!-- 日期筛选 -->
  309. <div class="filter-form mb-5">
  310. <form method="get" class="filter-form-inline">
  311. <div class="form-group mr-3">
  312. <label for="date_range" class="mr-2">选择日期范围</label>
  313. <select class="form-control" id="date_range" name="date_range" onchange="toggleCustomDates()">
  314. <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
  315. <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
  316. <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
  317. <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>最近30天</option>
  318. <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>最近90天</option>
  319. <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
  320. </select>
  321. </div>
  322. <div class="form-group custom-date-inputs mr-3" id="custom_start_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  323. <label for="start_date" class="mr-2">开始日期</label>
  324. <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
  325. </div>
  326. <div class="form-group custom-date-inputs mr-3" id="custom_end_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  327. <label for="end_date" class="mr-2">结束日期</label>
  328. <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
  329. </div>
  330. <!-- 业务员选择 -->
  331. <div class="form-group mr-3">
  332. <label for="selected_employee" class="mr-2">选择业务员</label>
  333. <select class="form-control" id="selected_employee" name="selected_employee">
  334. <option value="all">所有业务员</option>
  335. <?php
  336. // 获取当前用户可见的业务员列表
  337. $visible_employees_query = "";
  338. if ($current_permission_role == 1) {
  339. // 管理员可以看到所有业务员
  340. $visible_employees_query = "SELECT id, em_user FROM employee WHERE em_role IS NOT NULL ORDER BY em_user";
  341. } elseif ($current_permission_role == 2) {
  342. // 组长可以看到自己和组员
  343. $visible_employees_query = "SELECT id, em_user FROM employee WHERE id = $current_employee_id OR em_role = $current_employee_id ORDER BY em_user";
  344. } else {
  345. // 组员只能看到自己
  346. $visible_employees_query = "SELECT id, em_user FROM employee WHERE id = $current_employee_id";
  347. }
  348. $visible_employees_result = $conn->query($visible_employees_query);
  349. $selected_employee = isset($_GET['selected_employee']) ? $_GET['selected_employee'] : 'all';
  350. while ($emp = $visible_employees_result->fetch_assoc()) {
  351. $selected = ($selected_employee == $emp['id']) ? 'selected' : '';
  352. echo "<option value='".$emp['id']."' $selected>".$emp['em_user']."</option>";
  353. }
  354. ?>
  355. </select>
  356. </div>
  357. <div class="form-group">
  358. <button type="submit" class="btn btn-primary">应用筛选</button>
  359. </div>
  360. </form>
  361. </div>
  362. <!-- 统计内容区域 -->
  363. <div class="stats-content">
  364. <?php
  365. // 渲染表格
  366. renderMonthlyDealCustomersTable($monthly_deal_customers);
  367. renderDealStatsByEmployeeTable($deal_stats_by_employee);
  368. ?>
  369. </div>
  370. </div>
  371. <style>
  372. /* 添加一些额外的样式以改善表格显示 */
  373. .filter-form {
  374. background-color: #f8f9fa;
  375. padding: 20px;
  376. border-radius: 5px;
  377. margin-bottom: 30px;
  378. }
  379. .filter-form-inline {
  380. display: flex;
  381. flex-wrap: wrap;
  382. align-items: flex-end;
  383. }
  384. .filter-form .form-group {
  385. margin-bottom: 10px;
  386. margin-right: 15px;
  387. }
  388. /* 表格样式 */
  389. .table {
  390. width: 100%;
  391. margin-bottom: 1rem;
  392. }
  393. .table th, .table td {
  394. padding: 12px;
  395. vertical-align: middle;
  396. }
  397. /* 确保表头也是左对齐 */
  398. .thead-light th {
  399. background-color: #f8f9fa;
  400. border-color: #dee2e6;
  401. text-align: left;
  402. }
  403. .table-striped tbody tr:nth-of-type(odd) {
  404. background-color: rgba(0, 0, 0, 0.03);
  405. }
  406. .card {
  407. box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
  408. }
  409. .card-header {
  410. background-color: #f8f9fa;
  411. padding: 15px;
  412. }
  413. .card-body {
  414. padding: 1.5rem;
  415. }
  416. /* 添加按钮左边距 */
  417. .ml-3 {
  418. margin-left: 15px !important;
  419. }
  420. /* 确保卡片头部布局正确 */
  421. .card-header.d-flex {
  422. display: flex !important;
  423. justify-content: space-between !important;
  424. align-items: center !important;
  425. }
  426. </style>
  427. <script>
  428. function toggleCustomDates() {
  429. const dateRange = document.getElementById('date_range').value;
  430. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  431. if (dateRange === 'custom') {
  432. customDateInputs.forEach(el => el.style.display = 'inline-block');
  433. } else {
  434. customDateInputs.forEach(el => el.style.display = 'none');
  435. }
  436. }
  437. </script>
  438. <?php
  439. // 页面底部
  440. include('statistics_footer.php');
  441. ?>