statistics_order_warnings.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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 is_deleted = 0 AND 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" id="decreasing-customers">
  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. // 获取分页参数
  309. $decreasing_page = isset($_GET['decreasing_page']) ? intval($_GET['decreasing_page']) : 1;
  310. $decreasing_page_size = 10; // 每页显示10条记录
  311. // 获取总记录数
  312. $total_decreasing = getDecreasingOrderAmountCustomers(
  313. $conn,
  314. $current_start_date,
  315. $current_end_date,
  316. $previous_start_date,
  317. $previous_end_date,
  318. $order_amount_decrease_threshold,
  319. true,
  320. $query_employee_filter
  321. );
  322. // 计算总页数
  323. $decreasing_total_pages = ceil($total_decreasing / $decreasing_page_size);
  324. // 确保页码合法
  325. if ($decreasing_page < 1) $decreasing_page = 1;
  326. if ($decreasing_page > $decreasing_total_pages && $decreasing_total_pages > 0) $decreasing_page = $decreasing_total_pages;
  327. // 获取当页数据
  328. $decreasing_customers = getDecreasingOrderAmountCustomers(
  329. $conn,
  330. $current_start_date,
  331. $current_end_date,
  332. $previous_start_date,
  333. $previous_end_date,
  334. $order_amount_decrease_threshold,
  335. false,
  336. $query_employee_filter,
  337. $decreasing_page,
  338. $decreasing_page_size
  339. );
  340. while ($customer = $decreasing_customers->fetch_assoc()) {
  341. $change_percent = round((($customer['current_amount'] - $customer['previous_amount']) / $customer['previous_amount']) * 100, 1);
  342. $change_class = $change_percent < -20 ? 'text-danger' : 'text-warning';
  343. echo "<tr>";
  344. echo "<td title='{$customer['cs_code']}'>" . htmlspecialcharsFix($customer['cs_code']) . "</td>";
  345. echo "<td>¥" . number_format($customer['current_amount'], 2) . "</td>";
  346. echo "<td>¥" . number_format($customer['previous_amount'], 2) . "</td>";
  347. echo "<td class='{$change_class}'>" . $change_percent . "%</td>";
  348. echo "<td>" . $customer['last_order_date'] . "</td>";
  349. echo "<td>" . htmlspecialcharsFix($customer['em_user']) . "</td>";
  350. echo "<td><a href='customer_detail.php?id=" . $customer['id'] . "&from_warning=1' class='action-btn action-btn-view'>查看</a></td>";
  351. echo "</tr>";
  352. }
  353. if ($decreasing_customers->num_rows == 0) {
  354. echo "<tr><td colspan='7' class='text-center'>没有发现订单金额下降的客户</td></tr>";
  355. }
  356. ?>
  357. </tbody>
  358. </table>
  359. <!-- 分页控件 -->
  360. <?php if ($decreasing_total_pages > 1): ?>
  361. <div class="pagination-container">
  362. <ul class="pagination">
  363. <?php
  364. // 生成分页链接的基础URL
  365. $base_url = '?';
  366. foreach ($_GET as $key => $value) {
  367. if ($key != 'decreasing_page') {
  368. $base_url .= $key . '=' . urlencode($value) . '&';
  369. }
  370. }
  371. // 上一页链接
  372. if ($decreasing_page > 1) {
  373. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}decreasing_page=" . ($decreasing_page - 1) . "#decreasing-customers'>上一页</a></li>";
  374. } else {
  375. echo "<li class='pager-item disabled'><a class='pager-link' href='#decreasing-customers'>上一页</a></li>";
  376. }
  377. // 页码链接
  378. $start_page = max(1, $decreasing_page - 2);
  379. $end_page = min($decreasing_total_pages, $decreasing_page + 2);
  380. if ($start_page > 1) {
  381. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}decreasing_page=1#decreasing-customers'>1</a></li>";
  382. if ($start_page > 2) {
  383. echo "<li class='pager-item disabled'><a class='pager-link' href='#decreasing-customers'>...</a></li>";
  384. }
  385. }
  386. for ($i = $start_page; $i <= $end_page; $i++) {
  387. if ($i == $decreasing_page) {
  388. echo "<li class='pager-item active'><a class='pager-link' href='#decreasing-customers'>{$i}</a></li>";
  389. } else {
  390. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}decreasing_page={$i}#decreasing-customers'>{$i}</a></li>";
  391. }
  392. }
  393. if ($end_page < $decreasing_total_pages) {
  394. if ($end_page < $decreasing_total_pages - 1) {
  395. echo "<li class='pager-item disabled'><a class='pager-link' href='#decreasing-customers'>...</a></li>";
  396. }
  397. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}decreasing_page={$decreasing_total_pages}#decreasing-customers'>{$decreasing_total_pages}</a></li>";
  398. }
  399. // 下一页链接
  400. if ($decreasing_page < $decreasing_total_pages) {
  401. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}decreasing_page=" . ($decreasing_page + 1) . "#decreasing-customers'>下一页</a></li>";
  402. } else {
  403. echo "<li class='pager-item disabled'><a class='pager-link' href='#decreasing-customers'>下一页</a></li>";
  404. }
  405. ?>
  406. </ul>
  407. <div class="pagination-info">
  408. 共 <?php echo $total_decreasing; ?> 条记录,当前显示第 <?php echo $decreasing_page; ?> 页,共 <?php echo $decreasing_total_pages; ?> 页
  409. </div>
  410. </div>
  411. <?php endif; ?>
  412. </div>
  413. <!-- 复购周期异常客户列表 -->
  414. <div class="warning-section" id="abnormal-customers">
  415. <div class="section-header">
  416. <h2>复购周期异常客户</h2>
  417. <p>3个月内未录入订单的客户</p>
  418. </div>
  419. <table class="data-table">
  420. <thead>
  421. <tr>
  422. <th>客户编码</th>
  423. <th>上次订单日期</th>
  424. <th>未订单天数</th>
  425. <th>历史订单总数</th>
  426. <th>历史订单总额</th>
  427. <th>业务员</th>
  428. <th>操作</th>
  429. </tr>
  430. </thead>
  431. <tbody>
  432. <?php
  433. // 获取分页参数
  434. $abnormal_page = isset($_GET['abnormal_page']) ? intval($_GET['abnormal_page']) : 1;
  435. $abnormal_page_size = 10; // 每页显示10条记录
  436. // 获取总记录数
  437. $total_abnormal = getAbnormalRepurchaseCycleCustomers($conn, $current_start_date, $current_end_date, $repurchase_cycle_threshold, true, $query_employee_filter);
  438. // 计算总页数
  439. $abnormal_total_pages = ceil($total_abnormal / $abnormal_page_size);
  440. // 确保页码合法
  441. if ($abnormal_page < 1) $abnormal_page = 1;
  442. if ($abnormal_page > $abnormal_total_pages && $abnormal_total_pages > 0) $abnormal_page = $abnormal_total_pages;
  443. // 获取当页数据
  444. $abnormal_customers = getAbnormalRepurchaseCycleCustomers(
  445. $conn,
  446. $current_start_date,
  447. $current_end_date,
  448. $repurchase_cycle_threshold,
  449. false,
  450. $query_employee_filter,
  451. $abnormal_page,
  452. $abnormal_page_size
  453. );
  454. while ($customer = $abnormal_customers->fetch_assoc()) {
  455. $inactive_days = $customer['inactive_days'];
  456. $inactive_class = $inactive_days > 60 ? 'text-danger' : 'text-warning';
  457. echo "<tr>";
  458. echo "<td title='{$customer['cs_code']}'>" . htmlspecialcharsFix($customer['cs_code']) . "</td>";
  459. echo "<td>" . ($customer['last_order_date'] ? $customer['last_order_date'] : '从未下单') . "</td>";
  460. echo "<td class='{$inactive_class}'>" . $inactive_days . "</td>";
  461. echo "<td>" . $customer['order_count'] . "</td>";
  462. echo "<td>¥" . number_format($customer['total_amount'], 2) . "</td>";
  463. echo "<td>" . htmlspecialcharsFix($customer['em_user']) . "</td>";
  464. echo "<td><a href='customer_detail.php?id=" . $customer['id'] . "&from_warning=1' class='action-btn action-btn-view'>查看</a></td>";
  465. echo "</tr>";
  466. }
  467. if ($abnormal_customers->num_rows == 0) {
  468. echo "<tr><td colspan='7' class='text-center'>没有发现复购周期异常的客户</td></tr>";
  469. }
  470. ?>
  471. </tbody>
  472. </table>
  473. <!-- 分页控件 -->
  474. <?php if ($abnormal_total_pages > 1): ?>
  475. <div class="pagination-container">
  476. <ul class="pagination">
  477. <?php
  478. // 生成分页链接的基础URL
  479. $base_url = '?';
  480. foreach ($_GET as $key => $value) {
  481. if ($key != 'abnormal_page') {
  482. $base_url .= $key . '=' . urlencode($value) . '&';
  483. }
  484. }
  485. // 上一页链接
  486. if ($abnormal_page > 1) {
  487. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page=" . ($abnormal_page - 1) . "#abnormal-customers'>上一页</a></li>";
  488. } else {
  489. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>上一页</a></li>";
  490. }
  491. // 页码链接
  492. $start_page = max(1, $abnormal_page - 2);
  493. $end_page = min($abnormal_total_pages, $abnormal_page + 2);
  494. if ($start_page > 1) {
  495. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page=1#abnormal-customers'>1</a></li>";
  496. if ($start_page > 2) {
  497. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>...</a></li>";
  498. }
  499. }
  500. for ($i = $start_page; $i <= $end_page; $i++) {
  501. if ($i == $abnormal_page) {
  502. echo "<li class='pager-item active'><a class='pager-link' href='#abnormal-customers'>{$i}</a></li>";
  503. } else {
  504. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page={$i}#abnormal-customers'>{$i}</a></li>";
  505. }
  506. }
  507. if ($end_page < $abnormal_total_pages) {
  508. if ($end_page < $abnormal_total_pages - 1) {
  509. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>...</a></li>";
  510. }
  511. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page={$abnormal_total_pages}#abnormal-customers'>{$abnormal_total_pages}</a></li>";
  512. }
  513. // 下一页链接
  514. if ($abnormal_page < $abnormal_total_pages) {
  515. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}abnormal_page=" . ($abnormal_page + 1) . "#abnormal-customers'>下一页</a></li>";
  516. } else {
  517. echo "<li class='pager-item disabled'><a class='pager-link' href='#abnormal-customers'>下一页</a></li>";
  518. }
  519. ?>
  520. </ul>
  521. <div class="pagination-info">
  522. 共 <?php echo $total_abnormal; ?> 条记录,当前显示第 <?php echo $abnormal_page; ?> 页,共 <?php echo $abnormal_total_pages; ?> 页
  523. </div>
  524. </div>
  525. <?php endif; ?>
  526. </div>
  527. <!-- 流失客户列表 -->
  528. <div class="warning-section">
  529. <div class="section-header">
  530. <h2>流失客户</h2>
  531. <p>1年内未录入订单的客户</p>
  532. </div>
  533. <table class="data-table">
  534. <thead>
  535. <tr>
  536. <th>客户编码</th>
  537. <th>最后出货日期</th>
  538. <th>未订单天数</th>
  539. <th>历史订单数</th>
  540. <th>历史订单总额</th>
  541. <th>业务员</th>
  542. <th>操作</th>
  543. </tr>
  544. </thead>
  545. <tbody>
  546. <?php
  547. // 获取分页参数
  548. $page = isset($_GET['churn_page']) ? intval($_GET['churn_page']) : 1;
  549. $page_size = 10; // 每页显示10条记录
  550. // 获取总记录数
  551. $total_churn = getChurnCustomers($conn, $current_end_date, $churn_threshold, true, 1, 10, $query_employee_filter);
  552. // 计算总页数
  553. $total_pages = ceil($total_churn / $page_size);
  554. // 确保页码合法
  555. if ($page < 1) $page = 1;
  556. if ($page > $total_pages && $total_pages > 0) $page = $total_pages;
  557. // 获取当页数据
  558. $churn_customers = getChurnCustomers($conn, $current_end_date, $churn_threshold, false, $page, $page_size, $query_employee_filter);
  559. while ($customer = $churn_customers->fetch_assoc()) {
  560. $inactive_days = $customer['inactive_days'];
  561. $inactive_class = $inactive_days > 365 ? 'text-danger' : 'text-warning';
  562. echo "<tr>";
  563. echo "<td title='{$customer['cs_code']}'>" . htmlspecialcharsFix($customer['cs_code']) . "</td>";
  564. echo "<td>" . ($customer['last_order_date'] ? $customer['last_order_date'] : '从未下单') . "</td>";
  565. echo "<td class='{$inactive_class}'>" . $inactive_days . "</td>";
  566. echo "<td>" . $customer['order_count'] . "</td>";
  567. echo "<td>¥" . number_format($customer['total_amount'], 2) . "</td>";
  568. echo "<td>" . htmlspecialcharsFix($customer['em_user']) . "</td>";
  569. echo "<td><a href='customer_detail.php?id=" . $customer['id'] . "&from_warning=1' class='action-btn action-btn-view'>查看</a></td>";
  570. echo "</tr>";
  571. }
  572. if ($churn_customers->num_rows == 0) {
  573. echo "<tr><td colspan='7' class='text-center'>没有发现流失客户</td></tr>";
  574. }
  575. ?>
  576. </tbody>
  577. </table>
  578. <!-- 分页控件 -->
  579. <?php if ($total_pages > 1): ?>
  580. <div class="pagination-container">
  581. <ul class="pagination">
  582. <?php
  583. // 生成分页链接的基础URL
  584. $base_url = '?';
  585. foreach ($_GET as $key => $value) {
  586. if ($key != 'churn_page') {
  587. $base_url .= $key . '=' . urlencode($value) . '&';
  588. }
  589. }
  590. // 上一页链接
  591. if ($page > 1) {
  592. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page=" . ($page - 1) . "#churn-customers'>上一页</a></li>";
  593. } else {
  594. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>上一页</a></li>";
  595. }
  596. // 页码链接
  597. $start_page = max(1, $page - 2);
  598. $end_page = min($total_pages, $page + 2);
  599. if ($start_page > 1) {
  600. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page=1#churn-customers'>1</a></li>";
  601. if ($start_page > 2) {
  602. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>...</a></li>";
  603. }
  604. }
  605. for ($i = $start_page; $i <= $end_page; $i++) {
  606. if ($i == $page) {
  607. echo "<li class='pager-item active'><a class='pager-link' href='#churn-customers'>{$i}</a></li>";
  608. } else {
  609. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page={$i}#churn-customers'>{$i}</a></li>";
  610. }
  611. }
  612. if ($end_page < $total_pages) {
  613. if ($end_page < $total_pages - 1) {
  614. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>...</a></li>";
  615. }
  616. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page={$total_pages}#churn-customers'>{$total_pages}</a></li>";
  617. }
  618. // 下一页链接
  619. if ($page < $total_pages) {
  620. echo "<li class='pager-item'><a class='pager-link' href='{$base_url}churn_page=" . ($page + 1) . "#churn-customers'>下一页</a></li>";
  621. } else {
  622. echo "<li class='pager-item disabled'><a class='pager-link' href='#churn-customers'>下一页</a></li>";
  623. }
  624. ?>
  625. </ul>
  626. <div class="pagination-info">
  627. 共 <?php echo $total_churn; ?> 条记录,当前显示第 <?php echo $page; ?> 页,共 <?php echo $total_pages; ?> 页
  628. </div>
  629. </div>
  630. <?php endif; ?>
  631. </div>
  632. <!-- 总体订单趋势图 -->
  633. <div class="chart-section">
  634. <div class="section-header">
  635. <h2>总体订单趋势</h2>
  636. <p>最近12个月的订单数量和金额趋势</p>
  637. </div>
  638. <div class="order-trend-chart-container">
  639. <canvas id="orderTrendChart"></canvas>
  640. </div>
  641. <style>
  642. /* 响应式布局:调整图表高度 */
  643. .order-trend-chart-container {
  644. width: 100%;
  645. height: auto;
  646. }
  647. @media (min-width: 768px) {
  648. .order-trend-chart-container {
  649. height: 33vh; /* 视窗高度的1/3 */
  650. }
  651. }
  652. </style>
  653. <?php
  654. // 获取最近12个月的订单趋势数据
  655. $sql_trend = "SELECT
  656. DATE_FORMAT(order_date, '%Y-%m') as month,
  657. COUNT(*) as order_count,
  658. SUM(total_amount) as total_amount
  659. FROM orders o
  660. JOIN customer c ON o.customer_id = c.id
  661. WHERE o.is_deleted = 0 AND order_date >= DATE_SUB(?, INTERVAL 11 MONTH)";
  662. // 添加业务员筛选条件
  663. if (is_array($query_employee_filter) && !empty($query_employee_filter)) {
  664. // 如果是组长查看团队数据
  665. $sql_trend .= " AND c.cs_belong IN (" . $query_employee_filter . ")";
  666. } else if (!is_array($query_employee_filter) && $query_employee_filter > 0) {
  667. // 如果是查看单个业务员数据
  668. $sql_trend .= " AND c.cs_belong = ?";
  669. }
  670. $sql_trend .= " GROUP BY DATE_FORMAT(order_date, '%Y-%m') ORDER BY month";
  671. $stmt = $conn->prepare($sql_trend);
  672. if (!is_array($query_employee_filter) && $query_employee_filter > 0) {
  673. $stmt->bind_param("si", $current_end_date, $query_employee_filter);
  674. } else {
  675. $stmt->bind_param("s", $current_end_date);
  676. }
  677. $stmt->execute();
  678. $trend_result = $stmt->get_result();
  679. $months = [];
  680. $order_counts = [];
  681. $order_amounts = [];
  682. while ($row = $trend_result->fetch_assoc()) {
  683. $months[] = $row['month'];
  684. $order_counts[] = $row['order_count'];
  685. $order_amounts[] = $row['total_amount'];
  686. }
  687. // 转为JSON格式,用于JavaScript图表
  688. $months_json = json_encode($months);
  689. $order_counts_json = json_encode($order_counts);
  690. $order_amounts_json = json_encode($order_amounts);
  691. ?>
  692. </div>
  693. <style>
  694. .page-header {
  695. margin-bottom: 30px;
  696. }
  697. .page-title {
  698. font-size: 24px;
  699. margin-top: 0;
  700. margin-bottom: 5px;
  701. }
  702. .page-description {
  703. color: #666;
  704. margin-bottom: 5px;
  705. }
  706. .role-info {
  707. color: #2196f3;
  708. font-size: 14px;
  709. margin-top: 5px;
  710. font-weight: 500;
  711. }
  712. .filter-form {
  713. background-color: #f8f9fa;
  714. border-radius: 4px;
  715. padding: 20px;
  716. margin-bottom: 30px;
  717. box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  718. }
  719. .filter-form-inline {
  720. display: flex;
  721. flex-wrap: wrap;
  722. gap: 15px;
  723. align-items: end;
  724. }
  725. .warnings-overview {
  726. margin-bottom: 30px;
  727. }
  728. .warnings-overview .row {
  729. display: flex;
  730. flex-wrap: wrap;
  731. margin: 0 -10px;
  732. }
  733. .warnings-overview .col-md-3 {
  734. padding: 0 10px;
  735. width: 25%;
  736. flex: 0 0 25%;
  737. box-sizing: border-box;
  738. }
  739. @media (max-width: 992px) {
  740. .warnings-overview .col-md-3 {
  741. width: 50%;
  742. flex: 0 0 50%;
  743. }
  744. }
  745. @media (max-width: 576px) {
  746. .warnings-overview .col-md-3 {
  747. width: 100%;
  748. flex: 0 0 100%;
  749. }
  750. }
  751. .stat-card {
  752. border-radius: 8px;
  753. padding: 20px;
  754. color: #fff;
  755. height: 100%;
  756. margin-bottom: 20px;
  757. box-shadow: 0 3px 6px rgba(0,0,0,0.16);
  758. text-align: center;
  759. transition: transform 0.3s ease;
  760. }
  761. .stat-card:hover {
  762. transform: translateY(-5px);
  763. }
  764. .stat-card h3 {
  765. margin-top: 0;
  766. font-size: 16px;
  767. font-weight: 500;
  768. margin-bottom: 10px;
  769. }
  770. .stat-value {
  771. font-size: 32px;
  772. font-weight: 600;
  773. margin-bottom: 10px;
  774. }
  775. .stat-desc {
  776. font-size: 15px;
  777. opacity: 0.8;
  778. }
  779. .stat-card.warning {
  780. background-color: #ff9800;
  781. }
  782. .stat-card.danger {
  783. background-color: #f44336;
  784. }
  785. .stat-card.info {
  786. background-color: #2196f3;
  787. }
  788. .stat-card.success {
  789. background-color: #4caf50;
  790. }
  791. .warning-section {
  792. background-color: white;
  793. border-radius: 8px;
  794. padding: 25px;
  795. margin-bottom: 35px;
  796. box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  797. }
  798. .section-header {
  799. margin-bottom: 20px;
  800. }
  801. .section-header h2 {
  802. font-size: 20px;
  803. margin-top: 0;
  804. margin-bottom: 5px;
  805. }
  806. .section-header p {
  807. color: #666;
  808. font-size: 15px;
  809. margin-bottom: 15px;
  810. }
  811. .chart-section {
  812. background-color: white;
  813. border-radius: 4px;
  814. padding: 20px;
  815. margin-bottom: 30px;
  816. box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  817. }
  818. .chart-container {
  819. height: 350px;
  820. }
  821. .text-danger {
  822. color: #f44336;
  823. }
  824. .text-warning {
  825. color: #ff9800;
  826. }
  827. .text-info {
  828. color: #2196f3;
  829. }
  830. /* 调整表格和内容样式 */
  831. .data-table {
  832. margin-bottom: 20px;
  833. width: 100%;
  834. font-size: 14px; /* 增加表格字体大小 */
  835. border-collapse: separate;
  836. border-spacing: 0;
  837. table-layout: fixed; /* 固定表格布局 */
  838. }
  839. .data-table th {
  840. font-size: 15px; /* 表头字体大小 */
  841. padding: 12px 15px;
  842. background-color: #f5f5f5;
  843. font-weight: 600;
  844. }
  845. .data-table td {
  846. padding: 12px 15px;
  847. vertical-align: middle;
  848. white-space: nowrap; /* 防止文本换行 */
  849. overflow: hidden; /* 溢出隐藏 */
  850. text-overflow: ellipsis; /* 文本溢出显示省略号 */
  851. }
  852. /* 为长期不活跃客户表格设置列宽 */
  853. #inactive-customers .data-table th:nth-child(1), /* 客户编码 */
  854. #inactive-customers .data-table td:nth-child(1) {
  855. width: 12%;
  856. }
  857. #inactive-customers .data-table th:nth-child(2), /* 客户名称 */
  858. #inactive-customers .data-table td:nth-child(2) {
  859. width: 20%;
  860. }
  861. #inactive-customers .data-table th:nth-child(3), /* 最后出货日期 */
  862. #inactive-customers .data-table td:nth-child(3) {
  863. width: 12%;
  864. }
  865. #inactive-customers .data-table th:nth-child(4), /* 不活跃天数 */
  866. #inactive-customers .data-table td:nth-child(4) {
  867. width: 10%;
  868. }
  869. #inactive-customers .data-table th:nth-child(5), /* 历史订单数 */
  870. #inactive-customers .data-table td:nth-child(5) {
  871. width: 10%;
  872. }
  873. #inactive-customers .data-table th:nth-child(6), /* 历史订单总额 */
  874. #inactive-customers .data-table td:nth-child(6) {
  875. width: 12%;
  876. }
  877. #inactive-customers .data-table th:nth-child(7), /* 业务员 */
  878. #inactive-customers .data-table td:nth-child(7) {
  879. width: 12%;
  880. }
  881. #inactive-customers .data-table th:nth-child(8), /* 操作 */
  882. #inactive-customers .data-table td:nth-child(8) {
  883. width: 12%;
  884. }
  885. .data-table td:last-child {
  886. min-width: 80px; /* 确保操作列有足够宽度 */
  887. text-align: center;
  888. }
  889. .data-table tr:hover {
  890. background-color: #f9f9f9;
  891. }
  892. .action-btn {
  893. padding: 5px 10px;
  894. margin: 0 3px;
  895. min-width: 60px;
  896. display: inline-block;
  897. text-align: center;
  898. border-radius: 4px;
  899. font-size: 13px;
  900. transition: all 0.3s ease;
  901. }
  902. .action-btn-view {
  903. background-color: #2196f3;
  904. color: white;
  905. border: none;
  906. }
  907. .action-btn-view:hover {
  908. background-color: #0d8aee;
  909. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  910. }
  911. /* 为所有客户列表表格添加共同样式 */
  912. .warning-section .data-table {
  913. border: 1px solid #eee;
  914. border-radius: 4px;
  915. box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  916. }
  917. .warning-section .data-table th:first-child,
  918. .warning-section .data-table td:first-child {
  919. padding-left: 20px; /* 左侧留出更多间距 */
  920. }
  921. /* 表格内数字列的对齐方式 */
  922. .warning-section .data-table td:nth-child(3),
  923. .warning-section .data-table td:nth-child(4),
  924. .warning-section .data-table td:nth-child(5) {
  925. text-align: left;
  926. }
  927. /* 调整整体字体大小 */
  928. body {
  929. font-size: 14px;
  930. }
  931. /* 分页样式 */
  932. .pagination-container {
  933. margin-top: 25px;
  934. display: flex;
  935. flex-direction: column;
  936. align-items: center;
  937. }
  938. .pagination {
  939. display: flex;
  940. list-style: none;
  941. padding: 0;
  942. margin-bottom: 15px;
  943. }
  944. .pagination .pager-item {
  945. margin: 0 2px;
  946. }
  947. .pagination .pager-link {
  948. padding: 8px 16px;
  949. border-radius: 4px;
  950. margin: 0 3px;
  951. font-weight: 500;
  952. border: 1px solid #ddd;
  953. background-color: #fff;
  954. color: #2196f3;
  955. text-decoration: none;
  956. display: inline-block;
  957. }
  958. .pagination .pager-item.active .pager-link {
  959. background-color: #2196f3;
  960. color: white;
  961. border-color: #2196f3;
  962. }
  963. .pagination .pager-item.disabled .pager-link {
  964. color: #999;
  965. cursor: not-allowed;
  966. background-color: #f5f5f5;
  967. }
  968. .pagination-info {
  969. margin-top: 10px;
  970. font-size: 14px;
  971. color: #666;
  972. }
  973. .form-btn {
  974. padding: 8px 15px;
  975. border-radius: 4px;
  976. cursor: pointer;
  977. font-size: 14px;
  978. border: none;
  979. transition: all 0.3s ease;
  980. }
  981. .form-btn-primary {
  982. background-color: #2196f3;
  983. color: white;
  984. }
  985. .form-btn-primary:hover {
  986. background-color: #0d8aee;
  987. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  988. }
  989. /* 添加斑马纹和悬停效果 */
  990. .data-table tr:nth-child(even) {
  991. background-color: #f8f8f8;
  992. }
  993. .data-table tbody tr:hover {
  994. background-color: #f0f7ff;
  995. }
  996. </style>
  997. <script>
  998. function toggleCustomDates() {
  999. const dateRange = document.getElementById('date_range').value;
  1000. const customDateInputs = document.querySelectorAll('.custom-date-inputs');
  1001. if (dateRange === 'custom') {
  1002. customDateInputs.forEach(el => el.style.display = 'inline-block');
  1003. } else {
  1004. customDateInputs.forEach(el => el.style.display = 'none');
  1005. }
  1006. }
  1007. // 订单趋势图
  1008. document.addEventListener('DOMContentLoaded', function() {
  1009. const ctx = document.getElementById('orderTrendChart').getContext('2d');
  1010. const months = <?php echo $months_json; ?>;
  1011. const orderCounts = <?php echo $order_counts_json; ?>;
  1012. const orderAmounts = <?php echo $order_amounts_json; ?>;
  1013. const chart = new Chart(ctx, {
  1014. type: 'line',
  1015. data: {
  1016. labels: months,
  1017. datasets: [
  1018. {
  1019. label: '订单数量',
  1020. data: orderCounts,
  1021. backgroundColor: 'rgba(54, 162, 235, 0.2)',
  1022. borderColor: 'rgba(54, 162, 235, 1)',
  1023. borderWidth: 2,
  1024. yAxisID: 'y-axis-1'
  1025. },
  1026. {
  1027. label: '订单金额',
  1028. data: orderAmounts,
  1029. backgroundColor: 'rgba(255, 99, 132, 0.2)',
  1030. borderColor: 'rgba(255, 99, 132, 1)',
  1031. borderWidth: 2,
  1032. yAxisID: 'y-axis-2'
  1033. }
  1034. ]
  1035. },
  1036. options: {
  1037. responsive: true,
  1038. maintainAspectRatio: false,
  1039. scales: {
  1040. 'y-axis-1': {
  1041. type: 'linear',
  1042. position: 'left',
  1043. title: {
  1044. display: true,
  1045. text: '订单数量'
  1046. }
  1047. },
  1048. 'y-axis-2': {
  1049. type: 'linear',
  1050. position: 'right',
  1051. title: {
  1052. display: true,
  1053. text: '订单金额'
  1054. },
  1055. grid: {
  1056. drawOnChartArea: false
  1057. }
  1058. }
  1059. }
  1060. }
  1061. });
  1062. });
  1063. </script>
  1064. <?php
  1065. /**
  1066. * 获取订单金额下降的客户
  1067. */
  1068. function getDecreasingOrderAmountCustomers($conn, $current_start, $current_end, $previous_start, $previous_end, $threshold, $count_only = false, $selected_employee = 0, $page = 1, $page_size = 10) {
  1069. // 构建业务员筛选条件
  1070. $employee_filter = "";
  1071. if (!empty($selected_employee)) {
  1072. if (is_numeric($selected_employee)) {
  1073. // 单个业务员
  1074. $employee_filter = " AND c.cs_belong = " . intval($selected_employee);
  1075. } else if (strpos($selected_employee, ',') !== false) {
  1076. // 多个业务员(逗号分隔的字符串)
  1077. $employee_filter = " AND c.cs_belong IN (" . $selected_employee . ")";
  1078. }
  1079. }
  1080. // 如果只需要计数
  1081. if ($count_only) {
  1082. $sql = "SELECT COUNT(DISTINCT c.id) as count
  1083. FROM customer c
  1084. LEFT JOIN (
  1085. SELECT customer_id, SUM(total_amount) as amount
  1086. FROM orders
  1087. WHERE is_deleted = 0 AND order_date BETWEEN '{$current_start}' AND '{$current_end}'
  1088. GROUP BY customer_id
  1089. ) current_period ON c.id = current_period.customer_id
  1090. LEFT JOIN (
  1091. SELECT customer_id, SUM(total_amount) as amount
  1092. FROM orders
  1093. WHERE is_deleted = 0 AND order_date BETWEEN '{$previous_start}' AND '{$previous_end}'
  1094. GROUP BY customer_id
  1095. ) previous_period ON c.id = previous_period.customer_id
  1096. JOIN employee e ON c.cs_belong = e.id
  1097. WHERE previous_period.amount > 0
  1098. AND (current_period.amount IS NULL OR (current_period.amount / previous_period.amount - 1) * 100 <= {$threshold})
  1099. AND c.cs_deal = 3{$employee_filter}";
  1100. $result = $conn->query($sql);
  1101. $row = $result->fetch_assoc();
  1102. return $row['count'];
  1103. }
  1104. // 计算分页
  1105. $offset = ($page - 1) * $page_size;
  1106. // 如果需要详细数据
  1107. $sql = "SELECT
  1108. c.id,
  1109. c.cs_company,
  1110. c.cs_code,
  1111. IFNULL(current_period.amount, 0) as current_amount,
  1112. previous_period.amount as previous_amount,
  1113. e.em_user,
  1114. IFNULL((SELECT MAX(order_date) FROM orders WHERE is_deleted = 0 AND customer_id = c.id), '') as last_order_date
  1115. FROM customer c
  1116. LEFT JOIN (
  1117. SELECT customer_id, SUM(total_amount) as amount
  1118. FROM orders
  1119. WHERE is_deleted = 0 AND order_date BETWEEN '{$current_start}' AND '{$current_end}'
  1120. GROUP BY customer_id
  1121. ) current_period ON c.id = current_period.customer_id
  1122. LEFT JOIN (
  1123. SELECT customer_id, SUM(total_amount) as amount
  1124. FROM orders
  1125. WHERE is_deleted = 0 AND order_date BETWEEN '{$previous_start}' AND '{$previous_end}'
  1126. GROUP BY customer_id
  1127. ) previous_period ON c.id = previous_period.customer_id
  1128. JOIN employee e ON c.cs_belong = e.id
  1129. LEFT JOIN (
  1130. SELECT customer_id, MAX(order_date) as last_order_date
  1131. FROM orders
  1132. WHERE is_deleted = 0
  1133. GROUP BY customer_id
  1134. ) last_order ON c.id = last_order.customer_id
  1135. WHERE previous_period.amount > 0
  1136. AND (current_period.amount IS NULL OR (current_period.amount / previous_period.amount - 1) * 100 <= {$threshold})
  1137. AND c.cs_deal = 3{$employee_filter}
  1138. ORDER BY last_order.last_order_date DESC
  1139. LIMIT {$offset}, {$page_size}";
  1140. return $conn->query($sql);
  1141. }
  1142. /**
  1143. * 获取复购周期异常的客户(3个月内未录入订单)
  1144. */
  1145. function getAbnormalRepurchaseCycleCustomers($conn, $current_start, $current_end, $threshold, $count_only = false, $selected_employee = 0, $page = 1, $page_size = 10) {
  1146. // 构建业务员筛选条件
  1147. $employee_filter = "";
  1148. if (!empty($selected_employee)) {
  1149. if (is_numeric($selected_employee)) {
  1150. // 单个业务员
  1151. $employee_filter = " AND c.cs_belong = " . intval($selected_employee);
  1152. } else if (strpos($selected_employee, ',') !== false) {
  1153. // 多个业务员(逗号分隔的字符串)
  1154. $employee_filter = " AND c.cs_belong IN (" . $selected_employee . ")";
  1155. }
  1156. }
  1157. if ($count_only) {
  1158. $sql = "SELECT COUNT(DISTINCT c.id) as count
  1159. FROM customer c
  1160. LEFT JOIN (
  1161. SELECT customer_id, MAX(order_date) as last_order_date
  1162. FROM orders
  1163. WHERE is_deleted = 0
  1164. GROUP BY customer_id
  1165. ) last_orders ON c.id = last_orders.customer_id
  1166. JOIN employee e ON c.cs_belong = e.id
  1167. WHERE c.cs_deal = 3
  1168. AND (
  1169. last_orders.last_order_date IS NULL
  1170. OR DATEDIFF('{$current_end}', last_orders.last_order_date) > {$threshold}
  1171. ){$employee_filter}";
  1172. $result = $conn->query($sql);
  1173. $row = $result->fetch_assoc();
  1174. return $row['count'];
  1175. }
  1176. $offset = ($page - 1) * $page_size;
  1177. $sql = "SELECT
  1178. c.id,
  1179. c.cs_company,
  1180. c.cs_code,
  1181. last_orders.last_order_date,
  1182. CASE
  1183. WHEN last_orders.last_order_date IS NULL THEN DATEDIFF('{$current_end}', c.cs_addtime)
  1184. ELSE DATEDIFF('{$current_end}', last_orders.last_order_date)
  1185. END as inactive_days,
  1186. IFNULL(order_stats.order_count, 0) as order_count,
  1187. IFNULL(order_stats.total_amount, 0) as total_amount,
  1188. e.em_user
  1189. FROM customer c
  1190. LEFT JOIN (
  1191. SELECT customer_id, MAX(order_date) as last_order_date
  1192. FROM orders
  1193. WHERE is_deleted = 0
  1194. GROUP BY customer_id
  1195. ) last_orders ON c.id = last_orders.customer_id
  1196. LEFT JOIN (
  1197. SELECT customer_id, COUNT(*) as order_count, SUM(total_amount) as total_amount
  1198. FROM orders
  1199. WHERE is_deleted = 0
  1200. GROUP BY customer_id
  1201. ) order_stats ON c.id = order_stats.customer_id
  1202. JOIN employee e ON c.cs_belong = e.id
  1203. WHERE c.cs_deal = 3
  1204. AND (
  1205. last_orders.last_order_date IS NULL
  1206. OR DATEDIFF('{$current_end}', last_orders.last_order_date) > {$threshold}
  1207. ){$employee_filter}
  1208. ORDER BY inactive_days DESC
  1209. LIMIT {$offset}, {$page_size}";
  1210. return $conn->query($sql);
  1211. }
  1212. /**
  1213. * 获取长期不活跃的客户(3个月内没有客户信息修改)
  1214. */
  1215. function getInactiveCustomers($conn, $end_date, $inactive_days, $count_only = false, $page = 1, $page_size = 10, $selected_employee = 0) {
  1216. // 构建业务员筛选条件
  1217. $employee_filter = "";
  1218. if (!empty($selected_employee)) {
  1219. if (is_numeric($selected_employee)) {
  1220. // 单个业务员
  1221. $employee_filter = " AND c.cs_belong = " . intval($selected_employee);
  1222. } else if (strpos($selected_employee, ',') !== false) {
  1223. // 多个业务员(逗号分隔的字符串)
  1224. $employee_filter = " AND c.cs_belong IN (" . $selected_employee . ")";
  1225. }
  1226. }
  1227. if ($count_only) {
  1228. $sql = "SELECT COUNT(*) as count
  1229. FROM customer c
  1230. JOIN employee e ON c.cs_belong = e.id
  1231. WHERE c.cs_deal = 3
  1232. AND DATEDIFF('{$end_date}', c.cs_updatetime) > {$inactive_days}{$employee_filter}";
  1233. $result = $conn->query($sql);
  1234. $row = $result->fetch_assoc();
  1235. return $row['count'];
  1236. }
  1237. $offset = ($page - 1) * $page_size;
  1238. $sql = "SELECT
  1239. c.id,
  1240. c.cs_company,
  1241. c.cs_code,
  1242. last_orders.last_order_date,
  1243. DATEDIFF('{$end_date}', c.cs_updatetime) as inactive_days,
  1244. c.cs_updatetime as last_update_time,
  1245. IFNULL(order_stats.order_count, 0) as order_count,
  1246. IFNULL(order_stats.total_amount, 0) as total_amount,
  1247. e.em_user
  1248. FROM customer c
  1249. LEFT JOIN (
  1250. SELECT customer_id, MAX(order_date) as last_order_date
  1251. FROM orders
  1252. WHERE is_deleted = 0
  1253. GROUP BY customer_id
  1254. ) last_orders ON c.id = last_orders.customer_id
  1255. LEFT JOIN (
  1256. SELECT customer_id, COUNT(*) as order_count, SUM(total_amount) as total_amount
  1257. FROM orders
  1258. WHERE is_deleted = 0
  1259. GROUP BY customer_id
  1260. ) order_stats ON c.id = order_stats.customer_id
  1261. JOIN employee e ON c.cs_belong = e.id
  1262. WHERE c.cs_deal = 3
  1263. AND DATEDIFF('{$end_date}', c.cs_updatetime) > {$inactive_days}{$employee_filter}
  1264. ORDER BY inactive_days DESC
  1265. LIMIT {$offset}, {$page_size}";
  1266. return $conn->query($sql);
  1267. }
  1268. /**
  1269. * 获取流失客户(1年内未录入订单)
  1270. */
  1271. function getChurnCustomers($conn, $end_date, $churn_days, $count_only = false, $page = 1, $page_size = 10, $selected_employee = 0) {
  1272. // 构建业务员筛选条件
  1273. $employee_filter = "";
  1274. if (!empty($selected_employee)) {
  1275. if (is_numeric($selected_employee)) {
  1276. // 单个业务员
  1277. $employee_filter = " AND c.cs_belong = " . intval($selected_employee);
  1278. } else if (strpos($selected_employee, ',') !== false) {
  1279. // 多个业务员(逗号分隔的字符串)
  1280. $employee_filter = " AND c.cs_belong IN (" . $selected_employee . ")";
  1281. }
  1282. }
  1283. if ($count_only) {
  1284. $sql = "SELECT COUNT(*) as count
  1285. FROM customer c
  1286. LEFT JOIN (
  1287. SELECT customer_id, MAX(order_date) as last_order_date
  1288. FROM orders
  1289. WHERE is_deleted = 0
  1290. GROUP BY customer_id
  1291. ) last_orders ON c.id = last_orders.customer_id
  1292. JOIN employee e ON c.cs_belong = e.id
  1293. WHERE c.cs_deal = 3
  1294. AND (
  1295. last_orders.last_order_date IS NULL
  1296. OR DATEDIFF('{$end_date}', last_orders.last_order_date) > {$churn_days}
  1297. ){$employee_filter}";
  1298. $result = $conn->query($sql);
  1299. $row = $result->fetch_assoc();
  1300. return $row['count'];
  1301. }
  1302. $offset = ($page - 1) * $page_size;
  1303. $sql = "SELECT
  1304. c.id,
  1305. c.cs_company,
  1306. c.cs_code,
  1307. last_orders.last_order_date,
  1308. CASE
  1309. WHEN last_orders.last_order_date IS NULL THEN DATEDIFF('{$end_date}', c.cs_addtime)
  1310. ELSE DATEDIFF('{$end_date}', last_orders.last_order_date)
  1311. END as inactive_days,
  1312. IFNULL(order_stats.order_count, 0) as order_count,
  1313. IFNULL(order_stats.total_amount, 0) as total_amount,
  1314. e.em_user
  1315. FROM customer c
  1316. LEFT JOIN (
  1317. SELECT customer_id, MAX(order_date) as last_order_date
  1318. FROM orders
  1319. WHERE is_deleted = 0
  1320. GROUP BY customer_id
  1321. ) last_orders ON c.id = last_orders.customer_id
  1322. LEFT JOIN (
  1323. SELECT customer_id, COUNT(*) as order_count, SUM(total_amount) as total_amount
  1324. FROM orders
  1325. WHERE is_deleted = 0
  1326. GROUP BY customer_id
  1327. ) order_stats ON c.id = order_stats.customer_id
  1328. JOIN employee e ON c.cs_belong = e.id
  1329. WHERE c.cs_deal = 3
  1330. AND (
  1331. last_orders.last_order_date IS NULL
  1332. OR DATEDIFF('{$end_date}', last_orders.last_order_date) > {$churn_days}
  1333. ){$employee_filter}
  1334. ORDER BY inactive_days DESC
  1335. LIMIT {$offset}, {$page_size}";
  1336. return $conn->query($sql);
  1337. }
  1338. // 页面底部
  1339. include('statistics_footer.php');
  1340. ?>