Explorar el Código

fleat: customer add relationship

igb hace 3 días
padre
commit
b179089165
Se han modificado 1 ficheros con 578 adiciones y 0 borrados
  1. 578 0
      customerEdit.php

+ 578 - 0
customerEdit.php

@@ -185,6 +185,272 @@ if (!empty($id) && is_numeric($id)) {
             // Update available options in other selects
             updateAvailableMethodTypes(contactIndex);
         });
+        
+        // 客户关系相关JS代码
+        // 显示添加关系弹窗
+        $('#add-relationship-btn').click(function() {
+            $('#relationship-modal-title').text('添加客户关系');
+            $('#relationship_id').val('');
+            $('#related_customer_id').val('');
+            $('#related_customer_search').val('').show();
+            $('#related_customer_selected').hide();
+            $('#relationship_type').val('');
+            $('input[name="relationship_status"][value="1"]').prop('checked', true);
+            $('#relationship_description').val('');
+            
+            $('<div class="modal-backdrop"></div>').appendTo('body');
+            $('#relationship-modal').show();
+        });
+        
+        // 关闭弹窗
+        $('#cancel-relationship-btn').click(function() {
+            $('#relationship-modal').hide();
+            $('.modal-backdrop').remove();
+        });
+        
+        // 编辑关系
+        $(document).on('click', '.edit-relationship-btn', function() {
+            var relationshipId = $(this).data('id');
+            
+            // AJAX获取关系详情
+            $.ajax({
+                url: 'get_relationship.php',
+                type: 'GET',
+                data: {id: relationshipId},
+                dataType: 'json',
+                success: function(data) {
+                    if (data && data.success) {
+                        var relationship = data.relationship;
+                        $('#relationship_id').val(relationship.id);
+                        
+                        // 确定关联的客户(不是当前客户的那一方)
+                        var currentCustomerId = $('#current_customer_id').val();
+                        var relatedCustomerId = relationship.source_customer_id == currentCustomerId 
+                                            ? relationship.target_customer_id 
+                                            : relationship.source_customer_id;
+                        var relatedCustomerName = relationship.source_customer_id == currentCustomerId 
+                                            ? relationship.target_company 
+                                            : relationship.source_company;
+                        var relatedCustomerCode = relationship.source_customer_id == currentCustomerId 
+                                            ? relationship.target_code 
+                                            : relationship.source_code;
+                        
+                        var displayText = relatedCustomerName;
+                        if (relatedCustomerCode) {
+                            displayText = relatedCustomerCode + ' - ' + relatedCustomerName;
+                        }
+                        
+                        $('#related_customer_id').val(relatedCustomerId);
+                        $('#related_customer_search').hide();
+                        $('#related_customer_selected')
+                            .text(displayText)
+                            .append('<span class="customer-clear-btn" title="清除选择">X</span>')
+                            .show()
+                            .attr('title', displayText);
+                        
+                        $('#relationship_type').val(relationship.relationship_type);
+                        $('input[name="relationship_status"][value="' + relationship.relationship_status + '"]').prop('checked', true);
+                        $('#relationship_description').val(relationship.description);
+                        
+                        $('#relationship-modal-title').text('编辑客户关系');
+                        $('<div class="modal-backdrop"></div>').appendTo('body');
+                        $('#relationship-modal').show();
+                    } else {
+                        alert('获取关系信息失败');
+                    }
+                },
+                error: function() {
+                    alert('获取关系信息失败,请稍后重试');
+                }
+            });
+        });
+        
+        // 删除关系
+        $(document).on('click', '.delete-relationship-btn', function() {
+            if (confirm('确定要删除此客户关系吗?')) {
+                var relationshipId = $(this).data('id');
+                
+                $.ajax({
+                    url: 'delete_relationship.php',
+                    type: 'POST',
+                    data: {id: relationshipId},
+                    dataType: 'json',
+                    success: function(data) {
+                        if (data && data.success) {
+                            alert('删除成功');
+                            location.reload();
+                        } else {
+                            alert(data.message || '删除失败');
+                        }
+                    },
+                    error: function() {
+                        alert('操作失败,请稍后重试');
+                    }
+                });
+            }
+        });
+        
+        // 保存关系
+        $('#save-relationship-btn').click(function() {
+            var relationshipId = $('#relationship_id').val();
+            var currentCustomerId = $('#current_customer_id').val();
+            var relatedCustomerId = $('#related_customer_id').val();
+            var relationshipType = $('#relationship_type').val();
+            var relationshipStatus = $('input[name="relationship_status"]:checked').val();
+            var description = $('#relationship_description').val();
+            
+            // 验证
+            if (!relatedCustomerId) {
+                alert('请选择关联客户');
+                return;
+            }
+            
+            if (!relationshipType) {
+                alert('请选择关系类型');
+                return;
+            }
+            
+            var data = {
+                id: relationshipId,
+                source_customer_id: currentCustomerId,
+                target_customer_id: relatedCustomerId,
+                relationship_type: relationshipType,
+                relationship_status: relationshipStatus,
+                description: description
+            };
+            
+            $.ajax({
+                url: 'save_relationship.php',
+                type: 'POST',
+                data: data,
+                dataType: 'json',
+                success: function(response) {
+                    if (response && response.success) {
+                        alert('保存成功');
+                        $('#relationship-modal').hide();
+                        $('.modal-backdrop').remove();
+                        location.reload();
+                    } else {
+                        alert(response.message || '保存失败');
+                    }
+                },
+                error: function() {
+                    alert('操作失败,请稍后重试');
+                }
+            });
+        });
+        
+        // 客户搜索功能
+        var customerSearchTimeout = null;
+        var customerIsComposing = false;
+        
+        // 监听输入法组合事件
+        $(document).on('compositionstart', '#related_customer_search', function() {
+            customerIsComposing = true;
+        });
+
+        $(document).on('compositionend', '#related_customer_search', function() {
+            customerIsComposing = false;
+            $(this).trigger('input'); // 手动触发一次input事件
+        });
+        
+        // 客户搜索输入
+        $(document).on('input', '#related_customer_search', function() {
+            // 如果是输入法正在组合中文,不处理
+            if (customerIsComposing) return;
+            
+            var searchTerm = $(this).val().trim();
+            var customerDropdown = $('#related_customer_dropdown');
+            var currentCustomerId = $('#current_customer_id').val();
+            
+            // 清除之前的超时
+            clearTimeout(customerSearchTimeout);
+            
+            // 隐藏之前的结果
+            customerDropdown.hide();
+            
+            // 清除之前的选择
+            $('#related_customer_id').val('');
+            $('#related_customer_selected').hide();
+            
+            // 如果搜索词少于1个字符,不执行搜索
+            if (searchTerm.length < 1) {
+                return;
+            }
+            
+            // 设置一个300毫秒的超时,以减少请求数量
+            customerSearchTimeout = setTimeout(function() {
+                $.ajax({
+                    url: 'get_customer_search.php',
+                    type: 'GET',
+                    data: {
+                        search: searchTerm,
+                        exclude_id: currentCustomerId // 排除当前客户
+                    },
+                    dataType: 'json',
+                    success: function(data) {
+                        customerDropdown.empty();
+                        
+                        if (data && data.customers && data.customers.length > 0) {
+                            $.each(data.customers, function(i, customer) {
+                                var displayText = customer.cs_company;
+                                if (customer.cs_code) {
+                                    displayText = customer.cs_code + ' - ' + displayText;
+                                }
+                                
+                                var item = $('<div class="customer-item"></div>')
+                                    .attr('data-id', customer.id)
+                                    .attr('data-display', displayText)
+                                    .text(displayText);
+                                
+                                customerDropdown.append(item);
+                            });
+                            customerDropdown.show();
+                        }
+                    },
+                    error: function() {
+                        console.log('搜索客户失败,请重试');
+                    }
+                });
+            }, 300);
+        });
+        
+        // 点击选择客户
+        $(document).on('click', '.customer-item', function() {
+            var customerId = $(this).data('id');
+            var displayText = $(this).data('display');
+            
+            // 设置选中的客户ID和显示
+            $('#related_customer_id').val(customerId);
+            $('#related_customer_search').hide();
+            $('#related_customer_selected')
+                .text(displayText)
+                .append('<span class="customer-clear-btn" title="清除选择">X</span>')
+                .show()
+                .attr('title', displayText);
+            
+            // 隐藏下拉菜单
+            $('#related_customer_dropdown').hide();
+        });
+        
+        // 点击X按钮清除选择的客户
+        $(document).on('click', '.customer-clear-btn', function(e) {
+            e.stopPropagation();
+            
+            // 显示搜索框,隐藏已选信息
+            $('#related_customer_search').val('').show();
+            $('#related_customer_selected').hide();
+            
+            // 清空客户ID
+            $('#related_customer_id').val('');
+        });
+        
+        // 点击其他地方隐藏下拉列表
+        $(document).on('click', function(e) {
+            if (!$(e.target).closest('.customer-search-container').length) {
+                $('.customer-dropdown').hide();
+            }
+        });
     });
 
     // Update method fields based on selection
