customerSave.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. // For validation, we'll check the first contact (primary contact)
  57. $primary_contact = !empty($contacts) ? current($contacts) : [];
  58. $cs_name = textEncode($primary_contact['contact_name'] ?? '');
  59. $cs_tel = textEncode($primary_contact['tel'] ?? '');
  60. $cs_wechat = textEncode($primary_contact['wechat'] ?? '');
  61. $cs_whatsapp = textEncode($primary_contact['whatsapp'] ?? '');
  62. $cs_email = textEncode($primary_contact['email'] ?? '');
  63. $cs_linkedin = textEncode($primary_contact['linkedin'] ?? '');
  64. $cs_facebook = textEncode($primary_contact['facebook'] ?? '');
  65. $cs_alibaba = textEncode($primary_contact['alibaba'] ?? '');
  66. $cs_telformat = numFormat($cs_tel);
  67. $cs_whatsappformat = numFormat($cs_whatsapp);
  68. // Validation checks
  69. if ($allowedit != 1) {
  70. // Alibaba validation
  71. if (($cs_from == 1 || $cs_from == 2) && empty($cs_alibaba)) {
  72. echo "<script>alert('阿里旺旺为必填项');history.back();</script>";
  73. exit;
  74. }
  75. // Website source validation
  76. if (strpos($cs_code, '官网') !== false) {
  77. $cs_from = 3;
  78. }
  79. if ($cs_from == 3 && empty($cs_tel) && empty($cs_whatsapp) && empty($cs_wechat)) {
  80. echo "<script>alert('电话和WhatsApp为必填项');history.back();</script>";
  81. exit;
  82. }
  83. // Market customer validation
  84. if ($cs_from == 8 && empty($cs_wechat)) {
  85. echo "<script>alert('微信为必填项');history.back();</script>";
  86. exit;
  87. }
  88. // Facebook validation
  89. if ($cs_from == 12 && empty($cs_facebook)) {
  90. echo "<script>alert('Facebook为必填项');history.back();</script>";
  91. exit;
  92. }
  93. }
  94. // Determine action type
  95. $act = empty($id) || !is_numeric($id) ? 'addSave' : 'editSave';
  96. if ($act === 'editSave') {
  97. // Verify customer ownership
  98. $stmt = $conn->prepare("SELECT cs_belong FROM customer WHERE id = ?");
  99. $stmt->bind_param("i", $id);
  100. $stmt->execute();
  101. $result = $stmt->get_result();
  102. if ($row = $result->fetch_assoc()) {
  103. if ($row['cs_belong'] != $cs_belong) {
  104. echo "<script>alert('抱歉,该客户属于另一业务,你没有权限修改');history.back();</script>";
  105. exit;
  106. }
  107. } else {
  108. echo "<script>alert('没有此客户!');history.back();</script>";
  109. exit;
  110. }
  111. }
  112. // Validate customer code
  113. if (empty($cs_code)) {
  114. echo "<script>alert('客户编码不能为空');history.back();</script>";
  115. exit;
  116. }
  117. // Check for duplicate customer information
  118. $checkStr = "SELECT c.*, cc.*
  119. FROM customer c
  120. LEFT JOIN customer_contact cc ON c.id = cc.customer_id
  121. WHERE c.cs_belong != " . $_SESSION['employee_id'] . " AND (c.id = 0 ";
  122. $Dupli = "";
  123. // Get the first contact's information for validation
  124. $primary_contact = !empty($contacts) ? current($contacts) : [];
  125. // Check all phone numbers
  126. for ($i = 1; $i <= 3; $i++) {
  127. $tel_field = 'tel_' . $i;
  128. $tel_format_field = 'tel_' . $i . '_format';
  129. if (!empty($primary_contact[$tel_field])) {
  130. $tel_format = numFormat($primary_contact[$tel_field]);
  131. $checkStr .= " OR cc.tel_1_format LIKE '%" . substr($tel_format, 3, 9) . "%'" .
  132. " OR cc.tel_2_format LIKE '%" . substr($tel_format, 3, 9) . "%'" .
  133. " OR cc.tel_3_format LIKE '%" . substr($tel_format, 3, 9) . "%'" .
  134. " OR cc.wechat_1 LIKE '%" . substr($tel_format, 3, 9) . "%'" .
  135. " OR cc.wechat_2 LIKE '%" . substr($tel_format, 3, 9) . "%'" .
  136. " OR cc.wechat_3 LIKE '%" . substr($tel_format, 3, 9) . "%'";
  137. $Dupli .= "电话" . $i . ":" . $primary_contact[$tel_field] . " ";
  138. }
  139. }
  140. // Check all email addresses
  141. for ($i = 1; $i <= 3; $i++) {
  142. $email_field = 'email_' . $i;
  143. if (!empty($primary_contact[$email_field])) {
  144. $checkStr .= " OR cc.email_1 = '" . $conn->real_escape_string($primary_contact[$email_field]) . "'" .
  145. " OR cc.email_2 = '" . $conn->real_escape_string($primary_contact[$email_field]) . "'" .
  146. " OR cc.email_3 = '" . $conn->real_escape_string($primary_contact[$email_field]) . "'";
  147. $Dupli .= "邮箱" . $i . ":" . $primary_contact[$email_field] . " ";
  148. }
  149. }
  150. // Check all WhatsApp numbers
  151. for ($i = 1; $i <= 3; $i++) {
  152. $whatsapp_field = 'whatsapp_' . $i;
  153. $whatsapp_format_field = 'whatsapp_' . $i . '_format';
  154. if (!empty($primary_contact[$whatsapp_field])) {
  155. $whatsapp_format = numFormat($primary_contact[$whatsapp_field]);
  156. $checkStr .= " OR cc.whatsapp_1_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'" .
  157. " OR cc.whatsapp_2_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'" .
  158. " OR cc.whatsapp_3_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'" .
  159. " OR cc.tel_1_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'" .
  160. " OR cc.tel_2_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'" .
  161. " OR cc.tel_3_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'";
  162. $Dupli .= "WhatsApp" . $i . ":" . $primary_contact[$whatsapp_field] . " ";
  163. }
  164. }
  165. // Check all WeChat accounts
  166. for ($i = 1; $i <= 3; $i++) {
  167. $wechat_field = 'wechat_' . $i;
  168. if (!empty($primary_contact[$wechat_field])) {
  169. if (strlen($primary_contact[$wechat_field]) < 10) {
  170. $checkStr .= " OR cc.wechat_1 LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'" .
  171. " OR cc.wechat_2 LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'" .
  172. " OR cc.wechat_3 LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'" .
  173. " OR cc.tel_1_format LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'" .
  174. " OR cc.tel_2_format LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'" .
  175. " OR cc.tel_3_format LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'";
  176. } else {
  177. $checkStr .= " OR cc.wechat_1 LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'" .
  178. " OR cc.wechat_2 LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'" .
  179. " OR cc.wechat_3 LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'" .
  180. " OR cc.tel_1_format LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'" .
  181. " OR cc.tel_2_format LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'" .
  182. " OR cc.tel_3_format LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'";
  183. }
  184. $Dupli .= "微信" . $i . ":" . $primary_contact[$wechat_field] . " ";
  185. }
  186. }
  187. // Check all LinkedIn accounts
  188. for ($i = 1; $i <= 3; $i++) {
  189. $linkedin_field = 'linkedin_' . $i;
  190. if (!empty($primary_contact[$linkedin_field])) {
  191. $checkStr .= " OR cc.linkedin_1 LIKE '%" . $conn->real_escape_string($primary_contact[$linkedin_field]) . "%'" .
  192. " OR cc.linkedin_2 LIKE '%" . $conn->real_escape_string($primary_contact[$linkedin_field]) . "%'" .
  193. " OR cc.linkedin_3 LIKE '%" . $conn->real_escape_string($primary_contact[$linkedin_field]) . "%'";
  194. $Dupli .= "LinkedIn" . $i . ":" . $primary_contact[$linkedin_field] . " ";
  195. }
  196. }
  197. // Check all Facebook accounts
  198. for ($i = 1; $i <= 3; $i++) {
  199. $facebook_field = 'facebook_' . $i;
  200. if (!empty($primary_contact[$facebook_field])) {
  201. $checkStr .= " OR cc.facebook_1 LIKE '%" . $conn->real_escape_string($primary_contact[$facebook_field]) . "%'" .
  202. " OR cc.facebook_2 LIKE '%" . $conn->real_escape_string($primary_contact[$facebook_field]) . "%'" .
  203. " OR cc.facebook_3 LIKE '%" . $conn->real_escape_string($primary_contact[$facebook_field]) . "%'";
  204. $Dupli .= "Facebook" . $i . ":" . $primary_contact[$facebook_field] . " ";
  205. }
  206. }
  207. // Check all Alibaba accounts
  208. for ($i = 1; $i <= 3; $i++) {
  209. $alibaba_field = 'alibaba_' . $i;
  210. if (!empty($primary_contact[$alibaba_field])) {
  211. if (strlen($primary_contact[$alibaba_field]) < 10) {
  212. $checkStr .= " OR cc.alibaba_1 LIKE '" . $conn->real_escape_string($primary_contact[$alibaba_field]) . "'" .
  213. " OR cc.alibaba_2 LIKE '" . $conn->real_escape_string($primary_contact[$alibaba_field]) . "'" .
  214. " OR cc.alibaba_3 LIKE '" . $conn->real_escape_string($primary_contact[$alibaba_field]) . "'";
  215. } else {
  216. $checkStr .= " OR cc.alibaba_1 LIKE '%" . substr($primary_contact[$alibaba_field], 3, 12) . "%'" .
  217. " OR cc.alibaba_2 LIKE '%" . substr($primary_contact[$alibaba_field], 3, 12) . "%'" .
  218. " OR cc.alibaba_3 LIKE '%" . substr($primary_contact[$alibaba_field], 3, 12) . "%'";
  219. }
  220. $Dupli .= "阿里旺旺" . $i . ":" . $primary_contact[$alibaba_field] . " ";
  221. }
  222. }
  223. $checkStr .= " ) ORDER BY c.id ASC";
  224. if ($allowedit != 1) {
  225. $result = $conn->query($checkStr);
  226. if ($result && $result->num_rows > 0) {
  227. $row = $result->fetch_assoc();
  228. // Get owner name
  229. $ownerResult = $conn->query("SELECT em_user FROM employee WHERE id = " . $row['cs_belong']);
  230. $ownerRow = $ownerResult->fetch_assoc();
  231. $owner = textUncode($ownerRow['em_user']);
  232. // Determine who entered first
  233. if (strtotime($cs_addtime) > strtotime($row['cs_addtime'])) {
  234. $tstr = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('" .
  235. $_SESSION['employee_name'] . "', '" . getIp() . "', '" . date('Y-m-d H:i:s') . "', '" .
  236. $_SESSION['employee_name'] . "编辑客户\"" . $cs_code . "\",该客户与\"" .
  237. textUncode($row['cs_code']) . "\"高度类似,<br>重复项为:" . $Dupli . "<br>客户由:" .
  238. $_SESSION['employee_name'] . $cs_addtime . "首次录入')";
  239. } else {
  240. $tstr = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES ('" .
  241. $_SESSION['employee_name'] . "', '" . getIp() . "', '" . date('Y-m-d H:i:s') . "', '" .
  242. $_SESSION['employee_name'] . "编辑客户\"" . $cs_code . "\",该客户与\"" .
  243. textUncode($row['cs_code']) . "\"高度类似,<br>重复项为:" . $Dupli . "<br>客户由:" .
  244. $owner . $row['cs_addtime'] . "首次录入')";
  245. }
  246. $conn->query($tstr);
  247. echo "<script>alert('录入信息\\n与" . $owner . "客户编号:" . textUncode($row['cs_code']) .
  248. "\\n高度类似,未能保存,请联系管理员核实!');history.back();</script>";
  249. exit;
  250. }
  251. }
  252. // Save or update customer data
  253. if ($act == "editSave" || $allowedit == 1) {
  254. $hrefstr = "/customers.php?Keys=" . $keys . "&fliterBusiness=" . $fliterBusiness .
  255. "&fliterDeal=" . $fliterDeal . "&Page=" . $page;
  256. // 更新客户基本信息
  257. $updateSql = "UPDATE customer SET
  258. cs_code='" . $conn->real_escape_string($cs_code) . "',
  259. cs_company='" . $conn->real_escape_string($cs_company) . "',
  260. cs_country=" . $cs_country . ",
  261. cs_from=" . $cs_from . ",
  262. cs_address='" . $conn->real_escape_string($cs_address) . "',
  263. cs_updatetime='" . $cs_updatetime . "',
  264. cs_belong=" . $cs_belong . ",
  265. cs_belongclient=" . $cs_belongClient . ",
  266. cs_state=" . $cs_state . ",
  267. cs_deal=" . $cs_deal . ",
  268. cs_note='" . $conn->real_escape_string($cs_note) . "'";
  269. // 处理cs_dealdate
  270. if ($cs_deal == 3) {
  271. $updateSql .= ", cs_dealdate = CASE WHEN cs_dealdate IS NULL THEN NOW() ELSE cs_dealdate END";
  272. }
  273. $updateSql .= " WHERE id=" . intval($id);
  274. $conn->query($updateSql);
  275. // 处理联系人信息 - 首先删除已有的不在提交列表中的联系人
  276. $existingContactIds = [];
  277. foreach ($contacts as $contact) {
  278. if (!empty($contact['id'])) {
  279. $existingContactIds[] = (int)$contact['id'];
  280. }
  281. }
  282. if (!empty($existingContactIds)) {
  283. $idsToKeep = implode(',', $existingContactIds);
  284. $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = " . intval($id) .
  285. " AND id NOT IN (" . $idsToKeep . ")";
  286. } else {
  287. $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = " . intval($id);
  288. }
  289. $conn->query($deleteContactsSql);
  290. // 处理联系人信息 - 更新或添加联系人
  291. foreach ($contacts as $contact) {
  292. $contact_id = !empty($contact['id']) ? (int)$contact['id'] : 0;
  293. $contact_name = textEncode($contact['contact_name'] ?? '');
  294. // 准备SQL字段和值
  295. $fields = ['contact_name'];
  296. $values = ["'" . $conn->real_escape_string($contact_name) . "'"];
  297. $updates = ["contact_name = '" . $conn->real_escape_string($contact_name) . "'"];
  298. // 处理所有联系方式类型
  299. $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
  300. foreach ($methodTypes as $type) {
  301. for ($i = 1; $i <= 3; $i++) {
  302. $field = $type . '_' . $i;
  303. $format_field = $field . '_format';
  304. $bu_field = $field . '_bu';
  305. $value = textEncode($contact[$field] ?? '');
  306. $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
  307. $bu_value = textEncode($contact[$bu_field] ?? $value);
  308. // 添加字段名
  309. $fields[] = $field;
  310. $fields[] = $bu_field;
  311. if ($type == 'tel' || $type == 'whatsapp') {
  312. $fields[] = $format_field;
  313. }
  314. // 添加值
  315. $values[] = "'" . $conn->real_escape_string($value) . "'";
  316. $values[] = "'" . $conn->real_escape_string($bu_value) . "'";
  317. if ($type == 'tel' || $type == 'whatsapp') {
  318. $values[] = "'" . $conn->real_escape_string($format_value) . "'";
  319. }
  320. // 添加更新语句
  321. $updates[] = $field . " = '" . $conn->real_escape_string($value) . "'";
  322. $updates[] = $bu_field . " = '" . $conn->real_escape_string($bu_value) . "'";
  323. if ($type == 'tel' || $type == 'whatsapp') {
  324. $updates[] = $format_field . " = '" . $conn->real_escape_string($format_value) . "'";
  325. }
  326. }
  327. }
  328. if ($contact_id > 0) {
  329. // 更新已有联系人
  330. $updateContactSql = "UPDATE customer_contact SET " .
  331. implode(", ", $updates) . ", updated_at = NOW() " .
  332. "WHERE id = " . $contact_id . " AND customer_id = " . intval($id);
  333. $conn->query($updateContactSql);
  334. } else {
  335. // 添加新联系人
  336. $insertContactSql = "INSERT INTO customer_contact (" .
  337. implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
  338. implode(", ", $values) . ", " . intval($id) . ", NOW(), NOW())";
  339. $conn->query($insertContactSql);
  340. }
  341. }
  342. // Update tags
  343. $conn->query("DELETE FROM tagtable WHERE customerId = " . intval($id));
  344. foreach ($mytag as $tag) {
  345. if (!empty(trim($tag))) {
  346. $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" .
  347. $conn->real_escape_string($tag) . "', " .
  348. intval($_SESSION['employee_id']) . ", " .
  349. intval($id) . ")";
  350. $conn->query($tagSql);
  351. }
  352. }
  353. echo "<script>location.href='$hrefstr';</script>";
  354. } else {
  355. // Insert new customer record
  356. $insertSql = "INSERT INTO customer (
  357. cs_code, cs_company, cs_country, cs_from, cs_address,
  358. cs_type, cs_addtime, cs_updatetime, cs_belong, cs_belongClient,
  359. cs_state, cs_deal, cs_note, cs_chain, is_silent, cs_dealdate
  360. ) VALUES (
  361. '" . $conn->real_escape_string($cs_code) . "',
  362. '" . $conn->real_escape_string($cs_company) . "',
  363. " . $cs_country . ",
  364. " . $cs_from . ",
  365. '" . $conn->real_escape_string($cs_address) . "',
  366. " . $cs_type . ",
  367. NOW(),
  368. NOW(),
  369. " . $cs_belong . ",
  370. " . $cs_belongClient . ",
  371. " . $cs_state . ",
  372. " . $cs_deal . ",
  373. '" . $conn->real_escape_string($cs_note) . "',
  374. " . $cs_belong . ",
  375. 0,
  376. " . ($cs_deal == 3 ? "NOW()" : "NULL") . "
  377. )";
  378. $conn->query($insertSql);
  379. $new_customer_id = $conn->insert_id;
  380. // Insert contact information for all contacts
  381. if ($new_customer_id > 0) {
  382. foreach ($contacts as $contact) {
  383. $contact_name = textEncode($contact['contact_name'] ?? '');
  384. // 准备SQL字段和值
  385. $fields = ['contact_name'];
  386. $values = ["'" . $conn->real_escape_string($contact_name) . "'"];
  387. // 处理所有联系方式类型
  388. $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
  389. foreach ($methodTypes as $type) {
  390. for ($i = 1; $i <= 3; $i++) {
  391. $field = $type . '_' . $i;
  392. $format_field = $field . '_format';
  393. $bu_field = $field . '_bu';
  394. $value = textEncode($contact[$field] ?? '');
  395. $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
  396. $bu_value = textEncode($contact[$bu_field] ?? $value);
  397. // 添加字段名
  398. $fields[] = $field;
  399. $fields[] = $bu_field;
  400. if ($type == 'tel' || $type == 'whatsapp') {
  401. $fields[] = $format_field;
  402. }
  403. // 添加值
  404. $values[] = "'" . $conn->real_escape_string($value) . "'";
  405. $values[] = "'" . $conn->real_escape_string($bu_value) . "'";
  406. if ($type == 'tel' || $type == 'whatsapp') {
  407. $values[] = "'" . $conn->real_escape_string($format_value) . "'";
  408. }
  409. }
  410. }
  411. // 添加新联系人
  412. $insertContactSql = "INSERT INTO customer_contact (" .
  413. implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
  414. implode(", ", $values) . ", " . $new_customer_id . ", NOW(), NOW())";
  415. $conn->query($insertContactSql);
  416. }
  417. // Save tags for new customer
  418. foreach ($mytag as $tag) {
  419. if (!empty(trim($tag))) {
  420. $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" .
  421. $conn->real_escape_string($tag) . "', " .
  422. intval($_SESSION['employee_id']) . ", " .
  423. intval($new_customer_id) . ")";
  424. $conn->query($tagSql);
  425. }
  426. }
  427. }
  428. echo "<script>location.href='customerAdd.php';</script>";
  429. }
  430. ?>
  431. </body>
  432. </html>