customers_stats.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. $date_params = getDateRangeParams();
  18. $start_date = $date_params['start_date_sql'];
  19. $end_date = $date_params['end_date_sql'];
  20. $date_range = $date_params['date_range'];
  21. $period = $date_params['period'];
  22. // 页面头部
  23. include('statistics_header.php');
  24. ?>
  25. <div class="container">
  26. <div class="page-header">
  27. <h1 class="page-title">客户统计分析</h1>
  28. </div>
  29. <!-- 日期筛选 -->
  30. <div class="filter-form">
  31. <form method="get" class="filter-form-inline">
  32. <div class="form-group">
  33. <label for="date_range">选择日期范围</label>
  34. <select class="form-control" id="date_range" name="date_range" onchange="toggleCustomDates()">
  35. <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
  36. <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
  37. <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
  38. <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>最近30天</option>
  39. <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>最近90天</option>
  40. <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
  41. </select>
  42. </div>
  43. <div class="form-group custom-date-inputs" id="custom_start_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  44. <label for="start_date">开始日期</label>
  45. <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
  46. </div>
  47. <div class="form-group custom-date-inputs" id="custom_end_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  48. <label for="end_date">结束日期</label>
  49. <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
  50. </div>
  51. <div class="form-group">
  52. <button type="submit" class="btn">应用筛选</button>
  53. </div>
  54. </form>
  55. </div>
  56. <!-- 关键指标 -->
  57. <div class="key-metrics-section" >
  58. <div class="section-title">
  59. <h2>关键指标</h2>
  60. </div>
  61. <?php
  62. // 获取关键指标数据
  63. $total_customers = getTotalCustomers($conn);
  64. $new_customers = getNewCustomers($conn, $start_date, $end_date);
  65. $avg_customer_value = getAverageCustomerValue($conn, $start_date, $end_date);
  66. $retention_data = getCustomerRetentionRate($conn, $start_date, $end_date);
  67. $conversion_data = getOrderConversionRate($conn, $start_date, $end_date);
  68. // 组合所有指标数据
  69. $kpi_data = [
  70. 'total_customers' => $total_customers,
  71. 'new_customers' => $new_customers,
  72. 'avg_customer_value' => $avg_customer_value,
  73. 'retention_rate' => $retention_data['retention_rate'],
  74. 'retained_count' => $retention_data['retained_count'],
  75. 'total_previous' => $retention_data['total_previous'],
  76. 'conversion_rate' => $conversion_data['conversion_rate'],
  77. 'customers_with_orders' => $conversion_data['customers_with_orders']
  78. ];
  79. // 渲染关键指标卡片
  80. renderKeyMetricsCard($kpi_data);
  81. ?>
  82. </div>
  83. <!-- 新增客户列表 -->
  84. <div class="customer-section">
  85. <div class="section-title">
  86. <h2>新增客户明细</h2>
  87. </div>
  88. <?php
  89. // 获取新增客户详细数据
  90. $new_customers_details = getNewCustomersDetails($conn, $start_date, $end_date);
  91. // 渲染新增客户图表
  92. renderNewCustomersChart($new_customers_details);
  93. ?>
  94. </div>
  95. <!-- 业务员新增客户统计 -->
  96. <div class="employee-section">
  97. <div class="section-title">
  98. <h2>业务员新增客户统计</h2>
  99. </div>
  100. <?php
  101. // 获取各业务员新增客户数据
  102. $employee_new_customers = getNewCustomersByEmployee($conn, $start_date, $end_date);
  103. // 渲染业务员新增客户图表
  104. renderNewCustomersByEmployeeChart($employee_new_customers);
  105. ?>
  106. </div>
  107. </div>
  108. <script>
  109. function toggleCustomDates() {
  110. const dateRange = document.getElementById('date_range').value;
  111. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  112. if (dateRange === 'custom') {
  113. customDateInputs.forEach(el => el.style.display = 'inline-block');
  114. } else {
  115. customDateInputs.forEach(el => el.style.display = 'none');
  116. }
  117. }
  118. </script>
  119. <?php
  120. // 页面底部
  121. include('statistics_footer.php');
  122. ?>
  123. <?php
  124. /**
  125. * 获取新增客户详细信息
  126. *
  127. * @param mysqli $conn 数据库连接
  128. * @param string $start_date 开始日期
  129. * @param string $end_date 结束日期
  130. * @return array 新增客户详细数据
  131. */
  132. function getNewCustomersDetails($conn, $start_date, $end_date) {
  133. $sql = "SELECT
  134. c.id,
  135. c.cs_company as company_name,
  136. c.cs_code as customer_code,
  137. co.countryName as country,
  138. ct.businessType as customer_type,
  139. e.em_user as employee_name,
  140. c.cs_addtime as add_date
  141. FROM customer c
  142. LEFT JOIN country co ON c.cs_country = co.id
  143. LEFT JOIN clienttype ct ON c.cs_type = ct.id
  144. LEFT JOIN employee e ON c.cs_belong = e.id
  145. WHERE c.cs_addtime BETWEEN ? AND ?
  146. ORDER BY c.cs_addtime DESC
  147. LIMIT 30";
  148. $stmt = $conn->prepare($sql);
  149. $stmt->bind_param("ss", $start_date, $end_date);
  150. $stmt->execute();
  151. $result = $stmt->get_result();
  152. $customers = [];
  153. while ($row = $result->fetch_assoc()) {
  154. $customers[] = $row;
  155. }
  156. return $customers;
  157. }
  158. /**
  159. * 获取各业务员新增客户统计
  160. *
  161. * @param mysqli $conn 数据库连接
  162. * @param string $start_date 开始日期
  163. * @param string $end_date 结束日期
  164. * @return array 业务员新增客户统计数据
  165. */
  166. function getNewCustomersByEmployee($conn, $start_date, $end_date) {
  167. $sql = "SELECT
  168. e.id as employee_id,
  169. e.em_user as employee_name,
  170. COUNT(c.id) as customer_count
  171. FROM employee e
  172. LEFT JOIN customer c ON e.id = c.cs_belong AND c.cs_addtime BETWEEN ? AND ?
  173. WHERE e.em_role IS NOT NULL
  174. GROUP BY e.id
  175. ORDER BY customer_count DESC";
  176. $stmt = $conn->prepare($sql);
  177. $stmt->bind_param("ss", $start_date, $end_date);
  178. $stmt->execute();
  179. $result = $stmt->get_result();
  180. $data = [];
  181. while ($row = $result->fetch_assoc()) {
  182. $data[] = $row;
  183. }
  184. return $data;
  185. }
  186. /**
  187. * 渲染新增客户图表
  188. *
  189. * @param array $customers 新增客户数据
  190. * @return void
  191. */
  192. function renderNewCustomersChart($customers) {
  193. if (empty($customers)) {
  194. echo '<div class="alert alert-info">该时间段内没有新增客户数据</div>';
  195. return;
  196. }
  197. ?>
  198. <div class="chart-container">
  199. <div class="table-responsive">
  200. <table class="data-table">
  201. <thead>
  202. <tr>
  203. <th>客户名称</th>
  204. <th>客户编码</th>
  205. <th>国家/地区</th>
  206. <th>客户类型</th>
  207. <th>负责业务员</th>
  208. <th>添加日期</th>
  209. </tr>
  210. </thead>
  211. <tbody>
  212. <?php foreach ($customers as $customer): ?>
  213. <tr>
  214. <td><?php echo htmlspecialchars($customer['company_name']); ?></td>
  215. <td><?php echo htmlspecialchars($customer['customer_code']); ?></td>
  216. <td><?php echo htmlspecialchars($customer['country']); ?></td>
  217. <td><?php echo htmlspecialchars($customer['customer_type'] ?: '未分类'); ?></td>
  218. <td><?php echo htmlspecialchars($customer['employee_name']); ?></td>
  219. <td><?php echo date('Y-m-d', strtotime($customer['add_date'])); ?></td>
  220. </tr>
  221. <?php endforeach; ?>
  222. </tbody>
  223. </table>
  224. </div>
  225. </div>
  226. <!-- 新增客户添加时间分布图 -->
  227. <div class="chart-container">
  228. <div>
  229. <canvas id="newCustomersChart"></canvas>
  230. </div>
  231. </div>
  232. <script>
  233. // 准备图表数据
  234. <?php
  235. // 按日期分组客户数量
  236. $dates = [];
  237. $counts = [];
  238. $dateGroups = [];
  239. foreach ($customers as $customer) {
  240. $date = date('Y-m-d', strtotime($customer['add_date']));
  241. if (!isset($dateGroups[$date])) {
  242. $dateGroups[$date] = 0;
  243. }
  244. $dateGroups[$date]++;
  245. }
  246. // 排序日期
  247. ksort($dateGroups);
  248. foreach ($dateGroups as $date => $count) {
  249. $dates[] = $date;
  250. $counts[] = $count;
  251. }
  252. ?>
  253. var newCustomersCtx = document.getElementById('newCustomersChart').getContext('2d');
  254. new Chart(newCustomersCtx, {
  255. type: 'bar',
  256. data: {
  257. labels: <?php echo json_encode($dates); ?>,
  258. datasets: [{
  259. label: '新增客户数量',
  260. data: <?php echo json_encode($counts); ?>,
  261. backgroundColor: 'rgba(54, 162, 235, 0.5)',
  262. borderColor: 'rgba(54, 162, 235, 1)',
  263. borderWidth: 1
  264. }]
  265. },
  266. options: {
  267. responsive: true,
  268. scales: {
  269. y: {
  270. beginAtZero: true,
  271. title: {
  272. display: true,
  273. text: '客户数量'
  274. }
  275. },
  276. x: {
  277. title: {
  278. display: true,
  279. text: '日期'
  280. }
  281. }
  282. },
  283. plugins: {
  284. title: {
  285. display: true,
  286. text: '新增客户时间分布'
  287. }
  288. }
  289. }
  290. });
  291. </script>
  292. <?php
  293. }
  294. /**
  295. * 渲染业务员新增客户图表
  296. *
  297. * @param array $employee_data 业务员新增客户数据
  298. * @return void
  299. */
  300. function renderNewCustomersByEmployeeChart($employee_data) {
  301. if (empty($employee_data)) {
  302. echo '<div class="alert alert-info">该时间段内没有业务员新增客户数据</div>';
  303. return;
  304. }
  305. // 准备图表数据
  306. $employee_names = [];
  307. $customer_counts = [];
  308. foreach ($employee_data as $row) {
  309. $employee_names[] = $row['employee_name'];
  310. $customer_counts[] = $row['customer_count'];
  311. }
  312. // 生成图表背景色
  313. $colors = generateChartColors(count($employee_data));
  314. $backgroundColors = [];
  315. $borderColors = [];
  316. foreach ($colors as $color) {
  317. $backgroundColors[] = $color[0];
  318. $borderColors[] = $color[1];
  319. }
  320. ?>
  321. <div class="analysis-grid">
  322. <div>
  323. <canvas id="employeeNewCustomersChart"></canvas>
  324. </div>
  325. <div class="table-responsive">
  326. <table class="data-table">
  327. <thead>
  328. <tr>
  329. <th>业务员</th>
  330. <th>新增客户数量</th>
  331. </tr>
  332. </thead>
  333. <tbody>
  334. <?php foreach ($employee_data as $row): ?>
  335. <tr>
  336. <td><?php echo htmlspecialchars($row['employee_name']); ?></td>
  337. <td><?php echo number_format($row['customer_count']); ?></td>
  338. </tr>
  339. <?php endforeach; ?>
  340. </tbody>
  341. </table>
  342. </div>
  343. </div>
  344. <script>
  345. var employeeNewCustomersCtx = document.getElementById('employeeNewCustomersChart').getContext('2d');
  346. new Chart(employeeNewCustomersCtx, {
  347. type: 'bar',
  348. data: {
  349. labels: <?php echo json_encode($employee_names); ?>,
  350. datasets: [{
  351. label: '新增客户数量',
  352. data: <?php echo json_encode($customer_counts); ?>,
  353. backgroundColor: <?php echo json_encode($backgroundColors); ?>,
  354. borderColor: <?php echo json_encode($borderColors); ?>,
  355. borderWidth: 1
  356. }]
  357. },
  358. options: {
  359. responsive: true,
  360. scales: {
  361. y: {
  362. beginAtZero: true,
  363. title: {
  364. display: true,
  365. text: '客户数量'
  366. }
  367. }
  368. },
  369. plugins: {
  370. title: {
  371. display: true,
  372. text: '业务员新增客户统计'
  373. }
  374. }
  375. }
  376. });
  377. </script>
  378. <?php
  379. }