products_stats.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * 产品统计分析展示页面
  4. */
  5. require_once 'conn.php';
  6. require_once 'statistics_utils.php';
  7. require_once 'statistics_products.php';
  8. // 检查登录状态
  9. if (!isset($_SESSION['employee_id'])) {
  10. checkLogin();
  11. }
  12. // 获取日期范围参数
  13. $date_params = getDateRangeParams();
  14. $start_date = $date_params['start_date_sql'];
  15. $end_date = $date_params['end_date_sql'];
  16. $date_range = $date_params['date_range'];
  17. $period = $date_params['period'];
  18. // 特定产品过滤
  19. $product_id = isset($_GET['product_id']) ? intval($_GET['product_id']) : 0;
  20. // 页面头部
  21. include('statistics_header.php');
  22. ?>
  23. <div class="container">
  24. <div class="page-header">
  25. <h1 class="page-title">产品统计分析</h1>
  26. </div>
  27. <!-- 日期筛选 -->
  28. <div class="filter-form">
  29. <form method="get" class="filter-form-inline">
  30. <div class="form-group">
  31. <label for="date_range">选择日期范围</label>
  32. <select class="form-control" id="date_range" name="date_range" onchange="toggleCustomDates()">
  33. <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
  34. <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
  35. <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
  36. <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>最近30天</option>
  37. <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>最近90天</option>
  38. <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
  39. </select>
  40. </div>
  41. <div class="form-group custom-date-inputs" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  42. <label for="start_date">开始日期</label>
  43. <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
  44. </div>
  45. <div class="form-group custom-date-inputs" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  46. <label for="end_date">结束日期</label>
  47. <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
  48. </div>
  49. <div class="form-group">
  50. <label for="period">时间粒度</label>
  51. <select class="form-control" id="period" name="period">
  52. <option value="day" <?php echo $period == 'day' ? 'selected' : ''; ?>>日</option>
  53. <option value="week" <?php echo $period == 'week' ? 'selected' : ''; ?>>周</option>
  54. <option value="month" <?php echo $period == 'month' ? 'selected' : ''; ?>>月</option>
  55. </select>
  56. </div>
  57. <div class="form-group">
  58. <label for="category_id">产品分类</label>
  59. <select class="form-control" id="category_id" name="category_id">
  60. <option value="0">全部分类</option>
  61. <?php
  62. $category_filter = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;
  63. $categories = getProductCategories($conn);
  64. while($category = $categories->fetch_assoc()) {
  65. $selected = ($category_filter == $category['id']) ? 'selected' : '';
  66. echo "<option value='{$category['id']}' {$selected}>{$category['name']}</option>";
  67. }
  68. ?>
  69. </select>
  70. </div>
  71. <div class="form-group">
  72. <button type="submit" class="btn">应用筛选</button>
  73. </div>
  74. </form>
  75. </div>
  76. <!-- 产品销售概览 -->
  77. <div class="key-metrics-section">
  78. <div class="section-title">
  79. <h2>产品销售概览</h2>
  80. </div>
  81. <?php
  82. $sales_overview = getProductSalesOverview($conn, $start_date, $end_date, $category_filter);
  83. renderProductSalesOverview($sales_overview);
  84. ?>
  85. </div>
  86. <!-- 热门产品 -->
  87. <div class="chart-container">
  88. <?php
  89. $top_products = getTopProducts($conn, $start_date, $end_date, 10);
  90. renderTopProductsTable($top_products);
  91. ?>
  92. </div>
  93. <!-- 新客户购买产品明细 -->
  94. <div class="section-container">
  95. <div class="section-title">
  96. <h2>新客户购买产品明细</h2>
  97. </div>
  98. <?php
  99. $new_customer_products = getNewCustomerProductPurchases($conn, $start_date, $end_date, $category_filter);
  100. renderNewCustomerProductPurchases($new_customer_products);
  101. ?>
  102. </div>
  103. <!-- 产品销售趋势 -->
  104. <div class="chart-container" style="display: none">
  105. <?php
  106. $product_trends = getProductSalesTrend($conn, $start_date, $end_date, $product_id, $period);
  107. $time_labels = [];
  108. $quantities = [];
  109. $revenues = [];
  110. while ($row = $product_trends->fetch_assoc()) {
  111. $time_labels[] = $row['time_period'];
  112. $quantities[] = $row['total_quantity'];
  113. $revenues[] = $row['total_revenue'];
  114. }
  115. renderProductSalesTrendChart($time_labels, $quantities, $revenues);
  116. ?>
  117. </div>
  118. <!-- 产品类别销售分布 -->
  119. <div class="chart-container" style="display: none">
  120. <?php
  121. $category_sales = getProductCategorySales($conn, $start_date, $end_date);
  122. $categories = [];
  123. $category_quantities = [];
  124. $category_revenues = [];
  125. while ($row = $category_sales->fetch_assoc()) {
  126. $categories[] = $row['category_name'];
  127. $category_quantities[] = $row['total_quantity'];
  128. $category_revenues[] = $row['total_revenue'];
  129. }
  130. renderProductCategorySalesChart($categories, $category_quantities, $category_revenues);
  131. ?>
  132. </div>
  133. <!-- 产品价格趋势分析 -->
  134. <div class="chart-container" style="display: none">
  135. <?php
  136. $price_trend_data = getProductPriceTrendAnalysis($conn, $start_date, $end_date, $product_id, $period);
  137. renderProductPriceTrendChart($price_trend_data);
  138. ?>
  139. </div>
  140. <!-- 产品季节性分析 -->
  141. <div class="chart-container" style="display: none">
  142. <?php
  143. $seasonality_data = getProductSeasonalityAnalysis($conn, $start_date, $end_date, $product_id);
  144. renderProductSeasonalityChart($seasonality_data);
  145. ?>
  146. </div>
  147. <!-- 产品客户细分分析 -->
  148. <div class="chart-container" style="display: none">
  149. <?php
  150. $customer_segment_data = getProductCustomerSegmentAnalysis($conn, $start_date, $end_date, $product_id);
  151. renderProductCustomerSegmentChart($customer_segment_data);
  152. ?>
  153. </div>
  154. <!-- 产品地区关联分析 -->
  155. <div class="chart-container" style="display: none">
  156. <?php
  157. $product_region_data = getProductRegionAnalysis($conn, $start_date, $end_date);
  158. renderProductRegionAnalysisTable($product_region_data);
  159. ?>
  160. </div>
  161. <!-- 产品增长率分析 -->
  162. <div class="chart-container" style="display: none">
  163. <?php
  164. $growth_data = getProductGrowthAnalysis($conn, $start_date, $end_date, $period);
  165. renderProductGrowthAnalysis($growth_data);
  166. ?>
  167. </div>
  168. <!-- 产品购买频率分析 -->
  169. <div class="chart-container" style="display: none">
  170. <?php
  171. $frequency_data = getProductPurchaseFrequency($conn, $start_date, $end_date);
  172. renderProductPurchaseFrequency($frequency_data);
  173. ?>
  174. </div>
  175. </div>
  176. <script>
  177. function toggleCustomDates() {
  178. const dateRange = document.getElementById('date_range').value;
  179. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  180. if (dateRange === 'custom') {
  181. customDateInputs.forEach(el => el.style.display = 'inline-block');
  182. } else {
  183. customDateInputs.forEach(el => el.style.display = 'none');
  184. }
  185. }
  186. // 初始化所有图表的配置
  187. Chart.defaults.font.family = "'Arial', sans-serif";
  188. Chart.defaults.color = '#666';
  189. Chart.defaults.plugins.tooltip.backgroundColor = 'rgba(0, 0, 0, 0.8)';
  190. Chart.defaults.plugins.legend.position = 'bottom';
  191. </script>
  192. <?php
  193. // 页面底部
  194. include('statistics_footer.php');
  195. ?>