customers.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. // 辅助函数
  5. $act = $_GET['act'] ?? '';
  6. $urlStr = '';
  7. // 处理保存操作
  8. if ($act == "save") {
  9. $isedit = false;
  10. $id = $_POST['id'] ?? '';
  11. if (!empty($id) && is_numeric($id)) {
  12. $isedit = true;
  13. }
  14. // 获取表单数据 - 客户基本信息
  15. $cs_code = textEncode($_POST['cs_code']);
  16. $cs_company = textEncode($_POST['cs_company']);
  17. $cs_belong = $_POST['cs_belong'];
  18. $cs_country = $_POST['cs_country'];
  19. $cs_from = $_POST['cs_from'];
  20. $cs_state = $_POST['cs_state'];
  21. $cs_deal = $_POST['cs_deal'];
  22. $cs_type = $_POST['cs_type'] ?? 5;
  23. $cs_belongclient = $_POST['cs_belongclient'] ?? 0;
  24. $no_content = htmlEncode($_POST['no_content']);
  25. $allowedit = isset($_POST['allowedit']) ? 1 : 0;
  26. $cs_address = textEncode($_POST['cs_address'] ?? '');
  27. $mytag = textEncode($_POST['mytag'] ?? '');
  28. $mytag = str_replace(['&#60;&#47;span&#62;&#60;span&#62;', '&#60;&#47;span&#62;', '&#60;span&#62;'], [',', '', ''], $mytag);
  29. $mytag = explode(',', $mytag);
  30. // 获取联系人信息
  31. $contacts = $_POST['contact'] ?? [];
  32. // 验证必填字段
  33. if (empty($cs_code)) {
  34. echo "<script>alert('客户编码不能为空');history.back();</script>";
  35. exit;
  36. }
  37. if ($cs_country == 0) {
  38. echo "<script>alert('这是哪个国家的客户?');history.back();</script>";
  39. exit;
  40. }
  41. if ($cs_from == "0") {
  42. echo "<script>alert('请填写客户来源!');history.back();</script>";
  43. exit;
  44. }
  45. // 自动检测来源
  46. if (strpos($cs_code, ';1688') !== false) {
  47. $cs_from = 1; // 1688
  48. }
  49. if (strpos($cs_code, ';阿里') !== false) {
  50. $cs_from = 2; // International station
  51. }
  52. if (strpos($cs_code, '官网') !== false) {
  53. $cs_from = 3; // Website
  54. }
  55. // 验证联系方式
  56. $primary_contact = !empty($contacts) ? current($contacts) : [];
  57. if ($allowedit != 1) {
  58. // 阿里巴巴验证
  59. if (($cs_from == 1 || $cs_from == 2) && empty($primary_contact['alibaba_1'])) {
  60. echo "<script>alert('阿里旺旺为必填项');history.back();</script>";
  61. exit;
  62. }
  63. // 官网来源验证
  64. if ($cs_from == 3) {
  65. $has_required = false;
  66. for ($i = 1; $i <= 3; $i++) {
  67. if (!empty($primary_contact['tel_' . $i]) ||
  68. !empty($primary_contact['whatsapp_' . $i]) ||
  69. !empty($primary_contact['wechat_' . $i])) {
  70. $has_required = true;
  71. break;
  72. }
  73. }
  74. if (!$has_required) {
  75. echo "<script>alert('电话和WhatsApp为必填项');history.back();</script>";
  76. exit;
  77. }
  78. }
  79. // 市场客户验证
  80. if ($cs_from == 8) {
  81. $has_wechat = false;
  82. for ($i = 1; $i <= 3; $i++) {
  83. if (!empty($primary_contact['wechat_' . $i])) {
  84. $has_wechat = true;
  85. break;
  86. }
  87. }
  88. if (!$has_wechat) {
  89. echo "<script>alert('微信为必填项');history.back();</script>";
  90. exit;
  91. }
  92. }
  93. // Facebook验证
  94. if ($cs_from == 12) {
  95. $has_facebook = false;
  96. for ($i = 1; $i <= 3; $i++) {
  97. if (!empty($primary_contact['facebook_' . $i])) {
  98. $has_facebook = true;
  99. break;
  100. }
  101. }
  102. if (!$has_facebook) {
  103. echo "<script>alert('Facebook为必填项');history.back();</script>";
  104. exit;
  105. }
  106. }
  107. }
  108. if ($isedit) {
  109. // 验证客户所有权
  110. $sql = "SELECT cs_chain FROM customer WHERE id = $id";
  111. $result = mysqli_query($conn, $sql);
  112. if ($row = mysqli_fetch_assoc($result)) {
  113. $cs_chain = $row['cs_chain'];
  114. $chain_array = explode(',', $cs_chain);
  115. $last_item = end($chain_array);
  116. if ($last_item != $cs_belong) {
  117. $cs_chain .= ",$cs_belong";
  118. }
  119. // 更新客户基本信息
  120. $sql = "UPDATE customer SET
  121. cs_code = '$cs_code',
  122. cs_company = '$cs_company',
  123. cs_belong = '$cs_belong',
  124. cs_country = '$cs_country',
  125. cs_address = '$cs_address',
  126. cs_from = '$cs_from',
  127. cs_state = '$cs_state',
  128. cs_deal = '$cs_deal',
  129. cs_type = '$cs_type',
  130. cs_belongclient = '$cs_belongclient',
  131. cs_note = '$no_content',
  132. allowedit = $allowedit,
  133. cs_chain = '$cs_chain',
  134. cs_updatetime = NOW()";
  135. // 处理cs_dealdate
  136. if ($cs_deal == 3) {
  137. $sql .= ", cs_dealdate = CASE WHEN cs_dealdate IS NULL THEN NOW() ELSE cs_dealdate END";
  138. }
  139. $sql .= " WHERE id = $id";
  140. mysqli_query($conn, $sql);
  141. // 处理联系人信息
  142. $existingContactIds = [];
  143. foreach ($contacts as $contact) {
  144. if (!empty($contact['id'])) {
  145. $existingContactIds[] = (int)$contact['id'];
  146. }
  147. }
  148. // 删除不再使用的联系人记录
  149. if (!empty($existingContactIds)) {
  150. $idsToKeep = implode(',', $existingContactIds);
  151. $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = $id AND id NOT IN ($idsToKeep)";
  152. } else {
  153. $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = $id";
  154. }
  155. mysqli_query($conn, $deleteContactsSql);
  156. // 更新或添加联系人信息
  157. foreach ($contacts as $contact) {
  158. $contact_id = !empty($contact['id']) ? (int)$contact['id'] : 0;
  159. $contact_name = textEncode($contact['contact_name'] ?? '');
  160. // 准备SQL字段和值
  161. $fields = ['contact_name'];
  162. $values = ["'" . mysqli_real_escape_string($conn, $contact_name) . "'"];
  163. $updates = ["contact_name = '" . mysqli_real_escape_string($conn, $contact_name) . "'"];
  164. // 处理所有联系方式类型
  165. $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
  166. foreach ($methodTypes as $type) {
  167. for ($i = 1; $i <= 3; $i++) {
  168. $field = $type . '_' . $i;
  169. $format_field = $field . '_format';
  170. $bu_field = $field . '_bu';
  171. $value = textEncode($contact[$field] ?? '');
  172. $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
  173. $bu_value = textEncode($contact[$bu_field] ?? $value);
  174. // 添加字段名
  175. $fields[] = $field;
  176. $fields[] = $bu_field;
  177. if ($type == 'tel' || $type == 'whatsapp') {
  178. $fields[] = $format_field;
  179. }
  180. // 添加值
  181. $values[] = "'" . mysqli_real_escape_string($conn, $value) . "'";
  182. $values[] = "'" . mysqli_real_escape_string($conn, $bu_value) . "'";
  183. if ($type == 'tel' || $type == 'whatsapp') {
  184. $values[] = "'" . mysqli_real_escape_string($conn, $format_value) . "'";
  185. }
  186. // 添加更新语句
  187. $updates[] = $field . " = '" . mysqli_real_escape_string($conn, $value) . "'";
  188. $updates[] = $bu_field . " = '" . mysqli_real_escape_string($conn, $bu_value) . "'";
  189. if ($type == 'tel' || $type == 'whatsapp') {
  190. $updates[] = $format_field . " = '" . mysqli_real_escape_string($conn, $format_value) . "'";
  191. }
  192. }
  193. }
  194. if ($contact_id > 0) {
  195. // 更新已有联系人
  196. $updateContactSql = "UPDATE customer_contact SET " .
  197. implode(", ", $updates) . ", updated_at = NOW() " .
  198. "WHERE id = $contact_id AND customer_id = $id";
  199. mysqli_query($conn, $updateContactSql);
  200. } else {
  201. // 添加新联系人
  202. $insertContactSql = "INSERT INTO customer_contact (" .
  203. implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
  204. implode(", ", $values) . ", $id, NOW(), NOW())";
  205. mysqli_query($conn, $insertContactSql);
  206. }
  207. }
  208. // 更新标签
  209. mysqli_query($conn, "DELETE FROM tagtable WHERE customerId = $id");
  210. foreach ($mytag as $tag) {
  211. if (!empty(trim($tag))) {
  212. $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" .
  213. mysqli_real_escape_string($conn, $tag) . "', " .
  214. $_SESSION['employee_id'] . ", $id)";
  215. mysqli_query($conn, $tagSql);
  216. }
  217. }
  218. $page = $_GET['Page'] ?? '';
  219. $keys = urlencode($_GET['Keys'] ?? '');
  220. header("Location: ?keys=$keys&Page=$page$urlStr");
  221. exit;
  222. }
  223. } else {
  224. // 创建新记录
  225. // 插入客户基本信息
  226. $sql = "INSERT INTO customer (
  227. cs_code, cs_company, cs_country, cs_address, cs_from,
  228. cs_belong, cs_state, cs_deal, cs_type, cs_belongclient,
  229. cs_note, allowedit, cs_chain, cs_addtime, cs_updatetime,
  230. is_silent, cs_dealdate
  231. ) VALUES (
  232. '$cs_code', '$cs_company', '$cs_country', '$cs_address', '$cs_from',
  233. '$cs_belong', '$cs_state', '$cs_deal', '$cs_type', '$cs_belongclient',
  234. '$no_content', $allowedit, '$cs_belong', NOW(), NOW(),
  235. 0, " . ($cs_deal == 3 ? "NOW()" : "NULL") . "
  236. )";
  237. mysqli_query($conn, $sql);
  238. $new_customer_id = mysqli_insert_id($conn);
  239. // 插入联系人信息
  240. if ($new_customer_id > 0) {
  241. foreach ($contacts as $contact) {
  242. $contact_name = textEncode($contact['contact_name'] ?? '');
  243. // 准备SQL字段和值
  244. $fields = ['contact_name'];
  245. $values = ["'" . mysqli_real_escape_string($conn, $contact_name) . "'"];
  246. // 处理所有联系方式类型
  247. $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
  248. foreach ($methodTypes as $type) {
  249. for ($i = 1; $i <= 3; $i++) {
  250. $field = $type . '_' . $i;
  251. $format_field = $field . '_format';
  252. $bu_field = $field . '_bu';
  253. $value = textEncode($contact[$field] ?? '');
  254. $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
  255. $bu_value = textEncode($contact[$bu_field] ?? $value);
  256. // 添加字段名
  257. $fields[] = $field;
  258. $fields[] = $bu_field;
  259. if ($type == 'tel' || $type == 'whatsapp') {
  260. $fields[] = $format_field;
  261. }
  262. // 添加值
  263. $values[] = "'" . mysqli_real_escape_string($conn, $value) . "'";
  264. $values[] = "'" . mysqli_real_escape_string($conn, $bu_value) . "'";
  265. if ($type == 'tel' || $type == 'whatsapp') {
  266. $values[] = "'" . mysqli_real_escape_string($conn, $format_value) . "'";
  267. }
  268. }
  269. }
  270. // 添加新联系人
  271. $insertContactSql = "INSERT INTO customer_contact (" .
  272. implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
  273. implode(", ", $values) . ", $new_customer_id, NOW(), NOW())";
  274. mysqli_query($conn, $insertContactSql);
  275. }
  276. // 保存标签
  277. foreach ($mytag as $tag) {
  278. if (!empty(trim($tag))) {
  279. $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" .
  280. mysqli_real_escape_string($conn, $tag) . "', " .
  281. $_SESSION['employee_id'] . ", $new_customer_id)";
  282. mysqli_query($conn, $tagSql);
  283. }
  284. }
  285. $page = $_GET['Page'] ?? '';
  286. $keys = urlencode($_GET['Keys'] ?? '');
  287. header("Location: ?keys=$keys&Page=$page$urlStr");
  288. exit;
  289. }
  290. }
  291. }
  292. // 处理编辑操作
  293. if ($act == "edit") {
  294. $id = $_GET['id'] ?? '';
  295. $isedit = false;
  296. if (!empty($id) && is_numeric($id)) {
  297. $isedit = true;
  298. }
  299. if ($isedit) {
  300. // 联合查询客户基本信息和联系人信息
  301. $sql = "SELECT c.*,
  302. cc.id as contact_id, cc.contact_name,
  303. cc.tel_1, cc.tel_1_format, cc.tel_1_bu,
  304. cc.tel_2, cc.tel_2_format, cc.tel_2_bu,
  305. cc.tel_3, cc.tel_3_format, cc.tel_3_bu,
  306. cc.email_1, cc.email_1_bu,
  307. cc.email_2, cc.email_2_bu,
  308. cc.email_3, cc.email_3_bu,
  309. cc.whatsapp_1, cc.whatsapp_1_format, cc.whatsapp_1_bu,
  310. cc.whatsapp_2, cc.whatsapp_2_format, cc.whatsapp_2_bu,
  311. cc.whatsapp_3, cc.whatsapp_3_format, cc.whatsapp_3_bu,
  312. cc.wechat_1, cc.wechat_1_bu,
  313. cc.wechat_2, cc.wechat_2_bu,
  314. cc.wechat_3, cc.wechat_3_bu,
  315. cc.linkedin_1, cc.linkedin_1_bu,
  316. cc.linkedin_2, cc.linkedin_2_bu,
  317. cc.linkedin_3, cc.linkedin_3_bu,
  318. cc.facebook_1, cc.facebook_1_bu,
  319. cc.facebook_2, cc.facebook_2_bu,
  320. cc.facebook_3, cc.facebook_3_bu,
  321. cc.alibaba_1, cc.alibaba_1_bu,
  322. cc.alibaba_2, cc.alibaba_2_bu,
  323. cc.alibaba_3, cc.alibaba_3_bu
  324. FROM customer c
  325. LEFT JOIN customer_contact cc ON c.id = cc.customer_id
  326. WHERE c.id = $id";
  327. $result = mysqli_query($conn, $sql);
  328. if ($row = mysqli_fetch_assoc($result)) {
  329. $cs_code = textDecode($row['cs_code']);
  330. $cs_company = textDecode($row['cs_company']);
  331. $cs_name = textDecode($row['contact_name']);
  332. $cs_country = $row['cs_country'];
  333. $cs_from = $row['cs_from'];
  334. $cs_tel = textDecode($row['tel_1']);
  335. $cs_telBu = textDecode($row['tel_1_bu']);
  336. $cs_email = textDecode($row['email_1']);
  337. $cs_emailBu = textDecode($row['email_1_bu']);
  338. $cs_whatsapp = textDecode($row['whatsapp_1']);
  339. $cs_whatsappBu = textDecode($row['whatsapp_1_bu']);
  340. $cs_wechat = textDecode($row['wechat_1']);
  341. $cs_wechatBu = textDecode($row['wechat_1_bu']);
  342. $cs_linkedin = textDecode($row['linkedin_1']);
  343. $cs_linkedinBu = textDecode($row['linkedin_1_bu']);
  344. $cs_facebook = textDecode($row['facebook_1']);
  345. $cs_facebookBu = textDecode($row['facebook_1_bu']);
  346. $cs_alibaba = textDecode($row['alibaba_1']);
  347. $cs_alibabaBu = textDecode($row['alibaba_1_bu']);
  348. $cs_address = textDecode($row['cs_address']);
  349. $cs_addtime = $row['cs_addtime'];
  350. $cs_updatetime = $row['cs_updatetime'];
  351. $cs_belong = $row['cs_belong'];
  352. $cs_state = $row['cs_state'];
  353. $cs_deal = $row['cs_deal'];
  354. $cs_note = htmlDecode($row['cs_note']);
  355. $allowedit = $row['allowedit'];
  356. }
  357. }
  358. }
  359. // 处理批量操作
  360. if ($act == "postchk") {
  361. if (isset($_POST['chkbox']) && isset($_POST['chkact'])) {
  362. $chkact = $_POST['chkact'];
  363. $ids = implode(',', array_map('intval', $_POST['chkbox']));
  364. switch($chkact) {
  365. case "0":
  366. case "1":
  367. $sql = "UPDATE customer SET cs_state = $chkact WHERE id IN ($ids)";
  368. break;
  369. case "-1":
  370. // 删除客户记录和联系人记录(依靠外键级联删除)
  371. $sql = "DELETE FROM customer WHERE id IN ($ids)";
  372. break;
  373. }
  374. if (isset($sql)) {
  375. mysqli_query($conn, $sql);
  376. }
  377. $keys = urlencode($_GET['Keys'] ?? '');
  378. $page = $_GET['Page'] ?? '';
  379. header("Location: ?keys=$keys&Page=$page$urlStr");
  380. exit;
  381. }
  382. }
  383. /*
  384. // 处理筛选条件
  385. $fliterQudao = $_GET['fliterQudao'] ?? '';
  386. $fliterDeal = $_GET['fliterDeal'] ?? '';
  387. $fliterTeam = $_GET['fliterTeam'] ?? '';
  388. $fliterContact = $_GET['fliterContact'] ?? '';
  389. $fliterEmployee = $_GET['fliterEmployee'] ?? '';
  390. $fliterStr = "";
  391. if (!empty($fliterQudao)) {
  392. $fliterStr .= " AND c.cs_from = " . intval($fliterQudao);
  393. $urlStr .= "&fliterQudao=" . $fliterQudao;
  394. }
  395. if (!empty($fliterDeal)) {
  396. $fliterStr .= " AND c.cs_deal = " . intval($fliterDeal);
  397. $urlStr .= "&fliterDeal=" . $fliterDeal;
  398. }
  399. if (!empty($fliterTeam)) {
  400. $fliterStr .= " AND (c.cs_belong = " . intval($fliterTeam) .
  401. " OR c.cs_belong IN (SELECT id FROM employee WHERE em_role = " . intval($fliterTeam) . "))";
  402. $urlStr .= "&fliterTeam=" . $fliterTeam;
  403. }
  404. if (!empty($fliterEmployee)) {
  405. $fliterStr .= " AND c.cs_belong = " . intval($fliterEmployee);
  406. $urlStr .= "&fliterEmployee=" . $fliterEmployee;
  407. }
  408. if (!empty($fliterContact)) {
  409. switch ($fliterContact) {
  410. case "1": $fliterStr .= " AND (cc.tel_1 != '' OR cc.tel_2 != '' OR cc.tel_3 != '')"; break;
  411. case "2": $fliterStr .= " AND (cc.wechat_1 != '' OR cc.wechat_2 != '' OR cc.wechat_3 != '')"; break;
  412. case "3": $fliterStr .= " AND (cc.whatsapp_1 != '' OR cc.whatsapp_2 != '' OR cc.whatsapp_3 != '')"; break;
  413. case "4": $fliterStr .= " AND (cc.email_1 != '' OR cc.email_2 != '' OR cc.email_3 != '')"; break;
  414. case "5": $fliterStr .= " AND (cc.linkedin_1 != '' OR cc.linkedin_2 != '' OR cc.linkedin_3 != '')"; break;
  415. case "6": $fliterStr .= " AND (cc.facebook_1 != '' OR cc.facebook_2 != '' OR cc.facebook_3 != '')"; break;
  416. case "7": $fliterStr .= " AND (cc.alibaba_1 != '' OR cc.alibaba_2 != '' OR cc.alibaba_3 != '')"; break;
  417. }
  418. $urlStr .= "&fliterContact=" . $fliterContact;
  419. }
  420. // 搜索和排序
  421. $keys = $_GET['Keys'] ?? '';
  422. $keyscode = textEncode($keys);
  423. $page = $_GET['Page'] ?? 1;
  424. $ord = $_GET['Ord'] ?? '';
  425. $ordStr = !empty($ord) ? "$ord," : "";
  426. // 构建查询SQL - 修改为联合查询
  427. $sqlStr = "SELECT c.id, c.cs_code, c.cs_company, c.cs_country, c.cs_address, c.cs_from,
  428. c.cs_deal, c.cs_addtime, c.cs_updatetime, c.cs_belong, c.cs_note, c.cs_claimFrom,
  429. c.cs_chain, c.cs_dealdate, c.cs_type, c.cs_belongclient, c.allowedit,c.colortag,
  430. cc.id as contact_id, cc.contact_name,
  431. cc.tel_1, cc.tel_1_format, cc.tel_1_bu,
  432. cc.tel_2, cc.tel_2_format, cc.tel_2_bu,
  433. cc.tel_3, cc.tel_3_format, cc.tel_3_bu,
  434. cc.email_1, cc.email_1_bu,
  435. cc.email_2, cc.email_2_bu,
  436. cc.email_3, cc.email_3_bu,
  437. cc.whatsapp_1, cc.whatsapp_1_format, cc.whatsapp_1_bu,
  438. cc.whatsapp_2, cc.whatsapp_2_format, cc.whatsapp_2_bu,
  439. cc.whatsapp_3, cc.whatsapp_3_format, cc.whatsapp_3_bu,
  440. cc.wechat_1, cc.wechat_1_bu,
  441. cc.wechat_2, cc.wechat_2_bu,
  442. cc.wechat_3, cc.wechat_3_bu,
  443. cc.linkedin_1, cc.linkedin_1_bu,
  444. cc.linkedin_2, cc.linkedin_2_bu,
  445. cc.linkedin_3, cc.linkedin_3_bu,
  446. cc.facebook_1, cc.facebook_1_bu,
  447. cc.facebook_2, cc.facebook_2_bu,
  448. cc.facebook_3, cc.facebook_3_bu,
  449. cc.alibaba_1, cc.alibaba_1_bu,
  450. cc.alibaba_2, cc.alibaba_2_bu,
  451. cc.alibaba_3, cc.alibaba_3_bu
  452. FROM customer c
  453. LEFT JOIN customer_contact cc ON c.id = cc.customer_id
  454. WHERE 1=1";
  455. if (!empty($keyscode)) {
  456. $sqlStr .= " AND (c.cs_code LIKE '%$keyscode%'
  457. OR cc.contact_name LIKE '%$keyscode%'
  458. OR cc.tel_1 LIKE '%$keyscode%'
  459. OR cc.tel_2 LIKE '%$keyscode%'
  460. OR cc.tel_3 LIKE '%$keyscode%'
  461. OR cc.email_1 LIKE '%$keyscode%'
  462. OR cc.email_2 LIKE '%$keyscode%'
  463. OR cc.email_3 LIKE '%$keyscode%'
  464. OR cc.wechat_1 LIKE '%$keyscode%'
  465. OR cc.wechat_2 LIKE '%$keyscode%'
  466. OR cc.wechat_3 LIKE '%$keyscode%'
  467. OR cc.whatsapp_1_format LIKE '%$keyscode%'
  468. OR cc.whatsapp_2_format LIKE '%$keyscode%'
  469. OR cc.whatsapp_3_format LIKE '%$keyscode%'
  470. OR cc.linkedin_1 LIKE '%$keyscode%'
  471. OR cc.linkedin_2 LIKE '%$keyscode%'
  472. OR cc.linkedin_3 LIKE '%$keyscode%'
  473. OR cc.facebook_1 LIKE '%$keyscode%'
  474. OR cc.facebook_2 LIKE '%$keyscode%'
  475. OR cc.facebook_3 LIKE '%$keyscode%'
  476. OR cc.alibaba_1 LIKE '%$keyscode%'
  477. OR cc.alibaba_2 LIKE '%$keyscode%'
  478. OR cc.alibaba_3 LIKE '%$keyscode%'
  479. OR c.id IN (SELECT customerId FROM tagtable WHERE tagName LIKE '%$keyscode%'))";
  480. }
  481. $sqlStr .= " $fliterStr ORDER BY {$ordStr}c.cs_updatetime DESC";
  482. */
  483. ?>
  484. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  485. <html xmlns="http://www.w3.org/1999/xhtml">
  486. <head>
  487. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  488. <title>客户列表</title>
  489. <link rel="stylesheet" href="css/common.css" type="text/css" />
  490. <link rel="stylesheet" href="css/alert.css" type="text/css" />
  491. <script src="js/jquery-1.7.2.min.js"></script>
  492. <script src="js/js.js"></script>
  493. <style>
  494. body {
  495. margin: 0;
  496. padding: 20px;
  497. background: #fff;
  498. }
  499. #man_zone {
  500. margin-left: 0;
  501. }
  502. </style>
  503. </head>
  504. <body>
  505. <div id="man_zone">
  506. <?php
  507. $keys = $_GET['Keys'] ?? '';
  508. $keys = str_replace([" ", "+"], "", $keys);
  509. $keyscode = textEncode($keys);
  510. $page = $_GET['Page'] ?? 1;
  511. $filters = [
  512. 'Country' => $_GET['fliterCountry'] ?? '',
  513. 'Qudao' => $_GET['fliterQudao'] ?? '',
  514. 'Deal' => $_GET['fliterDeal'] ?? '',
  515. 'Business' => $_GET['fliterBusiness'] ?? '',
  516. 'Contact' => $_GET['fliterContact'] ?? ''
  517. ];
  518. $filterStr = "";
  519. $urlStr = "";
  520. // Build filter conditions
  521. if (!empty($filters['Country'])) {
  522. $filterStr .= " AND c.cs_country=" . (int)$filters['Country'];
  523. $urlStr .= "&fliterCountry=" . $filters['Country'];
  524. }
  525. if (!empty($filters['Qudao'])) {
  526. $filterStr .= " AND c.cs_from=" . (int)$filters['Qudao'];
  527. $urlStr .= "&fliterQudao=" . $filters['Qudao'];
  528. }
  529. if (!empty($filters['Deal'])) {
  530. $filterStr .= " AND c.cs_deal=" . (int)$filters['Deal'];
  531. $urlStr .= "&fliterDeal=" . $filters['Deal'];
  532. }
  533. if (!empty($filters['Business'])) {
  534. $filterStr .= " AND c.cs_type=" . (int)$filters['Business'];
  535. $urlStr .= "&fliterBusiness=" . $filters['Business'];
  536. }
  537. if (!empty($filters['Contact'])) {
  538. switch ($filters['Contact']) {
  539. case "1": $filterStr .= " AND (cc.tel_1 != '' OR cc.tel_2 != '' OR cc.tel_3 != '')"; break;
  540. case "2": $filterStr .= " AND (cc.wechat_1 != '' OR cc.wechat_2 != '' OR cc.wechat_3 != '')"; break;
  541. case "3": $filterStr .= " AND (cc.whatsapp_1 != '' OR cc.whatsapp_2 != '' OR cc.whatsapp_3 != '')"; break;
  542. case "4": $filterStr .= " AND (cc.email_1 != '' OR cc.email_2 != '' OR cc.email_3 != '')"; break;
  543. case "5": $filterStr .= " AND (cc.linkedin_1 != '' OR cc.linkedin_2 != '' OR cc.linkedin_3 != '')"; break;
  544. case "6": $filterStr .= " AND (cc.facebook_1 != '' OR cc.facebook_2 != '' OR cc.facebook_3 != '')"; break;
  545. case "7": $filterStr .= " AND (cc.alibaba_1 != '' OR cc.alibaba_2 != '' OR cc.alibaba_3 != '')"; break;
  546. }
  547. $urlStr .= "&fliterContact=" . $filters['Contact'];
  548. }
  549. $keys = urlencode($keys);
  550. $hrefstr = "?keys=" . $keys;
  551. ?>
  552. <form id="form1" method="post" action="?act=postchk&Keys=<?= $keys ?>&Page=<?= $page ?>" onSubmit="return false">
  553. <div class="fastSelect clear">
  554. <H1>搜索条件</H1>
  555. <div class="selectItem">
  556. <label>区域</label>
  557. <select name="fliterCountry" class="filterSearch">
  558. <option value="">请选择国家地区</option>
  559. <?php
  560. $result = $conn->query("SELECT id, countryName FROM country ORDER BY CONVERT(countryName USING gbk) COLLATE gbk_chinese_ci ASC");
  561. while ($row = $result->fetch_assoc()) {
  562. $selected = ($filters['Country'] == $row['id']) ? ' selected' : '';
  563. echo "<option value=\"{$row['id']}\"$selected>{$row['countryName']}</option>";
  564. }
  565. ?>
  566. </select>
  567. </div>
  568. <div class="selectItem">
  569. <label>来源渠道</label>
  570. <select name="fliterQudao" class="filterSearch">
  571. <option value="">请选择渠道</option>
  572. <?php
  573. $result = $conn->query("SELECT id, ch_name FROM qudao");
  574. while ($row = $result->fetch_assoc()) {
  575. $selected = ($filters['Qudao'] == $row['id']) ? ' selected' : '';
  576. echo "<option value=\"{$row['id']}\"$selected>{$row['ch_name']}</option>";
  577. }
  578. ?>
  579. </select>
  580. </div>
  581. <div class="selectItem">
  582. <label>跟进阶段</label>
  583. <select name="fliterDeal" class="filterSearch">
  584. <option value="">请选择</option>
  585. <option value="1"<?= ($filters['Deal'] == "1") ? ' selected' : '' ?>>背景调查</option>
  586. <option value="2"<?= ($filters['Deal'] == "2") ? ' selected' : '' ?>>明确需求</option>
  587. <option value="3"<?= ($filters['Deal'] == "3") ? ' selected' : '' ?>>已成交</option>
  588. </select>
  589. </div>
  590. <div class="selectItem">
  591. <label>客户类型</label>
  592. <select name="fliterBusiness" class="filterSearch">
  593. <option value="">请选择</option>
  594. <?php
  595. $result = $conn->query("SELECT id, businessType FROM clienttype");
  596. while ($row = $result->fetch_assoc()) {
  597. $selected = ($filters['Business'] == $row['id']) ? ' selected' : '';
  598. echo "<option value=\"{$row['id']}\"$selected>{$row['businessType']}</option>";
  599. }
  600. ?>
  601. </select>
  602. </div>
  603. <div class="selectItem">
  604. <label>联系方式</label>
  605. <select name="fliterContact" class="filterSearch">
  606. <option value="">请选择</option>
  607. <option value="1"<?= ($filters['Contact'] == "1") ? ' selected' : '' ?>>电话</option>
  608. <option value="2"<?= ($filters['Contact'] == "2") ? ' selected' : '' ?>>微信</option>
  609. <option value="3"<?= ($filters['Contact'] == "3") ? ' selected' : '' ?>>WhatsApp</option>
  610. <option value="4"<?= ($filters['Contact'] == "4") ? ' selected' : '' ?>>邮箱</option>
  611. <option value="5"<?= ($filters['Contact'] == "5") ? ' selected' : '' ?>>领英</option>
  612. <option value="6"<?= ($filters['Contact'] == "6") ? ' selected' : '' ?>>Facebook</option>
  613. <option value="7"<?= ($filters['Contact'] == "7") ? ' selected' : '' ?>>阿里巴巴</option>
  614. </select>
  615. </div>
  616. <div class="inputSearch">
  617. <input type="text" id="keys" class="inputTxt" placeholder="请输入搜索关键词"
  618. value="<?= empty($keyscode) ? '' : $keyscode ?>"
  619. onKeyDown="if(event.keyCode==13){location.href='?Keys='+encodeURIComponent(document.getElementById('keys').value)}" />
  620. <input type="button" id="searchgo" class="searchgo" value="go"
  621. onClick="location.href='?Keys='+encodeURIComponent(document.getElementById('keys').value)" />
  622. </div>
  623. </div>
  624. <div class="table2 em<?= $_SESSION['employee_id'] ?>">
  625. <div class="theader">
  626. <div class="col1"><input type="checkbox" name="chkall" id="chkall" onClick="chkboxall(this,'chkbox')" /></div>
  627. <div class="col2">序号</div>
  628. <div class="col3">客户编号</div>
  629. <div class="col4">渠道来源</div>
  630. <div class="col5">区域</div>
  631. <div class="col6">客户类型</div>
  632. <div class="col6">跟进阶段</div>
  633. <div class="colmark">颜色标记</div>
  634. <div class="col6">录入时间</div>
  635. <div class="col9">操作</div>
  636. </div>
  637. <?php
  638. $sql = "SELECT c.id, c.cs_code, c.cs_company, c.cs_country, c.cs_address, c.cs_from,
  639. c.cs_deal, c.cs_addtime, c.cs_updatetime, c.cs_belong, c.cs_note, c.cs_claimFrom,
  640. c.cs_chain, c.cs_dealdate, c.cs_type, c.cs_belongclient, c.allowedit,c.colortag
  641. FROM customer c
  642. WHERE c.is_silent=0 AND c.cs_deal>0 AND c.cs_belong=" . (int)$_SESSION['employee_id'];
  643. $searchPattern = mysqli_real_escape_string($conn, $keyscode);
  644. if(!empty($searchPattern)) {
  645. $sql .= " AND (c.cs_code LIKE '%$searchPattern%'
  646. OR c.id IN (SELECT customer_id FROM customer_contact WHERE
  647. contact_name LIKE '%$searchPattern%' OR
  648. tel_1 LIKE '%$searchPattern%' OR
  649. tel_2 LIKE '%$searchPattern%' OR
  650. tel_3 LIKE '%$searchPattern%' OR
  651. email_1 LIKE '%$searchPattern%' OR
  652. email_2 LIKE '%$searchPattern%' OR
  653. email_3 LIKE '%$searchPattern%' OR
  654. wechat_1 LIKE '%$searchPattern%' OR
  655. wechat_2 LIKE '%$searchPattern%' OR
  656. wechat_3 LIKE '%$searchPattern%' OR
  657. whatsapp_1_format LIKE '%$searchPattern%' OR
  658. whatsapp_2_format LIKE '%$searchPattern%' OR
  659. whatsapp_3_format LIKE '%$searchPattern%' OR
  660. linkedin_1 LIKE '%$searchPattern%' OR
  661. linkedin_2 LIKE '%$searchPattern%' OR
  662. linkedin_3 LIKE '%$searchPattern%' OR
  663. facebook_1 LIKE '%$searchPattern%' OR
  664. facebook_2 LIKE '%$searchPattern%' OR
  665. facebook_3 LIKE '%$searchPattern%' OR
  666. alibaba_1 LIKE '%$searchPattern%' OR
  667. alibaba_2 LIKE '%$searchPattern%' OR
  668. alibaba_3 LIKE '%$searchPattern%')
  669. OR c.id IN (SELECT customerId FROM tagtable WHERE tagName LIKE '%$searchPattern%'))";
  670. }
  671. $sql .= " $filterStr ORDER BY c.colortag DESC, c.id DESC";
  672. $result = mysqli_query($conn, $sql);
  673. // Pagination logic
  674. $perPage = 20;
  675. $totalRecords = mysqli_num_rows($result);
  676. $totalPages = max(1, ceil($totalRecords / $perPage));
  677. $page = max(1, min((int)$page, $totalPages));
  678. $offset = max(0, ($page - 1) * $perPage);
  679. // Add pagination to query
  680. $sql .= " LIMIT $offset, $perPage";
  681. $result = mysqli_query($conn, $sql);
  682. if (mysqli_num_rows($result) > 0) {
  683. $tempNum = $offset;
  684. while ($row = mysqli_fetch_assoc($result)) {
  685. $tempNum++;
  686. ?>
  687. <div class="tline color<?= $row['colortag'] ?>">
  688. <div class="col1" align="center"><input type="checkbox" name="chkbox[]" value="<?= $row['id'] ?>" /></div>
  689. <div class="col2"><?= $tempNum ?></div>
  690. <div class="col3 slidepanel"><?= $row['cs_code'] ?>
  691. <?php if ($row['cs_claimFrom'] > 0): ?>
  692. <img src="../images/yijiao.png" class="handover" title="来自认领">
  693. <?php endif; ?>
  694. </div>
  695. <div class="col4">
  696. <?php
  697. $qudao = $conn->query("SELECT ch_name FROM qudao WHERE id=" . (int)$row['cs_from']);
  698. echo $qudao->num_rows > 0 ? $qudao->fetch_assoc()['ch_name'] : '未填写';
  699. ?>
  700. </div>
  701. <div class="col5">
  702. <?php
  703. $country = $conn->query("SELECT countryName FROM country WHERE id=" . (int)$row['cs_country']);
  704. echo $country->num_rows > 0 ? $country->fetch_assoc()['countryName'] : '未填写';
  705. ?>
  706. </div>
  707. <div class="col6">
  708. <?php
  709. $clientType = $conn->query("SELECT businessType FROM clienttype WHERE id=" . (int)$row['cs_type']);
  710. echo $clientType->num_rows > 0 ? $clientType->fetch_assoc()['businessType'] : '未填写';
  711. ?>
  712. </div>
  713. <div class="col6">
  714. <?php
  715. switch ($row['cs_deal']) {
  716. case 3: echo '<span style="color:red;">成交</span>'; break;
  717. case 2: echo '明确需求'; break;
  718. case 1: echo '背景调查'; break;
  719. default: echo '无响应';
  720. }
  721. ?>
  722. </div>
  723. <div class="colmark colormark">
  724. <ul class="colorlist" data-id="<?= $row['id'] ?>">
  725. <li value="1" class="color1"></li>
  726. <li value="2" class="color2"></li>
  727. <li value="3" class="color3"></li>
  728. <li value="4" class="color4"></li>
  729. <li value="0" class="color0"></li>
  730. </ul>
  731. </div>
  732. <div class="col6"><?= $row['cs_addtime'] ?></div>
  733. <div class="col9">
  734. <a href="customerEdit.php?Keys=<?= $keys ?>&fliterDeal=<?= $filters['Deal'] ?>&fliterBusiness=<?= $filters['Business'] ?>&Page=<?= $page ?>&act=edit&id=<?= $row['id'] ?>" class="ico_edit ico">修改</a>
  735. <a href="order_add.php?customer_id=<?= $row['id'] ?>" class="ico_add ico">添加订单</a>
  736. </div>
  737. </div>
  738. <div class="notepanel clear">
  739. <div class="noteItem">联系方式</div>
  740. <div class="lx">
  741. <?php
  742. // Fetch all contacts for this customer
  743. $contact_sql = "SELECT * FROM customer_contact WHERE customer_id = " . $row['id'];
  744. $contact_result = mysqli_query($conn, $contact_sql);
  745. while ($contact = mysqli_fetch_assoc($contact_result)) {
  746. ?>
  747. <div class="contact-block">
  748. <?php if(!empty($contact['contact_name'])): ?>
  749. <div class="contact-name"><?= $contact['contact_name'] ?></div>
  750. <?php endif; ?>
  751. <div class="tel">
  752. <?php if(!empty($contact['tel_1'])): ?>
  753. <div><?= $contact['tel_1'] ?></div>
  754. <?php endif; ?>
  755. <?php if(!empty($contact['tel_2'])): ?>
  756. <div><?= $contact['tel_2'] ?></div>
  757. <?php endif; ?>
  758. <?php if(!empty($contact['tel_3'])): ?>
  759. <div><?= $contact['tel_3'] ?></div>
  760. <?php endif; ?>
  761. </div>
  762. <div class="mail">
  763. <?php if(!empty($contact['email_1'])): ?>
  764. <div><a href="mailto:<?= $contact['email_1'] ?>"><?= $contact['email_1'] ?></a></div>
  765. <?php endif; ?>
  766. <?php if(!empty($contact['email_2'])): ?>
  767. <div><a href="mailto:<?= $contact['email_2'] ?>"><?= $contact['email_2'] ?></a></div>
  768. <?php endif; ?>
  769. <?php if(!empty($contact['email_3'])): ?>
  770. <div><a href="mailto:<?= $contact['email_3'] ?>"><?= $contact['email_3'] ?></a></div>
  771. <?php endif; ?>
  772. </div>
  773. <div class="whatsapp">
  774. <?php if(!empty($contact['whatsapp_1'])): ?>
  775. <div><?= $contact['whatsapp_1'] ?></div>
  776. <?php endif; ?>
  777. <?php if(!empty($contact['whatsapp_2'])): ?>
  778. <div><?= $contact['whatsapp_2'] ?></div>
  779. <?php endif; ?>
  780. <?php if(!empty($contact['whatsapp_3'])): ?>
  781. <div><?= $contact['whatsapp_3'] ?></div>
  782. <?php endif; ?>
  783. </div>
  784. <div class="wechat">
  785. <?php if(!empty($contact['wechat_1'])): ?>
  786. <div><?= $contact['wechat_1'] ?></div>
  787. <?php endif; ?>
  788. <?php if(!empty($contact['wechat_2'])): ?>
  789. <div><?= $contact['wechat_2'] ?></div>
  790. <?php endif; ?>
  791. <?php if(!empty($contact['wechat_3'])): ?>
  792. <div><?= $contact['wechat_3'] ?></div>
  793. <?php endif; ?>
  794. </div>
  795. <div class="linkedin">
  796. <?php if(!empty($contact['linkedin_1'])): ?>
  797. <div><?= $contact['linkedin_1'] ?></div>
  798. <?php endif; ?>
  799. <?php if(!empty($contact['linkedin_2'])): ?>
  800. <div><?= $contact['linkedin_2'] ?></div>
  801. <?php endif; ?>
  802. <?php if(!empty($contact['linkedin_3'])): ?>
  803. <div><?= $contact['linkedin_3'] ?></div>
  804. <?php endif; ?>
  805. </div>
  806. <div class="facebook">
  807. <?php if(!empty($contact['facebook_1'])): ?>
  808. <div><?= $contact['facebook_1'] ?></div>
  809. <?php endif; ?>
  810. <?php if(!empty($contact['facebook_2'])): ?>
  811. <div><?= $contact['facebook_2'] ?></div>
  812. <?php endif; ?>
  813. <?php if(!empty($contact['facebook_3'])): ?>
  814. <div><?= $contact['facebook_3'] ?></div>
  815. <?php endif; ?>
  816. </div>
  817. <div class="alibaba">
  818. <?php if(!empty($contact['alibaba_1'])): ?>
  819. <div><?= $contact['alibaba_1'] ?></div>
  820. <?php endif; ?>
  821. <?php if(!empty($contact['alibaba_2'])): ?>
  822. <div><?= $contact['alibaba_2'] ?></div>
  823. <?php endif; ?>
  824. <?php if(!empty($contact['alibaba_3'])): ?>
  825. <div><?= $contact['alibaba_3'] ?></div>
  826. <?php endif; ?>
  827. </div>
  828. </div>
  829. <?php } ?>
  830. </div>
  831. <div class="noteItem2">备注</div>
  832. <div class="notecontent"><?= htmlUnCode($row['cs_note']) ?></div>
  833. </div>
  834. <?php
  835. }
  836. } else {
  837. if (empty($keys)) {
  838. echo '<tr><div align="center" colspan="9">Sorry,当前暂无信息</div></tr>';
  839. } else {
  840. echo '<tr><div align="center" colspan="9"><a href="?">Sorry,没有找到"' .
  841. htmlspecialcharsFix($keyscode) . '"相关的信息,点击返回</a></div></tr>';
  842. }
  843. }
  844. ?>
  845. <div colspan="9">
  846. <div class="showpagebox">
  847. <?php
  848. if ($totalPages > 1) {
  849. $pageName = "?Keys=$keys$urlStr&";
  850. $pageLen = 3;
  851. if ($page > 1) {
  852. echo "<a href=\"{$pageName}Page=1\">首页</a>";
  853. echo "<a href=\"{$pageName}Page=" . ($page - 1) . "\">上一页</a>";
  854. }
  855. if ($pageLen * 2 + 1 >= $totalPages) {
  856. $startPage = 1;
  857. $endPage = $totalPages;
  858. } else {
  859. if ($page <= $pageLen + 1) {
  860. $startPage = 1;
  861. $endPage = $pageLen * 2 + 1;
  862. } else {
  863. $startPage = $page - $pageLen;
  864. $endPage = $page + $pageLen;
  865. }
  866. if ($page + $pageLen > $totalPages) {
  867. $startPage = $totalPages - $pageLen * 2;
  868. $endPage = $totalPages;
  869. }
  870. }
  871. for ($i = $startPage; $i <= $endPage; $i++) {
  872. if ($i == $page) {
  873. echo "<a class=\"current\">$i</a>";
  874. } else {
  875. echo "<a href=\"{$pageName}Page=$i\">$i</a>";
  876. }
  877. }
  878. if ($page < $totalPages) {
  879. if ($totalPages - $page > $pageLen) {
  880. echo "<a href=\"{$pageName}Page=$totalPages\">...$totalPages</a>";
  881. }
  882. echo "<a href=\"{$pageName}Page=" . ($page + 1) . "\">下一页</a>";
  883. echo "<a href=\"{$pageName}Page=$totalPages\">尾页</a>";
  884. }
  885. }
  886. ?>
  887. </div>
  888. <?php require_once 'postchkbox.php'; ?>
  889. </div>
  890. </div>
  891. </form>
  892. </div>
  893. </body>
  894. </html>