|
@@ -15,24 +15,28 @@ if ($act == "save") {
|
|
|
$isEdit = true;
|
|
|
}
|
|
|
|
|
|
+ // Main customer table fields
|
|
|
$cs_code = textEncode($_POST['cs_code'] ?? '');
|
|
|
$cs_company = textEncode($_POST['cs_company'] ?? '');
|
|
|
- $cs_name = textEncode($_POST['cs_name'] ?? '');
|
|
|
$cs_belong = intval($_POST['cs_belong'] ?? 0);
|
|
|
$cs_country = intval($_POST['cs_country'] ?? 0);
|
|
|
$cs_from = intval($_POST['cs_from'] ?? 0);
|
|
|
- $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 = intval($_POST['cs_state'] ?? 0);
|
|
|
$cs_deal = intval($_POST['cs_deal'] ?? 0);
|
|
|
$no_content = htmlEncode($_POST['no_content'] ?? '');
|
|
|
+ $cs_address = textEncode($_POST['cs_address'] ?? '');
|
|
|
$allowedit = isset($_POST['allowedit']) ? 1 : 0;
|
|
|
|
|
|
+ // Contact fields
|
|
|
+ $contact_name = textEncode($_POST['cs_name'] ?? '');
|
|
|
+ $tel = textEncode($_POST['cs_tel'] ?? '');
|
|
|
+ $email = textEncode($_POST['cs_email'] ?? '');
|
|
|
+ $whatsapp = textEncode($_POST['cs_whatsapp'] ?? '');
|
|
|
+ $wechat = textEncode($_POST['cs_wechat'] ?? '');
|
|
|
+ $linkedin = textEncode($_POST['cs_linkedin'] ?? '');
|
|
|
+ $facebook = textEncode($_POST['cs_facebook'] ?? '');
|
|
|
+ $alibaba = textEncode($_POST['cs_alibaba'] ?? '');
|
|
|
+
|
|
|
if ($isEdit) {
|
|
|
$sql = "SELECT cs_chain FROM customer WHERE id=" . $id;
|
|
|
$result = $conn->query($sql);
|
|
@@ -46,30 +50,56 @@ if ($act == "save") {
|
|
|
$cs_chain .= ",$cs_belong";
|
|
|
}
|
|
|
|
|
|
+ // Update customer table
|
|
|
$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_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',
|
|
|
+ cs_address='$cs_address',
|
|
|
allowedit=$allowedit,
|
|
|
cs_chain='$cs_chain',
|
|
|
cs_updatetime=NOW()
|
|
|
WHERE id=$id";
|
|
|
-
|
|
|
$conn->query($sql);
|
|
|
|
|
|
+ // Check if contact exists for this customer
|
|
|
+ $sql = "SELECT id FROM customer_contact WHERE customer_id=$id";
|
|
|
+ $contact_result = $conn->query($sql);
|
|
|
+
|
|
|
+ if ($contact_result->num_rows > 0) {
|
|
|
+ $contact_row = $contact_result->fetch_assoc();
|
|
|
+ $contact_id = $contact_row['id'];
|
|
|
+
|
|
|
+ // Update existing contact
|
|
|
+ $sql = "UPDATE customer_contact SET
|
|
|
+ contact_name='$contact_name',
|
|
|
+ tel='$tel',
|
|
|
+ email='$email',
|
|
|
+ whatsapp='$whatsapp',
|
|
|
+ wechat='$wechat',
|
|
|
+ linkedin='$linkedin',
|
|
|
+ facebook='$facebook',
|
|
|
+ alibaba='$alibaba',
|
|
|
+ updated_at=NOW()
|
|
|
+ WHERE id=$contact_id";
|
|
|
+ $conn->query($sql);
|
|
|
+ } else {
|
|
|
+ // Create new contact for existing customer
|
|
|
+ $sql = "INSERT INTO customer_contact (
|
|
|
+ customer_id, contact_name, tel, email, whatsapp, wechat,
|
|
|
+ linkedin, facebook, alibaba, created_at, updated_at
|
|
|
+ ) VALUES (
|
|
|
+ $id, '$contact_name', '$tel', '$email', '$whatsapp', '$wechat',
|
|
|
+ '$linkedin', '$facebook', '$alibaba', NOW(), NOW()
|
|
|
+ )";
|
|
|
+ $conn->query($sql);
|
|
|
+ }
|
|
|
+
|
|
|
$page = $_GET['Page'] ?? '';
|
|
|
$keys = urlencode($_GET['Keys'] ?? '');
|
|
|
header("Location: ?keys=$keys&Page=$page$urlStr");
|
|
@@ -78,21 +108,43 @@ if ($act == "save") {
|
|
|
$output = "<script>alert('不存在该客户');history.back();</script>";
|
|
|
}
|
|
|
} else {
|
|
|
- $sql = "INSERT INTO customer (
|
|
|
- cs_code, cs_company, cs_name, cs_belong, cs_country, cs_from,
|
|
|
- cs_tel, cs_email, cs_whatsapp, cs_wechat, cs_linkedin,
|
|
|
- cs_facebook, cs_alibaba, cs_state, cs_deal, cs_note,
|
|
|
- allowedit, cs_chain, cs_addtime, cs_updatetime
|
|
|
- ) VALUES (
|
|
|
- '$cs_code', '$cs_company', '$cs_name', $cs_belong, $cs_country, $cs_from,
|
|
|
- '$cs_tel', '$cs_email', '$cs_whatsapp', '$cs_wechat', '$cs_linkedin',
|
|
|
- '$cs_facebook', '$cs_alibaba', $cs_state, $cs_deal, '$no_content',
|
|
|
- $allowedit, '$cs_belong', NOW(), NOW()
|
|
|
- )";
|
|
|
+ // Begin transaction
|
|
|
+ $conn->begin_transaction();
|
|
|
|
|
|
- $conn->query($sql);
|
|
|
- header("Location: ?");
|
|
|
- exit;
|
|
|
+ try {
|
|
|
+ // Insert into customer table
|
|
|
+ $sql = "INSERT INTO customer (
|
|
|
+ cs_code, cs_company, cs_belong, cs_country, cs_from,
|
|
|
+ cs_state, cs_deal, cs_note, cs_address,
|
|
|
+ allowedit, cs_chain, cs_addtime, cs_updatetime
|
|
|
+ ) VALUES (
|
|
|
+ '$cs_code', '$cs_company', $cs_belong, $cs_country, $cs_from,
|
|
|
+ $cs_state, $cs_deal, '$no_content', '$cs_address',
|
|
|
+ $allowedit, '$cs_belong', NOW(), NOW()
|
|
|
+ )";
|
|
|
+
|
|
|
+ $conn->query($sql);
|
|
|
+ $new_customer_id = $conn->insert_id;
|
|
|
+
|
|
|
+ // Insert into customer_contact table
|
|
|
+ $sql = "INSERT INTO customer_contact (
|
|
|
+ customer_id, contact_name, tel, email, whatsapp, wechat,
|
|
|
+ linkedin, facebook, alibaba, created_at, updated_at
|
|
|
+ ) VALUES (
|
|
|
+ $new_customer_id, '$contact_name', '$tel', '$email', '$whatsapp', '$wechat',
|
|
|
+ '$linkedin', '$facebook', '$alibaba', NOW(), NOW()
|
|
|
+ )";
|
|
|
+ $conn->query($sql);
|
|
|
+
|
|
|
+ // Commit transaction
|
|
|
+ $conn->commit();
|
|
|
+ header("Location: ?");
|
|
|
+ exit;
|
|
|
+ } catch (Exception $e) {
|
|
|
+ // Rollback on failure
|
|
|
+ $conn->rollback();
|
|
|
+ $output = "<script>alert('保存失败: " . $e->getMessage() . "');history.back();</script>";
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -149,32 +201,36 @@ if ($act == "edit" || $act == "add") {
|
|
|
if (!empty($id) && is_numeric($id)) {
|
|
|
$isEdit = true;
|
|
|
|
|
|
- $sql = "SELECT c.*, n.c_code
|
|
|
+ // Join customer and customer_contact tables
|
|
|
+ $sql = "SELECT c.*, cc.contact_name, cc.tel, cc.tel_bu, cc.email, cc.email_bu,
|
|
|
+ cc.whatsapp, cc.whatsapp_bu, cc.wechat, cc.wechat_bu,
|
|
|
+ cc.linkedin, cc.linkedin_bu, cc.facebook, cc.facebook_bu,
|
|
|
+ cc.alibaba, cc.alibaba_bu
|
|
|
FROM customer c
|
|
|
- LEFT JOIN mynote n ON c.cs_code = n.c_code
|
|
|
+ LEFT JOIN customer_contact cc ON c.id = cc.customer_id
|
|
|
WHERE c.id=$id";
|
|
|
$result = $conn->query($sql);
|
|
|
|
|
|
if ($row = $result->fetch_assoc()) {
|
|
|
$cs_code = textUncode($row['cs_code']);
|
|
|
$cs_company = textUncode($row['cs_company']);
|
|
|
- $cs_name = textUncode($row['cs_name']);
|
|
|
+ $cs_name = textUncode($row['contact_name']);
|
|
|
$cs_country = $row['cs_country'];
|
|
|
$cs_from = $row['cs_from'];
|
|
|
- $cs_tel = textUncode($row['cs_tel']);
|
|
|
- $cs_telBu = textUncode($row['cs_telBu']);
|
|
|
- $cs_wechat = textUncode($row['cs_wechat']);
|
|
|
- $cs_wechatBu = textUncode($row['cs_wechatBu']);
|
|
|
- $cs_whatsapp = textUncode($row['cs_whatsapp']);
|
|
|
- $cs_whatsappBu = textUncode($row['cs_whatsappBu']);
|
|
|
- $cs_email = textUncode($row['cs_email']);
|
|
|
- $cs_emailBu = textUncode($row['cs_emailBu']);
|
|
|
- $cs_linkedin = textUncode($row['cs_linkedin']);
|
|
|
- $cs_linkedinBu = textUncode($row['cs_linkedinBu']);
|
|
|
- $cs_facebook = textUncode($row['cs_facebook']);
|
|
|
- $cs_facebookBu = textUncode($row['cs_facebookBu']);
|
|
|
- $cs_alibaba = textUncode($row['cs_alibaba']);
|
|
|
- $cs_alibabaBu = textUncode($row['cs_alibabaBu']);
|
|
|
+ $cs_tel = textUncode($row['tel']);
|
|
|
+ $cs_telBu = textUncode($row['tel_bu']);
|
|
|
+ $cs_wechat = textUncode($row['wechat']);
|
|
|
+ $cs_wechatBu = textUncode($row['wechat_bu']);
|
|
|
+ $cs_whatsapp = textUncode($row['whatsapp']);
|
|
|
+ $cs_whatsappBu = textUncode($row['whatsapp_bu']);
|
|
|
+ $cs_email = textUncode($row['email']);
|
|
|
+ $cs_emailBu = textUncode($row['email_bu']);
|
|
|
+ $cs_linkedin = textUncode($row['linkedin']);
|
|
|
+ $cs_linkedinBu = textUncode($row['linkedin_bu']);
|
|
|
+ $cs_facebook = textUncode($row['facebook']);
|
|
|
+ $cs_facebookBu = textUncode($row['facebook_bu']);
|
|
|
+ $cs_alibaba = textUncode($row['alibaba']);
|
|
|
+ $cs_alibabaBu = textUncode($row['alibaba_bu']);
|
|
|
$cs_address = textUncode($row['cs_address']);
|
|
|
$cs_addtime = $row['cs_addtime'];
|
|
|
$cs_updatetime = $row['cs_updatetime'];
|
|
@@ -366,9 +422,26 @@ if ($act == "postchk") {
|
|
|
$sql = "UPDATE customer SET cs_state=$chkact WHERE id IN ($idList)";
|
|
|
break;
|
|
|
default:
|
|
|
- $sql = "DELETE FROM customer WHERE id IN ($idList)";
|
|
|
+ // In delete case, let's use transactions to ensure both tables are updated
|
|
|
+ $conn->begin_transaction();
|
|
|
+ try {
|
|
|
+ // Delete from customer_contact first (due to foreign key constraint)
|
|
|
+ $sql = "DELETE FROM customer_contact WHERE customer_id IN ($idList)";
|
|
|
+ $conn->query($sql);
|
|
|
+
|
|
|
+ // Then delete from customer table
|
|
|
+ $sql = "DELETE FROM customer WHERE id IN ($idList)";
|
|
|
+ $conn->query($sql);
|
|
|
+
|
|
|
+ $conn->commit();
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $conn->rollback();
|
|
|
+ echo "<script>alert('删除失败: " . $e->getMessage() . "');</script>";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($chkact == "0" || $chkact == "1") {
|
|
|
+ $conn->query($sql);
|
|
|
}
|
|
|
- $conn->query($sql);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -387,35 +460,35 @@ $filterStr = "";
|
|
|
$urlStr = "";
|
|
|
|
|
|
if (!empty($fliterQudao)) {
|
|
|
- $filterStr .= " AND cs_from=" . intval($fliterQudao);
|
|
|
+ $filterStr .= " AND c.cs_from=" . intval($fliterQudao);
|
|
|
$urlStr .= "&fliterQudao=$fliterQudao";
|
|
|
}
|
|
|
|
|
|
if (!empty($fliterDeal)) {
|
|
|
- $filterStr .= " AND cs_deal=" . intval($fliterDeal);
|
|
|
+ $filterStr .= " AND c.cs_deal=" . intval($fliterDeal);
|
|
|
$urlStr .= "&fliterDeal=$fliterDeal";
|
|
|
}
|
|
|
|
|
|
if (!empty($fliterTeam)) {
|
|
|
- $filterStr .= " AND (cs_belong=" . intval($fliterTeam) .
|
|
|
- " OR cs_belong IN (SELECT id FROM employee WHERE em_role=" . intval($fliterTeam) . "))";
|
|
|
+ $filterStr .= " AND (c.cs_belong=" . intval($fliterTeam) .
|
|
|
+ " OR c.cs_belong IN (SELECT id FROM employee WHERE em_role=" . intval($fliterTeam) . "))";
|
|
|
$urlStr .= "&fliterTeam=$fliterTeam";
|
|
|
}
|
|
|
|
|
|
if (!empty($fliterEmployee)) {
|
|
|
- $filterStr .= " AND cs_belong=" . intval($fliterEmployee);
|
|
|
+ $filterStr .= " AND c.cs_belong=" . intval($fliterEmployee);
|
|
|
$urlStr .= "&fliterEmployee=$fliterEmployee";
|
|
|
}
|
|
|
|
|
|
if (!empty($fliterContact)) {
|
|
|
switch($fliterContact) {
|
|
|
- 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=$fliterContact";
|
|
|
}
|
|
@@ -425,20 +498,23 @@ $keyscode = textEncode($keys);
|
|
|
$page = $_GET['Page'] ?? '';
|
|
|
$ord = $_GET['Ord'] ?? '';
|
|
|
|
|
|
-$sql = "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 = "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
|
|
|
+ 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 LIKE '%$keyscode%'
|
|
|
+ OR cc.whatsapp LIKE '%$keyscode%'
|
|
|
+ OR cc.email LIKE '%$keyscode%')
|
|
|
$filterStr
|
|
|
- ORDER BY cs_updatetime DESC";
|
|
|
+ ORDER BY c.cs_updatetime DESC";
|
|
|
|
|
|
$result = $conn->query($sql);
|
|
|
?>
|