claimCustomer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. $cid = $_GET['cid'] ?? '';
  5. if (empty($cid) || !is_numeric($cid)) {
  6. $cid = 0;
  7. }
  8. // Check claim limit for today
  9. $sqlstr = "SELECT COUNT(id) as claimcount FROM customer WHERE cs_belong = " . $_SESSION['employee_id'] .
  10. " AND cs_claimdate > '" . date('Y-m-d') . "'";
  11. $result = $conn->query($sqlstr);
  12. $row = $result->fetch_assoc();
  13. $reachedLimit = ($row['claimcount'] > 10);
  14. // Get employee code
  15. $result = $conn->query("SELECT em_code FROM employee WHERE id = " . $_SESSION['employee_id']);
  16. $row = $result->fetch_assoc();
  17. $em_code = $row['em_code'];
  18. if ($reachedLimit) {
  19. echo "-1";
  20. } else {
  21. // Get customer info and update
  22. $sql = "SELECT employee.id as originalId, cs_code, em_user, cs_belong, cs_updatetime, cs_claimdate,
  23. cs_claimFrom, cs_chain, is_silent
  24. FROM customer
  25. LEFT JOIN employee ON customer.cs_belong = employee.id
  26. WHERE customer.id = " . $conn->real_escape_string($cid);
  27. $result = $conn->query($sql);
  28. if ($row = $result->fetch_assoc()) {
  29. // 检查是否在认领自己的客户
  30. if ($row['cs_belong'] == $_SESSION['employee_id']) {
  31. echo "-2"; // 返回错误代码 -2 表示不能认领自己的客户
  32. exit;
  33. }
  34. $oldCode = $row['cs_code'];
  35. $originalEmp = $row['em_user'];
  36. $originalId= $row['cs_belong'];
  37. $newCode = str_replace("-", "/0" . substr($em_code, 1) . "-", $oldCode);
  38. // Update chain and check circulation
  39. $newChain = $row['cs_chain'] . "," . $_SESSION['employee_id'];
  40. $circulation = substr_count($newChain, ',');
  41. $is_silent = ($circulation > 3) ? 1 : 0;
  42. // Update customer
  43. $updateSql = "UPDATE customer SET
  44. cs_belong = " . $_SESSION['employee_id'] . ",
  45. cs_claimdate = NOW(),
  46. cs_code = '" . $conn->real_escape_string($newCode) . "',
  47. cs_updatetime = NOW(),
  48. cs_claimFrom = " . $row['originalId'] . ",
  49. cs_chain = '" . $conn->real_escape_string($newChain) . "',
  50. is_silent = " . $is_silent . "
  51. WHERE id = " . $cid;
  52. $conn->query($updateSql);
  53. // Insert claim record
  54. $insertSql = "INSERT INTO claimrecord (oldCode, originalEmp,original_employee_id, newEmp,new_employee_id, cs_id, claimTime, isread)
  55. VALUES (
  56. '" . $conn->real_escape_string($oldCode) . "',
  57. '" . $conn->real_escape_string($originalEmp) . "',
  58. '" . $conn->real_escape_string($originalId) . "',
  59. '" . $conn->real_escape_string($_SESSION['employee_name']) . "',
  60. '" . $conn->real_escape_string($_SESSION['employee_id']) . "',
  61. " . $cid . ",
  62. NOW(),
  63. 0
  64. )";
  65. $conn->query($insertSql);
  66. // Delete tags
  67. $conn->query("DELETE FROM tagtable WHERE customerId = " . $cid);
  68. // 发送消息给原业务员
  69. if ($originalId > 0) {
  70. // 获取客户名称
  71. $customerSql = "SELECT cs_code FROM customer WHERE id = " . $cid;
  72. $customerResult = $conn->query($customerSql);
  73. $customerRow = $customerResult->fetch_assoc();
  74. $customerName = $customerRow ? $customerRow['cs_code'] : '客户';
  75. // 构建消息内容
  76. $messageTitle = "客户被认领通知";
  77. $messageContent = "您的客户 {$customerName}(原编码:{$oldCode})已被 {$_SESSION['employee_name']} 认领。\n";
  78. $messageContent=textDecode($messageContent);
  79. $messageContent .= "认领时间:" . date('Y-m-d H:i:s') . "\n";
  80. $messageContent .= "新客户编号:{$newCode}\n";
  81. // 发送消息
  82. sendMessage(
  83. $messageTitle,
  84. $messageContent,
  85. 2, // 客户相关
  86. 0, // 个人消息
  87. $originalId, // 原业务员ID
  88. 1 // 重要
  89. );
  90. }
  91. echo "1";
  92. }
  93. }
  94. ?>