begin_transaction();
try {
if ($isEdit) {
// Get existing chain info
$sql = "SELECT cs_chain FROM customer WHERE id=$id";
$result = $conn->query($sql);
if ($row = $result->fetch_assoc()) {
$cs_chain = $row['cs_chain'];
$chain_array = explode(',', $cs_chain);
$last_item = end($chain_array);
if ($last_item != $cs_belong) {
$cs_chain .= ",$cs_belong";
}
// Update customer table
$sql = "UPDATE customer SET
cs_code='$cs_code',
cs_company='$cs_company',
cs_belong=$cs_belong,
cs_country=$cs_country,
cs_from=$cs_from,
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);
// Delete existing contacts to replace with new ones
$sql = "DELETE FROM customer_contact WHERE customer_id=$id";
$conn->query($sql);
} else {
throw new Exception('不存在该客户');
}
} else {
// Insert new customer
$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);
$id = $conn->insert_id;
}
// Process contacts array
if (isset($_POST['contact']) && is_array($_POST['contact'])) {
foreach ($_POST['contact'] as $contact) {
if (empty($contact['contact_name'])) continue;
$contact_name = textEncode($contact['contact_name']);
// Initialize arrays for contact methods
$methods = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
$fields = ['customer_id', 'contact_name'];
$values = [$id, "'".$conn->real_escape_string($contact_name)."'"];
// Process each contact method (up to 3 entries each)
foreach ($methods as $method) {
for ($i = 1; $i <= 3; $i++) {
$field_base = $method . '_' . $i;
$value = $contact[$field_base] ?? '';
$escaped_value = $conn->real_escape_string(textEncode($value));
$fields[] = $field_base;
$values[] = "'$escaped_value'";
// Add format field for tel and whatsapp
if ($method == 'tel' || $method == 'whatsapp') {
$format_value = numFormat($value);
$fields[] = $field_base . '_format';
$values[] = "'".$conn->real_escape_string($format_value)."'";
}
// Add backup field
$bu_value = $contact[$field_base . '_bu'] ?? $value;
$escaped_bu_value = $conn->real_escape_string(textEncode($bu_value));
$fields[] = $field_base . '_bu';
$values[] = "'$escaped_bu_value'";
}
}
// Create and execute insert statement for contact
$sql = "INSERT INTO customer_contact (" . implode(', ', $fields) . ", created_at, updated_at)
VALUES (" . implode(', ', $values) . ", NOW(), NOW())";
$conn->query($sql);
}
}
// Commit transaction
$conn->commit();
// Redirect after successful save
$page = $_GET['Page'] ?? '';
$keys = urlencode($_GET['Keys'] ?? '');
header("Location: ?keys=$keys&Page=$page$urlStr");
exit;
} catch (Exception $e) {
// Rollback on failure
$conn->rollback();
$output = "";
}
}
// If we have output from processing, we'll show it instead of the normal page
if (!empty($output)) {
echo $output;
exit;
}
?>
管理区域
query($sql);
if ($row = $result->fetch_assoc()) {
// Basic customer info
$cs_code = textUncode($row['cs_code']);
$cs_company = textUncode($row['cs_company']);
$cs_country = $row['cs_country'];
$cs_from = $row['cs_from'];
$cs_address = textUncode($row['cs_address']);
$cs_addtime = $row['cs_addtime'];
$cs_updatetime = $row['cs_updatetime'];
$cs_belong = $row['cs_belong'];
$cs_state = $row['cs_state'];
$cs_deal = $row['cs_deal'];
$cs_note = htmlUncode($row['cs_note']);
$allowedit = $row['allowedit'];
$cs_type = $row['cs_type'];
$cs_belongclient = $row['cs_belongclient'];
// Get all contacts for this customer
$contactSql = "SELECT * FROM customer_contact WHERE customer_id = $id";
$contactResult = $conn->query($contactSql);
while ($contactRow = $contactResult->fetch_assoc()) {
$contact = [
'id' => $contactRow['id'],
'contact_name' => textUncode($contactRow['contact_name']),
'created_at' => $contactRow['created_at'],
'updated_at' => $contactRow['updated_at']
];
// Process each contact method type (up to 3 entries each)
$methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
foreach ($methodTypes as $type) {
for ($i = 1; $i <= 3; $i++) {
$fieldBase = $type . '_' . $i;
$contact[$fieldBase] = textUncode($contactRow[$fieldBase]);
if ($type == 'tel' || $type == 'whatsapp') {
$contact[$fieldBase . '_format'] = textUncode($contactRow[$fieldBase . '_format']);
}
$contact[$fieldBase . '_bu'] = textUncode($contactRow[$fieldBase . '_bu']);
}
}
$contacts[] = $contact;
}
}
}
$page = $_GET['Page'] ?? '';
$keys = urlencode($_GET['Keys'] ?? '');
$ord = urlencode($_GET['Ord'] ?? '');
$hrefstr = "?keys=$keys&Page=$page&Ord=$ord";
?>
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 "";
}
}
if ($chkact == "0" || $chkact == "1") {
$conn->query($sql);
}
}
}
header("Location: ?Keys=$keys&Page=$page");
exit;
}
// 主列表页面
$fliterQudao = $_GET['fliterQudao'] ?? '';
$fliterDeal = $_GET['fliterDeal'] ?? '';
$fliterTeam = $_GET['fliterTeam'] ?? '';
$fliterContact = $_GET['fliterContact'] ?? '';
$fliterEmployee = $_GET['fliterEmployee'] ?? '';
$filterStr = "";
$urlStr = "";
if (!empty($fliterQudao)) {
$filterStr .= " AND c.cs_from=" . intval($fliterQudao);
$urlStr .= "&fliterQudao=$fliterQudao";
}
if (!empty($fliterDeal)) {
$filterStr .= " AND c.cs_deal=" . intval($fliterDeal);
$urlStr .= "&fliterDeal=$fliterDeal";
}
if (!empty($fliterTeam)) {
$teamId = intval($fliterTeam);
$filterStr .= " AND (c.cs_belong=$teamId OR c.cs_belong IN (SELECT id FROM employee WHERE em_role=$teamId))";
$urlStr .= "&fliterTeam=$fliterTeam";
}
if (!empty($fliterEmployee)) {
$filterStr .= " AND c.cs_belong=" . intval($fliterEmployee);
$urlStr .= "&fliterEmployee=$fliterEmployee";
}
if (!empty($fliterContact)) {
switch($fliterContact) {
case "1": $filterStr .= " AND (cc.tel_1 != '' OR cc.tel_2 != '' OR cc.tel_3 != '')"; break;
case "2": $filterStr .= " AND (cc.wechat_1 != '' OR cc.wechat_2 != '' OR cc.wechat_3 != '')"; break;
case "3": $filterStr .= " AND (cc.whatsapp_1 != '' OR cc.whatsapp_2 != '' OR cc.whatsapp_3 != '')"; break;
case "4": $filterStr .= " AND (cc.email_1 != '' OR cc.email_2 != '' OR cc.email_3 != '')"; break;
case "5": $filterStr .= " AND (cc.linkedin_1 != '' OR cc.linkedin_2 != '' OR cc.linkedin_3 != '')"; break;
case "6": $filterStr .= " AND (cc.facebook_1 != '' OR cc.facebook_2 != '' OR cc.facebook_3 != '')"; break;
default: $filterStr .= " AND (cc.alibaba_1 != '' OR cc.alibaba_2 != '' OR cc.alibaba_3 != '')";
}
$urlStr .= "&fliterContact=$fliterContact";
}
$keys = $_GET['Keys'] ?? '';
$keyscode = textEncode($keys);
$page = $_GET['Page'] ?? '';
$ord = $_GET['Ord'] ?? '';
$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_1 as cs_tel, cc.email_1 as cs_email,
cc.whatsapp_1 as cs_whatsapp, cc.wechat_1 as cs_wechat,
cc.linkedin_1 as cs_linkedin, cc.facebook_1 as cs_facebook,
cc.alibaba_1 as cs_alibaba
FROM customer c
LEFT JOIN customer_contact cc ON c.id = cc.customer_id
WHERE (c.cs_code LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.contact_name LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.tel_1 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.tel_2 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.tel_3 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.wechat_1 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.wechat_2 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.wechat_3 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.alibaba_1 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.alibaba_2 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.alibaba_3 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.whatsapp_1_format LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.whatsapp_2_format LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.whatsapp_3_format LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.email_1 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.email_2 LIKE '%".$conn->real_escape_string($keyscode)."%'
OR cc.email_3 LIKE '%".$conn->real_escape_string($keyscode)."%')
$filterStr
ORDER BY c.cs_updatetime DESC";
// Execute query to count total records
$countResult = $conn->query($sql);
if (!$countResult) {
die("查询失败: " . $conn->error . "
SQL: " . $sql);
}
$totalRecords = $countResult->num_rows;
$countResult->close(); // 关闭第一个结果集
// Create pagination variables
$pageSize = 18;
$totalPages = ceil($totalRecords / $pageSize);
if ($totalPages < 1) $totalPages = 1; // 确保至少有一页,即使没有结果
if (empty($page)) $page = 1;
if ($page == 'end') $page = $totalPages;
if (!is_numeric($page) || $page < 1) $page = 1;
$page = (int)$page;
if ($page > $totalPages) $page = $totalPages;
// Apply pagination
$offset = ($page - 1) * $pageSize;
if ($offset < 0) $offset = 0; // 确保偏移量不为负数
$sql_paginated = $sql . " LIMIT $offset, $pageSize"; // 使用新变量,不修改原始SQL
// Execute the paginated query
$result = $conn->query($sql_paginated);
if (!$result) {
die("分页查询失败: " . $conn->error . "
SQL: " . $sql_paginated);
}
$tempNum = $pageSize * ($page - 1);
?>
close();
?>