customers_stats.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. ?>