Browse Source

fleat:update customer 2 table

igb 3 weeks ago
parent
commit
08543bab34
6 changed files with 344 additions and 165 deletions
  1. 8 1
      customerEdit.php
  2. 127 62
      customerSave.php
  3. 7 3
      customerView.php
  4. 158 61
      customers.php
  5. 21 18
      customersFollow.php
  6. 23 20
      customersNew.php

+ 8 - 1
customerEdit.php

@@ -10,7 +10,14 @@ $hrefstr = "?keys=$keys&Page=$page";
 
 // Validate and fetch customer data
 if (!empty($id) && is_numeric($id)) {
-    $stmt = $conn->prepare("SELECT * FROM customer WHERE cs_belong = ? AND id = ?");
+    $sql = "SELECT c.*, cc.contact_name as cs_name, cc.tel as cs_tel, cc.email as cs_email, 
+            cc.whatsapp as cs_whatsapp, cc.wechat as cs_wechat, cc.linkedin as cs_linkedin, 
+            cc.facebook as cs_facebook, cc.alibaba as cs_alibaba 
+            FROM customer c 
+            LEFT JOIN customer_contact cc ON c.id = cc.customer_id
+            WHERE c.cs_belong = ? AND c.id = ?";
+    
+    $stmt = $conn->prepare($sql);
     $stmt->bind_param("ii", $_SESSION['employee_id'], $id);
     $stmt->execute();
     $result = $stmt->get_result();

+ 127 - 62
customerSave.php

@@ -127,59 +127,62 @@ if (empty($cs_code)) {
 }
 
 // Check for duplicate customer information
-$checkStr = "SELECT * FROM customer WHERE cs_belong != " . $_SESSION['employee_id'] . " AND (id = 0 ";
+$checkStr = "SELECT c.*, cc.tel, cc.tel_format, cc.email, cc.whatsapp, cc.whatsapp_format, cc.wechat, cc.linkedin, cc.facebook, cc.alibaba 
+             FROM customer c 
+             LEFT JOIN customer_contact cc ON c.id = cc.customer_id
+             WHERE c.cs_belong != " . $_SESSION['employee_id'] . " AND (c.id = 0 ";
 
 $Dupli = "";
 
 if (!empty($cs_tel)) {
-    $checkStr .= " OR cs_telformat LIKE '%" . substr($cs_telformat, 3, 9) . "%'" . 
-                 " OR cs_wechat LIKE '%" . substr($cs_telformat, 3, 9) . "%'" . 
-                 " OR cs_whatsappformat LIKE '%" . $cs_telformat . "%'";
+    $checkStr .= " OR cc.tel_format LIKE '%" . substr($cs_telformat, 3, 9) . "%'" . 
+                 " OR cc.wechat LIKE '%" . substr($cs_telformat, 3, 9) . "%'" . 
+                 " OR cc.whatsapp_format LIKE '%" . $cs_telformat . "%'";
     $Dupli .= "电话:" . $cs_tel;
 }
 
 if (!empty($cs_email)) {
-    $checkStr .= " OR cs_email = '" . $conn->real_escape_string($cs_email) . "'";
+    $checkStr .= " OR cc.email = '" . $conn->real_escape_string($cs_email) . "'";
     $Dupli .= "邮箱:" . $cs_email;
 }
 
 if (!empty($cs_whatsapp)) {
-    $checkStr .= " OR cs_whatsappformat LIKE '%" . substr($cs_whatsappformat, 3, 9) . "%'" . 
-                 " OR cs_telformat LIKE '%" . substr($cs_whatsappformat, 3, 9) . "%'";
+    $checkStr .= " OR cc.whatsapp_format LIKE '%" . substr($cs_whatsappformat, 3, 9) . "%'" . 
+                 " OR cc.tel_format LIKE '%" . substr($cs_whatsappformat, 3, 9) . "%'";
     $Dupli .= "WhatsApp:" . $cs_whatsapp;
 }
 
 if (!empty($cs_wechat)) {
     if (strlen($cs_wechat) < 10) {
-        $checkStr .= " OR cs_wechat LIKE '%" . $conn->real_escape_string($cs_wechat) . "%'" . 
-                    " OR cs_telformat LIKE '%" . $conn->real_escape_string($cs_wechat) . "%'";
+        $checkStr .= " OR cc.wechat LIKE '%" . $conn->real_escape_string($cs_wechat) . "%'" . 
+                    " OR cc.tel_format LIKE '%" . $conn->real_escape_string($cs_wechat) . "%'";
     } else {
-        $checkStr .= " OR cs_wechat LIKE '%" . substr($cs_wechat, 2, 12) . "%'" . 
-                    " OR cs_telformat LIKE '%" . substr($cs_wechat, 2, 12) . "%'";
+        $checkStr .= " OR cc.wechat LIKE '%" . substr($cs_wechat, 2, 12) . "%'" . 
+                    " OR cc.tel_format LIKE '%" . substr($cs_wechat, 2, 12) . "%'";
     }
     $Dupli .= "微信:" . $cs_wechat;
 }
 
 if (!empty($cs_linkedin)) {
-    $checkStr .= " OR cs_linkedin LIKE '%" . $conn->real_escape_string($cs_linkedin) . "%'";
+    $checkStr .= " OR cc.linkedin LIKE '%" . $conn->real_escape_string($cs_linkedin) . "%'";
     $Dupli .= "Linked:" . $cs_linkedin;
 }
 
 if (!empty($cs_facebook)) {
-    $checkStr .= " OR cs_facebook LIKE '%" . $conn->real_escape_string($cs_facebook) . "%'";
+    $checkStr .= " OR cc.facebook LIKE '%" . $conn->real_escape_string($cs_facebook) . "%'";
     $Dupli .= "Facebook:" . $cs_facebook;
 }
 
 if (!empty($cs_alibaba)) {
     if (strlen($cs_alibaba) < 10) {
-        $checkStr .= " OR cs_alibaba LIKE '" . $conn->real_escape_string($cs_alibaba) . "'";
+        $checkStr .= " OR cc.alibaba LIKE '" . $conn->real_escape_string($cs_alibaba) . "'";
     } else {
-        $checkStr .= " OR cs_alibaba LIKE '%" . substr($cs_alibaba, 3, 12) . "%'";
+        $checkStr .= " OR cc.alibaba LIKE '%" . substr($cs_alibaba, 3, 12) . "%'";
     }
     $Dupli .= "阿里旺旺:" . $cs_alibaba;
 }
 
-$checkStr .= " ) ORDER BY id ASC";
+$checkStr .= " ) ORDER BY c.id ASC";
 
 if ($allowedit != 1) {
     $result = $conn->query($checkStr);
@@ -218,28 +221,17 @@ if ($act == "editSave" || $allowedit == 1) {
     $hrefstr = "/customers.php?Keys=" . $keys . "&fliterBusiness=" . $fliterBusiness . 
                "&fliterDeal=" . $fliterDeal . "&Page=" . $page;
     
-    // 直接使用SQL拼接,与ASP版本保持一致
+    // 更新客户基本信息
     $updateSql = "UPDATE customer SET 
         cs_code='" . $conn->real_escape_string($cs_code) . "',
         cs_company='" . $conn->real_escape_string($cs_company) . "',
-        cs_name='" . $conn->real_escape_string($cs_name) . "',
         cs_country=" . $cs_country . ",
         cs_from=" . $cs_from . ",
-        cs_tel='" . $conn->real_escape_string($cs_tel) . "',
-        cs_wechat='" . $conn->real_escape_string($cs_wechat) . "',
-        cs_whatsapp='" . $conn->real_escape_string($cs_whatsapp) . "',
-        cs_email='" . $conn->real_escape_string($cs_email) . "',
-        cs_linkedin='" . $conn->real_escape_string($cs_linkedin) . "',
-        cs_facebook='" . $conn->real_escape_string($cs_facebook) . "',
         cs_address='" . $conn->real_escape_string($cs_address) . "',
-        cs_type=" . $cs_type . ",
-        cs_alibaba='" . $conn->real_escape_string($cs_alibaba) . "',
         cs_updatetime='" . $cs_updatetime . "',
         cs_belong=" . $cs_belong . ",
         cs_belongclient=" . $cs_belongClient . ",
         cs_state=" . $cs_state . ",
-        cs_telformat='" . $conn->real_escape_string($cs_telformat) . "',
-        cs_whatsappformat='" . $conn->real_escape_string($cs_whatsappformat) . "',
         cs_deal=" . $cs_deal . ",
         cs_note='" . $conn->real_escape_string($cs_note) . "'";
 
@@ -251,6 +243,66 @@ if ($act == "editSave" || $allowedit == 1) {
     $updateSql .= " WHERE id=" . intval($id);
 
     $conn->query($updateSql);
+    
+    // 检查是否已有联系人记录
+    $contact_sql = "SELECT id FROM customer_contact WHERE customer_id = " . intval($id);
+    $contact_result = mysqli_query($conn, $contact_sql);
+    
+    if ($contact_row = mysqli_fetch_assoc($contact_result)) {
+        // 更新联系人信息
+        $contact_id = $contact_row['id'];
+        $contact_sql = "UPDATE customer_contact SET 
+            contact_name='" . $conn->real_escape_string($cs_name) . "',
+            tel='" . $conn->real_escape_string($cs_tel) . "',
+            tel_format='" . $conn->real_escape_string($cs_telformat) . "',
+            tel_bu='" . $conn->real_escape_string($cs_tel) . "',
+            email='" . $conn->real_escape_string($cs_email) . "',
+            email_bu='" . $conn->real_escape_string($cs_email) . "',
+            whatsapp='" . $conn->real_escape_string($cs_whatsapp) . "',
+            whatsapp_format='" . $conn->real_escape_string($cs_whatsappformat) . "',
+            whatsapp_bu='" . $conn->real_escape_string($cs_whatsapp) . "',
+            wechat='" . $conn->real_escape_string($cs_wechat) . "',
+            wechat_bu='" . $conn->real_escape_string($cs_wechat) . "',
+            linkedin='" . $conn->real_escape_string($cs_linkedin) . "',
+            linkedin_bu='" . $conn->real_escape_string($cs_linkedin) . "',
+            facebook='" . $conn->real_escape_string($cs_facebook) . "',
+            facebook_bu='" . $conn->real_escape_string($cs_facebook) . "',
+            alibaba='" . $conn->real_escape_string($cs_alibaba) . "',
+            alibaba_bu='" . $conn->real_escape_string($cs_alibaba) . "',
+            updated_at='" . $cs_updatetime . "'
+            WHERE id=" . $contact_id;
+        $conn->query($contact_sql);
+    } else {
+        // 插入新的联系人记录
+        $contact_sql = "INSERT INTO customer_contact (
+            customer_id, contact_name, tel, tel_format, tel_bu, 
+            email, email_bu, whatsapp, whatsapp_format, whatsapp_bu, 
+            wechat, wechat_bu, linkedin, linkedin_bu, facebook, 
+            facebook_bu, alibaba, alibaba_bu, created_at, updated_at
+        ) VALUES (
+            " . intval($id) . ",
+            '" . $conn->real_escape_string($cs_name) . "',
+            '" . $conn->real_escape_string($cs_tel) . "',
+            '" . $conn->real_escape_string($cs_telformat) . "',
+            '" . $conn->real_escape_string($cs_tel) . "',
+            '" . $conn->real_escape_string($cs_email) . "',
+            '" . $conn->real_escape_string($cs_email) . "',
+            '" . $conn->real_escape_string($cs_whatsapp) . "',
+            '" . $conn->real_escape_string($cs_whatsappformat) . "',
+            '" . $conn->real_escape_string($cs_whatsapp) . "',
+            '" . $conn->real_escape_string($cs_wechat) . "',
+            '" . $conn->real_escape_string($cs_wechat) . "',
+            '" . $conn->real_escape_string($cs_linkedin) . "',
+            '" . $conn->real_escape_string($cs_linkedin) . "',
+            '" . $conn->real_escape_string($cs_facebook) . "',
+            '" . $conn->real_escape_string($cs_facebook) . "',
+            '" . $conn->real_escape_string($cs_alibaba) . "',
+            '" . $conn->real_escape_string($cs_alibaba) . "',
+            NOW(), 
+            NOW()
+        )";
+        $conn->query($contact_sql);
+    }
 
     // Update tags
     $conn->query("DELETE FROM tagtable WHERE customerId = " . intval($id));
@@ -266,43 +318,23 @@ if ($act == "editSave" || $allowedit == 1) {
 
     echo "<script>location.href='$hrefstr';</script>";
 } else {
-    // Insert new customer using direct SQL
+    // Insert new customer record
     $insertSql = "INSERT INTO customer (
-        cs_code, cs_company, cs_name, cs_country, cs_from, cs_tel, cs_wechat, 
-        cs_whatsapp, cs_email, cs_linkedin, cs_facebook, cs_alibaba, cs_address,
-        cs_telBu, cs_wechatBu, cs_whatsappBu, cs_emailBu, cs_linkedinBu, 
-        cs_facebookBu, cs_alibabaBu, cs_type, cs_addtime, cs_updatetime, 
-        cs_belong, cs_belongClient, cs_state, cs_telformat, cs_whatsappformat,
-        cs_deal, cs_note, cs_chain, is_silent, cs_dealdate
+        cs_code, cs_company, cs_country, cs_from, cs_address,
+        cs_type, cs_addtime, cs_updatetime, cs_belong, cs_belongClient, 
+        cs_state, cs_deal, cs_note, cs_chain, is_silent, cs_dealdate
     ) VALUES (
         '" . $conn->real_escape_string($cs_code) . "',
         '" . $conn->real_escape_string($cs_company) . "',
-        '" . $conn->real_escape_string($cs_name) . "',
         " . $cs_country . ",
         " . $cs_from . ",
-        '" . $conn->real_escape_string($cs_tel) . "',
-        '" . $conn->real_escape_string($cs_wechat) . "',
-        '" . $conn->real_escape_string($cs_whatsapp) . "',
-        '" . $conn->real_escape_string($cs_email) . "',
-        '" . $conn->real_escape_string($cs_linkedin) . "',
-        '" . $conn->real_escape_string($cs_facebook) . "',
-        '" . $conn->real_escape_string($cs_alibaba) . "',
         '" . $conn->real_escape_string($cs_address) . "',
-        '" . $conn->real_escape_string($cs_tel) . "',
-        '" . $conn->real_escape_string($cs_wechat) . "',
-        '" . $conn->real_escape_string($cs_whatsapp) . "',
-        '" . $conn->real_escape_string($cs_email) . "',
-        '" . $conn->real_escape_string($cs_linkedin) . "',
-        '" . $conn->real_escape_string($cs_facebook) . "',
-        '" . $conn->real_escape_string($cs_alibaba) . "',
         " . $cs_type . ",
         NOW(),
         NOW(),
         " . $cs_belong . ",
         " . $cs_belongClient . ",
         " . $cs_state . ",
-        '" . $conn->real_escape_string($cs_telformat) . "',
-        '" . $conn->real_escape_string($cs_whatsappformat) . "',
         " . $cs_deal . ",
         '" . $conn->real_escape_string($cs_note) . "',
         " . $cs_belong . ",
@@ -311,16 +343,49 @@ if ($act == "editSave" || $allowedit == 1) {
     )";
 
     $conn->query($insertSql);
-    $id = $conn->insert_id;
-
-    // Save tags for new customer
-    foreach ($mytag as $tag) {
-        if (!empty(trim($tag))) {
-            $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" . 
-                     $conn->real_escape_string($tag) . "', " . 
-                     intval($_SESSION['employee_id']) . ", " . 
-                     intval($id) . ")";
-            $conn->query($tagSql);
+    $new_customer_id = $conn->insert_id;
+    
+    // Insert contact information
+    if ($new_customer_id > 0) {
+        $contactSql = "INSERT INTO customer_contact (
+            customer_id, contact_name, tel, tel_format, tel_bu, 
+            email, email_bu, whatsapp, whatsapp_format, whatsapp_bu, 
+            wechat, wechat_bu, linkedin, linkedin_bu, facebook, 
+            facebook_bu, alibaba, alibaba_bu, created_at, updated_at
+        ) VALUES (
+            " . $new_customer_id . ",
+            '" . $conn->real_escape_string($cs_name) . "',
+            '" . $conn->real_escape_string($cs_tel) . "',
+            '" . $conn->real_escape_string($cs_telformat) . "',
+            '" . $conn->real_escape_string($cs_tel) . "',
+            '" . $conn->real_escape_string($cs_email) . "',
+            '" . $conn->real_escape_string($cs_email) . "',
+            '" . $conn->real_escape_string($cs_whatsapp) . "',
+            '" . $conn->real_escape_string($cs_whatsappformat) . "',
+            '" . $conn->real_escape_string($cs_whatsapp) . "',
+            '" . $conn->real_escape_string($cs_wechat) . "',
+            '" . $conn->real_escape_string($cs_wechat) . "',
+            '" . $conn->real_escape_string($cs_linkedin) . "',
+            '" . $conn->real_escape_string($cs_linkedin) . "',
+            '" . $conn->real_escape_string($cs_facebook) . "',
+            '" . $conn->real_escape_string($cs_facebook) . "',
+            '" . $conn->real_escape_string($cs_alibaba) . "',
+            '" . $conn->real_escape_string($cs_alibaba) . "',
+            NOW(), 
+            NOW()
+        )";
+        
+        $conn->query($contactSql);
+        
+        // Save tags for new customer
+        foreach ($mytag as $tag) {
+            if (!empty(trim($tag))) {
+                $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" . 
+                         $conn->real_escape_string($tag) . "', " . 
+                         intval($_SESSION['employee_id']) . ", " . 
+                         intval($new_customer_id) . ")";
+                $conn->query($tagSql);
+            }
         }
     }
 

+ 7 - 3
customerView.php

@@ -33,9 +33,13 @@ $ord = urlencode($_GET['Ord'] ?? '');
 $hrefstr = "?keys=" . $keys . "&Ord=" . $ord . "&Page=" . $page;
 
 if (!empty($id) && is_numeric($id)) {
-    $sql = "SELECT * FROM customer 
-            WHERE cs_belong IN (SELECT id FROM employee WHERE em_role=" . $_SESSION['employee_id'] . ") 
-            AND customer.id=" . (int)$id;
+    $sql = "SELECT c.*, cc.contact_name as cs_name, cc.tel as cs_tel, cc.email as cs_email, 
+            cc.whatsapp as cs_whatsapp, cc.wechat as cs_wechat, cc.linkedin as cs_linkedin, 
+            cc.facebook as cs_facebook, cc.alibaba as cs_alibaba 
+            FROM customer c 
+            LEFT JOIN customer_contact cc ON c.id = cc.customer_id
+            WHERE c.cs_belong IN (SELECT id FROM employee WHERE em_role=" . $_SESSION['employee_id'] . ") 
+            AND c.id=" . (int)$id;
             
     $result = $conn->query($sql);
     

+ 158 - 61
customers.php

@@ -26,24 +26,36 @@ if ($act == "save") {
         $isedit = true;
     }
     
-    // 获取表单数据
+    // 获取表单数据 - 客户基本信息
     $cs_code = textEncode($_POST['cs_code']);
     $cs_company = textEncode($_POST['cs_company']);
-    $cs_name = textEncode($_POST['cs_name']);
     $cs_belong = $_POST['cs_belong'];
     $cs_country = $_POST['cs_country'];
     $cs_from = $_POST['cs_from'];
-    $cs_tel = textEncode($_POST['cs_tel']);
-    $cs_email = textEncode($_POST['cs_email']);
-    $cs_whatsapp = textEncode($_POST['cs_whatsapp']);
-    $cs_wechat = textEncode($_POST['cs_wechat']);
-    $cs_linkedin = textEncode($_POST['cs_linkedin']);
-    $cs_facebook = textEncode($_POST['cs_facebook']);
-    $cs_alibaba = textEncode($_POST['cs_alibaba']);
     $cs_state = $_POST['cs_state'];
     $cs_deal = $_POST['cs_deal'];
     $no_content = htmlEncode($_POST['no_content']);
     $allowedit = isset($_POST['allowedit']) ? 1 : 0;
+    $cs_address = textEncode($_POST['cs_address'] ?? '');
+    
+    // 获取表单数据 - 联系人信息
+    $contact_name = textEncode($_POST['cs_name']);
+    $tel = textEncode($_POST['cs_tel']);
+    $tel_format = textEncode($_POST['cs_telformat'] ?? '');
+    $tel_bu = textEncode($_POST['cs_telBu'] ?? '');
+    $email = textEncode($_POST['cs_email']);
+    $email_bu = textEncode($_POST['cs_emailBu'] ?? '');
+    $whatsapp = textEncode($_POST['cs_whatsapp']);
+    $whatsapp_format = textEncode($_POST['cs_whatsappformat'] ?? '');
+    $whatsapp_bu = textEncode($_POST['cs_whatsappBu'] ?? '');
+    $wechat = textEncode($_POST['cs_wechat']);
+    $wechat_bu = textEncode($_POST['cs_wechatBu'] ?? '');
+    $linkedin = textEncode($_POST['cs_linkedin']);
+    $linkedin_bu = textEncode($_POST['cs_linkedinBu'] ?? '');
+    $facebook = textEncode($_POST['cs_facebook']);
+    $facebook_bu = textEncode($_POST['cs_facebookBu'] ?? '');
+    $alibaba = textEncode($_POST['cs_alibaba']);
+    $alibaba_bu = textEncode($_POST['cs_alibabaBu'] ?? '');
 
     if ($isedit) {
         // 更新现有记录
@@ -58,20 +70,14 @@ if ($act == "save") {
                 $cs_chain .= ",$cs_belong";
             }
             
+            // 更新客户基本信息
             $sql = "UPDATE customer SET 
                     cs_code = '$cs_code',
                     cs_company = '$cs_company',
-                    cs_name = '$cs_name',
                     cs_belong = '$cs_belong',
                     cs_country = '$cs_country',
+                    cs_address = '$cs_address',
                     cs_from = '$cs_from',
-                    cs_tel = '$cs_tel',
-                    cs_email = '$cs_email',
-                    cs_whatsapp = '$cs_whatsapp',
-                    cs_wechat = '$cs_wechat',
-                    cs_linkedin = '$cs_linkedin',
-                    cs_facebook = '$cs_facebook',
-                    cs_alibaba = '$cs_alibaba',
                     cs_state = '$cs_state',
                     cs_deal = '$cs_deal',
                     cs_note = '$no_content',
@@ -82,6 +88,85 @@ if ($act == "save") {
                     
             mysqli_query($conn, $sql);
             
+            // 检查是否已有联系人记录
+            $contact_sql = "SELECT id FROM customer_contact WHERE customer_id = $id";
+            $contact_result = mysqli_query($conn, $contact_sql);
+            
+            if ($contact_row = mysqli_fetch_assoc($contact_result)) {
+                // 更新联系人信息
+                $contact_id = $contact_row['id'];
+                $sql = "UPDATE customer_contact SET 
+                        contact_name = '$contact_name',
+                        tel = '$tel',
+                        tel_format = '$tel_format',
+                        tel_bu = '$tel_bu',
+                        email = '$email',
+                        email_bu = '$email_bu',
+                        whatsapp = '$whatsapp',
+                        whatsapp_format = '$whatsapp_format',
+                        whatsapp_bu = '$whatsapp_bu',
+                        wechat = '$wechat',
+                        wechat_bu = '$wechat_bu',
+                        linkedin = '$linkedin',
+                        linkedin_bu = '$linkedin_bu',
+                        facebook = '$facebook',
+                        facebook_bu = '$facebook_bu',
+                        alibaba = '$alibaba',
+                        alibaba_bu = '$alibaba_bu',
+                        updated_at = NOW()
+                        WHERE id = $contact_id";
+                mysqli_query($conn, $sql);
+            } else {
+                // 插入新的联系人记录
+                $sql = "INSERT INTO customer_contact (
+                        customer_id, contact_name, tel, tel_format, tel_bu, 
+                        email, email_bu, whatsapp, whatsapp_format, whatsapp_bu, 
+                        wechat, wechat_bu, linkedin, linkedin_bu, facebook, 
+                        facebook_bu, alibaba, alibaba_bu, created_at, updated_at
+                    ) VALUES (
+                        $id, '$contact_name', '$tel', '$tel_format', '$tel_bu',
+                        '$email', '$email_bu', '$whatsapp', '$whatsapp_format', '$whatsapp_bu',
+                        '$wechat', '$wechat_bu', '$linkedin', '$linkedin_bu', '$facebook',
+                        '$facebook_bu', '$alibaba', '$alibaba_bu', NOW(), NOW()
+                    )";
+                mysqli_query($conn, $sql);
+            }
+            
+            $page = $_GET['Page'] ?? '';
+            $keys = urlencode($_GET['Keys'] ?? '');
+            header("Location: ?keys=$keys&Page=$page$urlStr");
+            exit;
+        }
+    } else {
+        // 创建新记录
+        // 插入客户基本信息
+        $sql = "INSERT INTO customer (
+                cs_code, cs_company, cs_country, cs_address, cs_from, 
+                cs_belong, cs_state, cs_deal, cs_note, allowedit, 
+                cs_chain, cs_addtime, cs_updatetime
+            ) VALUES (
+                '$cs_code', '$cs_company', '$cs_country', '$cs_address', '$cs_from',
+                '$cs_belong', '$cs_state', '$cs_deal', '$no_content', $allowedit,
+                '$cs_belong', NOW(), NOW()
+            )";
+        mysqli_query($conn, $sql);
+        $new_customer_id = mysqli_insert_id($conn);
+        
+        // 插入联系人信息
+        if ($new_customer_id > 0) {
+            $sql = "INSERT INTO customer_contact (
+                    customer_id, contact_name, tel, tel_format, tel_bu, 
+                    email, email_bu, whatsapp, whatsapp_format, whatsapp_bu, 
+                    wechat, wechat_bu, linkedin, linkedin_bu, facebook, 
+                    facebook_bu, alibaba, alibaba_bu, created_at, updated_at
+                ) VALUES (
+                    $new_customer_id, '$contact_name', '$tel', '$tel_format', '$tel_bu',
+                    '$email', '$email_bu', '$whatsapp', '$whatsapp_format', '$whatsapp_bu',
+                    '$wechat', '$wechat_bu', '$linkedin', '$linkedin_bu', '$facebook',
+                    '$facebook_bu', '$alibaba', '$alibaba_bu', NOW(), NOW()
+                )";
+            mysqli_query($conn, $sql);
+            
             $page = $_GET['Page'] ?? '';
             $keys = urlencode($_GET['Keys'] ?? '');
             header("Location: ?keys=$keys&Page=$page$urlStr");
@@ -99,30 +184,35 @@ if ($act == "edit") {
     }
     
     if ($isedit) {
-        $sql = "SELECT c.*, n.c_code FROM customer c 
-                LEFT JOIN mynote n ON c.cs_code = n.c_code 
+        // 联合查询客户基本信息和联系人信息
+        $sql = "SELECT c.*, cc.contact_name, cc.tel, cc.tel_format, cc.tel_bu, 
+                cc.email, cc.email_bu, cc.whatsapp, cc.whatsapp_format, cc.whatsapp_bu, 
+                cc.wechat, cc.wechat_bu, cc.linkedin, cc.linkedin_bu, cc.facebook, 
+                cc.facebook_bu, cc.alibaba, cc.alibaba_bu, n.c_code 
+                FROM customer c 
+                LEFT JOIN customer_contact cc ON c.id = cc.customer_id
                 WHERE c.id = $id";
         $result = mysqli_query($conn, $sql);
         if ($row = mysqli_fetch_assoc($result)) {
             $cs_code = textDecode($row['cs_code']);
             $cs_company = textDecode($row['cs_company']);
-            $cs_name = textDecode($row['cs_name']);
+            $cs_name = textDecode($row['contact_name']);
             $cs_country = $row['cs_country'];
             $cs_from = $row['cs_from'];
-            $cs_tel = textDecode($row['cs_tel']);
-            $cs_telBu = textDecode($row['cs_telBu']);
-            $cs_email = textDecode($row['cs_email']);
-            $cs_emailBu = textDecode($row['cs_emailBu']);
-            $cs_whatsapp = textDecode($row['cs_whatsapp']);
-            $cs_whatsappBu = textDecode($row['cs_whatsappBu']);
-            $cs_wechat = textDecode($row['cs_wechat']);
-            $cs_wechatBu = textDecode($row['cs_wechatBu']);
-            $cs_linkedin = textDecode($row['cs_linkedin']);
-            $cs_linkedinBu = textDecode($row['cs_linkedinBu']);
-            $cs_facebook = textDecode($row['cs_facebook']);
-            $cs_facebookBu = textDecode($row['cs_facebookBu']);
-            $cs_alibaba = textDecode($row['cs_alibaba']);
-            $cs_alibabaBu = textDecode($row['cs_alibabaBu']);
+            $cs_tel = textDecode($row['tel']);
+            $cs_telBu = textDecode($row['tel_bu']);
+            $cs_email = textDecode($row['email']);
+            $cs_emailBu = textDecode($row['email_bu']);
+            $cs_whatsapp = textDecode($row['whatsapp']);
+            $cs_whatsappBu = textDecode($row['whatsapp_bu']);
+            $cs_wechat = textDecode($row['wechat']);
+            $cs_wechatBu = textDecode($row['wechat_bu']);
+            $cs_linkedin = textDecode($row['linkedin']);
+            $cs_linkedinBu = textDecode($row['linkedin_bu']);
+            $cs_facebook = textDecode($row['facebook']);
+            $cs_facebookBu = textDecode($row['facebook_bu']);
+            $cs_alibaba = textDecode($row['alibaba']);
+            $cs_alibabaBu = textDecode($row['alibaba_bu']);
             $cs_address = textDecode($row['cs_address']);
             $cs_addtime = $row['cs_addtime'];
             $cs_updatetime = $row['cs_updatetime'];
@@ -147,6 +237,7 @@ if ($act == "postchk") {
                 $sql = "UPDATE customer SET cs_state = $chkact WHERE id IN ($ids)";
                 break;
             case "-1":
+                // 删除客户记录和联系人记录(依靠外键级联删除)
                 $sql = "DELETE FROM customer WHERE id IN ($ids)";
                 break;
         }
@@ -213,21 +304,24 @@ $ord = $_GET['Ord'] ?? '';
 
 $ordStr = !empty($ord) ? "$ord," : "";
 
-// 构建查询SQL
-$sqlStr = "SELECT id, cs_code, cs_name, cs_country, cs_address, cs_tel, cs_email, 
-           cs_whatsapp, cs_wechat, cs_linkedin, cs_facebook, cs_addtime, cs_alibaba,
-           cs_from, cs_deal, cs_updatetime, cs_belong, cs_note, cs_claimFrom, cs_chain, 
-           cs_dealdate 
-           FROM customer 
-           WHERE (cs_code LIKE '%$keyscode%' 
-           OR cs_name LIKE '%$keyscode%' 
-           OR cs_wechat LIKE '%$keyscode%' 
-           OR cs_alibaba LIKE '%$keyscode%' 
-           OR cs_telformat LIKE '%$keyscode%' 
-           OR cs_whatsappformat LIKE '%$keyscode%' 
-           OR cs_email LIKE '%$keyscode%') 
+// 构建查询SQL - 修改为联合查询
+$sqlStr = "SELECT c.id, c.cs_code, c.cs_company, c.cs_country, c.cs_address, c.cs_from, 
+           c.cs_deal, c.cs_addtime, c.cs_updatetime, c.cs_belong, c.cs_note, c.cs_claimFrom, 
+           c.cs_chain, c.cs_dealdate, cc.contact_name as cs_name, cc.tel as cs_tel, 
+           cc.email as cs_email, cc.whatsapp as cs_whatsapp, cc.wechat as cs_wechat, 
+           cc.linkedin as cs_linkedin, cc.facebook as cs_facebook, cc.alibaba as cs_alibaba,
+           cc.tel_format as cs_telformat, cc.whatsapp_format as cs_whatsappformat
+           FROM customer c 
+           LEFT JOIN customer_contact cc ON c.id = cc.customer_id
+           WHERE (c.cs_code LIKE '%$keyscode%' 
+           OR cc.contact_name LIKE '%$keyscode%' 
+           OR cc.wechat LIKE '%$keyscode%' 
+           OR cc.alibaba LIKE '%$keyscode%' 
+           OR cc.tel_format LIKE '%$keyscode%' 
+           OR cc.whatsapp_format LIKE '%$keyscode%' 
+           OR cc.email LIKE '%$keyscode%') 
            $fliterStr 
-           ORDER BY {$ordStr}cs_updatetime DESC";
+           ORDER BY {$ordStr}c.cs_updatetime DESC";
 
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@@ -400,24 +494,27 @@ $hrefstr = "?keys=" . $keys;
         </div>
 
         <?php
-        $sql = "SELECT id, cs_code, cs_from, cs_country, cs_type, cs_deal, cs_addtime, 
-                cs_tel, cs_email, cs_whatsapp, cs_wechat, cs_linkedin, cs_facebook, 
-                cs_alibaba, colortag, cs_note, cs_claimFrom 
-                FROM customer 
-                WHERE is_silent=0 AND cs_deal>0 AND cs_belong=" . (int)$_SESSION['employee_id'];
+        $sql = "SELECT c.id, c.cs_code, c.cs_from, c.cs_country, c.cs_type, c.cs_deal, c.cs_addtime, 
+                c.colortag, c.cs_note, c.cs_claimFrom, cc.tel as cs_tel, cc.email as cs_email, 
+                cc.whatsapp as cs_whatsapp, cc.wechat as cs_wechat, cc.linkedin as cs_linkedin, 
+                cc.facebook as cs_facebook, cc.alibaba as cs_alibaba, cc.tel_format as cs_telformat,
+                cc.whatsapp_format as cs_whatsappformat
+                FROM customer c 
+                LEFT JOIN customer_contact cc ON c.id = cc.customer_id 
+                WHERE c.is_silent=0 AND c.cs_deal>0 AND c.cs_belong=" . (int)$_SESSION['employee_id'];
         
         $searchPattern = mysqli_real_escape_string($conn, $keyscode);
         if(!empty($searchPattern)) {
-            $sql .= " AND (cs_code LIKE '%$searchPattern%' 
-                    OR cs_name LIKE '%$searchPattern%' 
-                    OR cs_email LIKE '%$searchPattern%'
-                    OR cs_wechat LIKE '%$searchPattern%' 
-                    OR cs_telformat LIKE '%$searchPattern%'
-                    OR cs_whatsappformat LIKE '%$searchPattern%' 
-                    OR cs_code LIKE '%$searchPattern%')";
+            $sql .= " AND (c.cs_code LIKE '%$searchPattern%' 
+                    OR cc.contact_name LIKE '%$searchPattern%' 
+                    OR cc.email LIKE '%$searchPattern%'
+                    OR cc.wechat LIKE '%$searchPattern%' 
+                    OR cc.tel_format LIKE '%$searchPattern%'
+                    OR cc.whatsapp_format LIKE '%$searchPattern%' 
+                    OR c.cs_code LIKE '%$searchPattern%')";
         }
         
-        $sql .= " $filterStr ORDER BY colortag DESC, id DESC";
+        $sql .= " $filterStr ORDER BY c.colortag DESC, c.id DESC";
         
         $result = mysqli_query($conn, $sql);
 

+ 21 - 18
customersFollow.php

@@ -91,13 +91,13 @@ if (!empty($filterBusiness)) {
 
 if (!empty($filterContact)) {
     switch ($filterContact) {
-        case "1": $filterStr .= " AND cs_tel<>''"; break;
-        case "2": $filterStr .= " AND cs_wechat<>''"; break;
-        case "3": $filterStr .= " AND cs_whatsapp<>''"; break;
-        case "4": $filterStr .= " AND cs_email<>''"; break;
-        case "5": $filterStr .= " AND cs_linkedin<>''"; break;
-        case "6": $filterStr .= " AND cs_facebook<>''"; break;
-        default: $filterStr .= " AND cs_alibaba<>''";
+        case "1": $filterStr .= " AND cc.tel<>''"; break;
+        case "2": $filterStr .= " AND cc.wechat<>''"; break;
+        case "3": $filterStr .= " AND cc.whatsapp<>''"; break;
+        case "4": $filterStr .= " AND cc.email<>''"; break;
+        case "5": $filterStr .= " AND cc.linkedin<>''"; break;
+        case "6": $filterStr .= " AND cc.facebook<>''"; break;
+        default: $filterStr .= " AND cc.alibaba<>''";
     }
     $urlStr .= "&fliterContact=" . $filterContact;
 }
@@ -195,17 +195,20 @@ $hrefstr = "?keys=" . $keys;
         </div>
 
 <?php
-$sql = "SELECT id, cs_code, cs_from, cs_country, cs_type, cs_deal, cs_addtime, cs_tel, cs_email, 
-        cs_whatsapp, cs_wechat, cs_linkedin, cs_facebook, cs_alibaba, colortag, cs_note 
-        FROM customer 
-        WHERE cs_deal<>3 AND DATEDIFF(NOW(), cs_updatetime) > 55 AND cs_belong=" . $_SESSION['employee_id'] . "
-        AND (cs_code LIKE '%" . $conn->real_escape_string($keyscode) . "%' 
-        OR cs_name LIKE '%" . $conn->real_escape_string($keyscode) . "%'
-        OR cs_email LIKE '%" . $conn->real_escape_string($keyscode) . "%'
-        OR cs_wechat LIKE '%" . $conn->real_escape_string($keyscode) . "%'
-        OR cs_tel LIKE '%" . $conn->real_escape_string($keyscode) . "%'
-        OR cs_whatsapp LIKE '%" . $conn->real_escape_string($keyscode) . "%')" . 
-        $filterStr . " ORDER BY cs_state DESC, id DESC";
+$sql = "SELECT c.id, c.cs_code, c.cs_from, c.cs_country, c.cs_type, c.cs_deal, c.cs_addtime, 
+        c.colortag, c.cs_note, cc.tel as cs_tel, cc.email as cs_email, cc.whatsapp as cs_whatsapp, 
+        cc.wechat as cs_wechat, cc.linkedin as cs_linkedin, cc.facebook as cs_facebook, 
+        cc.alibaba as cs_alibaba 
+        FROM customer c 
+        LEFT JOIN customer_contact cc ON c.id = cc.customer_id
+        WHERE c.cs_deal<>3 AND DATEDIFF(NOW(), c.cs_updatetime) > 55 AND c.cs_belong=" . $_SESSION['employee_id'] . "
+        AND (c.cs_code LIKE '%" . $conn->real_escape_string($keyscode) . "%' 
+        OR cc.contact_name LIKE '%" . $conn->real_escape_string($keyscode) . "%'
+        OR cc.email LIKE '%" . $conn->real_escape_string($keyscode) . "%'
+        OR cc.wechat LIKE '%" . $conn->real_escape_string($keyscode) . "%'
+        OR cc.tel LIKE '%" . $conn->real_escape_string($keyscode) . "%'
+        OR cc.whatsapp LIKE '%" . $conn->real_escape_string($keyscode) . "%')" . 
+        $filterStr . " ORDER BY c.cs_state DESC, c.id DESC";
 
 $result = $conn->query($sql);
 

+ 23 - 20
customersNew.php

@@ -90,13 +90,13 @@ if (!empty($filterBusiness)) {
 
 if (!empty($filterContact)) {
     switch ($filterContact) {
-        case "1": $filterStr .= " AND cs_tel<>''"; break;
-        case "2": $filterStr .= " AND cs_wechat<>''"; break;
-        case "3": $filterStr .= " AND cs_whatsapp<>''"; break;
-        case "4": $filterStr .= " AND cs_email<>''"; break;
-        case "5": $filterStr .= " AND cs_linkedin<>''"; break;
-        case "6": $filterStr .= " AND cs_facebook<>''"; break;
-        default: $filterStr .= " AND cs_alibaba<>''";
+        case "1": $filterStr .= " AND cc.tel<>''"; break;
+        case "2": $filterStr .= " AND cc.wechat<>''"; break;
+        case "3": $filterStr .= " AND cc.whatsapp<>''"; break;
+        case "4": $filterStr .= " AND cc.email<>''"; break;
+        case "5": $filterStr .= " AND cc.linkedin<>''"; break;
+        case "6": $filterStr .= " AND cc.facebook<>''"; break;
+        default: $filterStr .= " AND cc.alibaba<>''";
     }
     $urlStr .= "&fliterContact=" . $filterContact;
 }
@@ -195,19 +195,22 @@ $hrefstr = "?keys=" . $keys;
         </div>
 
 <?php
-$sql = "SELECT id, cs_code, cs_from, cs_country, cs_type, cs_deal, cs_addtime, cs_tel, cs_email, 
-        cs_whatsapp, cs_wechat, cs_linkedin, cs_facebook, cs_alibaba, colorTag, cs_note 
-        FROM customer 
-        WHERE cs_belong=" . $_SESSION['employee_id'] . " 
-        AND MONTH(cs_addtime) = MONTH(CURRENT_DATE()) 
-        AND YEAR(cs_addtime) = YEAR(CURRENT_DATE())
-        AND (cs_code LIKE '%" . $conn->real_escape_string($keyscode) . "%' 
-        OR cs_name LIKE '%" . $conn->real_escape_string($keyscode) . "%'
-        OR cs_email LIKE '%" . $conn->real_escape_string($keyscode) . "%'
-        OR cs_wechat LIKE '%" . $conn->real_escape_string($keyscode) . "%'
-        OR cs_tel LIKE '%" . $conn->real_escape_string($keyscode) . "%'
-        OR cs_whatsapp LIKE '%" . $conn->real_escape_string($keyscode) . "%')" . 
-        $filterStr . " ORDER BY id DESC";
+$sql = "SELECT c.id, c.cs_code, c.cs_from, c.cs_country, c.cs_type, c.cs_deal, c.cs_addtime, 
+        cc.tel as cs_tel, cc.email as cs_email, cc.whatsapp as cs_whatsapp, cc.wechat as cs_wechat, 
+        cc.linkedin as cs_linkedin, cc.facebook as cs_facebook, cc.alibaba as cs_alibaba, 
+        c.colorTag, c.cs_note 
+        FROM customer c 
+        LEFT JOIN customer_contact cc ON c.id = cc.customer_id 
+        WHERE c.cs_belong=" . $_SESSION['employee_id'] . " 
+        AND MONTH(c.cs_addtime) = MONTH(CURRENT_DATE()) 
+        AND YEAR(c.cs_addtime) = YEAR(CURRENT_DATE())
+        AND (c.cs_code LIKE '%" . $conn->real_escape_string($keyscode) . "%' 
+        OR cc.contact_name LIKE '%" . $conn->real_escape_string($keyscode) . "%'
+        OR cc.email LIKE '%" . $conn->real_escape_string($keyscode) . "%'
+        OR cc.wechat LIKE '%" . $conn->real_escape_string($keyscode) . "%'
+        OR cc.tel LIKE '%" . $conn->real_escape_string($keyscode) . "%'
+        OR cc.whatsapp LIKE '%" . $conn->real_escape_string($keyscode) . "%')" . 
+        $filterStr . " ORDER BY c.id DESC";
 
 $result = $conn->query($sql);