@@ -526,6 +792,184 @@ if (!empty($id) && is_numeric($id)) {
         .contact-table td, .contact-table th {
             padding: 4px 6px;
         }
+        
+        /* 客户关系样式 */
+        .relationships-table {
+            width: 100%;
+            border-collapse: collapse;
+            margin-bottom: 10px;
+        }
+        .relationships-table th, .relationships-table td {
+            border: 1px solid #ddd;
+            padding: 8px;
+            text-align: left;
+        }
+        .relationships-table th {
+            background-color: #f2f2f2;
+        }
+        .btn-add-relationship {
+            background-color: #4CAF50;
+            color: white;
+            border: none;
+            padding: 6px 12px;
+            margin-bottom: 10px;
+            cursor: pointer;
+        }
+        .edit-relationship-btn, .delete-relationship-btn {
+            margin-right: 5px;
+            cursor: pointer;
+            padding: 3px 8px;
+            border: none;
+        }
+        .edit-relationship-btn {
+            background-color: #2196F3;
+            color: white;
+        }
+        .delete-relationship-btn {
+            background-color: #f44336;
+            color: white;
+        }
+        #relationship-modal {
+            position: fixed;
+            top: 50%;
+            left: 50%;
+            transform: translate(-50%, -50%);
+            background-color: white;
+            padding: 20px;
+            border: 1px solid #ddd;
+            box-shadow: 0 0 10px rgba(0,0,0,0.3);
+            z-index: 1000;
+            width: 600px;
+            max-width: 90%;
+        }
+        /* 弹窗内表格样式修复 */
+        #relationship-modal table.table1 {
+            width: 100%;
+            border-collapse: collapse;
+            table-layout: fixed;
+        }
+        #relationship-modal table.table1 th {
+            width: 120px;
+            text-align: right;
+            padding-right: 10px;
+            vertical-align: top;
+            white-space: nowrap;
+        }
+        #relationship-modal table.table1 td {
+            padding: 6px 8px;
+        }
+        #relationship-modal .txt1 {
+            width: 90%;
+        }
+        #relationship-modal select.txt1 {
+            max-width: 300px;
+        }
+        .modal-backdrop {
+            position: fixed;
+            top: 0;
+            left: 0;
+            width: 100%;
+            height: 100%;
+            background-color: rgba(0,0,0,0.5);
+            z-index: 999;
+        }
+        .modal-actions {
+            text-align: right;
+            margin-top: 15px;
+        }
+        .modal-actions button {
+            padding: 5px 15px;
+            margin-left: 10px;
+            cursor: pointer;
+        }
+        /* 弹窗按钮样式 */
+        .modal-actions .btn1 {
+            border: 1px solid #ccc;
+            background: #f5f5f5;
+            border-radius: 3px;
+            color: #333;
+            font-size: 12px;
+            height: 26px;
+            padding: 0 15px;
+            transition: all 0.3s;
+        }
+        .modal-actions .btn1:hover {
+            background: #e6e6e6;
+            border-color: #adadad;
+        }
+        #save-relationship-btn {
+            background-color: #428bca;
+            color: white;
+            border-color: #357ebd;
+        }
+        #save-relationship-btn:hover {
+            background-color: #3071a9;
+            border-color: #285e8e;
+        }
+        .customer-search-container {
+            display: flex;
+            align-items: center;
+            margin-bottom: 10px;
+            position: relative;
+            width: 80%;
+        }
+        .customer-dropdown {
+            display: none;
+            position: absolute;
+            background: white;
+            border: 1px solid #ccc;
+            max-height: 200px;
+            overflow-y: auto;
+            width: 100%;
+            z-index: 1000;
+            box-shadow: 0 3px 8px rgba(0,0,0,0.25);
+            border-radius: 0 0 4px 4px;
+            top: 100%;
+            left: 0;
+        }
+        .customer-item {
+            padding: 10px 12px;
+            cursor: pointer;
+            border-bottom: 1px solid #eee;
+            transition: background-color 0.2s;
+        }
+        .customer-item:hover {
+            background-color: #f0f0f0;
+        }
+        .selected-customer-info {
+            font-weight: bold;
+            padding: 8px 10px;
+            border: 1px solid #ddd;
+            background-color: #f9f9f9;
+            display: none;
+            width: 100%;
+            box-sizing: border-box;
+            word-break: break-all;
+            overflow: hidden;
+            text-overflow: ellipsis;
+            white-space: normal;
+            min-height: 38px;
+            position: relative;
+            padding-right: 25px;
+        }
+        .customer-clear-btn {
+            position: absolute;
+            right: 5px;
+            top: 8px;
+            color: #e74c3c;
+            font-weight: bold;
+            cursor: pointer;
+            width: 16px;
+            height: 16px;
+            text-align: center;
+            line-height: 16px;
+            background: #f5f5f5;
+            border-radius: 50%;
+        }
+        .customer-clear-btn:hover {
+            background: #e74c3c;
+            color: white;
+        }
     </style>
 </head>
 <body class="clear">
