12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178 |
- <?php
- /**
- * 销售统计分析模块
- *
- * 包含与销售相关的数据分析功能
- */
- require_once 'statistics_utils.php';
- /**
- * 获取销售概览数据
- *
- * @param mysqli $conn 数据库连接
- * @param string $start_date 开始日期
- * @param string $end_date 结束日期
- * @return array 销售概览数据
- */
- function getSalesOverview($conn, $start_date, $end_date) {
- $sql = "SELECT
- COUNT(DISTINCT o.id) as total_orders,
- SUM(o.total_amount) as total_revenue,
- AVG(o.total_amount) as avg_order_value,
- COUNT(DISTINCT o.customer_id) as unique_customers,
- SUM(oi.quantity) as total_items_sold
- FROM orders o
- LEFT JOIN order_items oi ON o.id = oi.order_id
- WHERE o.order_date BETWEEN ? AND ?
- AND o.order_status != 0"; // 排除已取消订单
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("ss", $start_date, $end_date);
- $stmt->execute();
- return $stmt->get_result()->fetch_assoc();
- }
- /**
- * 获取订单转化率统计
- */
- function getOrderConversionStats($conn, $start_date, $end_date) {
- $sql = "SELECT
- order_status,
- COUNT(*) as count,
- SUM(total_amount) as amount
- FROM orders
- WHERE order_date BETWEEN ? AND ?
- GROUP BY order_status";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("ss", $start_date, $end_date);
- $stmt->execute();
- return $stmt->get_result();
- }
- /**
- * 获取产品类别销售统计
- */
- function getProductCategorySales($conn, $start_date, $end_date) {
- $sql = "SELECT
- pc.name as category_name,
- COUNT(DISTINCT o.id) as order_count,
- SUM(oi.quantity) as total_quantity,
- SUM(oi.total_price) as total_revenue
- FROM orders o
- JOIN order_items oi ON o.id = oi.order_id
- JOIN products p ON oi.product_id = p.id
- JOIN product_categories pc ON p.category_id = pc.id
- WHERE o.order_date BETWEEN ? AND ?
- AND o.order_status != 0
- GROUP BY pc.id
- ORDER BY total_revenue DESC";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("ss", $start_date, $end_date);
- $stmt->execute();
- return $stmt->get_result();
- }
- /**
- * 获取客户地区分布
- */
- function getCustomerDistribution($conn, $start_date, $end_date) {
- $sql = "SELECT
- c.countryName as region,
- COUNT(DISTINCT o.customer_id) as customer_count,
- COUNT(o.id) as order_count,
- SUM(o.total_amount) as total_revenue
- FROM orders o
- JOIN customer cu ON o.customer_id = cu.id
- JOIN country c ON cu.cs_country = c.id
- WHERE o.order_date BETWEEN ? AND ?
- AND o.order_status != 0
- GROUP BY c.id
- ORDER BY total_revenue DESC";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("ss", $start_date, $end_date);
- $stmt->execute();
- return $stmt->get_result();
- }
- /**
- * 获取销售员业绩统计
- */
- function getEmployeePerformance($conn, $start_date, $end_date) {
- $sql = "SELECT
- e.em_user as employee_name,
- COUNT(DISTINCT o.id) as order_count,
- COUNT(DISTINCT o.customer_id) as customer_count,
- SUM(o.total_amount) as total_revenue,
- AVG(o.total_amount) as avg_order_value
- FROM orders o
- JOIN employee e ON o.employee_id = e.id
- WHERE o.order_date BETWEEN ? AND ?
- AND o.order_status != 0
- GROUP BY e.id
- ORDER BY total_revenue DESC";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("ss", $start_date, $end_date);
- $stmt->execute();
- return $stmt->get_result();
- }
- /**
- * 获取支付状态统计
- */
- function getPaymentStatusStats($conn, $start_date, $end_date) {
- $sql = "SELECT
- payment_status,
- COUNT(*) as count,
- SUM(total_amount) as amount
- FROM orders
- WHERE order_date BETWEEN ? AND ?
- AND order_status != 0
- GROUP BY payment_status";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("ss", $start_date, $end_date);
- $stmt->execute();
- return $stmt->get_result();
- }
- /**
- * 获取每月销售趋势
- *
- * @param mysqli $conn 数据库连接
- * @param string $start_date 开始日期
- * @param string $end_date 结束日期
- * @return mysqli_result 月度销售数据结果集
- */
- function getMonthlySalesTrend($conn, $start_date, $end_date) {
- $sql = "SELECT
- DATE_FORMAT(order_date, '%Y-%m') as month,
- COUNT(DISTINCT id) as orders,
- SUM(total_amount) as revenue,
- COUNT(DISTINCT customer_id) as unique_customers
- FROM orders
- WHERE order_date BETWEEN ? AND ?
- AND order_status != 0
- GROUP BY DATE_FORMAT(order_date, '%Y-%m')
- ORDER BY month";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("ss", $start_date, $end_date);
- $stmt->execute();
- return $stmt->get_result();
- }
- /**
- * 获取详细时间段订单趋势
- *
- * @param mysqli $conn 数据库连接
- * @param string $start_date 开始日期
- * @param string $end_date 结束日期
- * @param string $period 时间粒度 (day/week/month)
- * @return mysqli_result 订单趋势数据结果集
- */
- function getDetailedOrderTrend($conn, $start_date, $end_date, $period = 'day') {
- $groupFormat = '%Y-%m-%d';
-
- if ($period == 'week') {
- $groupFormat = '%x-W%v';
- } else if ($period == 'month') {
- $groupFormat = '%Y-%m';
- }
-
- $sql = "SELECT
- DATE_FORMAT(o.order_date, '$groupFormat') as time_period,
- COUNT(DISTINCT o.id) as order_count,
- SUM(oi.quantity) as total_quantity,
- SUM(o.total_amount) as total_amount,
- COUNT(DISTINCT o.customer_id) as unique_customers
- FROM orders o
- LEFT JOIN order_items oi ON o.id = oi.order_id
- WHERE o.order_date BETWEEN ? AND ?
- AND o.order_status != 0
- GROUP BY time_period
- ORDER BY MIN(o.order_date)";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("ss", $start_date, $end_date);
- $stmt->execute();
- return $stmt->get_result();
- }
- /**
- * 渲染销售概览卡片
- *
- * @param array $sales_overview 销售概览数据
- * @return void
- */
- function renderSalesOverviewCards($sales_overview) {
- // 添加空值检查函数
-
- function formatCurrency($value) {
- return '¥' . number_format($value ?? 0, 2);
- }
- ?>
- <div class="stats-grid">
- <div class="stat-card">
- <h3>总订单数</h3>
- <div class="stat-value"><?php echo formatNumber($sales_overview['total_orders']); ?></div>
- </div>
-
- <div class="stat-card">
- <h3>总收入</h3>
- <div class="stat-value"><?php echo formatCurrency($sales_overview['total_revenue']); ?></div>
- </div>
-
- <div class="stat-card">
- <h3>平均订单金额</h3>
- <div class="stat-value"><?php echo formatCurrency($sales_overview['avg_order_value']); ?></div>
- </div>
- <div class="stat-card">
- <h3>独立客户数</h3>
- <div class="stat-value"><?php echo formatNumber($sales_overview['unique_customers']); ?></div>
- </div>
- <div class="stat-card">
- <h3>总销售数量</h3>
- <div class="stat-value"><?php echo formatNumber($sales_overview['total_items_sold']); ?></div>
- </div>
- </div>
- <?php
- }
- /**
- * 渲染订单转化率分析
- */
- function renderConversionAnalysis($conversion_stats) {
- // 添加空值检查函数
-
- $status_names = [
- 1 => '待确认',
- 2 => '已确认',
- 3 => '生产中',
- 4 => '已发货',
- 5 => '已完成',
- 0 => '已取消'
- ];
-
- $total_orders = 0;
- $data = [];
- while ($row = $conversion_stats->fetch_assoc()) {
- $total_orders += $row['count'];
- $data[$row['order_status']] = $row;
- }
- ?>
- <div class="analysis-grid">
- <div>
- <canvas id="orderStatusChart"></canvas>
- </div>
- <div class="table-responsive">
- <table class="data-table">
- <thead>
- <tr>
- <th>订单状态</th>
- <th>订单数</th>
- <th>转化率</th>
- <th>金额</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($status_names as $status_id => $status_name): ?>
- <?php if (isset($data[$status_id])): ?>
- <tr>
- <td><?php echo $status_name; ?></td>
- <td><?php echo formatNumber($data[$status_id]['count']); ?></td>
- <td><?php echo formatNumber(($data[$status_id]['count'] / ($total_orders ?: 1)) * 100, 1); ?>%</td>
- <td><?php echo formatCurrency($data[$status_id]['amount']); ?></td>
- </tr>
- <?php endif; ?>
- <?php endforeach; ?>
- </tbody>
- </table>
- </div>
- </div>
- <script>
- var orderStatusCtx = document.getElementById('orderStatusChart').getContext('2d');
- new Chart(orderStatusCtx, {
- type: 'doughnut',
- data: {
- labels: <?php
- $labels = [];
- $values = [];
- foreach ($status_names as $status_id => $status_name) {
- if (isset($data[$status_id])) {
- $labels[] = $status_name;
- $values[] = $data[$status_id]['count'];
- }
- }
- echo json_encode($labels);
- ?>,
- datasets: [{
- data: <?php echo json_encode($values); ?>,
- backgroundColor: [
- '#FF6384',
- '#36A2EB',
- '#FFCE56',
- '#4BC0C0',
- '#9966FF',
- '#FF9F40'
- ]
- }]
- },
- options: {
- responsive: true,
- plugins: {
- legend: {
- position: 'right'
- },
- title: {
- display: true,
- text: '订单状态分布'
- }
- }
- }
- });
- </script>
- <?php
- }
- /**
- * 渲染产品类别销售分析图表
- */
- function renderCategorySalesChart($category_sales) {
- $labels = [];
- $quantities = [];
- $revenues = [];
-
- while ($row = $category_sales->fetch_assoc()) {
- $labels[] = $row['category_name'];
- $quantities[] = $row['total_quantity'];
- $revenues[] = $row['total_revenue'];
- }
- ?>
- <div class="analysis-grid">
- <div>
- <canvas id="categorySalesChart"></canvas>
- </div>
- <div class="table-responsive">
- <table class="data-table">
- <thead>
- <tr>
- <th>产品类别</th>
- <th>订单数</th>
- <th>销售数量</th>
- <th>销售金额</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $category_sales->data_seek(0);
- while ($row = $category_sales->fetch_assoc()):
- ?>
- <tr>
- <td><?php echo $row['category_name']; ?></td>
- <td><?php echo number_format($row['order_count']); ?></td>
- <td><?php echo number_format($row['total_quantity']); ?></td>
- <td>¥<?php echo number_format($row['total_revenue'], 2); ?></td>
- </tr>
- <?php endwhile; ?>
- </tbody>
- </table>
- </div>
- </div>
- <script>
- var categorySalesCtx = document.getElementById('categorySalesChart').getContext('2d');
- new Chart(categorySalesCtx, {
- type: 'bar',
- data: {
- labels: <?php echo json_encode($labels); ?>,
- datasets: [
- {
- label: '销售数量',
- data: <?php echo json_encode($quantities); ?>,
- backgroundColor: 'rgba(54, 162, 235, 0.5)',
- borderColor: 'rgba(54, 162, 235, 1)',
- borderWidth: 1,
- yAxisID: 'y-quantity'
- },
- {
- label: '销售金额',
- data: <?php echo json_encode($revenues); ?>,
- backgroundColor: 'rgba(255, 99, 132, 0.5)',
- borderColor: 'rgba(255, 99, 132, 1)',
- borderWidth: 1,
- yAxisID: 'y-revenue'
- }
- ]
- },
- options: {
- responsive: true,
- scales: {
- 'y-quantity': {
- type: 'linear',
- position: 'left',
- title: {
- display: true,
- text: '销售数量'
- }
- },
- 'y-revenue': {
- type: 'linear',
- position: 'right',
- title: {
- display: true,
- text: '销售金额'
- },
- grid: {
- drawOnChartArea: false
- }
- }
- }
- }
- });
- </script>
- <?php
- }
- /**
- * 渲染客户地区分布图表
- */
- function renderCustomerDistributionChart($customer_distribution) {
- $regions = [];
- $customers = [];
- $revenues = [];
-
- while ($row = $customer_distribution->fetch_assoc()) {
- $regions[] = $row['region'];
- $customers[] = $row['customer_count'];
- $revenues[] = $row['total_revenue'];
- }
- ?>
- <div class="analysis-grid">
- <div>
- <canvas id="regionDistributionChart"></canvas>
- </div>
- <div class="table-responsive">
- <table class="data-table">
- <thead>
- <tr>
- <th>地区</th>
- <th>客户数</th>
- <th>订单数</th>
- <th>销售金额</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $customer_distribution->data_seek(0);
- while ($row = $customer_distribution->fetch_assoc()):
- ?>
- <tr>
- <td><?php echo $row['region']; ?></td>
- <td><?php echo number_format($row['customer_count']); ?></td>
- <td><?php echo number_format($row['order_count']); ?></td>
- <td>¥<?php echo number_format($row['total_revenue'], 2); ?></td>
- </tr>
- <?php endwhile; ?>
- </tbody>
- </table>
- </div>
- </div>
- <script>
- var regionDistributionCtx = document.getElementById('regionDistributionChart').getContext('2d');
- new Chart(regionDistributionCtx, {
- type: 'bar',
- data: {
- labels: <?php echo json_encode($regions); ?>,
- datasets: [
- {
- label: '客户数',
- data: <?php echo json_encode($customers); ?>,
- backgroundColor: 'rgba(75, 192, 192, 0.5)',
- borderColor: 'rgba(75, 192, 192, 1)',
- borderWidth: 1,
- yAxisID: 'y-customers'
- },
- {
- label: '销售金额',
- data: <?php echo json_encode($revenues); ?>,
- backgroundColor: 'rgba(255, 159, 64, 0.5)',
- borderColor: 'rgba(255, 159, 64, 1)',
- borderWidth: 1,
- yAxisID: 'y-revenue'
- }
- ]
- },
- options: {
- responsive: true,
- scales: {
- 'y-customers': {
- type: 'linear',
- position: 'left',
- title: {
- display: true,
- text: '客户数'
- }
- },
- 'y-revenue': {
- type: 'linear',
- position: 'right',
- title: {
- display: true,
- text: '销售金额'
- },
- grid: {
- drawOnChartArea: false
- }
- }
- }
- }
- });
- </script>
- <?php
- }
- /**
- * 渲染销售员业绩表格
- */
- function renderEmployeePerformanceTable($employee_performance) {
- // 添加空值检查函数
- ?>
- <div class="table-responsive">
- <table class="data-table">
- <thead>
- <tr>
- <th>销售员</th>
- <th>订单数</th>
- <th>客户数</th>
- <th>总销售额</th>
- <th>平均订单金额</th>
- </tr>
- </thead>
- <tbody>
- <?php while ($row = $employee_performance->fetch_assoc()): ?>
- <tr>
- <td><?php echo htmlspecialchars($row['employee_name']); ?></td>
- <td><?php echo formatNumber($row['order_count']); ?></td>
- <td><?php echo formatNumber($row['customer_count']); ?></td>
- <td><?php echo formatCurrency($row['total_revenue']); ?></td>
- <td><?php echo formatCurrency($row['avg_order_value']); ?></td>
- </tr>
- <?php endwhile; ?>
- </tbody>
- </table>
- </div>
- <?php
- }
- /**
- * 渲染支付状态分析图表
- */
- function renderPaymentStatusChart($payment_stats) {
- $status_names = [
- 0 => '未付款',
- 1 => '部分付款',
- 2 => '已付清'
- ];
-
- $data = [];
- while ($row = $payment_stats->fetch_assoc()) {
- $data[$row['payment_status']] = $row;
- }
- ?>
- <div class="analysis-grid">
- <div>
- <canvas id="paymentStatusChart"></canvas>
- </div>
- <div class="table-responsive">
- <table class="data-table">
- <thead>
- <tr>
- <th>支付状态</th>
- <th>订单数</th>
- <th>订单金额</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($status_names as $status_id => $status_name): ?>
- <?php if (isset($data[$status_id])): ?>
- <tr>
- <td><?php echo $status_name; ?></td>
- <td><?php echo number_format($data[$status_id]['count']); ?></td>
- <td>¥<?php echo number_format($data[$status_id]['amount'], 2); ?></td>
- </tr>
- <?php endif; ?>
- <?php endforeach; ?>
- </tbody>
- </table>
- </div>
- </div>
- <script>
- var paymentStatusCtx = document.getElementById('paymentStatusChart').getContext('2d');
- new Chart(paymentStatusCtx, {
- type: 'pie',
- data: {
- labels: <?php
- $labels = [];
- $values = [];
- foreach ($status_names as $status_id => $status_name) {
- if (isset($data[$status_id])) {
- $labels[] = $status_name;
- $values[] = $data[$status_id]['amount'];
- }
- }
- echo json_encode($labels);
- ?>,
- datasets: [{
- data: <?php echo json_encode($values); ?>,
- backgroundColor: [
- '#FF6384',
- '#36A2EB',
- '#4BC0C0'
- ]
- }]
- },
- options: {
- responsive: true,
- plugins: {
- legend: {
- position: 'right'
- },
- title: {
- display: true,
- text: '支付状态分布'
- }
- }
- }
- });
- </script>
- <?php
- }
- /**
- * 渲染月度销售趋势图
- *
- * @param array $monthly_labels 月份标签
- * @param array $monthly_orders 月度订单数量
- * @param array $monthly_revenue 月度收入
- * @return void
- */
- function renderMonthlySalesTrendChart($monthly_labels, $monthly_orders, $monthly_revenue) {
- ?>
- <div class="chart-container">
- <div class="chart-header">
- <h2 class="chart-title">销售趋势</h2>
- </div>
- <canvas id="salesTrendChart"></canvas>
- </div>
-
- <script>
- var salesTrendCtx = document.getElementById('salesTrendChart').getContext('2d');
- new Chart(salesTrendCtx, {
- type: 'line',
- data: {
- labels: <?php echo json_encode($monthly_labels); ?>,
- datasets: [
- {
- label: '订单数量',
- data: <?php echo json_encode($monthly_orders); ?>,
- backgroundColor: 'rgba(54, 162, 235, 0.2)',
- borderColor: 'rgba(54, 162, 235, 1)',
- borderWidth: 2,
- yAxisID: 'y-orders',
- tension: 0.1
- },
- {
- label: '销售收入',
- data: <?php echo json_encode($monthly_revenue); ?>,
- backgroundColor: 'rgba(255, 99, 132, 0.2)',
- borderColor: 'rgba(255, 99, 132, 1)',
- borderWidth: 2,
- yAxisID: 'y-revenue',
- tension: 0.1
- }
- ]
- },
- options: {
- responsive: true,
- scales: {
- 'y-orders': {
- type: 'linear',
- position: 'left',
- title: {
- display: true,
- text: '订单数量'
- }
- },
- 'y-revenue': {
- type: 'linear',
- position: 'right',
- title: {
- display: true,
- text: '销售收入'
- },
- grid: {
- drawOnChartArea: false
- }
- }
- }
- }
- });
- </script>
- <?php
- }
- /**
- * 渲染详细订单趋势图
- *
- * @param array $time_labels 时间标签
- * @param array $time_orders 时间段订单数量
- * @param array $time_quantities 时间段产品数量
- * @param string $period 时间粒度
- * @return void
- */
- function renderDetailedOrderTrendChart($time_labels, $time_orders, $time_quantities, $period = 'day') {
- $period_text = $period == 'day' ? '日' : ($period == 'week' ? '周' : '月');
- ?>
- <div class="chart-container">
- <div class="chart-header">
- <h2 class="chart-title">详细订单趋势 (<?php echo $period_text; ?>)</h2>
- </div>
- <canvas id="detailedOrdersChart"></canvas>
- </div>
-
- <script>
- var detailedOrdersCtx = document.getElementById('detailedOrdersChart').getContext('2d');
- new Chart(detailedOrdersCtx, {
- type: 'line',
- data: {
- labels: <?php echo json_encode($time_labels); ?>,
- datasets: [
- {
- label: '订单数量',
- data: <?php echo json_encode($time_orders); ?>,
- backgroundColor: 'rgba(75, 192, 192, 0.2)',
- borderColor: 'rgba(75, 192, 192, 1)',
- borderWidth: 2,
- yAxisID: 'y-orders',
- tension: 0.1
- },
- {
- label: '产品订购数量',
- data: <?php echo json_encode($time_quantities); ?>,
- backgroundColor: 'rgba(255, 159, 64, 0.2)',
- borderColor: 'rgba(255, 159, 64, 1)',
- borderWidth: 2,
- yAxisID: 'y-quantity',
- tension: 0.1
- }
- ]
- },
- options: {
- responsive: true,
- scales: {
- x: {
- title: {
- display: true,
- text: '时间'
- }
- },
- 'y-orders': {
- type: 'linear',
- position: 'left',
- title: {
- display: true,
- text: '订单数量'
- },
- beginAtZero: true
- },
- 'y-quantity': {
- type: 'linear',
- position: 'right',
- title: {
- display: true,
- text: '产品订购数量'
- },
- beginAtZero: true,
- grid: {
- drawOnChartArea: false
- }
- }
- }
- }
- });
- </script>
- <?php
- }
- /**
- * 获取所有业务员列表
- */
- function getAllEmployees($conn) {
- $sql = "SELECT id, em_user, em_email, em_tel FROM employee ORDER BY em_user";
- $result = $conn->query($sql);
- return $result->fetch_all(MYSQLI_ASSOC);
- }
- /**
- * 获取业务员详细信息
- */
- function getEmployeeDetail($conn, $employee_id) {
- $sql = "SELECT id, em_user, em_email, em_tel FROM employee WHERE id = ?";
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("i", $employee_id);
- $stmt->execute();
- return $stmt->get_result()->fetch_assoc();
- }
- /**
- * 获取业务员统计数据
- */
- function getEmployeeStats($conn, $employee_id, $start_date, $end_date) {
- $sql = "SELECT
- COUNT(DISTINCT o.id) as total_orders,
- SUM(o.total_amount) as total_revenue,
- COUNT(DISTINCT o.customer_id) as customer_count,
- AVG(o.total_amount) as avg_order_value,
- SUM(CASE WHEN o.order_status = 5 THEN 1 ELSE 0 END) * 100.0 / COUNT(*) as completion_rate
- FROM orders o
- WHERE o.employee_id = ?
- AND o.order_date BETWEEN ? AND ?
- AND o.order_status != 0";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("iss", $employee_id, $start_date, $end_date);
- $stmt->execute();
- return $stmt->get_result()->fetch_assoc();
- }
- /**
- * 渲染业务员销售趋势
- */
- function renderEmployeeSalesTrend($conn, $employee_id, $start_date, $end_date) {
- $sql = "SELECT
- DATE_FORMAT(order_date, '%Y-%m-%d') as date,
- COUNT(DISTINCT id) as orders,
- SUM(total_amount) as revenue
- FROM orders
- WHERE employee_id = ?
- AND order_date BETWEEN ? AND ?
- AND order_status != 0
- GROUP BY DATE_FORMAT(order_date, '%Y-%m-%d')
- ORDER BY date";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("iss", $employee_id, $start_date, $end_date);
- $stmt->execute();
- $result = $stmt->get_result();
-
- $dates = [];
- $orders = [];
- $revenues = [];
-
- while ($row = $result->fetch_assoc()) {
- $dates[] = $row['date'];
- $orders[] = $row['orders'];
- $revenues[] = $row['revenue'];
- }
- ?>
- <div class="chart-container">
- <div class="chart-header">
- <h2 class="chart-title">销售趋势</h2>
- </div>
- <canvas id="employeeSalesTrendChart"></canvas>
- </div>
-
- <script>
- var employeeSalesTrendCtx = document.getElementById('employeeSalesTrendChart').getContext('2d');
- new Chart(employeeSalesTrendCtx, {
- type: 'line',
- data: {
- labels: <?php echo json_encode($dates); ?>,
- datasets: [
- {
- label: '订单数量',
- data: <?php echo json_encode($orders); ?>,
- backgroundColor: 'rgba(54, 162, 235, 0.2)',
- borderColor: 'rgba(54, 162, 235, 1)',
- borderWidth: 2,
- yAxisID: 'y-orders',
- tension: 0.1
- },
- {
- label: '销售收入',
- data: <?php echo json_encode($revenues); ?>,
- backgroundColor: 'rgba(255, 99, 132, 0.2)',
- borderColor: 'rgba(255, 99, 132, 1)',
- borderWidth: 2,
- yAxisID: 'y-revenue',
- tension: 0.1
- }
- ]
- },
- options: {
- responsive: true,
- scales: {
- 'y-orders': {
- type: 'linear',
- position: 'left',
- title: {
- display: true,
- text: '订单数量'
- }
- },
- 'y-revenue': {
- type: 'linear',
- position: 'right',
- title: {
- display: true,
- text: '销售收入'
- },
- grid: {
- drawOnChartArea: false
- }
- }
- }
- }
- });
- </script>
- <?php
- }
- /**
- * 渲染业务员客户分布
- */
- function renderEmployeeCustomerDistribution($conn, $employee_id, $start_date, $end_date) {
- $sql = "SELECT
- c.countryName as region,
- COUNT(DISTINCT o.customer_id) as customer_count,
- SUM(o.total_amount) as total_revenue
- FROM orders o
- JOIN customer cu ON o.customer_id = cu.id
- JOIN country c ON cu.cs_country = c.id
- WHERE o.employee_id = ?
- AND o.order_date BETWEEN ? AND ?
- AND o.order_status != 0
- GROUP BY c.id
- ORDER BY total_revenue DESC
- LIMIT 10";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("iss", $employee_id, $start_date, $end_date);
- $stmt->execute();
- $result = $stmt->get_result();
-
- $regions = [];
- $customers = [];
- $revenues = [];
-
- while ($row = $result->fetch_assoc()) {
- $regions[] = $row['region'];
- $customers[] = $row['customer_count'];
- $revenues[] = $row['total_revenue'];
- }
- ?>
- <div class="chart-container">
- <div class="chart-header">
- <h2 class="chart-title">客户地区分布 (Top 10)</h2>
- </div>
- <canvas id="employeeCustomerChart"></canvas>
- </div>
-
- <script>
- var employeeCustomerCtx = document.getElementById('employeeCustomerChart').getContext('2d');
- new Chart(employeeCustomerCtx, {
- type: 'bar',
- data: {
- labels: <?php echo json_encode($regions); ?>,
- datasets: [
- {
- label: '客户数',
- data: <?php echo json_encode($customers); ?>,
- backgroundColor: 'rgba(75, 192, 192, 0.5)',
- borderColor: 'rgba(75, 192, 192, 1)',
- borderWidth: 1,
- yAxisID: 'y-customers'
- },
- {
- label: '销售金额',
- data: <?php echo json_encode($revenues); ?>,
- backgroundColor: 'rgba(255, 159, 64, 0.5)',
- borderColor: 'rgba(255, 159, 64, 1)',
- borderWidth: 1,
- yAxisID: 'y-revenue'
- }
- ]
- },
- options: {
- responsive: true,
- scales: {
- 'y-customers': {
- type: 'linear',
- position: 'left',
- title: {
- display: true,
- text: '客户数'
- }
- },
- 'y-revenue': {
- type: 'linear',
- position: 'right',
- title: {
- display: true,
- text: '销售金额'
- },
- grid: {
- drawOnChartArea: false
- }
- }
- }
- }
- });
- </script>
- <?php
- }
- /**
- * 渲染业务员产品销售分析
- */
- function renderEmployeeProductAnalysis($conn, $employee_id, $start_date, $end_date) {
- $sql = "SELECT
- pc.name as category_name,
- COUNT(DISTINCT o.id) as order_count,
- SUM(oi.quantity) as total_quantity,
- SUM(oi.total_price) as total_revenue
- FROM orders o
- JOIN order_items oi ON o.id = oi.order_id
- JOIN products p ON oi.product_id = p.id
- JOIN product_categories pc ON p.category_id = pc.id
- WHERE o.employee_id = ?
- AND o.order_date BETWEEN ? AND ?
- AND o.order_status != 0
- GROUP BY pc.id
- ORDER BY total_revenue DESC";
-
- $stmt = $conn->prepare($sql);
- $stmt->bind_param("iss", $employee_id, $start_date, $end_date);
- $stmt->execute();
- $result = $stmt->get_result();
-
- $categories = [];
- $quantities = [];
- $revenues = [];
-
- while ($row = $result->fetch_assoc()) {
- $categories[] = $row['category_name'];
- $quantities[] = $row['total_quantity'];
- $revenues[] = $row['total_revenue'];
- }
- ?>
- <div class="chart-container">
- <div class="chart-header">
- <h2 class="chart-title">产品类别销售分析</h2>
- </div>
- <canvas id="employeeProductChart"></canvas>
- </div>
-
- <script>
- var employeeProductCtx = document.getElementById('employeeProductChart').getContext('2d');
- new Chart(employeeProductCtx, {
- type: 'bar',
- data: {
- labels: <?php echo json_encode($categories); ?>,
- datasets: [
- {
- label: '销售数量',
- data: <?php echo json_encode($quantities); ?>,
- backgroundColor: 'rgba(54, 162, 235, 0.5)',
- borderColor: 'rgba(54, 162, 235, 1)',
- borderWidth: 1,
- yAxisID: 'y-quantity'
- },
- {
- label: '销售金额',
- data: <?php echo json_encode($revenues); ?>,
- backgroundColor: 'rgba(255, 99, 132, 0.5)',
- borderColor: 'rgba(255, 99, 132, 1)',
- borderWidth: 1,
- yAxisID: 'y-revenue'
- }
- ]
- },
- options: {
- responsive: true,
- scales: {
- 'y-quantity': {
- type: 'linear',
- position: 'left',
- title: {
- display: true,
- text: '销售数量'
- }
- },
- 'y-revenue': {
- type: 'linear',
- position: 'right',
- title: {
- display: true,
- text: '销售金额'
- },
- grid: {
- drawOnChartArea: false
- }
- }
- }
- }
- });
- </script>
- <?php
- }
- /**
- * 渲染业务员统计卡片
- */
- function renderEmployeeStats($employee_stats) {
- // 添加空值检查函数
- function formatNumber($value, $decimals = 0) {
- return number_format($value ?? 0, $decimals);
- }
-
- function formatCurrency($value) {
- return '¥' . number_format($value ?? 0, 2);
- }
- ?>
- <div class="performance-grid">
- <div class="performance-card">
- <div class="performance-label">总订单数</div>
- <div class="performance-value"><?php echo formatNumber($employee_stats['total_orders']); ?></div>
- </div>
- <div class="performance-card">
- <div class="performance-label">总销售额</div>
- <div class="performance-value"><?php echo formatCurrency($employee_stats['total_revenue']); ?></div>
- </div>
- <div class="performance-card">
- <div class="performance-label">客户数量</div>
- <div class="performance-value"><?php echo formatNumber($employee_stats['customer_count']); ?></div>
- </div>
- <div class="performance-card">
- <div class="performance-label">平均订单金额</div>
- <div class="performance-value"><?php echo formatCurrency($employee_stats['avg_order_value']); ?></div>
- </div>
- <div class="performance-card">
- <div class="performance-label">订单完成率</div>
- <div class="performance-value"><?php echo formatNumber($employee_stats['completion_rate'], 1); ?>%</div>
- </div>
- </div>
- <?php
- }
|