Browse Source

fleat: order update

igb 4 weeks ago
parent
commit
2e2c8c30f1
2 changed files with 299 additions and 210 deletions
  1. 136 93
      order_add.php
  2. 163 117
      order_edit.php

+ 136 - 93
order_add.php

@@ -130,52 +130,26 @@ $hrefstr = "keys=$keys&Page=$page";
             <tr>
                 <th width="8%" valign="top">产品列表</th>
                 <td>
-                    <button type="button" id="add-product-btn" class="add-product-btn">添加产品</button>
                     <div id="product-container">
-                        <div class="product-row" data-index="0">
-                            <span class="delete-product">×</span>
-                            <div>
-                                <label>产品:</label>
-                                <div style="display: inline-block; width: 60%;" class="productinput">
-                                    <input type="text" class="product-search fastsearch" placeholder="输入产品名称搜索..." style="width: 100%;">
-                                    <div class="productlist"><ul></ul></div>
-                                    <input type="hidden" name="items[0][product_id]" class="product-id-input" value="">
-                                    <div class="selected-product-info" style="margin-top: 5px; font-weight: bold;"></div>
-                                </div>
-                            </div>
-                            <div style="margin-top: 5px;">
-                                <label>数量:</label>
-                                <input type="number" name="items[0][quantity]" value="1" min="1" class="quantity-input" onchange="calculateItemTotal(this)">
-
-                                <label style="margin-left: 10px;">单位:</label>
-                                <input type="text" name="items[0][unit]" value="" class="unit-input">
-
-                                <label style="margin-left: 10px;">单价:</label>
-                                <input type="number" step="0.01" name="items[0][unit_price]" value="0.00" class="price-input" onchange="calculateItemTotal(this)">
-                            </div>
-                            <div style="margin-top: 5px;">
-                                <label>总价:</label>
-                                <input type="number" step="0.01" name="items[0][total_price]" value="0.00" class="total-price-input" readonly>
-                                
-                                <label style="margin-left: 10px;">备注:</label>
-                                <input type="text" name="items[0][notes]" value="" style="width: 60%;">
-                            </div>
+                        <!-- 产品行将通过搜索添加 -->
+                        <div id="no-products-message" style="padding: 20px; text-align: center; color: #777; background-color: #f9f9f9; border: 1px dashed #ddd; display: block;">
+                            请使用下方的产品搜索框添加产品
                         </div>
                     </div>
                     <div class="total-section">
                         <div>
-                            <label>订单小计:</label>
-                            <span id="subtotal">0.00</span>
-                            <input type="hidden" name="subtotal" id="subtotal-input" value="0.00">
-                        </div>
-                        <div style="margin-top: 5px;">
-                            <label>订单折扣:</label>
-                            <input type="number" step="0.01" name="discount_amount" id="order-discount" value="0.00" onchange="calculateOrderTotal()">
-                        </div>
-                        <div style="margin-top: 5px;">
                             <label>订单总额:</label>
                             <span id="total-amount">0.00</span>
                             <input type="hidden" name="total_amount" id="total-amount-input" value="0.00">
+                            <input type="hidden" name="subtotal" id="subtotal-input" value="0.00">
+                        </div>
+                    </div>
+                    
+                    <div style="margin-top: 15px; border-top: 1px dashed #ccc; padding-top: 15px;">
+                        <div class="productinput" style="position: relative; width: 60%;">
+                            <label>产品搜索:</label>
+                            <input type="text" id="product-search-single" class="fastsearch" placeholder="输入产品名称搜索并添加..." style="width: 100%;">
+                            <div id="product-list-single" class="productlist" style="width: 100%;"><ul></ul></div>
                         </div>
                     </div>
                 </td>
@@ -204,19 +178,23 @@ $hrefstr = "keys=$keys&Page=$page";
             // 初始化计算
             calculateOrderTotal();
 
-            // 添加产品行
-            $('#add-product-btn').click(function() {
-                addProductRow();
-            });
-
+            // 移除第一个默认产品行(如果不需要的话)
+            if ($('.product-row').length > 0) {
+                $('.product-row').first().remove();
+            }
+            
             // 删除产品行
             $(document).on('click', '.delete-product', function() {
                 if ($('.product-row').length > 1) {
                     $(this).closest('.product-row').remove();
                     reindexProductRows();
                     calculateOrderTotal();
+                    updateNoProductsMessage();
                 } else {
-                    alert('订单至少需要一个产品');
+                    // 如果只剩最后一个产品行,则清空它而不是删除
+                    $(this).closest('.product-row').remove();
+                    updateNoProductsMessage();
+                    calculateOrderTotal(); // 确保在删除最后一个产品时也更新总额
                 }
             });
             
