alert('阿里旺旺为必填项');history.back();";
exit;
}
// Website source validation
if (strpos($cs_code, '官网') !== false) {
$cs_from = 3;
}
if ($cs_from == 3 && empty($cs_tel) && empty($cs_whatsapp) && empty($cs_wechat)) {
echo "";
exit;
}
// Market customer validation
if ($cs_from == 8 && empty($cs_wechat)) {
echo "";
exit;
}
// Facebook validation
if ($cs_from == 12 && empty($cs_facebook)) {
echo "";
exit;
}
}
// Determine action type
$act = empty($id) || !is_numeric($id) ? 'addSave' : 'editSave';
if ($act === 'editSave') {
// Verify customer ownership
$stmt = $conn->prepare("SELECT cs_belong FROM Customer WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
if ($row = $result->fetch_assoc()) {
if ($row['cs_belong'] != $cs_belong) {
echo "";
exit;
}
} else {
echo "";
exit;
}
}
// Validate customer code
if (empty($cs_code)) {
echo "";
exit;
}
// Check for duplicate customer information
$checkStr = "SELECT * FROM Customer WHERE cs_belong != " . $_SESSION['employee_id'] . " AND (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 . "%'";
$Dupli .= "电话:" . $cs_tel;
}
if (!empty($cs_email)) {
$checkStr .= " OR cs_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) . "%'";
$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) . "%'";
} else {
$checkStr .= " OR cs_wechat LIKE '%" . substr($cs_wechat, 2, 12) . "%'" .
" OR cs_telformat LIKE '%" . substr($cs_wechat, 2, 12) . "%'";
}
$Dupli .= "微信:" . $cs_wechat;
}
if (!empty($cs_linkedin)) {
$checkStr .= " OR cs_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) . "%'";
$Dupli .= "Facebook:" . $cs_facebook;
}
if (!empty($cs_alibaba)) {
if (strlen($cs_alibaba) < 10) {
$checkStr .= " OR cs_alibaba LIKE '" . $conn->real_escape_string($cs_alibaba) . "'";
} else {
$checkStr .= " OR cs_alibaba LIKE '%" . substr($cs_alibaba, 3, 12) . "%'";
}
$Dupli .= "阿里旺旺:" . $cs_alibaba;
}
$checkStr .= " ) ORDER BY id ASC";
if ($allowedit != 1) {
$result = $conn->query($checkStr);
if ($result && $result->num_rows > 0) {
$row = $result->fetch_assoc();
// Get owner name
$ownerResult = $conn->query("SELECT em_user FROM employee WHERE id = " . $row['cs_belong']);
$ownerRow = $ownerResult->fetch_assoc();
$owner = textUncode($ownerRow['em_user']);
// Determine who entered first
if (strtotime($cs_addtime) > strtotime($row['cs_addtime'])) {
$tstr = "INSERT INTO logRecord (loginName, loginIp, loginTime, loginAct) VALUES ('" .
$_SESSION['employee_name'] . "', '" . getIp() . "', '" . date('Y-m-d H:i:s') . "', '" .
$_SESSION['employee_name'] . "编辑客户\"" . $cs_code . "\",该客户与\"" .
textUncode($row['cs_code']) . "\"高度类似,
重复项为:" . $Dupli . "
客户由:" .
$_SESSION['employee_name'] . $cs_addtime . "首次录入')";
} else {
$tstr = "INSERT INTO logRecord (loginName, loginIp, loginTime, loginAct) VALUES ('" .
$_SESSION['employee_name'] . "', '" . getIp() . "', '" . date('Y-m-d H:i:s') . "', '" .
$_SESSION['employee_name'] . "编辑客户\"" . $cs_code . "\",该客户与\"" .
textUncode($row['cs_code']) . "\"高度类似,
重复项为:" . $Dupli . "
客户由:" .
$owner . $row['cs_addtime'] . "首次录入')";
}
$conn->query($tstr);
echo "";
exit;
}
}
// Save or update customer data
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) . "'";
// 处理cs_dealdate
if ($cs_deal == 3) {
$updateSql .= ", cs_dealdate = CASE WHEN cs_dealdate IS NULL THEN NOW() ELSE cs_dealdate END";
}
$updateSql .= " WHERE id=" . intval($id);
$conn->query($updateSql);
// Update tags
$conn->query("DELETE FROM tagTable WHERE customerId = " . intval($id));
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);
}
}
echo "";
} else {
// Insert new customer using direct SQL
$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
) 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 . ",
0,
" . ($cs_deal == 3 ? "NOW()" : "NULL") . "
)";
$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);
}
}
echo "";
}
?>