statistics_utils.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /**
  3. * 统计分析工具函数
  4. *
  5. * 包含所有统计模块共用的工具函数和日期处理逻辑
  6. */
  7. // 确保直接访问时需要先登录
  8. require_once 'conn.php';
  9. if (!isset($_SESSION['employee_id'])) {
  10. checkLogin();
  11. }
  12. //检查是否管理员
  13. checkPermissionDie(1,2);
  14. /**
  15. * 获取和处理日期范围参数
  16. *
  17. * @return array 包含开始日期、结束日期和其他日期相关参数
  18. */
  19. function getDateRangeParams() {
  20. // 计算日期范围
  21. $current_month_start = date('Y-m-01');
  22. $current_month_end = date('Y-m-t');
  23. $last_month_start = date('Y-m-01', strtotime('-1 month'));
  24. $last_month_end = date('Y-m-t', strtotime('-1 month'));
  25. $current_year_start = date('Y-01-01');
  26. $current_year_end = date('Y-12-31');
  27. // 可选的日期范围筛选
  28. $date_range = isset($_GET['date_range']) ? $_GET['date_range'] : 'current_month';
  29. $custom_start = isset($_GET['start_date']) ? $_GET['start_date'] : '';
  30. $custom_end = isset($_GET['end_date']) ? $_GET['end_date'] : '';
  31. $period = isset($_GET['period']) ? $_GET['period'] : 'day';
  32. // 设置日期范围
  33. if ($date_range == 'custom' && !empty($custom_start) && !empty($custom_end)) {
  34. $start_date = $custom_start;
  35. $end_date = $custom_end;
  36. } else {
  37. switch ($date_range) {
  38. case 'last_month':
  39. $start_date = $last_month_start;
  40. $end_date = $last_month_end;
  41. break;
  42. case 'current_year':
  43. $start_date = $current_year_start;
  44. $end_date = $current_year_end;
  45. break;
  46. case 'last_30_days':
  47. $start_date = date('Y-m-d', strtotime('-30 days'));
  48. $end_date = date('Y-m-d');
  49. break;
  50. case 'last_90_days':
  51. $start_date = date('Y-m-d', strtotime('-90 days'));
  52. $end_date = date('Y-m-d');
  53. break;
  54. case 'current_month':
  55. default:
  56. $start_date = $current_month_start;
  57. $end_date = $current_month_end;
  58. break;
  59. }
  60. }
  61. // 格式化日期用于SQL查询
  62. $start_date_sql = date('Y-m-d', strtotime($start_date));
  63. $end_date_sql = date('Y-m-d', strtotime($end_date)) . ' 23:59:59';
  64. return [
  65. 'date_range' => $date_range,
  66. 'custom_start' => $custom_start,
  67. 'custom_end' => $custom_end,
  68. 'period' => $period,
  69. 'start_date' => $start_date,
  70. 'end_date' => $end_date,
  71. 'start_date_sql' => $start_date_sql,
  72. 'end_date_sql' => $end_date_sql
  73. ];
  74. }
  75. /**
  76. * 生成图表颜色数组
  77. *
  78. * @param int $count 需要的颜色数量
  79. * @param bool $transparent 是否透明
  80. * @return array 背景色和边框色数组
  81. */
  82. function generateChartColors($count = 10, $transparent = true) {
  83. $colors = [
  84. ['rgba(255, 99, 132, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(255, 99, 132, 1)'],
  85. ['rgba(54, 162, 235, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(54, 162, 235, 1)'],
  86. ['rgba(255, 206, 86, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(255, 206, 86, 1)'],
  87. ['rgba(75, 192, 192, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(75, 192, 192, 1)'],
  88. ['rgba(153, 102, 255, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(153, 102, 255, 1)'],
  89. ['rgba(255, 159, 64, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(255, 159, 64, 1)'],
  90. ['rgba(199, 199, 199, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(199, 199, 199, 1)'],
  91. ['rgba(83, 102, 255, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(83, 102, 255, 1)'],
  92. ['rgba(40, 159, 64, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(40, 159, 64, 1)'],
  93. ['rgba(210, 199, 199, ' . ($transparent ? '0.7' : '1') . ')', 'rgba(210, 199, 199, 1)']
  94. ];
  95. $result = [];
  96. // 确保有足够的颜色
  97. while (count($result) < $count) {
  98. foreach ($colors as $color) {
  99. $result[] = $color;
  100. if (count($result) >= $count) {
  101. break;
  102. }
  103. }
  104. }
  105. return array_slice($result, 0, $count);
  106. }
  107. /**
  108. * 格式化数值,处理空值和小数位数
  109. *
  110. * @param mixed $value 要格式化的值
  111. * @param int $decimals 小数位数
  112. * @return string 格式化后的数值
  113. */
  114. function formatNumber($value, $decimals = 2) {
  115. if ($value === null || $value === '') {
  116. return '0';
  117. }
  118. return number_format((float)$value, $decimals);
  119. }
  120. /**
  121. * 获取时间粒度对应的MySQL DATE_FORMAT格式
  122. *
  123. * @param string $period 时间粒度 (day/week/month)
  124. * @return string MySQL DATE_FORMAT格式字符串
  125. */
  126. function getPeriodFormat($period) {
  127. switch ($period) {
  128. case 'week':
  129. return '%x-W%v'; // ISO year and week number
  130. case 'month':
  131. return '%Y-%m';
  132. case 'day':
  133. default:
  134. return '%Y-%m-%d';
  135. }
  136. }
  137. /**
  138. * 获取新增客户详细信息
  139. *
  140. * @param mysqli $conn 数据库连接
  141. * @param string $start_date 开始日期
  142. * @param string $end_date 结束日期
  143. * @return array 新增客户详细数据
  144. */
  145. function getNewCustomersDetails($conn, $start_date, $end_date) {
  146. $sql = "SELECT
  147. c.id,
  148. c.cs_company as company_name,
  149. c.cs_code as customer_code,
  150. co.countryName as country,
  151. ct.businessType as customer_type,
  152. e.em_user as employee_name,
  153. c.cs_addtime as add_date
  154. FROM customer c
  155. LEFT JOIN country co ON c.cs_country = co.id
  156. LEFT JOIN clienttype ct ON c.cs_type = ct.id
  157. LEFT JOIN employee e ON c.cs_belong = e.id
  158. WHERE c.cs_addtime BETWEEN ? AND ?
  159. ORDER BY c.cs_addtime DESC
  160. LIMIT 30";
  161. $stmt = $conn->prepare($sql);
  162. $stmt->bind_param("ss", $start_date, $end_date);
  163. $stmt->execute();
  164. $result = $stmt->get_result();
  165. $customers = [];
  166. while ($row = $result->fetch_assoc()) {
  167. $customers[] = $row;
  168. }
  169. return $customers;
  170. }
  171. /**
  172. * 获取各业务员新增客户统计
  173. *
  174. * @param mysqli $conn 数据库连接
  175. * @param string $start_date 开始日期
  176. * @param string $end_date 结束日期
  177. * @return array 业务员新增客户统计数据
  178. */
  179. function getNewCustomersByEmployee($conn, $start_date, $end_date) {
  180. $sql = "SELECT
  181. e.id as employee_id,
  182. e.em_user as employee_name,
  183. COUNT(c.id) as customer_count
  184. FROM employee e
  185. LEFT JOIN customer c ON e.id = c.cs_belong AND c.cs_addtime BETWEEN ? AND ?
  186. WHERE e.em_role IS NOT NULL
  187. GROUP BY e.id
  188. ORDER BY customer_count DESC";
  189. $stmt = $conn->prepare($sql);
  190. $stmt->bind_param("ss", $start_date, $end_date);
  191. $stmt->execute();
  192. $result = $stmt->get_result();
  193. $data = [];
  194. while ($row = $result->fetch_assoc()) {
  195. $data[] = $row;
  196. }
  197. return $data;
  198. }
  199. /**
  200. * 渲染新增客户图表
  201. *
  202. * @param array $customers 新增客户数据
  203. * @return void
  204. */
  205. function renderNewCustomersChart($customers) {
  206. if (empty($customers)) {
  207. echo '<div class="alert alert-info">该时间段内没有新增客户数据</div>';
  208. return;
  209. }
  210. ?>
  211. <div class="chart-container">
  212. <div class="table-responsive">
  213. <table class="data-table">
  214. <thead>
  215. <tr>
  216. <th>客户名称</th>
  217. <th>客户编码</th>
  218. <th>国家/地区</th>
  219. <th>客户类型</th>
  220. <th>负责业务员</th>
  221. <th>添加日期</th>
  222. </tr>
  223. </thead>
  224. <tbody>
  225. <?php foreach ($customers as $customer): ?>
  226. <tr>
  227. <td><?php echo htmlspecialchars($customer['company_name']); ?></td>
  228. <td><?php echo htmlspecialchars($customer['customer_code']); ?></td>
  229. <td><?php echo htmlspecialchars($customer['country']); ?></td>
  230. <td><?php echo htmlspecialchars($customer['customer_type'] ?: '未分类'); ?></td>
  231. <td><?php echo htmlspecialchars($customer['employee_name']); ?></td>
  232. <td><?php echo date('Y-m-d', strtotime($customer['add_date'])); ?></td>
  233. </tr>
  234. <?php endforeach; ?>
  235. </tbody>
  236. </table>
  237. </div>
  238. </div>
  239. <!-- 新增客户添加时间分布图 -->
  240. <div class="chart-container">
  241. <div>
  242. <canvas id="newCustomersChart"></canvas>
  243. </div>
  244. </div>
  245. <script>
  246. // 准备图表数据
  247. <?php
  248. // 按日期分组客户数量
  249. $dates = [];
  250. $counts = [];
  251. $dateGroups = [];
  252. foreach ($customers as $customer) {
  253. $date = date('Y-m-d', strtotime($customer['add_date']));
  254. if (!isset($dateGroups[$date])) {
  255. $dateGroups[$date] = 0;
  256. }
  257. $dateGroups[$date]++;
  258. }
  259. // 排序日期
  260. ksort($dateGroups);
  261. foreach ($dateGroups as $date => $count) {
  262. $dates[] = $date;
  263. $counts[] = $count;
  264. }
  265. ?>
  266. var newCustomersCtx = document.getElementById('newCustomersChart').getContext('2d');
  267. new Chart(newCustomersCtx, {
  268. type: 'bar',
  269. data: {
  270. labels: <?php echo json_encode($dates); ?>,
  271. datasets: [{
  272. label: '新增客户数量',
  273. data: <?php echo json_encode($counts); ?>,
  274. backgroundColor: 'rgba(54, 162, 235, 0.5)',
  275. borderColor: 'rgba(54, 162, 235, 1)',
  276. borderWidth: 1
  277. }]
  278. },
  279. options: {
  280. responsive: true,
  281. scales: {
  282. y: {
  283. beginAtZero: true,
  284. title: {
  285. display: true,
  286. text: '客户数量'
  287. }
  288. },
  289. x: {
  290. title: {
  291. display: true,
  292. text: '日期'
  293. }
  294. }
  295. },
  296. plugins: {
  297. title: {
  298. display: true,
  299. text: '新增客户时间分布'
  300. }
  301. }
  302. }
  303. });
  304. </script>
  305. <?php
  306. }
  307. /**
  308. * 渲染业务员新增客户图表
  309. *
  310. * @param array $employee_data 业务员新增客户数据
  311. * @return void
  312. */
  313. function renderNewCustomersByEmployeeChart($employee_data) {
  314. if (empty($employee_data)) {
  315. echo '<div class="alert alert-info">该时间段内没有业务员新增客户数据</div>';
  316. return;
  317. }
  318. // 准备图表数据
  319. $employee_names = [];
  320. $customer_counts = [];
  321. foreach ($employee_data as $row) {
  322. $employee_names[] = $row['employee_name'];
  323. $customer_counts[] = $row['customer_count'];
  324. }
  325. // 生成图表背景色
  326. $colors = generateChartColors(count($employee_data));
  327. $backgroundColors = [];
  328. $borderColors = [];
  329. foreach ($colors as $color) {
  330. $backgroundColors[] = $color[0];
  331. $borderColors[] = $color[1];
  332. }
  333. ?>
  334. <div class="analysis-grid">
  335. <div>
  336. <canvas id="employeeNewCustomersChart"></canvas>
  337. </div>
  338. <div class="table-responsive">
  339. <table class="data-table">
  340. <thead>
  341. <tr>
  342. <th>业务员</th>
  343. <th>新增客户数量</th>
  344. </tr>
  345. </thead>
  346. <tbody>
  347. <?php foreach ($employee_data as $row): ?>
  348. <tr>
  349. <td><?php echo htmlspecialchars($row['employee_name']); ?></td>
  350. <td><?php echo number_format($row['customer_count']); ?></td>
  351. </tr>
  352. <?php endforeach; ?>
  353. </tbody>
  354. </table>
  355. </div>
  356. </div>
  357. <script>
  358. var employeeNewCustomersCtx = document.getElementById('employeeNewCustomersChart').getContext('2d');
  359. new Chart(employeeNewCustomersCtx, {
  360. type: 'bar',
  361. data: {
  362. labels: <?php echo json_encode($employee_names); ?>,
  363. datasets: [{
  364. label: '新增客户数量',
  365. data: <?php echo json_encode($customer_counts); ?>,
  366. backgroundColor: <?php echo json_encode($backgroundColors); ?>,
  367. borderColor: <?php echo json_encode($borderColors); ?>,
  368. borderWidth: 1
  369. }]
  370. },
  371. options: {
  372. responsive: true,
  373. scales: {
  374. y: {
  375. beginAtZero: true,
  376. title: {
  377. display: true,
  378. text: '客户数量'
  379. }
  380. }
  381. },
  382. plugins: {
  383. title: {
  384. display: true,
  385. text: '业务员新增客户统计'
  386. }
  387. }
  388. }
  389. });
  390. </script>
  391. <?php
  392. }