@@ -285,16 +263,15 @@ $hrefstr = "keys=$keys&Page=$page";
                 loadCustomerContacts(customerId);
             });
 
-            // 实时产品搜索功能 - 使用防抖动以减少请求数量
-            var searchTimeout = null;
-            $(document).on('keyup', '.product-search', function() {
+            // 单一产品搜索功能 - 使用防抖动以减少请求数量
+            var productSearchTimeout = null;
+            $(document).on('keyup', '#product-search-single', function() {
                 var $this = $(this);
-                var row = $this.closest('.product-row');
                 var searchTerm = $this.val().trim();
-                var productList = row.find('.productlist');
+                var productList = $('#product-list-single');
                 
                 // 清除之前的超时
-                clearTimeout(searchTimeout);
+                clearTimeout(productSearchTimeout);
                 
                 // 隐藏之前的结果
                 productList.hide();
@@ -305,14 +282,13 @@ $hrefstr = "keys=$keys&Page=$page";
                 }
                 
                 // 设置一个300毫秒的超时,以减少请求数量
-                searchTimeout = setTimeout(function() {
+                productSearchTimeout = setTimeout(function() {
                     $.ajax({
                         url: 'get_product_info.php',
                         type: 'GET',
                         data: {search: searchTerm},
                         dataType: 'json',
                         success: function(data) {
-                            var productList = row.find('.productlist');
                             var ul = productList.find('ul');
                             ul.empty();
                             
@@ -333,8 +309,8 @@ $hrefstr = "keys=$keys&Page=$page";
                 }, 300);
             });
             
-            // 点击选择产品
-            $(document).on('click', '.productlist li', function() {
+            // 点击选择产品 - 从单一搜索框
+            $(document).on('click', '#product-list-single li', function() {
                 var $this = $(this);
                 var productId = $this.data('id');
                 var productName = $this.text(); // 这会包含分类名,需要清理
@@ -345,28 +321,34 @@ $hrefstr = "keys=$keys&Page=$page";
                     productName = productName.replace(categoryTag, '').trim();
                 }
                 
-                var row = $this.closest('.product-row');
-                
-                // 设置选中的产品ID和名称
-                row.find('.product-id-input').val(productId);
-                row.find('.product-search').val(productName);
+                // 检查产品是否已经添加
+                var isDuplicate = false;
+                $('.product-id-input').each(function() {
+                    if ($(this).val() == productId) {
+                        isDuplicate = true;
+                        return false; // 跳出循环
+                    }
+                });
                 
-                // 显示包含分类的完整产品信息
-                var displayName = productName;
-                if (categoryTag) {
-                    displayName += ' <span class="category-tag">' + categoryTag + '</span>';
+                if (isDuplicate) {
+                    alert('该产品已添加,不能重复添加');
+                    $('#product-search-single').val('');
+                    $('#product-list-single').hide();
+                    return;
                 }
-                row.find('.selected-product-info').html(displayName);
-                row.find('.productlist').hide();
                 
-                // 获取产品信息
-                getProductInfo(productId, row);
+                // 添加产品行
+                addProductRowWithProduct(productId, productName, categoryTag);
+                
+                // 清空搜索框并隐藏结果
+                $('#product-search-single').val('');
+                $('#product-list-single').hide();
             });
             
             // 点击其他地方隐藏下拉列表
             $(document).on('click', function(e) {
-                if (!$(e.target).closest('.productinput').length) {
-                    $('.productlist').hide();
+                if (!$(e.target).closest('#product-search-single').length) {
+                    $('#product-list-single').hide();
                 }
                 if (!$(e.target).closest('.customerinput').length) {
                     $('.customerlist').hide();
@@ -374,15 +356,13 @@ $hrefstr = "keys=$keys&Page=$page";
             });
         });
 
-        function addProductRow() {
+        function addEmptyProductRow() {
             var html = `
             <div class="product-row" data-index="${productIndex}">
                 <span class="delete-product">×</span>
                 <div>
                     <label>产品:</label>
-                    <div style="display: inline-block; width: 60%;" class="productinput">
-                        <input type="text" class="product-search fastsearch" placeholder="输入产品名称搜索..." style="width: 100%;">
-                        <div class="productlist"><ul></ul></div>
+                    <div style="display: inline-block; width: 60%;">
                         <input type="hidden" name="items[${productIndex}][product_id]" class="product-id-input" value="">
                         <div class="selected-product-info" style="margin-top: 5px; font-weight: bold;"></div>
                     </div>
@@ -395,20 +375,43 @@ $hrefstr = "keys=$keys&Page=$page";
                     <input type="text" name="items[${productIndex}][unit]" value="" class="unit-input">
 
                     <label style="margin-left: 10px;">单价:</label>
-                    <input type="number" step="0.01" name="items[${productIndex}][unit_price]" value="0.00" class="price-input" onchange="calculateItemTotal(this)">
+                    <input type="number" step="0.01" name="items[${productIndex}][unit_price]" value="" class="price-input" placeholder="请输入单价" onchange="calculateItemTotal(this)">
+                    
+                    <label style="margin-left: 10px;">总价:</label>
+                    <input type="number" step="0.01" name="items[${productIndex}][total_price]" value="0.00" class="total-price-input" readonly>
                 </div>
                 <div style="margin-top: 5px;">
-                    <label>总价:</label>
-                    <input type="number" step="0.01" name="items[${productIndex}][total_price]" value="0.00" class="total-price-input" readonly>
-                    
-                    <label style="margin-left: 10px;">备注:</label>
-                    <input type="text" name="items[${productIndex}][notes]" value="" style="width: 60%;">
+                    <label>备注:</label>
+                    <input type="text" name="items[${productIndex}][notes]" value="" style="width: 90%;">
                 </div>
             </div>
-        `;
+            `;
 
             $('#product-container').append(html);
             productIndex++;
+            return productIndex - 1; // 返回新添加行的索引
+        }
+        
+        function addProductRowWithProduct(productId, productName, categoryTag) {
+            // 添加空行
+            var rowIndex = addEmptyProductRow();
+            var row = $('.product-row[data-index="' + rowIndex + '"]');
+            
+            // 设置产品ID和名称
+            row.find('.product-id-input').val(productId);
+            
+            // 显示包含分类的完整产品信息
+            var displayName = productName;
+            if (categoryTag) {
+                displayName += ' <span class="category-tag">' + categoryTag + '</span>';
+            }
+            row.find('.selected-product-info').html(displayName);
+            
+            // 获取产品信息
+            getProductInfo(productId, row);
+            
+            // 更新无产品消息显示
+            updateNoProductsMessage();
         }
 
         function reindexProductRows() {
@@ -436,7 +439,13 @@ $hrefstr = "keys=$keys&Page=$page";
                 success: function(data) {
                     if (data) {
                         row.find('.unit-input').val(data.unit);
-                        row.find('.price-input').val(data.price);
+
+                        // 只有当数据库中有价格信息时才设置价格
+                        if (data.price && data.price !== '0' && data.price !== '0.00') {
+                            //默认不设置单价
+                            //row.find('.price-input').val(data.price);
+
+                        }
                         calculateItemTotal(row.find('.price-input')[0]);
                     }
                 }
@@ -446,7 +455,9 @@ $hrefstr = "keys=$keys&Page=$page";
         function calculateItemTotal(element) {
             var row = $(element).closest('.product-row');
             var quantity = parseInt(row.find('.quantity-input').val()) || 0;
-            var price = parseFloat(row.find('.price-input').val()) || 0;
+            var priceInput = row.find('.price-input');
+            var priceValue = priceInput.val().trim();
+            var price = (priceValue === '') ? 0 : (parseFloat(priceValue) || 0);
 
             var total = quantity * price;
             if (total < 0) total = 0;
@@ -457,20 +468,15 @@ $hrefstr = "keys=$keys&Page=$page";
         }
 
         function calculateOrderTotal() {
-            var subtotal = 0;
+            var total = 0;
             $('.total-price-input').each(function() {
-                subtotal += parseFloat($(this).val()) || 0;
+                total += parseFloat($(this).val()) || 0;
             });
 
-            var orderDiscount = parseFloat($('#order-discount').val()) || 0;
-            var total = subtotal - orderDiscount;
-
-            if (total < 0) total = 0;
-
-            $('#subtotal').text(subtotal.toFixed(2));
-            $('#subtotal-input').val(subtotal.toFixed(2));
+            // 更新总额和隐藏的小计字段
             $('#total-amount').text(total.toFixed(2));
             $('#total-amount-input').val(total.toFixed(2));
+            $('#subtotal-input').val(total.toFixed(2)); // 为兼容性保留
         }
 
         function loadCustomerContacts(customerId) {
@@ -499,12 +505,26 @@ $hrefstr = "keys=$keys&Page=$page";
             var orderCode = $('#order_code').val();
             var customerId = $('#customer_id').val();
             var customerName = $('.customer-search').val();
+            var contactId = $('#contact_id').val(); 
             var hasProducts = false;
+            var allPricesValid = true;
+            var firstEmptyPriceField = null;
 
             $('.product-id-input').each(function() {
                 if ($(this).val()) {
                     hasProducts = true;
-                    return false; // break the loop
+                    
+                    // 检查该产品的单价是否已填写
+                    var row = $(this).closest('.product-row');
+                    var priceInput = row.find('.price-input');
+                    var priceValue = priceInput.val().trim();
+                    
+                    if (priceValue === '' || isNaN(parseFloat(priceValue))) {
+                        allPricesValid = false;
+                        if (!firstEmptyPriceField) {
+                            firstEmptyPriceField = priceInput;
+                        }
+                    }
                 }
             });
 
@@ -525,14 +545,37 @@ $hrefstr = "keys=$keys&Page=$page";
                 $('.customer-search').focus();
                 return false;
             }
+            
+            if (!contactId) {
+                alert('请选择联系人');
+                $('#contact_id').focus();
+                return false;
+            }
 
             if (!hasProducts) {
-                alert('请至少选择一个产品');
+                alert('请至少添加一个产品');
+                $('#product-search-single').focus();
+                return false;
+            }
+            
+            if (!allPricesValid) {
+                alert('请为所有产品输入有效的单价');
+                if (firstEmptyPriceField) {
+                    firstEmptyPriceField.focus();
+                }
                 return false;
             }
 
             return true;
         }
+
+        function updateNoProductsMessage() {
+            if ($('.product-row').length > 0) {
+                $('#no-products-message').hide();
+            } else {
+                $('#no-products-message').show();
+            }
+        }
     </script>
 </div>
 </body>

+ 163 - 117
order_edit.php

@@ -28,9 +28,10 @@ if (!empty($id) && is_numeric($id)) {
     }
 
     // 获取订单项信息
-    $sql = "SELECT oi.*, p.ProductName 
+    $sql = "SELECT oi.*, p.ProductName, pc.name as category_name 
             FROM order_items oi 
             LEFT JOIN products p ON oi.product_id = p.id 
+            LEFT JOIN product_categories pc ON p.category_id = pc.id
             WHERE oi.order_id = $id";
 
     $itemsResult = mysqli_query($conn, $sql);
@@ -139,6 +140,12 @@ if (!empty($id) && is_numeric($id)) {
             font-style: italic;
             margin-left: 5px;
         }
+        .selected-product-info .category-tag {
+            color: #777;
+            font-size: 12px;
+            font-style: italic;
+            margin-left: 5px;
+        }
     </style>
 </head>
 <body>
@@ -193,18 +200,20 @@ if (!empty($id) && is_numeric($id)) {
             <tr>
                 <th width="8%" valign="top">产品列表</th>
                 <td>
-                    <button type="button" id="add-product-btn" class="add-product-btn">添加产品</button>
                     <div id="product-container">
                         <?php foreach ($orderItems as $index => $item): ?>
                             <div class="product-row" data-index="<?= $index ?>">
                                 <span class="delete-product">×</span>
                                 <div>
                                     <label>产品:</label>
-                                    <div style="display: inline-block; width: 60%;" class="productinput">
-                                        <input type="text" class="product-search fastsearch" placeholder="输入产品名称搜索..." style="width: 100%;" value="<?= htmlspecialcharsFix($item['ProductName'] ?? '') ?>">
-                                        <div class="productlist"><ul></ul></div>
+                                    <div style="display: inline-block; width: 60%;">
                                         <input type="hidden" name="items[<?= $index ?>][product_id]" class="product-id-input" value="<?= $item['product_id'] ?>">
-                                        <div class="selected-product-info" style="margin-top: 5px; font-weight: bold;"><?= htmlspecialcharsFix($item['ProductName'] ?? '') ?></div>
+                                        <div class="selected-product-info" style="margin-top: 5px; font-weight: bold;">
+                                            <?= htmlspecialcharsFix($item['ProductName'] ?? '') ?>
+                                            <?php if (!empty($item['category_name'])): ?>
+                                            <span class="category-tag">(<?= htmlspecialcharsFix($item['category_name']) ?>)</span>
+                                            <?php endif; ?>
+                                        </div>
                                     </div>
                                 </div>
                                 <div style="margin-top: 5px;">
@@ -215,39 +224,43 @@ if (!empty($id) && is_numeric($id)) {
                                     <input type="text" name="items[<?= $index ?>][unit]" value="<?= htmlspecialcharsFix($item['unit']) ?>" class="unit-input">
 
                                     <label style="margin-left: 10px;">单价:</label>
-                                    <input type="number" step="0.01" name="items[<?= $index ?>][unit_price]" value="<?= $item['unit_price'] ?>" class="price-input" onchange="calculateItemTotal(this)">
-                                </div>
-                                <div style="margin-top: 5px;">
-                                    <label>折扣金额:</label>
-                                    <input type="number" step="0.01" name="items[<?= $index ?>][discount_amount]" value="<?= $item['discount_amount'] ?>" class="discount-amount-input" onchange="calculateItemTotal(this)">
-
-                                    <label style="margin-left: 10px;">折扣率:</label>
-                                    <input type="number" step="0.01" name="items[<?= $index ?>][discount_percent]" value="<?= $item['discount_percent'] ?>" class="discount-percent-input" max="100" min="0">%
-
+                                    <input type="number" step="0.01" name="items[<?= $index ?>][unit_price]" value="<?= $item['unit_price'] ?>" class="price-input" placeholder="请输入单价" onchange="calculateItemTotal(this)">
+                                    
                                     <label style="margin-left: 10px;">总价:</label>
                                     <input type="number" step="0.01" name="items[<?= $index ?>][total_price]" value="<?= $item['total_price'] ?>" class="total-price-input" readonly>
                                 </div>
                                 <div style="margin-top: 5px;">
                                     <label>备注:</label>
-                                    <input type="text" name="items[<?= $index ?>][notes]" value="<?= htmlspecialcharsFix($item['notes']) ?>" style="width: 80%;">
+                                    <input type="text" name="items[<?= $index ?>][notes]" value="<?= htmlspecialcharsFix($item['notes']) ?>" style="width: 90%;">
+                                    
+                                    <!-- 保留隐藏的折扣字段以便后端兼容 -->
+                                    <input type="hidden" name="items[<?= $index ?>][discount_amount]" value="0" class="discount-amount-input">
+                                    <input type="hidden" name="items[<?= $index ?>][discount_percent]" value="0" class="discount-percent-input">
                                 </div>
                             </div>
                         <?php endforeach; ?>
+                        <!-- 产品行将通过搜索添加 -->
+                        <?php if (count($orderItems) == 0): ?>
+                        <div id="no-products-message" style="padding: 20px; text-align: center; color: #777; background-color: #f9f9f9; border: 1px dashed #ddd; display: block;">
+                            请使用下方的产品搜索框添加产品
+                        </div>
+                        <?php endif; ?>
                     </div>
                     <div class="total-section">
                         <div>
-                            <label>订单小计:</label>
-                            <span id="subtotal"><?= number_format($order['subtotal'], 2) ?></span>
-                            <input type="hidden" name="subtotal" id="subtotal-input" value="<?= $order['subtotal'] ?>">
-                        </div>
-                        <div style="margin-top: 5px;">
-                            <label>订单折扣:</label>
-                            <input type="number" step="0.01" name="discount_amount" id="order-discount" value="<?= $order['discount_amount'] ?>" onchange="calculateOrderTotal()">
-                        </div>
-                        <div style="margin-top: 5px;">
                             <label>订单总额:</label>
                             <span id="total-amount"><?= number_format($order['total_amount'], 2) ?></span>
                             <input type="hidden" name="total_amount" id="total-amount-input" value="<?= $order['total_amount'] ?>">
+                            <input type="hidden" name="subtotal" id="subtotal-input" value="<?= $order['subtotal'] ?>">
+                            <input type="hidden" name="discount_amount" id="order-discount" value="0">
+                        </div>
+                    </div>
+                    
+                    <div style="margin-top: 15px; border-top: 1px dashed #ccc; padding-top: 15px;">
+                        <div class="productinput" style="position: relative; width: 60%;">
+                            <label>产品搜索:</label>
+                            <input type="text" id="product-search-single" class="fastsearch" placeholder="输入产品名称搜索并添加..." style="width: 100%;">
+                            <div id="product-list-single" class="productlist" style="width: 100%;"><ul></ul></div>
                         </div>
                     </div>
                 </td>
@@ -275,21 +288,25 @@ if (!empty($id) && is_numeric($id)) {
         $(document).ready(function() {
             // 初始化计算
             calculateOrderTotal();
-
-            // 添加产品行
-            $('#add-product-btn').click(function() {
-                addProductRow();
-            });
-
+            
+            // 初始化无产品消息
+            updateNoProductsMessage();
+            
             // 删除产品行
             $(document).on('click', '.delete-product', function() {
                 if ($('.product-row').length > 1) {
                     $(this).closest('.product-row').remove();
                     reindexProductRows();
                     calculateOrderTotal();
+                    updateNoProductsMessage();
                 } else {
-                    alert('订单至少需要一个产品');
+                    // 如果只剩最后一个产品行,则清空它而不是删除
+                    $(this).closest('.product-row').remove();
+                    updateNoProductsMessage();
+                    calculateOrderTotal();
                 }
+
+
             });
 
             // 客户搜索功能
@@ -357,16 +374,15 @@ if (!empty($id) && is_numeric($id)) {
                 loadCustomerContacts(customerId);
             });
             
-            // 实时产品搜索功能 - 使用防抖动以减少请求数量
-            var searchTimeout = null;
-            $(document).on('keyup', '.product-search', function() {
+            // 单一产品搜索功能 - 使用防抖动以减少请求数量
+            var productSearchTimeout = null;
+            $(document).on('keyup', '#product-search-single', function() {
                 var $this = $(this);
-                var row = $this.closest('.product-row');
                 var searchTerm = $this.val().trim();
-                var productList = row.find('.productlist');
+                var productList = $('#product-list-single');
                 
                 // 清除之前的超时
-                clearTimeout(searchTimeout);
+                clearTimeout(productSearchTimeout);
                 
                 // 隐藏之前的结果
                 productList.hide();
@@ -377,14 +393,13 @@ if (!empty($id) && is_numeric($id)) {
                 }
                 
                 // 设置一个300毫秒的超时,以减少请求数量
-                searchTimeout = setTimeout(function() {
+                productSearchTimeout = setTimeout(function() {
                     $.ajax({
                         url: 'get_product_info.php',
                         type: 'GET',
                         data: {search: searchTerm},
                         dataType: 'json',
                         success: function(data) {
-                            var productList = row.find('.productlist');
                             var ul = productList.find('ul');
                             ul.empty();
                             
@@ -405,8 +420,8 @@ if (!empty($id) && is_numeric($id)) {
                 }, 300);
             });
             
-            // 点击选择产品
-            $(document).on('click', '.productlist li', function() {
+            // 点击选择产品 - 从单一搜索框
+            $(document).on('click', '#product-list-single li', function() {
                 var $this = $(this);
                 var productId = $this.data('id');
                 var productName = $this.text(); // 这会包含分类名,需要清理
@@ -417,73 +432,48 @@ if (!empty($id) && is_numeric($id)) {
                     productName = productName.replace(categoryTag, '').trim();
                 }
                 
-                var row = $this.closest('.product-row');
-                
-                // 设置选中的产品ID和名称
-                row.find('.product-id-input').val(productId);
-                row.find('.product-search').val(productName);
+                // 检查产品是否已经添加
+                var isDuplicate = false;
+                $('.product-id-input').each(function() {
+                    if ($(this).val() == productId) {
+                        isDuplicate = true;
+                        return false; // 跳出循环
+                    }
+                });
                 
-                // 显示包含分类的完整产品信息
-                var displayName = productName;
-                if (categoryTag) {
-                    displayName += ' <span class="category-tag">' + categoryTag + '</span>';
+                if (isDuplicate) {
+                    alert('该产品已添加,不能重复添加');
+                    $('#product-search-single').val('');
+                    $('#product-list-single').hide();
+                    return;
                 }
-                row.find('.selected-product-info').html(displayName);
-                row.find('.productlist').hide();
                 
-                // 获取产品信息
-                getProductInfo(productId, row);
+                // 添加产品行
+                addProductRowWithProduct(productId, productName, categoryTag);
+                
+                // 清空搜索框并隐藏结果
+                $('#product-search-single').val('');
+                $('#product-list-single').hide();
             });
             
             // 点击其他地方隐藏下拉列表
             $(document).on('click', function(e) {
-                if (!$(e.target).closest('.productinput').length) {
-                    $('.productlist').hide();
+                if (!$(e.target).closest('#product-search-single').length) {
+                    $('#product-list-single').hide();
                 }
                 if (!$(e.target).closest('.customerinput').length) {
                     $('.customerlist').hide();
                 }
             });
-
-            // 更新折扣率和折扣金额之间的关系
-            $(document).on('change', '.discount-percent-input', function() {
-                var row = $(this).closest('.product-row');
-                var percent = parseFloat($(this).val()) || 0;
-                var quantity = parseInt(row.find('.quantity-input').val()) || 0;
-                var price = parseFloat(row.find('.price-input').val()) || 0;
-                var subtotal = quantity * price;
-
-                var discountAmount = (subtotal * percent / 100).toFixed(2);
-                row.find('.discount-amount-input').val(discountAmount);
-
-                calculateItemTotal(this);
-            });
-
-            $(document).on('change', '.discount-amount-input', function() {
-                var row = $(this).closest('.product-row');
-                var amount = parseFloat($(this).val()) || 0;
-                var quantity = parseInt(row.find('.quantity-input').val()) || 0;
-                var price = parseFloat(row.find('.price-input').val()) || 0;
-                var subtotal = quantity * price;
-
-                if (subtotal > 0) {
-                    var percent = ((amount / subtotal) * 100).toFixed(2);
-                    row.find('.discount-percent-input').val(percent);
-                }
-
-                calculateItemTotal(this);
-            });
         });
 
-        function addProductRow() {
+        function addEmptyProductRow() {
             var html = `
             <div class="product-row" data-index="${productIndex}">
                 <span class="delete-product">×</span>
                 <div>
                     <label>产品:</label>
-                    <div style="display: inline-block; width: 60%;" class="productinput">
-                        <input type="text" class="product-search fastsearch" placeholder="输入产品名称搜索..." style="width: 100%;">
-                        <div class="productlist"><ul></ul></div>
+                    <div style="display: inline-block; width: 60%;">
                         <input type="hidden" name="items[${productIndex}][product_id]" class="product-id-input" value="">
                         <div class="selected-product-info" style="margin-top: 5px; font-weight: bold;"></div>
                     </div>
@@ -496,27 +486,47 @@ if (!empty($id) && is_numeric($id)) {
                     <input type="text" name="items[${productIndex}][unit]" value="" class="unit-input">
 
                     <label style="margin-left: 10px;">单价:</label>
-                    <input type="number" step="0.01" name="items[${productIndex}][unit_price]" value="0.00" class="price-input" onchange="calculateItemTotal(this)">
-                </div>
-                <div style="margin-top: 5px;">
-                    <label>折扣金额:</label>
-                    <input type="number" step="0.01" name="items[${productIndex}][discount_amount]" value="0.00" class="discount-amount-input" onchange="calculateItemTotal(this)">
-
-                    <label style="margin-left: 10px;">折扣率:</label>
-                    <input type="number" step="0.01" name="items[${productIndex}][discount_percent]" value="0" class="discount-percent-input" max="100" min="0">%
-
+                    <input type="number" step="0.01" name="items[${productIndex}][unit_price]" value="" class="price-input" placeholder="请输入单价" onchange="calculateItemTotal(this)">
+                    
                     <label style="margin-left: 10px;">总价:</label>
                     <input type="number" step="0.01" name="items[${productIndex}][total_price]" value="0.00" class="total-price-input" readonly>
                 </div>
                 <div style="margin-top: 5px;">
                     <label>备注:</label>
-                    <input type="text" name="items[${productIndex}][notes]" value="" style="width: 80%;">
+                    <input type="text" name="items[${productIndex}][notes]" value="" style="width: 90%;">
+                    
+                    <!-- 保留隐藏的折扣字段以便后端兼容 -->
+                    <input type="hidden" name="items[${productIndex}][discount_amount]" value="0" class="discount-amount-input">
+                    <input type="hidden" name="items[${productIndex}][discount_percent]" value="0" class="discount-percent-input">
                 </div>
             </div>
-        `;
+            `;
 
             $('#product-container').append(html);
             productIndex++;
+            return productIndex - 1; // 返回新添加行的索引
+        }
+        
+        function addProductRowWithProduct(productId, productName, categoryTag) {
+            // 添加空行
+            var rowIndex = addEmptyProductRow();
+            var row = $('.product-row[data-index="' + rowIndex + '"]');
+            
+            // 设置产品ID和名称
+            row.find('.product-id-input').val(productId);
+            
+            // 显示包含分类的完整产品信息
+            var displayName = productName;
+            if (categoryTag) {
+                displayName += ' <span class="category-tag">' + categoryTag + '</span>';
+            }
+            row.find('.selected-product-info').html(displayName);
+            
+            // 获取产品信息
+            getProductInfo(productId, row);
+            
+            // 更新无产品消息显示
+            updateNoProductsMessage();
         }
 
         function reindexProductRows() {
@@ -544,7 +554,11 @@ if (!empty($id) && is_numeric($id)) {
                 success: function(data) {
                     if (data) {
                         row.find('.unit-input').val(data.unit);
-                        row.find('.price-input').val(data.price);
+                        // 只有当数据库中有价格信息时才设置价格
+                        if (data.price && data.price !== '0' && data.price !== '0.00') {
+                            //默认不设置单价
+                            //row.find('.price-input').val(data.price);
+                        }
                         calculateItemTotal(row.find('.price-input')[0]);
                     }
                 }
@@ -554,11 +568,11 @@ if (!empty($id) && is_numeric($id)) {
         function calculateItemTotal(element) {
             var row = $(element).closest('.product-row');
             var quantity = parseInt(row.find('.quantity-input').val()) || 0;
-            var price = parseFloat(row.find('.price-input').val()) || 0;
-            var discountAmount = parseFloat(row.find('.discount-amount-input').val()) || 0;
+            var priceInput = row.find('.price-input');
+            var priceValue = priceInput.val().trim();
+            var price = (priceValue === '') ? 0 : (parseFloat(priceValue) || 0);
 
-            var subtotal = quantity * price;
-            var total = subtotal - discountAmount;
+            var total = quantity * price;
             if (total < 0) total = 0;
 
             row.find('.total-price-input').val(total.toFixed(2));
@@ -567,20 +581,15 @@ if (!empty($id) && is_numeric($id)) {
         }
 
         function calculateOrderTotal() {
-            var subtotal = 0;
+            var total = 0;
             $('.total-price-input').each(function() {
-                subtotal += parseFloat($(this).val()) || 0;
+                total += parseFloat($(this).val()) || 0;
             });
 
-            var orderDiscount = parseFloat($('#order-discount').val()) || 0;
-            var total = subtotal - orderDiscount;
-
-            if (total < 0) total = 0;
-
-            $('#subtotal').text(subtotal.toFixed(2));
-            $('#subtotal-input').val(subtotal.toFixed(2));
+            // 更新总额和隐藏的小计字段
             $('#total-amount').text(total.toFixed(2));
             $('#total-amount-input').val(total.toFixed(2));
+            $('#subtotal-input').val(total.toFixed(2)); // 为兼容性保留
         }
 
         function loadCustomerContacts(customerId) {
@@ -609,12 +618,26 @@ if (!empty($id) && is_numeric($id)) {
             var orderCode = $('#order_code').val();
             var customerId = $('#customer_id').val();
             var customerName = $('.customer-search').val();
+            var contactId = $('#contact_id').val();
             var hasProducts = false;
+            var allPricesValid = true;
+            var firstEmptyPriceField = null;
 
             $('.product-id-input').each(function() {
                 if ($(this).val()) {
                     hasProducts = true;
-                    return false; // break the loop
+                    
+                    // 检查该产品的单价是否已填写
+                    var row = $(this).closest('.product-row');
+                    var priceInput = row.find('.price-input');
+                    var priceValue = priceInput.val().trim();
+                    
+                    if (priceValue === '' || isNaN(parseFloat(priceValue))) {
+                        allPricesValid = false;
+                        if (!firstEmptyPriceField) {
+                            firstEmptyPriceField = priceInput;
+                        }
+                    }
                 }
             });
 
@@ -635,14 +658,37 @@ if (!empty($id) && is_numeric($id)) {
                 $('.customer-search').focus();
                 return false;
             }
+            
+            if (!contactId) {
+                alert('请选择联系人');
+                $('#contact_id').focus();
+                return false;
+            }
 
             if (!hasProducts) {
-                alert('订单必须包含至少一个产品');
+                alert('请至少添加一个产品');
+                $('#product-search-single').focus();
+                return false;
+            }
+            
+            if (!allPricesValid) {
+                alert('请为所有产品输入有效的单价');
+                if (firstEmptyPriceField) {
+                    firstEmptyPriceField.focus();
+                }
                 return false;
             }
 
             return true;
         }
+        
+        function updateNoProductsMessage() {
+            if ($('.product-row').length > 0) {
+                $('#no-products-message').hide();
+            } else {
+                $('#no-products-message').show();
+            }
+        }
     </script>
 </div>
 </body>