statistics_order_warnings.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. <?php
  2. /**
  3. * 订单预警系统 - 监控订单异常情况
  4. */
  5. require_once 'conn.php';
  6. require_once 'statistics_utils.php';
  7. // 检查登录状态
  8. if (!isset($_SESSION['employee_id'])) {
  9. checkLogin();
  10. }
  11. // 获取当前登录用户信息
  12. $current_user_id = $_SESSION['employee_id'];
  13. $current_permission_role = 0;
  14. // 获取当前用户权限角色
  15. $current_user_id = intval($current_user_id); // 确保是整数
  16. $query = "SELECT em_permission_role_id FROM employee WHERE id = $current_user_id";
  17. $result = $conn->query($query);
  18. if ($result && $row = $result->fetch_assoc()) {
  19. $current_permission_role = $row['em_permission_role_id'];
  20. }
  21. // 获取日期范围参数
  22. $date_params = getDateRangeParams();
  23. $current_start_date = $date_params['start_date_sql'];
  24. $current_end_date = $date_params['end_date_sql'];
  25. $date_range = $date_params['date_range'];
  26. // 获取选中的业务员ID
  27. $selected_employee = isset($_GET['employee_id']) ? intval($_GET['employee_id']) : 0;
  28. // 确定要显示哪些业务员的数据
  29. $employee_filter = null;
  30. if ($selected_employee > 0) {
  31. // 如果选择了特定业务员,检查当前用户是否有权限查看该业务员的数据
  32. $has_permission = false;
  33. if ($current_permission_role == 1) {
  34. // 管理员可以查看所有业务员
  35. $has_permission = true;
  36. } else if ($current_permission_role == 2) {
  37. // 组长可以查看自己和组员
  38. $query = "SELECT id FROM employee WHERE id = $selected_employee AND (id = $current_user_id OR em_role = $current_user_id)";
  39. $result = $conn->query($query);
  40. $has_permission = ($result && $result->num_rows > 0);
  41. } else {
  42. // 普通业务员只能查看自己
  43. $has_permission = ($selected_employee == $current_user_id);
  44. }
  45. if ($has_permission) {
  46. $employee_filter = $selected_employee;
  47. } else {
  48. // 如果没有权限,重置为查看自己的数据
  49. $selected_employee = $current_user_id;
  50. $employee_filter = $current_user_id;
  51. }
  52. } else {
  53. // 如果没有选择特定业务员,则按权限显示相应的业务员数据
  54. if ($current_permission_role == 1) {
  55. // 管理员可以看到所有业务员
  56. $employee_filter = null;
  57. } else if ($current_permission_role == 2) {
  58. // 组长可以看到自己和组员
  59. $visible_employees = [];
  60. $query = "SELECT id FROM employee WHERE id = $current_user_id OR em_role = $current_user_id";
  61. $result = $conn->query($query);
  62. if ($result) {
  63. while ($row = $result->fetch_assoc()) {
  64. $visible_employees[] = intval($row['id']);
  65. }
  66. }
  67. if (!empty($visible_employees)) {
  68. $employee_filter = $visible_employees;
  69. } else {
  70. $employee_filter = $current_user_id;
  71. }
  72. } else {
  73. // 普通业务员只能看到自己
  74. $employee_filter = $current_user_id;
  75. }
  76. }
  77. // 获取业务员列表(基于权限)
  78. $sql_employees = "";
  79. if ($current_permission_role == 1) {
  80. // 管理员可以看到所有业务员
  81. $sql_employees = "SELECT id, em_user FROM employee WHERE em_role IS NOT NULL ORDER BY em_user";
  82. } else if ($current_permission_role == 2) {
  83. // 组长可以看到自己和组员
  84. $sql_employees = "SELECT id, em_user FROM employee WHERE id = $current_user_id OR em_role = $current_user_id ORDER BY em_user";
  85. } else {
  86. // 普通业务员只能看到自己
  87. $sql_employees = "SELECT id, em_user FROM employee WHERE id = $current_user_id";
  88. }
  89. $employees_result = $conn->query($sql_employees);
  90. // 计算上一个时间段范围(用于比较)
  91. $previous_start_date = '';
  92. $previous_end_date = '';
  93. // 根据当前选择的日期范围,计算上一个对比时段
  94. if ($date_range == 'current_month') {
  95. // 上个月
  96. $previous_start_date = date('Y-m-01', strtotime('-1 month', strtotime($current_start_date)));
  97. $previous_end_date = date('Y-m-t', strtotime('-1 month', strtotime($current_end_date)));
  98. } elseif ($date_range == 'last_month') {
  99. // 上上个月
  100. $previous_start_date = date('Y-m-01', strtotime('-2 month', strtotime($current_start_date)));
  101. $previous_end_date = date('Y-m-t', strtotime('-2 month', strtotime($current_end_date)));
  102. } elseif ($date_range == 'current_year') {
  103. // 去年同期
  104. $previous_start_date = date('Y-01-01', strtotime('-1 year', strtotime($current_start_date)));
  105. $previous_end_date = date('Y-12-31', strtotime('-1 year', strtotime($current_end_date)));
  106. } elseif ($date_range == 'last_30_days' || $date_range == 'last_90_days' || $date_range == 'custom') {
  107. // 上一个同长度周期
  108. $date_diff = (strtotime($current_end_date) - strtotime($current_start_date)) / (60 * 60 * 24);
  109. $previous_end_date = date('Y-m-d', strtotime('-1 day', strtotime($current_start_date)));
  110. $previous_start_date = date('Y-m-d', strtotime("-{$date_diff} day", strtotime($previous_end_date)));
  111. }
  112. // 阈值设置(可以移到数据库或配置文件中)
  113. $order_amount_decrease_threshold = -15; // 订单金额下降超过15%触发预警
  114. $repurchase_cycle_threshold = 90; // 复购周期超过90天触发预警(3个月内未录入订单)
  115. $inactive_threshold = 90; // 90天未有客户信息修改视为不活跃客户(3个月)
  116. $churn_threshold = 365; // 365天未下单视为流失客户(1年)
  117. $normal_repurchase_days = 30; // 正常复购周期参考值(天)
  118. // 页面头部
  119. include('statistics_header.php');
  120. ?>
  121. <div class="page-header">
  122. <h1 class="page-title">订单预警系统</h1>
  123. <p class="page-description">监控订单异常情况,提前预警潜在问题</p>
  124. <?php
  125. // 获取当前用户角色显示提示信息
  126. $role_info = "";
  127. if ($current_permission_role == 1) {
  128. // 管理员
  129. if ($selected_employee > 0) {
  130. $employee_name = "";
  131. $emp_query = "SELECT em_user FROM employee WHERE id = $selected_employee";
  132. $emp_result = $conn->query($emp_query);
  133. if ($emp_result && $emp_row = $emp_result->fetch_assoc()) {
  134. $employee_name = $emp_row['em_user'];
  135. $role_info = "您正在查看业务员 {$employee_name} 的数据";
  136. }
  137. } else {
  138. $role_info = "您正在查看所有业务员的数据";
  139. }
  140. } else if ($current_permission_role == 2) {
  141. // 组长
  142. if ($selected_employee > 0 && $selected_employee != $current_user_id) {
  143. $employee_name = "";
  144. $emp_query = "SELECT em_user FROM employee WHERE id = $selected_employee";
  145. $emp_result = $conn->query($emp_query);
  146. if ($emp_result && $emp_row = $emp_result->fetch_assoc()) {
  147. $employee_name = $emp_row['em_user'];
  148. $role_info = "您正在查看业务员 {$employee_name} 的数据";
  149. }
  150. } else if ($selected_employee == 0 || $selected_employee == $current_user_id) {
  151. $role_info = "您正在查看您的团队数据";
  152. }
  153. } else {
  154. // 普通业务员
  155. $role_info = "您正在查看自己的数据";
  156. }
  157. ?>
  158. <div class="role-info"><?php echo $role_info; ?></div>
  159. </div>
  160. <!-- 日期筛选 -->
  161. <div class="filter-form">
  162. <form method="get" class="filter-form-inline">
  163. <div class="form-group">
  164. <label for="date_range">分析周期</label>
  165. <select class="form-control" id="date_range" name="date_range" onchange="toggleCustomDates()">
  166. <option value="current_month" <?php echo $date_range == 'current_month' ? 'selected' : ''; ?>>本月</option>
  167. <option value="last_month" <?php echo $date_range == 'last_month' ? 'selected' : ''; ?>>上月</option>
  168. <option value="current_year" <?php echo $date_range == 'current_year' ? 'selected' : ''; ?>>今年</option>
  169. <option value="last_30_days" <?php echo $date_range == 'last_30_days' ? 'selected' : ''; ?>>最近30天</option>
  170. <option value="last_90_days" <?php echo $date_range == 'last_90_days' ? 'selected' : ''; ?>>最近90天</option>
  171. <option value="custom" <?php echo $date_range == 'custom' ? 'selected' : ''; ?>>自定义日期范围</option>
  172. </select>
  173. </div>
  174. <div class="form-group custom-date-inputs" id="custom_start_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  175. <label for="start_date">开始日期</label>
  176. <input type="date" class="form-control" id="start_date" name="start_date" value="<?php echo $date_params['custom_start']; ?>">
  177. </div>
  178. <div class="form-group custom-date-inputs" id="custom_end_date" style="display: <?php echo $date_range == 'custom' ? 'inline-block' : 'none'; ?>">
  179. <label for="end_date">结束日期</label>
  180. <input type="date" class="form-control" id="end_date" name="end_date" value="<?php echo $date_params['custom_end']; ?>">
  181. </div>
  182. <div class="form-group">
  183. <label for="employee_id">业务员</label>
  184. <select class="form-control" id="employee_id" name="employee_id">
  185. <option value="0">全部业务员</option>
  186. <?php while ($emp = $employees_result->fetch_assoc()): ?>
  187. <option value="<?php echo $emp['id']; ?>" <?php echo $selected_employee == $emp['id'] ? 'selected' : ''; ?>><?php echo htmlspecialcharsFix($emp['em_user']); ?></option>
  188. <?php endwhile; ?>
  189. </select>
  190. </div>
  191. <div class="form-group">
  192. <button type="submit" class="form-btn form-btn-primary">应用筛选</button>
  193. </div>
  194. </form>
  195. </div>
  196. <!-- 预警概览 -->
  197. <div class="warnings-overview">
  198. <div class="row">
  199. <?php
  200. // 获取总预警数
  201. $sql_total_warnings = "SELECT
  202. (SELECT COUNT(*) FROM customer WHERE cs_deal = 3) as total_customers,
  203. (SELECT COUNT(DISTINCT customer_id) FROM orders WHERE order_date BETWEEN ? AND ?) as active_customers";
  204. $stmt = $conn->prepare($sql_total_warnings);
  205. $stmt->bind_param("ss", $current_start_date, $current_end_date);
  206. $stmt->execute();
  207. $result = $stmt->get_result();
  208. $warning_count = $result->fetch_assoc();
  209. // 获取订单金额下降的客户数 - 使用当前用户的筛选条件
  210. $query_employee_filter = null;
  211. if (is_array($employee_filter) && !empty($employee_filter)) {
  212. // 如果是组长查看团队,转换为SQL中的IN条件
  213. $query_employee_filter = implode(',', $employee_filter);
  214. } else if (!is_array($employee_filter) && $employee_filter > 0) {
  215. // 如果是查看单个业务员
  216. $query_employee_filter = $employee_filter;
  217. }
  218. $decreasing_amount_count = getDecreasingOrderAmountCustomers(
  219. $conn,
  220. $current_start_date,
  221. $current_end_date,
  222. $previous_start_date,
  223. $previous_end_date,
  224. $order_amount_decrease_threshold,
  225. true,
  226. $query_employee_filter
  227. );
  228. // 获取复购周期异常(3个月内未录入订单)的客户数
  229. $abnormal_cycle_count = getAbnormalRepurchaseCycleCustomers(
  230. $conn,
  231. $current_start_date,
  232. $current_end_date,
  233. $repurchase_cycle_threshold,
  234. true,
  235. $query_employee_filter
  236. );
  237. // 获取长期不活跃(3个月内没有客户信息修改)客户数
  238. $inactive_customers_count = getInactiveCustomers(
  239. $conn,
  240. $current_end_date,
  241. $inactive_threshold,
  242. true,
  243. 1,
  244. 10,
  245. $query_employee_filter
  246. );
  247. // 获取流失客户(1年内未录入订单)数
  248. $churn_customers_count = getChurnCustomers(
  249. $conn,
  250. $current_end_date,
  251. $churn_threshold,
  252. true,
  253. 1,
  254. 10,
  255. $query_employee_filter
  256. );
  257. ?>
  258. <div class="col-md-3">
  259. <div class="stat-card warning">
  260. <h3>订单金额下降客户</h3>
  261. <div class="stat-value"><?php echo $decreasing_amount_count; ?></div>
  262. <div class="stat-desc">金额下降超过<?php echo abs($order_amount_decrease_threshold); ?>%</div>
  263. </div>
  264. </div>
  265. <div class="col-md-3">
  266. <div class="stat-card warning">
  267. <h3>复购周期异常客户</h3>
  268. <div class="stat-value"><?php echo $abnormal_cycle_count; ?></div>
  269. <div class="stat-desc">3个月内未录入订单</div>
  270. </div>
  271. </div>
  272. <div class="col-md-3">
  273. <div class="stat-card danger">
  274. <h3>流失客户</h3>
  275. <div class="stat-value"><?php echo $churn_customers_count; ?></div>
  276. <div class="stat-desc">1年内未录入订单</div>
  277. </div>
  278. </div>
  279. <div class="col-md-3">
  280. <div class="stat-card info">
  281. <h3>长期不活跃客户</h3>
  282. <div class="stat-value"><?php echo $inactive_customers_count; ?></div>
  283. <div class="stat-desc">3个月内无客户信息更新</div>
  284. </div>
  285. </div>
  286. </div>
  287. </div>
  288. <!-- 订单金额下降客户列表 -->
  289. <div class="warning-section">
  290. <div class="section-header">
  291. <h2>订单金额下降客户</h2>
  292. <p>与上一周期相比,订单金额显著下降的客户</p>
  293. </div>
  294. <table class="data-table">
  295. <thead>
  296. <tr>
  297. <th>客户名称</th>
  298. <th>本期订单金额</th>
  299. <th>上期订单金额</th>
  300. <th>变化百分比</th>
  301. <th>最近出货日期</th>
  302. <th>业务员</th>
  303. <th>操作</th>
  304. </tr>
  305. </thead>
  306. <tbody>
  307. <?php
  308. $decreasing_customers = getDecreasingOrderAmountCustomers(
  309. $conn,
  310. $current_start_date,
  311. $current_end_date,
  312. $previous_start_date,
  313. $previous_end_date,
  314. $order_amount_decrease_threshold,
  315. false,
  316. $query_employee_filter
  317. );
  318. while ($customer = $decreasing_customers->fetch_assoc()) {
  319. $change_percent = round((($customer['current_amount'] - $customer['previous_amount']) / $customer['previous_amount']) * 100, 1);
  320. $change_class = $change_percent < -20 ? 'text-danger' : 'text-warning';
  321. echo "<tr>";
  322. echo "<td>" . htmlspecialcharsFix($customer['cs_company']) . "</td>";
  323. echo "<td>¥" . number_format($customer['current_amount'], 2) . "</td>";
  324. echo "<td>¥" . number_format($customer['previous_amount'], 2) . "</td>";
  325. echo "<td class='{$change_class}'>" . $change_percent . "%</td>";
  326. echo "<td>" . $customer['last_order_date'] . "</td>";
  327. echo "<td>" . htmlspecialcharsFix($customer['em_user']) . "</td>";
  328. echo "<td><a href='customer_detail.php?id=" . $customer['id'] . "&from_warning=1' class='action-btn action-btn-view'>查看</a></td>";
  329. echo "</tr>";
  330. }
  331. if ($decreasing_customers->num_rows == 0) {
  332. echo "<tr><td colspan='7' class='text-center'>没有发现订单金额下降的客户</td></tr>";
  333. }
  334. ?>
  335. </tbody>
  336. </table>
  337. </div>
  338. <!-- 复购周期异常客户列表 -->
  339. <div class="warning-section" id="abnormal-customers">
  340. <div class="section-header">
  341. <h2>复购周期异常客户</h2>
  342. <p>3个月内未录入订单的客户</p>
  343. </div>
  344. <table class="data-table">
  345. <thead>
  346. <tr>
  347. <th>客户编码</th>
  348. <th>客户名称</th>
  349. <th>上次订单日期</th>
  350. <th>未订单天数</th>
  351. <th>历史订单总数</th>
  352. <th>历史订单总额</th>
  353. <th>业务员</th>
  354. <th>操作</th>
  355. </tr>
  356. </thead>
  357. <tbody>
  358. <?php
  359. // 获取分页参数
  360. $abnormal_page = isset($_GET['abnormal_page']) ? intval($_GET['abnormal_page']) : 1;
  361. $abnormal_page_size = 10; // 每页显示10条记录
  362. // 获取总记录数
  363. $total_abnormal = getAbnormalRepurchaseCycleCustomers($conn, $current_start_date, $current_end_date, $repurchase_cycle_threshold, true, $query_employee_filter);
  364. // 计算总页数
  365. $abnormal_total_pages = ceil($total_abnormal / $abnormal_page_size);
  366. // 确保页码合法
  367. if ($abnormal_page < 1) $abnormal_page = 1;
  368. if ($abnormal_page > $abnormal_total_pages && $abnormal_total_pages > 0) $abnormal_page = $abnormal_total_pages;
  369. // 获取当页数据
  370. $abnormal_customers = getAbnormalRepurchaseCycleCustomers(
  371. $conn,
  372. $current_start_date,
  373. $current_end_date,
  374. $repurchase_cycle_threshold,
  375. false,
  376. $query_employee_filter,
  377. $abnormal_page,
  378. $abnormal_page_size
  379. );
  380. while ($customer = $abnormal_customers->fetch_assoc()) {
  381. $inactive_days = $customer['inactive_days'];
  382. $inactive_class = $inactive_days > 60 ? 'text-danger' : 'text-warning';
  383. echo "<tr>";
  384. echo "<td title='{$customer['cs_code']}'>" . htmlspecialcharsFix($customer['cs_code']) . "</td>";
  385. echo "<td>" . htmlspecialcharsFix($customer['cs_company'] ?: '未填写') . "</td>";
  386. echo "<td>" . ($customer['last_order_date'] ? $customer['last_order_date'] : '从未下单') . "</td>";
  387. echo "<td class='{$inactive_class}'>" . $inactive_days . "</td>";
  388. echo "<td>" . $customer['order_count'] . "</td>";
  389. echo "<td>¥" . number_format($customer['total_amount'], 2) . "</td>";
  390. echo "<td>" . htmlspecialcharsFix($customer['em_user']) . "</td>";
  391. echo "<td><a href='customer_detail.php?id=" . $customer['id'] . "&from_warning=1' class='action-btn action-btn-view'>查看</a></td>";
  392. echo "</tr>";
  393. }
  394. if ($abnormal_customers->num_rows == 0) {
  395. echo "<tr><td colspan='8' class='text-center'>没有发现复购周期异常的客户</td></tr>";
  396. }
  397. ?>
  398. </tbody>
  399. </table>
  400. <!-- 分页控件 -->
  401. <?php if ($abnormal_total_pages > 1): ?>
  402. <div class="pagination-container">
  403. <ul class="pagination">
  404. <?php
  405. // 生成分页链接的基础URL
  406. $base_url = '?';
  407. foreach ($_GET as $key => $value) {
  408. if ($key != 'abnormal_page') {
  409. $base_url .= $key . '=' . urlencode($value) . '&';
  410. }
  411. }
  412. // 上一页链接
  413. if ($abnormal_page > 1) {
  414. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page=" . ($abnormal_page - 1) . "#abnormal-customers'>上一页</a></li>";
  415. } else {
  416. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>上一页</a></li>";
  417. }
  418. // 页码链接
  419. $start_page = max(1, $abnormal_page - 2);
  420. $end_page = min($abnormal_total_pages, $abnormal_page + 2);
  421. if ($start_page > 1) {
  422. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page=1#abnormal-customers'>1</a></li>";
  423. if ($start_page > 2) {
  424. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>...</a></li>";
  425. }
  426. }
  427. for ($i = $start_page; $i <= $end_page; $i++) {
  428. if ($i == $abnormal_page) {
  429. echo "<li class='pager-item active'><a class='pager-link' href='#abnormal-customers'>{$i}</a></li>";
  430. } else {
  431. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page={$i}#abnormal-customers'>{$i}</a></li>";
  432. }
  433. }
  434. if ($end_page < $abnormal_total_pages) {
  435. if ($end_page < $abnormal_total_pages - 1) {
  436. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>...</a></li>";
  437. }
  438. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page={$abnormal_total_pages}#abnormal-customers'>{$abnormal_total_pages}</a></li>";
  439. }
  440. // 下一页链接
  441. if ($abnormal_page < $abnormal_total_pages) {
  442. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page=" . ($abnormal_page + 1) . "#abnormal-customers'>下一页</a></li>";
  443. } else {
  444. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>下一页</a></li>";
  445. }
  446. ?>
  447. </ul>
  448. <div class="pagination-info">
  449. 共 <?php echo $total_abnormal; ?> 条记录,当前显示第 <?php echo $abnormal_page; ?> 页,共 <?php echo $abnormal_total_pages; ?> 页
  450. </div>
  451. </div>
  452. <?php endif; ?>
  453. </div>
  454. <!-- 流失客户列表 -->
  455. <div class="warning-section">
  456. <div class="section-header">
  457. <h2>流失客户</h2>
  458. <p>1年内未录入订单的客户</p>
  459. </div>
  460. <table class="data-table">
  461. <thead>
  462. <tr>
  463. <th>客户编码</th>
  464. <th>客户名称</th>
  465. <th>最后出货日期</th>
  466. <th>未订单天数</th>
  467. <th>历史订单数</th>
  468. <th>历史订单总额</th>
  469. <th>业务员</th>
  470. <th>操作</th>
  471. </tr>
  472. </thead>
  473. <tbody>
  474. <?php
  475. // 获取分页参数
  476. $page = isset($_GET['churn_page']) ? intval($_GET['churn_page']) : 1;
  477. $page_size = 10; // 每页显示10条记录
  478. // 获取总记录数
  479. $total_churn = getChurnCustomers($conn, $current_end_date, $churn_threshold, true, 1, 10, $query_employee_filter);
  480. // 计算总页数
  481. $total_pages = ceil($total_churn / $page_size);
  482. // 确保页码合法
  483. if ($page < 1) $page = 1;
  484. if ($page > $total_pages && $total_pages > 0) $page = $total_pages;
  485. // 获取当页数据
  486. $churn_customers = getChurnCustomers($conn, $current_end_date, $churn_threshold, false, $page, $page_size, $query_employee_filter);
  487. while ($customer = $churn_customers->fetch_assoc()) {
  488. $inactive_days = $customer['inactive_days'];
  489. $inactive_class = $inactive_days > 365 ? 'text-danger' : 'text-warning';
  490. echo "<tr>";
  491. echo "<td title='{$customer['cs_code']}'>" . htmlspecialcharsFix($customer['cs_code']) . "</td>";
  492. echo "<td>" . htmlspecialcharsFix($customer['cs_company'] ?: '未填写') . "</td>";
  493. echo "<td>" . ($customer['last_order_date'] ? $customer['last_order_date'] : '从未下单') . "</td>";
  494. echo "<td class='{$inactive_class}'>" . $inactive_days . "</td>";
  495. echo "<td>" . $customer['order_count'] . "</td>";
  496. echo "<td>¥" . number_format($customer['total_amount'], 2) . "</td>";
  497. echo "<td>" . htmlspecialcharsFix($customer['em_user']) . "</td>";
  498. echo "<td><a href='customer_detail.php?id=" . $customer['id'] . "&from_warning=1' class='action-btn action-btn-view'>查看</a></td>";
  499. echo "</tr>";
  500. }
  501. if ($churn_customers->num_rows == 0) {
  502. echo "<tr><td colspan='8' class='text-center'>没有发现流失客户</td></tr>";
  503. }
  504. ?>
  505. </tbody>
  506. </table>
  507. <!-- 分页控件 -->
  508. <?php if ($total_pages > 1): ?>
  509. <div class="pagination-container">
  510. <ul class="pagination">
  511. <?php
  512. // 生成分页链接的基础URL
  513. $base_url = '?';
  514. foreach ($_GET as $key => $value) {
  515. if ($key != 'churn_page') {
  516. $base_url .= $key . '=' . urlencode($value) . '&';
  517. }
  518. }
  519. // 上一页链接
  520. if ($page > 1) {
  521. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page=" . ($page - 1) . "#churn-customers'>上一页</a></li>";
  522. } else {
  523. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>上一页</a></li>";
  524. }
  525. // 页码链接
  526. $start_page = max(1, $page - 2);
  527. $end_page = min($total_pages, $page + 2);
  528. if ($start_page > 1) {
  529. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page=1#churn-customers'>1</a></li>";
  530. if ($start_page > 2) {
  531. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>...</a></li>";
  532. }
  533. }
  534. for ($i = $start_page; $i <= $end_page; $i++) {
  535. if ($i == $page) {
  536. echo "<li class='pager-item active'><a class='pager-link' href='#churn-customers'>{$i}</a></li>";
  537. } else {
  538. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page={$i}#churn-customers'>{$i}</a></li>";
  539. }
  540. }
  541. if ($end_page < $total_pages) {
  542. if ($end_page < $total_pages - 1) {
  543. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>...</a></li>";
  544. }
  545. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page={$total_pages}#churn-customers'>{$total_pages}</a></li>";
  546. }
  547. // 下一页链接
  548. if ($page < $total_pages) {
  549. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page=" . ($page + 1) . "#churn-customers'>下一页</a></li>";
  550. } else {
  551. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>下一页</a></li>";
  552. }
  553. ?>
  554. </ul>
  555. <div class="pagination-info">
  556. 共 <?php echo $total_churn; ?> 条记录,当前显示第 <?php echo $page; ?> 页,共 <?php echo $total_pages; ?> 页
  557. </div>
  558. </div>
  559. <?php endif; ?>
  560. </div>
  561. <!-- 总体订单趋势图 -->
  562. <div class="chart-section">
  563. <div class="section-header">
  564. <h2>总体订单趋势</h2>
  565. <p>最近12个月的订单数量和金额趋势</p>
  566. </div>
  567. <div class="chart-container">
  568. <canvas id="orderTrendChart"></canvas>
  569. </div>
  570. <?php
  571. // 获取最近12个月的订单趋势数据
  572. $sql_trend = "SELECT
  573. DATE_FORMAT(order_date, '%Y-%m') as month,
  574. COUNT(*) as order_count,
  575. SUM(total_amount) as total_amount
  576. FROM orders o
  577. JOIN customer c ON o.customer_id = c.id
  578. WHERE order_date >= DATE_SUB(?, INTERVAL 11 MONTH)";
  579. // 添加业务员筛选条件
  580. if (is_array($query_employee_filter) && !empty($query_employee_filter)) {
  581. // 如果是组长查看团队数据
  582. $sql_trend .= " AND c.cs_belong IN (" . $query_employee_filter . ")";
  583. } else if (!is_array($query_employee_filter) && $query_employee_filter > 0) {
  584. // 如果是查看单个业务员数据
  585. $sql_trend .= " AND c.cs_belong = ?";
  586. }
  587. $sql_trend .= " GROUP BY DATE_FORMAT(order_date, '%Y-%m') ORDER BY month";
  588. $stmt = $conn->prepare($sql_trend);
  589. if (!is_array($query_employee_filter) && $query_employee_filter > 0) {
  590. $stmt->bind_param("si", $current_end_date, $query_employee_filter);
  591. } else {
  592. $stmt->bind_param("s", $current_end_date);
  593. }
  594. $stmt->execute();
  595. $trend_result = $stmt->get_result();
  596. $months = [];
  597. $order_counts = [];
  598. $order_amounts = [];
  599. while ($row = $trend_result->fetch_assoc()) {
  600. $months[] = $row['month'];
  601. $order_counts[] = $row['order_count'];
  602. $order_amounts[] = $row['total_amount'];
  603. }
  604. // 转为JSON格式,用于JavaScript图表
  605. $months_json = json_encode($months);
  606. $order_counts_json = json_encode($order_counts);
  607. $order_amounts_json = json_encode($order_amounts);
  608. ?>
  609. </div>
  610. <style>
  611. .page-header {
  612. margin-bottom: 30px;
  613. }
  614. .page-title {
  615. font-size: 24px;
  616. margin-top: 0;
  617. margin-bottom: 5px;
  618. }
  619. .page-description {
  620. color: #666;
  621. margin-bottom: 5px;
  622. }
  623. .role-info {
  624. color: #2196f3;
  625. font-size: 14px;
  626. margin-top: 5px;
  627. font-weight: 500;
  628. }
  629. .filter-form {
  630. background-color: #f8f9fa;
  631. border-radius: 4px;
  632. padding: 20px;
  633. margin-bottom: 30px;
  634. box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  635. }
  636. .filter-form-inline {
  637. display: flex;
  638. flex-wrap: wrap;
  639. gap: 15px;
  640. align-items: end;
  641. }
  642. .warnings-overview {
  643. margin-bottom: 30px;
  644. }
  645. .warnings-overview .row {
  646. display: flex;
  647. flex-wrap: wrap;
  648. margin: 0 -10px;
  649. }
  650. .warnings-overview .col-md-3 {
  651. padding: 0 10px;
  652. width: 25%;
  653. flex: 0 0 25%;
  654. box-sizing: border-box;
  655. }
  656. @media (max-width: 992px) {
  657. .warnings-overview .col-md-3 {
  658. width: 50%;
  659. flex: 0 0 50%;
  660. }
  661. }
  662. @media (max-width: 576px) {
  663. .warnings-overview .col-md-3 {
  664. width: 100%;
  665. flex: 0 0 100%;
  666. }
  667. }
  668. .stat-card {
  669. border-radius: 8px;
  670. padding: 20px;
  671. color: #fff;
  672. height: 100%;
  673. margin-bottom: 20px;
  674. box-shadow: 0 3px 6px rgba(0,0,0,0.16);
  675. text-align: center;
  676. transition: transform 0.3s ease;
  677. }
  678. .stat-card:hover {
  679. transform: translateY(-5px);
  680. }
  681. .stat-card h3 {
  682. margin-top: 0;
  683. font-size: 16px;
  684. font-weight: 500;
  685. margin-bottom: 10px;
  686. }
  687. .stat-value {
  688. font-size: 32px;
  689. font-weight: 600;
  690. margin-bottom: 10px;
  691. }
  692. .stat-desc {
  693. font-size: 15px;
  694. opacity: 0.8;
  695. }
  696. .stat-card.warning {
  697. background-color: #ff9800;
  698. }
  699. .stat-card.danger {
  700. background-color: #f44336;
  701. }
  702. .stat-card.info {
  703. background-color: #2196f3;
  704. }
  705. .stat-card.success {
  706. background-color: #4caf50;
  707. }
  708. .warning-section {
  709. background-color: white;
  710. border-radius: 8px;
  711. padding: 25px;
  712. margin-bottom: 35px;
  713. box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  714. }
  715. .section-header {
  716. margin-bottom: 20px;
  717. }
  718. .section-header h2 {
  719. font-size: 20px;
  720. margin-top: 0;
  721. margin-bottom: 5px;
  722. }
  723. .section-header p {
  724. color: #666;
  725. font-size: 15px;
  726. margin-bottom: 15px;
  727. }
  728. .chart-section {
  729. background-color: white;
  730. border-radius: 4px;
  731. padding: 20px;
  732. margin-bottom: 30px;
  733. box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  734. }
  735. .chart-container {
  736. height: 350px;
  737. }
  738. .text-danger {
  739. color: #f44336;
  740. }
  741. .text-warning {
  742. color: #ff9800;
  743. }
  744. .text-info {
  745. color: #2196f3;
  746. }
  747. /* 调整表格和内容样式 */
  748. .data-table {
  749. margin-bottom: 20px;
  750. width: 100%;
  751. font-size: 14px; /* 增加表格字体大小 */
  752. border-collapse: separate;
  753. border-spacing: 0;
  754. table-layout: fixed; /* 固定表格布局 */
  755. }
  756. .data-table th {
  757. font-size: 15px; /* 表头字体大小 */
  758. padding: 12px 15px;
  759. background-color: #f5f5f5;
  760. font-weight: 600;
  761. }
  762. .data-table td {
  763. padding: 12px 15px;
  764. vertical-align: middle;
  765. white-space: nowrap; /* 防止文本换行 */
  766. overflow: hidden; /* 溢出隐藏 */
  767. text-overflow: ellipsis; /* 文本溢出显示省略号 */
  768. }
  769. /* 为长期不活跃客户表格设置列宽 */
  770. #inactive-customers .data-table th:nth-child(1), /* 客户编码 */
  771. #inactive-customers .data-table td:nth-child(1) {
  772. width: 12%;
  773. }
  774. #inactive-customers .data-table th:nth-child(2), /* 客户名称 */
  775. #inactive-customers .data-table td:nth-child(2) {
  776. width: 20%;
  777. }
  778. #inactive-customers .data-table th:nth-child(3), /* 最后出货日期 */
  779. #inactive-customers .data-table td:nth-child(3) {
  780. width: 12%;
  781. }
  782. #inactive-customers .data-table th:nth-child(4), /* 不活跃天数 */
  783. #inactive-customers .data-table td:nth-child(4) {
  784. width: 10%;
  785. }
  786. #inactive-customers .data-table th:nth-child(5), /* 历史订单数 */
  787. #inactive-customers .data-table td:nth-child(5) {
  788. width: 10%;
  789. }
  790. #inactive-customers .data-table th:nth-child(6), /* 历史订单总额 */
  791. #inactive-customers .data-table td:nth-child(6) {
  792. width: 12%;
  793. }
  794. #inactive-customers .data-table th:nth-child(7), /* 业务员 */
  795. #inactive-customers .data-table td:nth-child(7) {
  796. width: 12%;
  797. }
  798. #inactive-customers .data-table th:nth-child(8), /* 操作 */
  799. #inactive-customers .data-table td:nth-child(8) {
  800. width: 12%;
  801. }
  802. .data-table td:last-child {
  803. min-width: 80px; /* 确保操作列有足够宽度 */
  804. text-align: center;
  805. }
  806. .data-table tr:hover {
  807. background-color: #f9f9f9;
  808. }
  809. .action-btn {
  810. padding: 5px 10px;
  811. margin: 0 3px;
  812. min-width: 60px;
  813. display: inline-block;
  814. text-align: center;
  815. border-radius: 4px;
  816. font-size: 13px;
  817. transition: all 0.3s ease;
  818. }
  819. .action-btn-view {
  820. background-color: #2196f3;
  821. color: white;
  822. border: none;
  823. }
  824. .action-btn-view:hover {
  825. background-color: #0d8aee;
  826. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  827. }
  828. /* 为所有客户列表表格添加共同样式 */
  829. .warning-section .data-table {
  830. border: 1px solid #eee;
  831. border-radius: 4px;
  832. box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  833. }
  834. .warning-section .data-table th:first-child,
  835. .warning-section .data-table td:first-child {
  836. padding-left: 20px; /* 左侧留出更多间距 */
  837. }
  838. /* 表格内数字列的对齐方式 */
  839. .warning-section .data-table td:nth-child(3),
  840. .warning-section .data-table td:nth-child(4),
  841. .warning-section .data-table td:nth-child(5) {
  842. text-align: left;
  843. }
  844. /* 调整整体字体大小 */
  845. body {
  846. font-size: 14px;
  847. }
  848. /* 分页样式 */
  849. .pagination-container {
  850. margin-top: 25px;
  851. display: flex;
  852. flex-direction: column;
  853. align-items: center;
  854. }
  855. .pagination {
  856. display: flex;
  857. list-style: none;
  858. padding: 0;
  859. margin-bottom: 15px;
  860. }
  861. .pagination .pager-item {
  862. margin: 0 2px;
  863. }
  864. .pagination .pager-link {
  865. padding: 8px 16px;
  866. border-radius: 4px;
  867. margin: 0 3px;
  868. font-weight: 500;
  869. border: 1px solid #ddd;
  870. background-color: #fff;
  871. color: #2196f3;
  872. text-decoration: none;
  873. display: inline-block;
  874. }
  875. .pagination .pager-item.active .pager-link {
  876. background-color: #2196f3;
  877. color: white;
  878. border-color: #2196f3;
  879. }
  880. .pagination .pager-item.disabled .pager-link {
  881. color: #999;
  882. cursor: not-allowed;
  883. background-color: #f5f5f5;
  884. }
  885. .pagination-info {
  886. margin-top: 10px;
  887. font-size: 14px;
  888. color: #666;
  889. }
  890. .form-btn {
  891. padding: 8px 15px;
  892. border-radius: 4px;
  893. cursor: pointer;
  894. font-size: 14px;
  895. border: none;
  896. transition: all 0.3s ease;
  897. }
  898. .form-btn-primary {
  899. background-color: #2196f3;
  900. color: white;
  901. }
  902. .form-btn-primary:hover {
  903. background-color: #0d8aee;
  904. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  905. }
  906. /* 添加斑马纹和悬停效果 */
  907. .data-table tr:nth-child(even) {
  908. background-color: #f8f8f8;
  909. }
  910. .data-table tbody tr:hover {
  911. background-color: #f0f7ff;
  912. }
  913. </style>
  914. <script>
  915. function toggleCustomDates() {
  916. const dateRange = document.getElementById('date_range').value;
  917. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  918. if (dateRange === 'custom') {
  919. customDateInputs.forEach(el => el.style.display = 'inline-block');
  920. } else {
  921. customDateInputs.forEach(el => el.style.display = 'none');
  922. }
  923. }
  924. // 订单趋势图
  925. document.addEventListener('DOMContentLoaded', function() {
  926. const ctx = document.getElementById('orderTrendChart').getContext('2d');
  927. const months = <?php echo $months_json; ?>;
  928. const orderCounts = <?php echo $order_counts_json; ?>;
  929. const orderAmounts = <?php echo $order_amounts_json; ?>;
  930. const chart = new Chart(ctx, {
  931. type: 'line',
  932. data: {
  933. labels: months,
  934. datasets: [
  935. {
  936. label: '订单数量',
  937. data: orderCounts,
  938. backgroundColor: 'rgba(54, 162, 235, 0.2)',
  939. borderColor: 'rgba(54, 162, 235, 1)',
  940. borderWidth: 2,
  941. yAxisID: 'y-axis-1'
  942. },
  943. {
  944. label: '订单金额',
  945. data: orderAmounts,
  946. backgroundColor: 'rgba(255, 99, 132, 0.2)',
  947. borderColor: 'rgba(255, 99, 132, 1)',
  948. borderWidth: 2,
  949. yAxisID: 'y-axis-2'
  950. }
  951. ]
  952. },
  953. options: {
  954. responsive: true,
  955. maintainAspectRatio: false,
  956. scales: {
  957. 'y-axis-1': {
  958. type: 'linear',
  959. position: 'left',
  960. title: {
  961. display: true,
  962. text: '订单数量'
  963. }
  964. },
  965. 'y-axis-2': {
  966. type: 'linear',
  967. position: 'right',
  968. title: {
  969. display: true,
  970. text: '订单金额'
  971. },
  972. grid: {
  973. drawOnChartArea: false
  974. }
  975. }
  976. }
  977. }
  978. });
  979. });
  980. </script>
  981. <?php
  982. /**
  983. * 获取订单金额下降的客户
  984. */
  985. function getDecreasingOrderAmountCustomers($conn, $current_start, $current_end, $previous_start, $previous_end, $threshold, $count_only = false, $selected_employee = 0) {
  986. // 构建业务员筛选条件
  987. $employee_filter = "";
  988. if (!empty($selected_employee)) {
  989. if (is_numeric($selected_employee)) {
  990. // 单个业务员
  991. $employee_filter = " AND c.cs_belong = " . intval($selected_employee);
  992. } else if (strpos($selected_employee, ',') !== false) {
  993. // 多个业务员(逗号分隔的字符串)
  994. $employee_filter = " AND c.cs_belong IN (" . $selected_employee . ")";
  995. }
  996. }
  997. // 如果只需要计数
  998. if ($count_only) {
  999. $sql = "SELECT COUNT(DISTINCT c.id) as count
  1000. FROM customer c
  1001. LEFT JOIN (
  1002. SELECT customer_id, SUM(total_amount) as amount
  1003. FROM orders
  1004. WHERE order_date BETWEEN '{$current_start}' AND '{$current_end}'
  1005. GROUP BY customer_id
  1006. ) current_period ON c.id = current_period.customer_id
  1007. LEFT JOIN (
  1008. SELECT customer_id, SUM(total_amount) as amount
  1009. FROM orders
  1010. WHERE order_date BETWEEN '{$previous_start}' AND '{$previous_end}'
  1011. GROUP BY customer_id
  1012. ) previous_period ON c.id = previous_period.customer_id
  1013. JOIN employee e ON c.cs_belong = e.id
  1014. WHERE previous_period.amount > 0
  1015. AND (current_period.amount IS NULL OR (current_period.amount / previous_period.amount - 1) * 100 <= {$threshold})
  1016. AND c.cs_deal = 3{$employee_filter}";
  1017. $result = $conn->query($sql);
  1018. $row = $result->fetch_assoc();
  1019. return $row['count'];
  1020. }
  1021. // 如果需要详细数据
  1022. $sql = "SELECT
  1023. c.id,
  1024. c.cs_company,
  1025. IFNULL(current_period.amount, 0) as current_amount,
  1026. previous_period.amount as previous_amount,
  1027. e.em_user,
  1028. IFNULL((SELECT MAX(order_date) FROM orders WHERE customer_id = c.id), '') as last_order_date
  1029. FROM customer c
  1030. LEFT JOIN (
  1031. SELECT customer_id, SUM(total_amount) as amount
  1032. FROM orders
  1033. WHERE order_date BETWEEN '{$current_start}' AND '{$current_end}'
  1034. GROUP BY customer_id
  1035. ) current_period ON c.id = current_period.customer_id
  1036. LEFT JOIN (
  1037. SELECT customer_id, SUM(total_amount) as amount
  1038. FROM orders
  1039. WHERE order_date BETWEEN '{$previous_start}' AND '{$previous_end}'
  1040. GROUP BY customer_id
  1041. ) previous_period ON c.id = previous_period.customer_id
  1042. JOIN employee e ON c.cs_belong = e.id
  1043. WHERE previous_period.amount > 0
  1044. AND (current_period.amount IS NULL OR (current_period.amount / previous_period.amount - 1) * 100 <= {$threshold})
  1045. AND c.cs_deal = 3{$employee_filter}
  1046. ORDER BY (current_period.amount / previous_period.amount) ASC";
  1047. return $conn->query($sql);
  1048. }
  1049. /**
  1050. * 获取复购周期异常的客户(3个月内未录入订单)
  1051. */
  1052. function getAbnormalRepurchaseCycleCustomers($conn, $current_start, $current_end, $threshold, $count_only = false, $selected_employee = 0, $page = 1, $page_size = 10) {
  1053. // 构建业务员筛选条件
  1054. $employee_filter = "";
  1055. if (!empty($selected_employee)) {
  1056. if (is_numeric($selected_employee)) {
  1057. // 单个业务员
  1058. $employee_filter = " AND c.cs_belong = " . intval($selected_employee);
  1059. } else if (strpos($selected_employee, ',') !== false) {
  1060. // 多个业务员(逗号分隔的字符串)
  1061. $employee_filter = " AND c.cs_belong IN (" . $selected_employee . ")";
  1062. }
  1063. }
  1064. if ($count_only) {
  1065. $sql = "SELECT COUNT(DISTINCT c.id) as count
  1066. FROM customer c
  1067. LEFT JOIN (
  1068. SELECT customer_id, MAX(order_date) as last_order_date
  1069. FROM orders
  1070. GROUP BY customer_id
  1071. ) last_orders ON c.id = last_orders.customer_id
  1072. JOIN employee e ON c.cs_belong = e.id
  1073. WHERE c.cs_deal = 3
  1074. AND (
  1075. last_orders.last_order_date IS NULL
  1076. OR DATEDIFF('{$current_end}', last_orders.last_order_date) > {$threshold}
  1077. ){$employee_filter}";
  1078. $result = $conn->query($sql);
  1079. $row = $result->fetch_assoc();
  1080. return $row['count'];
  1081. }
  1082. $offset = ($page - 1) * $page_size;
  1083. $sql = "SELECT
  1084. c.id,
  1085. c.cs_company,
  1086. c.cs_code,
  1087. last_orders.last_order_date,
  1088. CASE
  1089. WHEN last_orders.last_order_date IS NULL THEN DATEDIFF('{$current_end}', c.cs_addtime)
  1090. ELSE DATEDIFF('{$current_end}', last_orders.last_order_date)
  1091. END as inactive_days,
  1092. IFNULL(order_stats.order_count, 0) as order_count,
  1093. IFNULL(order_stats.total_amount, 0) as total_amount,
  1094. e.em_user
  1095. FROM customer c
  1096. LEFT JOIN (
  1097. SELECT customer_id, MAX(order_date) as last_order_date
  1098. FROM orders
  1099. GROUP BY customer_id
  1100. ) last_orders ON c.id = last_orders.customer_id
  1101. LEFT JOIN (
  1102. SELECT customer_id, COUNT(*) as order_count, SUM(total_amount) as total_amount
  1103. FROM orders
  1104. GROUP BY customer_id
  1105. ) order_stats ON c.id = order_stats.customer_id
  1106. JOIN employee e ON c.cs_belong = e.id
  1107. WHERE c.cs_deal = 3
  1108. AND (
  1109. last_orders.last_order_date IS NULL
  1110. OR DATEDIFF('{$current_end}', last_orders.last_order_date) > {$threshold}
  1111. ){$employee_filter}
  1112. ORDER BY inactive_days DESC
  1113. LIMIT {$offset}, {$page_size}";
  1114. return $conn->query($sql);
  1115. }
  1116. /**
  1117. * 获取长期不活跃的客户(3个月内没有客户信息修改)
  1118. */
  1119. function getInactiveCustomers($conn, $end_date, $inactive_days, $count_only = false, $page = 1, $page_size = 10, $selected_employee = 0) {
  1120. // 构建业务员筛选条件
  1121. $employee_filter = "";
  1122. if (!empty($selected_employee)) {
  1123. if (is_numeric($selected_employee)) {
  1124. // 单个业务员
  1125. $employee_filter = " AND c.cs_belong = " . intval($selected_employee);
  1126. } else if (strpos($selected_employee, ',') !== false) {
  1127. // 多个业务员(逗号分隔的字符串)
  1128. $employee_filter = " AND c.cs_belong IN (" . $selected_employee . ")";
  1129. }
  1130. }
  1131. if ($count_only) {
  1132. $sql = "SELECT COUNT(*) as count
  1133. FROM customer c
  1134. JOIN employee e ON c.cs_belong = e.id
  1135. WHERE c.cs_deal = 3
  1136. AND DATEDIFF('{$end_date}', c.cs_updatetime) > {$inactive_days}{$employee_filter}";
  1137. $result = $conn->query($sql);
  1138. $row = $result->fetch_assoc();
  1139. return $row['count'];
  1140. }
  1141. $offset = ($page - 1) * $page_size;
  1142. $sql = "SELECT
  1143. c.id,
  1144. c.cs_company,
  1145. c.cs_code,
  1146. last_orders.last_order_date,
  1147. DATEDIFF('{$end_date}', c.cs_updatetime) as inactive_days,
  1148. c.cs_updatetime as last_update_time,
  1149. IFNULL(order_stats.order_count, 0) as order_count,
  1150. IFNULL(order_stats.total_amount, 0) as total_amount,
  1151. e.em_user
  1152. FROM customer c
  1153. LEFT JOIN (
  1154. SELECT customer_id, MAX(order_date) as last_order_date
  1155. FROM orders
  1156. GROUP BY customer_id
  1157. ) last_orders ON c.id = last_orders.customer_id
  1158. LEFT JOIN (
  1159. SELECT customer_id, COUNT(*) as order_count, SUM(total_amount) as total_amount
  1160. FROM orders
  1161. GROUP BY customer_id
  1162. ) order_stats ON c.id = order_stats.customer_id
  1163. JOIN employee e ON c.cs_belong = e.id
  1164. WHERE c.cs_deal = 3
  1165. AND DATEDIFF('{$end_date}', c.cs_updatetime) > {$inactive_days}{$employee_filter}
  1166. ORDER BY inactive_days DESC
  1167. LIMIT {$offset}, {$page_size}";
  1168. return $conn->query($sql);
  1169. }
  1170. /**
  1171. * 获取流失客户(1年内未录入订单)
  1172. */
  1173. function getChurnCustomers($conn, $end_date, $churn_days, $count_only = false, $page = 1, $page_size = 10, $selected_employee = 0) {
  1174. // 构建业务员筛选条件
  1175. $employee_filter = "";
  1176. if (!empty($selected_employee)) {
  1177. if (is_numeric($selected_employee)) {
  1178. // 单个业务员
  1179. $employee_filter = " AND c.cs_belong = " . intval($selected_employee);
  1180. } else if (strpos($selected_employee, ',') !== false) {
  1181. // 多个业务员(逗号分隔的字符串)
  1182. $employee_filter = " AND c.cs_belong IN (" . $selected_employee . ")";
  1183. }
  1184. }
  1185. if ($count_only) {
  1186. $sql = "SELECT COUNT(*) as count
  1187. FROM customer c
  1188. LEFT JOIN (
  1189. SELECT customer_id, MAX(order_date) as last_order_date
  1190. FROM orders
  1191. GROUP BY customer_id
  1192. ) last_orders ON c.id = last_orders.customer_id
  1193. JOIN employee e ON c.cs_belong = e.id
  1194. WHERE c.cs_deal = 3
  1195. AND (
  1196. last_orders.last_order_date IS NULL
  1197. OR DATEDIFF('{$end_date}', last_orders.last_order_date) > {$churn_days}
  1198. ){$employee_filter}";
  1199. $result = $conn->query($sql);
  1200. $row = $result->fetch_assoc();
  1201. return $row['count'];
  1202. }
  1203. $offset = ($page - 1) * $page_size;
  1204. $sql = "SELECT
  1205. c.id,
  1206. c.cs_company,
  1207. c.cs_code,
  1208. last_orders.last_order_date,
  1209. CASE
  1210. WHEN last_orders.last_order_date IS NULL THEN DATEDIFF('{$end_date}', c.cs_addtime)
  1211. ELSE DATEDIFF('{$end_date}', last_orders.last_order_date)
  1212. END as inactive_days,
  1213. IFNULL(order_stats.order_count, 0) as order_count,
  1214. IFNULL(order_stats.total_amount, 0) as total_amount,
  1215. e.em_user
  1216. FROM customer c
  1217. LEFT JOIN (
  1218. SELECT customer_id, MAX(order_date) as last_order_date
  1219. FROM orders
  1220. GROUP BY customer_id
  1221. ) last_orders ON c.id = last_orders.customer_id
  1222. LEFT JOIN (
  1223. SELECT customer_id, COUNT(*) as order_count, SUM(total_amount) as total_amount
  1224. FROM orders
  1225. GROUP BY customer_id
  1226. ) order_stats ON c.id = order_stats.customer_id
  1227. JOIN employee e ON c.cs_belong = e.id
  1228. WHERE c.cs_deal = 3
  1229. AND (
  1230. last_orders.last_order_date IS NULL
  1231. OR DATEDIFF('{$end_date}', last_orders.last_order_date) > {$churn_days}
  1232. ){$employee_filter}
  1233. ORDER BY inactive_days DESC
  1234. LIMIT {$offset}, {$page_size}";
  1235. return $conn->query($sql);
  1236. }
  1237. // 页面底部
  1238. include('statistics_footer.php');
  1239. ?>