Ver código fonte

fleat: update statistics

igb 1 semana atrás
pai
commit
8a02789a46
2 arquivos alterados com 281 adições e 282 exclusões
  1. 1 282
      customers_stats.php
  2. 280 0
      statistics_utils.php

+ 1 - 282
customers_stats.php

@@ -135,285 +135,4 @@ function toggleCustomDates() {
 <?php
 // 页面底部
 include('statistics_footer.php');
-?>
-
-<?php
-/**
- * 获取新增客户详细信息
- * 
- * @param mysqli $conn 数据库连接
- * @param string $start_date 开始日期
- * @param string $end_date 结束日期
- * @return array 新增客户详细数据
- */
-function getNewCustomersDetails($conn, $start_date, $end_date) {
-    $sql = "SELECT 
-                c.id,
-                c.cs_company as company_name,
-                c.cs_code as customer_code,
-                co.countryName as country,
-                ct.businessType as customer_type,
-                e.em_user as employee_name,
-                c.cs_addtime as add_date
-            FROM customer c
-            LEFT JOIN country co ON c.cs_country = co.id
-            LEFT JOIN clienttype ct ON c.cs_type = ct.id
-            LEFT JOIN employee e ON c.cs_belong = e.id
-            WHERE c.cs_addtime BETWEEN ? AND ?
-            ORDER BY c.cs_addtime DESC
-            LIMIT 30";
-            
-    $stmt = $conn->prepare($sql);
-    $stmt->bind_param("ss", $start_date, $end_date);
-    $stmt->execute();
-    
-    $result = $stmt->get_result();
-    $customers = [];
-    
-    while ($row = $result->fetch_assoc()) {
-        $customers[] = $row;
-    }
-    
-    return $customers;
-}
-
-/**
- * 获取各业务员新增客户统计
- * 
- * @param mysqli $conn 数据库连接
- * @param string $start_date 开始日期
- * @param string $end_date 结束日期
- * @return array 业务员新增客户统计数据
- */
-function getNewCustomersByEmployee($conn, $start_date, $end_date) {
-    $sql = "SELECT 
-                e.id as employee_id,
-                e.em_user as employee_name,
-                COUNT(c.id) as customer_count
-            FROM employee e
-            LEFT JOIN customer c ON e.id = c.cs_belong AND c.cs_addtime BETWEEN ? AND ?
-            WHERE e.em_role IS NOT NULL
-            GROUP BY e.id
-            ORDER BY customer_count DESC";
-            
-    $stmt = $conn->prepare($sql);
-    $stmt->bind_param("ss", $start_date, $end_date);
-    $stmt->execute();
-    
-    $result = $stmt->get_result();
-    $data = [];
-    
-    while ($row = $result->fetch_assoc()) {
-        $data[] = $row;
-    }
-    
-    return $data;
-}
-
-/**
- * 渲染新增客户图表
- * 
- * @param array $customers 新增客户数据
- * @return void
- */
-function renderNewCustomersChart($customers) {
-    if (empty($customers)) {
-        echo '<div class="alert alert-info">该时间段内没有新增客户数据</div>';
-        return;
-    }
-    ?>
-    <div class="chart-container">
-        <div class="table-responsive">
-            <table class="data-table">
-                <thead>
-                    <tr>
-                        <th>客户名称</th>
-                        <th>客户编码</th>
-                        <th>国家/地区</th>
-                        <th>客户类型</th>
-                        <th>负责业务员</th>
-                        <th>添加日期</th>
-                    </tr>
-                </thead>
-                <tbody>
-                    <?php foreach ($customers as $customer): ?>
-                    <tr>
-                        <td><?php echo htmlspecialchars($customer['company_name']); ?></td>
-                        <td><?php echo htmlspecialchars($customer['customer_code']); ?></td>
-                        <td><?php echo htmlspecialchars($customer['country']); ?></td>
-                        <td><?php echo htmlspecialchars($customer['customer_type'] ?: '未分类'); ?></td>
-                        <td><?php echo htmlspecialchars($customer['employee_name']); ?></td>
-                        <td><?php echo date('Y-m-d', strtotime($customer['add_date'])); ?></td>
-                    </tr>
-                    <?php endforeach; ?>
-                </tbody>
-            </table>
-        </div>
-    </div>
-    
-    <!-- 新增客户添加时间分布图 -->
-    <div class="chart-container">
-        <div>
-            <canvas id="newCustomersChart"></canvas>
-        </div>
-    </div>
-    
-    <script>
-    // 准备图表数据
-    <?php
-    // 按日期分组客户数量
-    $dates = [];
-    $counts = [];
-    $dateGroups = [];
-    
-    foreach ($customers as $customer) {
-        $date = date('Y-m-d', strtotime($customer['add_date']));
-        if (!isset($dateGroups[$date])) {
-            $dateGroups[$date] = 0;
-        }
-        $dateGroups[$date]++;
-    }
-    
-    // 排序日期
-    ksort($dateGroups);
-    
-    foreach ($dateGroups as $date => $count) {
-        $dates[] = $date;
-        $counts[] = $count;
-    }
-    ?>
-    
-    var newCustomersCtx = document.getElementById('newCustomersChart').getContext('2d');
-    new Chart(newCustomersCtx, {
-        type: 'bar',
-        data: {
-            labels: <?php echo json_encode($dates); ?>,
-            datasets: [{
-                label: '新增客户数量',
-                data: <?php echo json_encode($counts); ?>,
-                backgroundColor: 'rgba(54, 162, 235, 0.5)',
-                borderColor: 'rgba(54, 162, 235, 1)',
-                borderWidth: 1
-            }]
-        },
-        options: {
-            responsive: true,
-            scales: {
-                y: {
-                    beginAtZero: true,
-                    title: {
-                        display: true,
-                        text: '客户数量'
-                    }
-                },
-                x: {
-                    title: {
-                        display: true,
-                        text: '日期'
-                    }
-                }
-            },
-            plugins: {
-                title: {
-                    display: true,
-                    text: '新增客户时间分布'
-                }
-            }
-        }
-    });
-    </script>
-    <?php
-}
-
-/**
- * 渲染业务员新增客户图表
- * 
- * @param array $employee_data 业务员新增客户数据
- * @return void
- */
-function renderNewCustomersByEmployeeChart($employee_data) {
-    if (empty($employee_data)) {
-        echo '<div class="alert alert-info">该时间段内没有业务员新增客户数据</div>';
-        return;
-    }
-    
-    // 准备图表数据
-    $employee_names = [];
-    $customer_counts = [];
-    
-    foreach ($employee_data as $row) {
-        $employee_names[] = $row['employee_name'];
-        $customer_counts[] = $row['customer_count'];
-    }
-    
-    // 生成图表背景色
-    $colors = generateChartColors(count($employee_data));
-    $backgroundColors = [];
-    $borderColors = [];
-    
-    foreach ($colors as $color) {
-        $backgroundColors[] = $color[0];
-        $borderColors[] = $color[1];
-    }
-    ?>
-    
-    <div class="analysis-grid">
-        <div>
-            <canvas id="employeeNewCustomersChart"></canvas>
-        </div>
-        <div class="table-responsive">
-            <table class="data-table">
-                <thead>
-                    <tr>
-                        <th>业务员</th>
-                        <th>新增客户数量</th>
-                    </tr>
-                </thead>
-                <tbody>
-                    <?php foreach ($employee_data as $row): ?>
-                    <tr>
-                        <td><?php echo htmlspecialchars($row['employee_name']); ?></td>
-                        <td><?php echo number_format($row['customer_count']); ?></td>
-                    </tr>
-                    <?php endforeach; ?>
-                </tbody>
-            </table>
-        </div>
-    </div>
-    
-    <script>
-    var employeeNewCustomersCtx = document.getElementById('employeeNewCustomersChart').getContext('2d');
-    new Chart(employeeNewCustomersCtx, {
-        type: 'bar',
-        data: {
-            labels: <?php echo json_encode($employee_names); ?>,
-            datasets: [{
-                label: '新增客户数量',
-                data: <?php echo json_encode($customer_counts); ?>,
-                backgroundColor: <?php echo json_encode($backgroundColors); ?>,
-                borderColor: <?php echo json_encode($borderColors); ?>,
-                borderWidth: 1
-            }]
-        },
-        options: {
-            responsive: true,
-            scales: {
-                y: {
-                    beginAtZero: true,
-                    title: {
-                        display: true,
-                        text: '客户数量'
-                    }
-                }
-            },
-            plugins: {
-                title: {
-                    display: true,
-                    text: '业务员新增客户统计'
-                }
-            }
-        }
-    });
-    </script>
-    <?php
-} 
+?> 

+ 280 - 0
statistics_utils.php

@@ -148,4 +148,284 @@ function getPeriodFormat($period) {
         default:
             return '%Y-%m-%d';
     }
+}
+
+/**
+ * 获取新增客户详细信息
+ * 
+ * @param mysqli $conn 数据库连接
+ * @param string $start_date 开始日期
+ * @param string $end_date 结束日期
+ * @return array 新增客户详细数据
+ */
+function getNewCustomersDetails($conn, $start_date, $end_date) {
+    $sql = "SELECT 
+                c.id,
+                c.cs_company as company_name,
+                c.cs_code as customer_code,
+                co.countryName as country,
+                ct.businessType as customer_type,
+                e.em_user as employee_name,
+                c.cs_addtime as add_date
+            FROM customer c
+            LEFT JOIN country co ON c.cs_country = co.id
+            LEFT JOIN clienttype ct ON c.cs_type = ct.id
+            LEFT JOIN employee e ON c.cs_belong = e.id
+            WHERE c.cs_addtime BETWEEN ? AND ?
+            ORDER BY c.cs_addtime DESC
+            LIMIT 30";
+            
+    $stmt = $conn->prepare($sql);
+    $stmt->bind_param("ss", $start_date, $end_date);
+    $stmt->execute();
+    
+    $result = $stmt->get_result();
+    $customers = [];
+    
+    while ($row = $result->fetch_assoc()) {
+        $customers[] = $row;
+    }
+    
+    return $customers;
+}
+
+/**
+ * 获取各业务员新增客户统计
+ * 
+ * @param mysqli $conn 数据库连接
+ * @param string $start_date 开始日期
+ * @param string $end_date 结束日期
+ * @return array 业务员新增客户统计数据
+ */
+function getNewCustomersByEmployee($conn, $start_date, $end_date) {
+    $sql = "SELECT 
+                e.id as employee_id,
+                e.em_user as employee_name,
+                COUNT(c.id) as customer_count
+            FROM employee e
+            LEFT JOIN customer c ON e.id = c.cs_belong AND c.cs_addtime BETWEEN ? AND ?
+            WHERE e.em_role IS NOT NULL
+            GROUP BY e.id
+            ORDER BY customer_count DESC";
+            
+    $stmt = $conn->prepare($sql);
+    $stmt->bind_param("ss", $start_date, $end_date);
+    $stmt->execute();
+    
+    $result = $stmt->get_result();
+    $data = [];
+    
+    while ($row = $result->fetch_assoc()) {
+        $data[] = $row;
+    }
+    
+    return $data;
+}
+
+/**
+ * 渲染新增客户图表
+ * 
+ * @param array $customers 新增客户数据
+ * @return void
+ */
+function renderNewCustomersChart($customers) {
+    if (empty($customers)) {
+        echo '<div class="alert alert-info">该时间段内没有新增客户数据</div>';
+        return;
+    }
+    ?>
+    <div class="chart-container">
+        <div class="table-responsive">
+            <table class="data-table">
+                <thead>
+                    <tr>
+                        <th>客户名称</th>
+                        <th>客户编码</th>
+                        <th>国家/地区</th>
+                        <th>客户类型</th>
+                        <th>负责业务员</th>
+                        <th>添加日期</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    <?php foreach ($customers as $customer): ?>
+                    <tr>
+                        <td><?php echo htmlspecialchars($customer['company_name']); ?></td>
+                        <td><?php echo htmlspecialchars($customer['customer_code']); ?></td>
+                        <td><?php echo htmlspecialchars($customer['country']); ?></td>
+                        <td><?php echo htmlspecialchars($customer['customer_type'] ?: '未分类'); ?></td>
+                        <td><?php echo htmlspecialchars($customer['employee_name']); ?></td>
+                        <td><?php echo date('Y-m-d', strtotime($customer['add_date'])); ?></td>
+                    </tr>
+                    <?php endforeach; ?>
+                </tbody>
+            </table>
+        </div>
+    </div>
+    
+    <!-- 新增客户添加时间分布图 -->
+    <div class="chart-container">
+        <div>
+            <canvas id="newCustomersChart"></canvas>
+        </div>
+    </div>
+    
+    <script>
+    // 准备图表数据
+    <?php
+    // 按日期分组客户数量
+    $dates = [];
+    $counts = [];
+    $dateGroups = [];
+    
+    foreach ($customers as $customer) {
+        $date = date('Y-m-d', strtotime($customer['add_date']));
+        if (!isset($dateGroups[$date])) {
+            $dateGroups[$date] = 0;
+        }
+        $dateGroups[$date]++;
+    }
+    
+    // 排序日期
+    ksort($dateGroups);
+    
+    foreach ($dateGroups as $date => $count) {
+        $dates[] = $date;
+        $counts[] = $count;
+    }
+    ?>
+    
+    var newCustomersCtx = document.getElementById('newCustomersChart').getContext('2d');
+    new Chart(newCustomersCtx, {
+        type: 'bar',
+        data: {
+            labels: <?php echo json_encode($dates); ?>,
+            datasets: [{
+                label: '新增客户数量',
+                data: <?php echo json_encode($counts); ?>,
+                backgroundColor: 'rgba(54, 162, 235, 0.5)',
+                borderColor: 'rgba(54, 162, 235, 1)',
+                borderWidth: 1
+            }]
+        },
+        options: {
+            responsive: true,
+            scales: {
+                y: {
+                    beginAtZero: true,
+                    title: {
+                        display: true,
+                        text: '客户数量'
+                    }
+                },
+                x: {
+                    title: {
+                        display: true,
+                        text: '日期'
+                    }
+                }
+            },
+            plugins: {
+                title: {
+                    display: true,
+                    text: '新增客户时间分布'
+                }
+            }
+        }
+    });
+    </script>
+    <?php
+}
+
+/**
+ * 渲染业务员新增客户图表
+ * 
+ * @param array $employee_data 业务员新增客户数据
+ * @return void
+ */
+function renderNewCustomersByEmployeeChart($employee_data) {
+    if (empty($employee_data)) {
+        echo '<div class="alert alert-info">该时间段内没有业务员新增客户数据</div>';
+        return;
+    }
+    
+    // 准备图表数据
+    $employee_names = [];
+    $customer_counts = [];
+    
+    foreach ($employee_data as $row) {
+        $employee_names[] = $row['employee_name'];
+        $customer_counts[] = $row['customer_count'];
+    }
+    
+    // 生成图表背景色
+    $colors = generateChartColors(count($employee_data));
+    $backgroundColors = [];
+    $borderColors = [];
+    
+    foreach ($colors as $color) {
+        $backgroundColors[] = $color[0];
+        $borderColors[] = $color[1];
+    }
+    ?>
+    
+    <div class="analysis-grid">
+        <div>
+            <canvas id="employeeNewCustomersChart"></canvas>
+        </div>
+        <div class="table-responsive">
+            <table class="data-table">
+                <thead>
+                    <tr>
+                        <th>业务员</th>
+                        <th>新增客户数量</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    <?php foreach ($employee_data as $row): ?>
+                    <tr>
+                        <td><?php echo htmlspecialchars($row['employee_name']); ?></td>
+                        <td><?php echo number_format($row['customer_count']); ?></td>
+                    </tr>
+                    <?php endforeach; ?>
+                </tbody>
+            </table>
+        </div>
+    </div>
+    
+    <script>
+    var employeeNewCustomersCtx = document.getElementById('employeeNewCustomersChart').getContext('2d');
+    new Chart(employeeNewCustomersCtx, {
+        type: 'bar',
+        data: {
+            labels: <?php echo json_encode($employee_names); ?>,
+            datasets: [{
+                label: '新增客户数量',
+                data: <?php echo json_encode($customer_counts); ?>,
+                backgroundColor: <?php echo json_encode($backgroundColors); ?>,
+                borderColor: <?php echo json_encode($borderColors); ?>,
+                borderWidth: 1
+            }]
+        },
+        options: {
+            responsive: true,
+            scales: {
+                y: {
+                    beginAtZero: true,
+                    title: {
+                        display: true,
+                        text: '客户数量'
+                    }
+                }
+            },
+            plugins: {
+                title: {
+                    display: true,
+                    text: '业务员新增客户统计'
+                }
+            }
+        }
+    });
+    </script>
+    <?php
 }