@@ -548,6 +992,140 @@ if (!empty($id) && is_numeric($id)) {
                     <th width="8%">公司名称</th>
                     <td><input type="text" id="cs_company" name="cs_company" value="<?= htmlspecialcharsFix($customer['cs_company']) ?>" class="txt1" /></td>
                 </tr>
+                <!-- 客户关系管理部分 -->
+                <tr>
+                    <th width="8%" valign="top">客户关系</th>
+                    <td>
+                        <div id="relationships-container">
+                            <?php
+                            // 获取当前客户的所有关系
+                            $relationshipSql = "SELECT cr.*, 
+                                                c1.cs_company as source_company, c1.cs_code as source_code,
+                                                c2.cs_company as target_company, c2.cs_code as target_code
+                                                FROM customer_relationship cr
+                                                LEFT JOIN customer c1 ON cr.source_customer_id = c1.id
+                                                LEFT JOIN customer c2 ON cr.target_customer_id = c2.id
+                                                WHERE cr.source_customer_id = ? OR cr.target_customer_id = ?";
+                            $relationshipStmt = $conn->prepare($relationshipSql);
+                            $relationshipStmt->bind_param("ii", $id, $id);
+                            $relationshipStmt->execute();
+                            $relationshipResult = $relationshipStmt->get_result();
+                            
+                            $hasRelationships = false;
+                            if ($relationshipResult->num_rows > 0) {
+                                $hasRelationships = true;
+                                echo '<table width="100%" border="0" cellpadding="3" cellspacing="1" class="relationships-table">';
+                                echo '<tr><th>关系类型</th><th>相关客户</th><th>关系状态</th><th>关系描述</th><th>操作</th></tr>';
+                                
+                                while ($relationship = $relationshipResult->fetch_assoc()) {
+                                    $relationType = '';
+                                    switch ($relationship['relationship_type']) {
+                                        case 1: $relationType = '母公司-子公司'; break;
+                                        case 2: $relationType = '供应商-客户'; break;
+                                        case 3: $relationType = '合作伙伴'; break;
+                                        case 4: $relationType = '竞争对手'; break;
+                                        case 5: $relationType = '推荐人'; break;
+                                        case 6: $relationType = '其他'; break;
+                                    }
+                                    
+                                    $relationStatus = $relationship['relationship_status'] == 1 ? '启用' : '停用';
+                                    
+                                    // 确定关联的客户(不是当前客户的那一方)
+                                    $relatedCustomerId = $relationship['source_customer_id'] == $id 
+                                                        ? $relationship['target_customer_id'] 
+                                                        : $relationship['source_customer_id'];
+                                    $relatedCustomerName = $relationship['source_customer_id'] == $id 
+                                                        ? textUncode($relationship['target_company']) 
+                                                        : textUncode($relationship['source_company']);
+                                    $relatedCustomerCode = $relationship['source_customer_id'] == $id 
+                                                        ? textUncode($relationship['target_code']) 
+                                                        : textUncode($relationship['source_code']);
+                                    
+                                    $displayText = $relatedCustomerName;
+                                    if ($relatedCustomerCode) {
+                                        $displayText = $relatedCustomerCode . ' - ' . $relatedCustomerName;
+                                    }
+                                    
+                                    echo '<tr>';
+                                    echo '<td>' . $relationType . '</td>';
+                                    echo '<td>' . htmlspecialchars($displayText) . '</td>';
+                                    echo '<td>' . $relationStatus . '</td>';
+                                    echo '<td>' . htmlspecialchars(textUncode($relationship['description'])) . '</td>';
+                                    echo '<td>';
+                                    echo '<button type="button" class="edit-relationship-btn" data-id="' . $relationship['id'] . '">编辑</button>';
+                                    echo '<button type="button" class="delete-relationship-btn" data-id="' . $relationship['id'] . '">删除</button>';
+                                    echo '</td>';
+                                    echo '</tr>';
+                                }
+                                
+                                echo '</table>';
+                            } else {
+                                echo '<p>暂无关联客户关系。</p>';
+                            }
+                            ?>
+                            <button type="button" id="add-relationship-btn" class="btn-add-relationship">添加客户关系</button>
+                        </div>
+                        
+                        <!-- 添加/编辑关系的弹出框 -->
+                        <div id="relationship-modal" style="display: none;">
+                            <h3 id="relationship-modal-title" style="margin-top: 0; margin-bottom: 15px;">添加客户关系</h3>
+                            <input type="hidden" id="relationship_id" value="">
+                            <input type="hidden" id="current_customer_id" value="<?= $id ?>">
+                            
+                            <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1 relationship-modal-table">
+                                <tr>
+                                    <th width="120">关联客户</th>
+                                    <td>
+                                        <div class="customer-search-container" style="width: 90%; position: relative;">
+                                            <input type="text" id="related_customer_search" class="customer-search txt1" placeholder="输入客户编码或名称搜索..." value="" style="width: 100%; box-sizing: border-box;" />
+                                            <div id="related_customer_selected" class="selected-customer-info"></div>
+                                            <div id="related_customer_dropdown" class="customer-dropdown"></div>
+                                        </div>
+                                        <input type="hidden" id="related_customer_id" value="" />
+                                    </td>
+                                </tr>
+                                <tr>
+                                    <th>关系类型</th>
+                                    <td>
+                                        <select id="relationship_type" class="txt1" style="width: auto; min-width: 200px;">
+                                            <option value="">请选择关系类型</option>
+                                            <option value="1">母公司-子公司</option>
+                                            <option value="2">供应商-客户</option>
+                                            <option value="3">合作伙伴</option>
+                                            <option value="4">竞争对手</option>
+                                            <option value="5">推荐人</option>
+                                            <option value="6">其他</option>
+                                        </select>
+                                    </td>
+                                </tr>
+                                <tr>
+                                    <th>关系状态</th>
+                                    <td>
+                                        <label style="margin-right: 15px;">
+                                            <input type="radio" name="relationship_status" value="1" checked>
+                                            启用
+                                        </label>
+                                        <label>
+                                            <input type="radio" name="relationship_status" value="0">
+                                            停用
+                                        </label>
+                                    </td>
+                                </tr>
+                                <tr>
+                                    <th>关系描述</th>
+                                    <td>
+                                        <textarea id="relationship_description" class="txt1" style="width: 90%; height: 100px; resize: vertical;"></textarea>
+                                    </td>
+                                </tr>
+                            </table>
+                            
+                            <div class="modal-actions">
+                                <button type="button" id="save-relationship-btn" class="btn1">保存</button>
+                                <button type="button" id="cancel-relationship-btn" class="btn1">取消</button>
+                            </div>
+                        </div>
+                    </td>
+                </tr>
                 <tr>
                     <th width="8%">地区/国家</th>
                     <td>