customerSave.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. ?>
  5. <!DOCTYPE html>
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  9. <title>管理区域</title>
  10. <link rel="stylesheet" href="system/css/common.css" type="text/css" />
  11. <script src="system/js/jquery-1.7.2.min.js"></script>
  12. <script src="js/js.js"></script>
  13. </head>
  14. <body class="clear">
  15. <?php
  16. // Get query parameters
  17. $page = $_GET['Page'] ?? '';
  18. $keys = urlencode($_GET['Keys'] ?? '');
  19. $fliterBusiness = $_GET['fliterBusiness'] ?? '';
  20. $fliterDeal = $_GET['Deal'] ?? '';
  21. // Get form data
  22. $id = $_POST['id'] ?? '';
  23. $cs_code = textEncode($_POST['cs_code'] ?? '');
  24. $cs_company = textEncode($_POST['cs_company'] ?? '');
  25. $cs_country = $_POST['cs_country'] ?? '';
  26. $cs_from = $_POST['cs_from'] ?? '';
  27. $cs_address = textEncode($_POST['cs_address'] ?? '');
  28. $cs_type = textEncode($_POST['cs_type'] ?? '');
  29. $cs_belongclient = $_POST['cs_belongclient'] ?? '';
  30. $cs_addtime = $_POST['cs_addtime'] ?? '';
  31. $cs_updatetime = date('Y-m-d H:i:s');
  32. $cs_belong = $_SESSION['employee_id'];
  33. $cs_state = 1;
  34. $cs_deal = $_POST['cs_deal'] ?? '';
  35. $allowedit = $_POST['Permissions'] ?? '0';
  36. // Get contact information from the form
  37. $contacts = $_POST['contact'] ?? [];
  38. // Validate numeric values
  39. $allowedit = is_numeric($allowedit) ? $allowedit : 0;
  40. $cs_country = (is_numeric($cs_country) && $cs_country !== '') ? $cs_country : 0;
  41. $cs_from = (is_numeric($cs_from) && $cs_from !== '') ? $cs_from : 0;
  42. $cs_deal = (is_numeric($cs_deal) && $cs_deal !== '') ? $cs_deal : 1;
  43. $cs_type = (is_numeric($cs_type) && $cs_type !== '') ? $cs_type : 5;
  44. $cs_belongClient = (is_numeric($cs_belongclient) && $cs_belongclient !== '') ? $cs_belongclient : 0;
  45. $cs_note = htmlEncode($_POST['cs_note'] ?? '');
  46. $mytag = textEncode($_POST['mytag'] ?? '');
  47. $mytag = str_replace(['&#60;&#47;span&#62;&#60;span&#62;', '&#60;&#47;span&#62;', '&#60;span&#62;'], [',', '', ''], $mytag);
  48. $mytag = explode(',', $mytag);
  49. // Auto-detect source from code
  50. if (strpos($cs_code, ';1688') !== false) {
  51. $cs_from = 1; // 1688
  52. }
  53. if (strpos($cs_code, ';阿里') !== false) {
  54. $cs_from = 2; // International station
  55. }
  56. // Get the first contact for validation (if any)
  57. $primary_contact = !empty($contacts) ? current($contacts) : [];
  58. // Check contact-specific validation requirements based on source
  59. if ($allowedit != 1) {
  60. // Get the first contact's information for validation
  61. $contact_name = textEncode($primary_contact['contact_name'] ?? '');
  62. $tel_1 = textEncode($primary_contact['tel_1'] ?? '');
  63. $wechat_1 = textEncode($primary_contact['wechat_1'] ?? '');
  64. $whatsapp_1 = textEncode($primary_contact['whatsapp_1'] ?? '');
  65. $email_1 = textEncode($primary_contact['email_1'] ?? '');
  66. $alibaba_1 = textEncode($primary_contact['alibaba_1'] ?? '');
  67. $facebook_1 = textEncode($primary_contact['facebook_1'] ?? '');
  68. // Alibaba validation
  69. if (($cs_from == 1 || $cs_from == 2) && empty($alibaba_1)) {
  70. echo "<script>alert('阿里旺旺为必填项');history.back();</script>";
  71. exit;
  72. }
  73. // Website source validation
  74. if (strpos($cs_code, '官网') !== false) {
  75. $cs_from = 3;
  76. }
  77. if ($cs_from == 3 && empty($tel_1) && empty($whatsapp_1) && empty($wechat_1)) {
  78. echo "<script>alert('电话和WhatsApp为必填项');history.back();</script>";
  79. exit;
  80. }
  81. // Market customer validation
  82. if ($cs_from == 8 && empty($wechat_1)) {
  83. echo "<script>alert('微信为必填项');history.back();</script>";
  84. exit;
  85. }
  86. // Facebook validation
  87. if ($cs_from == 12 && empty($facebook_1)) {
  88. echo "<script>alert('Facebook为必填项');history.back();</script>";
  89. exit;
  90. }
  91. }
  92. // Determine action type
  93. $act = empty($id) || !is_numeric($id) ? 'addSave' : 'editSave';
  94. if ($act === 'editSave') {
  95. // Verify customer ownership - 将bind_param改为SQL拼接
  96. $id = intval($id); // 确保ID是整数,防止SQL注入
  97. $sql = "SELECT cs_belong FROM customer WHERE id = " . $id;
  98. $result = $conn->query($sql);
  99. if ($row = $result->fetch_assoc()) {
  100. if ($row['cs_belong'] != $cs_belong) {
  101. echo "<script>alert('抱歉,该客户属于另一业务,你没有权限修改');history.back();</script>";
  102. exit;
  103. }
  104. } else {
  105. echo "<script>alert('没有此客户!');history.back();</script>";
  106. exit;
  107. }
  108. }
  109. // Validate customer code
  110. if (empty($cs_code)) {
  111. echo "<script>alert('客户编码不能为空');history.back();</script>";
  112. exit;
  113. }
  114. // Check for duplicate customer information
  115. $checkStr = "SELECT c.*, cc.*
  116. FROM customer c
  117. LEFT JOIN customer_contact cc ON c.id = cc.customer_id
  118. WHERE c.cs_belong != " . $_SESSION['employee_id'] . " AND (c.id = 0 ";
  119. $Dupli = "";
  120. // Company name similarity check
  121. if (!empty($cs_company)) {
  122. $companyWords = explode(' ', strtolower($cs_company));
  123. foreach ($companyWords as $word) {
  124. if (strlen($word) > 3) { // Only check words longer than 3 characters
  125. $checkStr .= " OR LOWER(c.cs_company) LIKE '%" . $conn->real_escape_string($word) . "%'";
  126. }
  127. }
  128. }
  129. // Address similarity check
  130. if (!empty($cs_address)) {
  131. $addressWords = explode(' ', strtolower($cs_address));
  132. foreach ($addressWords as $word) {
  133. if (strlen($word) > 3) { // Only check words longer than 3 characters
  134. $checkStr .= " OR LOWER(c.cs_address) LIKE '%" . $conn->real_escape_string($word) . "%'";
  135. }
  136. }
  137. }
  138. // Check all contacts for duplicates
  139. foreach ($contacts as $contact) {
  140. // Check all phone numbers
  141. for ($i = 1; $i <= 3; $i++) {
  142. $tel_field = 'tel_' . $i;
  143. if (!empty($contact[$tel_field])) {
  144. $tel_format = numFormat($contact[$tel_field]);
  145. // Remove country code and spaces for better matching
  146. $tel_clean = preg_replace('/[^0-9]/', '', $tel_format);
  147. $checkStr .= " OR cc.tel_1_format LIKE '%" . substr($tel_clean, -9) . "%'" .
  148. " OR cc.tel_2_format LIKE '%" . substr($tel_clean, -9) . "%'" .
  149. " OR cc.tel_3_format LIKE '%" . substr($tel_clean, -9) . "%'" .
  150. " OR cc.wechat_1 LIKE '%" . substr($tel_clean, -9) . "%'" .
  151. " OR cc.wechat_2 LIKE '%" . substr($tel_clean, -9) . "%'" .
  152. " OR cc.wechat_3 LIKE '%" . substr($tel_clean, -9) . "%'";
  153. $Dupli .= "电话" . $i . ":" . $contact[$tel_field] . " ";
  154. }
  155. }
  156. // Check all email addresses
  157. for ($i = 1; $i <= 3; $i++) {
  158. $email_field = 'email_' . $i;
  159. if (!empty($contact[$email_field])) {
  160. $email = strtolower($contact[$email_field]);
  161. $checkStr .= " OR LOWER(cc.email_1) = '" . $conn->real_escape_string($email) . "'" .
  162. " OR LOWER(cc.email_2) = '" . $conn->real_escape_string($email) . "'" .
  163. " OR LOWER(cc.email_3) = '" . $conn->real_escape_string($email) . "'";
  164. $Dupli .= "邮箱" . $i . ":" . $contact[$email_field] . " ";
  165. }
  166. }
  167. // Check all WhatsApp numbers
  168. for ($i = 1; $i <= 3; $i++) {
  169. $whatsapp_field = 'whatsapp_' . $i;
  170. if (!empty($contact[$whatsapp_field])) {
  171. $whatsapp_format = numFormat($contact[$whatsapp_field]);
  172. $whatsapp_clean = preg_replace('/[^0-9]/', '', $whatsapp_format);
  173. $checkStr .= " OR cc.whatsapp_1_format LIKE '%" . substr($whatsapp_clean, -9) . "%'" .
  174. " OR cc.whatsapp_2_format LIKE '%" . substr($whatsapp_clean, -9) . "%'" .
  175. " OR cc.whatsapp_3_format LIKE '%" . substr($whatsapp_clean, -9) . "%'" .
  176. " OR cc.tel_1_format LIKE '%" . substr($whatsapp_clean, -9) . "%'" .
  177. " OR cc.tel_2_format LIKE '%" . substr($whatsapp_clean, -9) . "%'" .
  178. " OR cc.tel_3_format LIKE '%" . substr($whatsapp_clean, -9) . "%'";
  179. $Dupli .= "WhatsApp" . $i . ":" . $contact[$whatsapp_field] . " ";
  180. }
  181. }
  182. // Check all WeChat accounts
  183. for ($i = 1; $i <= 3; $i++) {
  184. $wechat_field = 'wechat_' . $i;
  185. if (!empty($contact[$wechat_field])) {
  186. $wechat = $contact[$wechat_field];
  187. if (strlen($wechat) < 10) {
  188. $checkStr .= " OR cc.wechat_1 LIKE '%" . $conn->real_escape_string($wechat) . "%'" .
  189. " OR cc.wechat_2 LIKE '%" . $conn->real_escape_string($wechat) . "%'" .
  190. " OR cc.wechat_3 LIKE '%" . $conn->real_escape_string($wechat) . "%'" .
  191. " OR cc.tel_1_format LIKE '%" . $conn->real_escape_string($wechat) . "%'" .
  192. " OR cc.tel_2_format LIKE '%" . $conn->real_escape_string($wechat) . "%'" .
  193. " OR cc.tel_3_format LIKE '%" . $conn->real_escape_string($wechat) . "%'";
  194. } else {
  195. $wechat_clean = preg_replace('/[^0-9]/', '', $wechat);
  196. $checkStr .= " OR cc.wechat_1 LIKE '%" . substr($wechat_clean, -9) . "%'" .
  197. " OR cc.wechat_2 LIKE '%" . substr($wechat_clean, -9) . "%'" .
  198. " OR cc.wechat_3 LIKE '%" . substr($wechat_clean, -9) . "%'" .
  199. " OR cc.tel_1_format LIKE '%" . substr($wechat_clean, -9) . "%'" .
  200. " OR cc.tel_2_format LIKE '%" . substr($wechat_clean, -9) . "%'" .
  201. " OR cc.tel_3_format LIKE '%" . substr($wechat_clean, -9) . "%'";
  202. }
  203. $Dupli .= "微信" . $i . ":" . $contact[$wechat_field] . " ";
  204. }
  205. }
  206. // Check all LinkedIn accounts
  207. for ($i = 1; $i <= 3; $i++) {
  208. $linkedin_field = 'linkedin_' . $i;
  209. if (!empty($contact[$linkedin_field])) {
  210. $linkedin = strtolower($contact[$linkedin_field]);
  211. $checkStr .= " OR LOWER(cc.linkedin_1) LIKE '%" . $conn->real_escape_string($linkedin) . "%'" .
  212. " OR LOWER(cc.linkedin_2) LIKE '%" . $conn->real_escape_string($linkedin) . "%'" .
  213. " OR LOWER(cc.linkedin_3) LIKE '%" . $conn->real_escape_string($linkedin) . "%'";
  214. $Dupli .= "LinkedIn" . $i . ":" . $contact[$linkedin_field] . " ";
  215. }
  216. }
  217. // Check all Facebook accounts
  218. for ($i = 1; $i <= 3; $i++) {
  219. $facebook_field = 'facebook_' . $i;
  220. if (!empty($contact[$facebook_field])) {
  221. $facebook = strtolower($contact[$facebook_field]);
  222. $checkStr .= " OR LOWER(cc.facebook_1) LIKE '%" . $conn->real_escape_string($facebook) . "%'" .
  223. " OR LOWER(cc.facebook_2) LIKE '%" . $conn->real_escape_string($facebook) . "%'" .
  224. " OR LOWER(cc.facebook_3) LIKE '%" . $conn->real_escape_string($facebook) . "%'";
  225. $Dupli .= "Facebook" . $i . ":" . $contact[$facebook_field] . " ";
  226. }
  227. }
  228. // Check all Alibaba accounts
  229. for ($i = 1; $i <= 3; $i++) {
  230. $alibaba_field = 'alibaba_' . $i;
  231. if (!empty($contact[$alibaba_field])) {
  232. $alibaba = strtolower($contact[$alibaba_field]);
  233. if (strlen($alibaba) < 10) {
  234. $checkStr .= " OR LOWER(cc.alibaba_1) LIKE '" . $conn->real_escape_string($alibaba) . "'" .
  235. " OR LOWER(cc.alibaba_2) LIKE '" . $conn->real_escape_string($alibaba) . "'" .
  236. " OR LOWER(cc.alibaba_3) LIKE '" . $conn->real_escape_string($alibaba) . "'";
  237. } else {
  238. $alibaba_clean = preg_replace('/[^0-9a-zA-Z]/', '', $alibaba);
  239. $checkStr .= " OR LOWER(cc.alibaba_1) LIKE '%" . substr($alibaba_clean, -9) . "%'" .
  240. " OR LOWER(cc.alibaba_2) LIKE '%" . substr($alibaba_clean, -9) . "%'" .
  241. " OR LOWER(cc.alibaba_3) LIKE '%" . substr($alibaba_clean, -9) . "%'";
  242. }
  243. $Dupli .= "阿里旺旺" . $i . ":" . $contact[$alibaba_field] . " ";
  244. }
  245. }
  246. }
  247. $checkStr .= " ) ORDER BY c.id ASC";
  248. if ($allowedit != 1) {
  249. $result = $conn->query($checkStr);
  250. if ($result && $result->num_rows > 0) {
  251. $row = $result->fetch_assoc();
  252. // Get owner name
  253. $ownerResult = $conn->query("SELECT em_user FROM employee WHERE id = " . $row['cs_belong']);
  254. $ownerRow = $ownerResult->fetch_assoc();
  255. $owner = textUncode($ownerRow['em_user']);
  256. // Determine who entered first
  257. if (strtotime($cs_addtime) > strtotime($row['cs_addtime'])) {
  258. $tstr = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('" .
  259. $_SESSION['employee_name'] . "', '" . getIp() . "', '" . date('Y-m-d H:i:s') . "', '" .
  260. $_SESSION['employee_name'] . "编辑客户\"" . $cs_code . "\",该客户与\"" .
  261. textUncode($row['cs_code']) . "\"高度类似,<br>重复项为:" . $Dupli . "<br>客户由:" .
  262. $_SESSION['employee_name'] . $cs_addtime . "首次录入')";
  263. } else {
  264. $tstr = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('" .
  265. $_SESSION['employee_name'] . "', '" . getIp() . "', '" . date('Y-m-d H:i:s') . "', '" .
  266. $_SESSION['employee_name'] . "编辑客户\"" . $cs_code . "\",该客户与\"" .
  267. textUncode($row['cs_code']) . "\"高度类似,<br>重复项为:" . $Dupli . "<br>客户由:" .
  268. $owner . $row['cs_addtime'] . "首次录入')";
  269. }
  270. $conn->query($tstr);
  271. echo "<script>alert('录入信息\\n与" . $owner . "客户编号:" . textUncode($row['cs_code']) .
  272. "\\n高度类似,未能保存,请联系管理员核实!');history.back();</script>";
  273. exit;
  274. }
  275. }
  276. // Save or update customer data
  277. if ($act == "editSave" || $allowedit == 1) {
  278. $hrefstr = "/customers.php?Keys=" . $keys . "&fliterBusiness=" . $fliterBusiness .
  279. "&fliterDeal=" . $fliterDeal . "&Page=" . $page;
  280. // 更新客户基本信息
  281. $updateSql = "UPDATE customer SET
  282. cs_code='" . $conn->real_escape_string($cs_code) . "',
  283. cs_company='" . $conn->real_escape_string($cs_company) . "',
  284. cs_country=" . $cs_country . ",
  285. cs_from=" . $cs_from . ",
  286. cs_address='" . $conn->real_escape_string($cs_address) . "',
  287. cs_updatetime='" . $cs_updatetime . "',
  288. cs_belong=" . $cs_belong . ",
  289. cs_belongclient=" . $cs_belongClient . ",
  290. cs_state=" . $cs_state . ",
  291. cs_deal=" . $cs_deal . ",
  292. cs_note='" . $conn->real_escape_string($cs_note) . "'";
  293. // 处理cs_dealdate
  294. if ($cs_deal == 3) {
  295. $updateSql .= ", cs_dealdate = CASE WHEN cs_dealdate IS NULL THEN NOW() ELSE cs_dealdate END";
  296. }
  297. $updateSql .= " WHERE id=" . intval($id);
  298. $conn->query($updateSql);
  299. // 处理联系人信息 - 首先删除已有的不在提交列表中的联系人
  300. $existingContactIds = [];
  301. foreach ($contacts as $contact) {
  302. if (!empty($contact['id'])) {
  303. $existingContactIds[] = (int)$contact['id'];
  304. }
  305. }
  306. if (!empty($existingContactIds)) {
  307. $idsToKeep = implode(',', $existingContactIds);
  308. $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = " . intval($id) .
  309. " AND id NOT IN (" . $idsToKeep . ")";
  310. } else {
  311. $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = " . intval($id);
  312. }
  313. $conn->query($deleteContactsSql);
  314. // 处理联系人信息 - 更新或添加联系人
  315. foreach ($contacts as $contact) {
  316. $contact_id = !empty($contact['id']) ? (int)$contact['id'] : 0;
  317. $contact_name = textEncode($contact['contact_name'] ?? '');
  318. // 准备SQL字段和值
  319. $fields = ['contact_name'];
  320. $values = ["'" . $conn->real_escape_string($contact_name) . "'"];
  321. $updates = ["contact_name = '" . $conn->real_escape_string($contact_name) . "'"];
  322. // 处理所有联系方式类型
  323. $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
  324. foreach ($methodTypes as $type) {
  325. for ($i = 1; $i <= 3; $i++) {
  326. $field = $type . '_' . $i;
  327. $format_field = $field . '_format';
  328. $bu_field = $field . '_bu';
  329. $value = textEncode($contact[$field] ?? '');
  330. $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
  331. $bu_value = textEncode($contact[$bu_field] ?? $value);
  332. // 添加字段名
  333. $fields[] = $field;
  334. $fields[] = $bu_field;
  335. if ($type == 'tel' || $type == 'whatsapp') {
  336. $fields[] = $format_field;
  337. }
  338. // 添加值
  339. $values[] = "'" . $conn->real_escape_string($value) . "'";
  340. $values[] = "'" . $conn->real_escape_string($bu_value) . "'";
  341. if ($type == 'tel' || $type == 'whatsapp') {
  342. $values[] = "'" . $conn->real_escape_string($format_value) . "'";
  343. }
  344. // 添加更新语句
  345. $updates[] = $field . " = '" . $conn->real_escape_string($value) . "'";
  346. $updates[] = $bu_field . " = '" . $conn->real_escape_string($bu_value) . "'";
  347. if ($type == 'tel' || $type == 'whatsapp') {
  348. $updates[] = $format_field . " = '" . $conn->real_escape_string($format_value) . "'";
  349. }
  350. }
  351. }
  352. if ($contact_id > 0) {
  353. // 更新已有联系人
  354. $updateContactSql = "UPDATE customer_contact SET " .
  355. implode(", ", $updates) . ", updated_at = NOW() " .
  356. "WHERE id = " . $contact_id . " AND customer_id = " . intval($id);
  357. $conn->query($updateContactSql);
  358. } else {
  359. // 添加新联系人
  360. $insertContactSql = "INSERT INTO customer_contact (" .
  361. implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
  362. implode(", ", $values) . ", " . intval($id) . ", NOW(), NOW())";
  363. $conn->query($insertContactSql);
  364. }
  365. }
  366. // Update tags
  367. $conn->query("DELETE FROM tagtable WHERE customerId = " . intval($id));
  368. foreach ($mytag as $tag) {
  369. if (!empty(trim($tag))) {
  370. $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" .
  371. $conn->real_escape_string($tag) . "', " .
  372. intval($_SESSION['employee_id']) . ", " .
  373. intval($id) . ")";
  374. $conn->query($tagSql);
  375. }
  376. }
  377. echo "<script>location.href='$hrefstr';</script>";
  378. } else {
  379. // Insert new customer record
  380. $insertSql = "INSERT INTO customer (
  381. cs_code, cs_company, cs_country, cs_from, cs_address,
  382. cs_type, cs_addtime, cs_updatetime, cs_belong, cs_belongClient,
  383. cs_state, cs_deal, cs_note, cs_chain, is_silent, cs_dealdate
  384. ) VALUES (
  385. '" . $conn->real_escape_string($cs_code) . "',
  386. '" . $conn->real_escape_string($cs_company) . "',
  387. " . $cs_country . ",
  388. " . $cs_from . ",
  389. '" . $conn->real_escape_string($cs_address) . "',
  390. " . $cs_type . ",
  391. NOW(),
  392. NOW(),
  393. " . $cs_belong . ",
  394. " . $cs_belongClient . ",
  395. " . $cs_state . ",
  396. " . $cs_deal . ",
  397. '" . $conn->real_escape_string($cs_note) . "',
  398. " . $cs_belong . ",
  399. 0,
  400. " . ($cs_deal == 3 ? "NOW()" : "NULL") . "
  401. )";
  402. $conn->query($insertSql);
  403. $new_customer_id = $conn->insert_id;
  404. // Insert contact information for all contacts
  405. if ($new_customer_id > 0) {
  406. foreach ($contacts as $contact) {
  407. $contact_name = textEncode($contact['contact_name'] ?? '');
  408. // 准备SQL字段和值
  409. $fields = ['contact_name'];
  410. $values = ["'" . $conn->real_escape_string($contact_name) . "'"];
  411. // 处理所有联系方式类型
  412. $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
  413. foreach ($methodTypes as $type) {
  414. for ($i = 1; $i <= 3; $i++) {
  415. $field = $type . '_' . $i;
  416. $format_field = $field . '_format';
  417. $bu_field = $field . '_bu';
  418. $value = textEncode($contact[$field] ?? '');
  419. $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
  420. $bu_value = textEncode($contact[$bu_field] ?? $value);
  421. // 添加字段名
  422. $fields[] = $field;
  423. $fields[] = $bu_field;
  424. if ($type == 'tel' || $type == 'whatsapp') {
  425. $fields[] = $format_field;
  426. }
  427. // 添加值
  428. $values[] = "'" . $conn->real_escape_string($value) . "'";
  429. $values[] = "'" . $conn->real_escape_string($bu_value) . "'";
  430. if ($type == 'tel' || $type == 'whatsapp') {
  431. $values[] = "'" . $conn->real_escape_string($format_value) . "'";
  432. }
  433. }
  434. }
  435. // 添加新联系人
  436. $insertContactSql = "INSERT INTO customer_contact (" .
  437. implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
  438. implode(", ", $values) . ", " . $new_customer_id . ", NOW(), NOW())";
  439. $conn->query($insertContactSql);
  440. }
  441. // Save tags for new customer
  442. foreach ($mytag as $tag) {
  443. if (!empty(trim($tag))) {
  444. $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" .
  445. $conn->real_escape_string($tag) . "', " .
  446. intval($_SESSION['employee_id']) . ", " .
  447. intval($new_customer_id) . ")";
  448. $conn->query($tagSql);
  449. }
  450. }
  451. }
  452. echo "<script>location.href='customerAdd.php';</script>";
  453. }
  454. ?>
  455. </body>
  456. </html>