Browse Source

fleat: order

igb 4 weeks ago
parent
commit
8468b4bb1a
4 changed files with 338 additions and 271 deletions
  1. 35 0
      get_product_info.php
  2. 131 130
      order.php
  3. 154 108
      order_add.php
  4. 18 33
      order_save.php

+ 35 - 0
get_product_info.php

@@ -3,6 +3,41 @@ require_once 'conn.php';
 checkLogin();
 
 header('Content-Type: application/json');
+
+// Product search functionality
+if (isset($_GET['search'])) {
+    $search = mysqli_real_escape_string($conn, $_GET['search']);
+    
+    // Limit to 30 results for better performance in real-time searching
+    // Search in ProductName, note, and tips fields, and include category information
+    $sql = "SELECT p.id, p.ProductName, pc.name as category_name 
+            FROM products p 
+            LEFT JOIN product_categories pc ON p.category_id = pc.id
+            WHERE p.ProductName LIKE '%$search%' 
+               OR p.note LIKE '%$search%'
+               OR p.tips LIKE '%$search%'
+            ORDER BY 
+                CASE 
+                    WHEN p.ProductName LIKE '$search%' THEN 1  /* Exact start match first */
+                    WHEN p.ProductName LIKE '%$search%' THEN 2 /* Contains match in name */
+                    WHEN p.note LIKE '%$search%' THEN 3        /* Contains match in note */
+                    WHEN p.tips LIKE '%$search%' THEN 4        /* Contains match in tips */
+                END,
+                p.ProductName 
+            LIMIT 30";
+    
+    $result = mysqli_query($conn, $sql);
+    
+    $products = [];
+    while ($row = mysqli_fetch_assoc($result)) {
+        $products[] = $row;
+    }
+    
+    echo json_encode(['products' => $products]);
+    exit;
+}
+
+// Original get product info functionality
 $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
 
 $sql = "SELECT p.unit, pr.price 

+ 131 - 130
order.php

@@ -14,39 +14,6 @@ function htmlDecode($str) {
 $act = $_GET['act'] ?? '';
 $urlStr = '';
 
-// 处理批量操作
-if ($act == "postchk") {
-    if (isset($_POST['chkbox']) && isset($_POST['chkact'])) {
-        $chkact = $_POST['chkact'];
-        $ids = implode(',', array_map('intval', $_POST['chkbox']));
-        $employee_id = $_SESSION['employee_id'];
-
-        switch($chkact) {
-            case "0":
-            case "1":
-            case "2":
-            case "3":
-            case "4":
-            case "5":
-                $sql = "UPDATE orders SET order_status = $chkact WHERE id IN ($ids) AND employee_id = $employee_id";
-                break;
-            case "-1":
-                // 删除订单记录和订单项记录(依靠外键级联删除)
-                $sql = "DELETE FROM orders WHERE id IN ($ids) AND employee_id = $employee_id";
-                break;
-        }
-
-        if (isset($sql)) {
-            mysqli_query($conn, $sql);
-        }
-
-        $keys = urlencode($_GET['Keys'] ?? '');
-        $page = $_GET['Page'] ?? '';
-        header("Location: ?keys=$keys&Page=$page$urlStr");
-        exit;
-    }
-}
-
 // 处理筛选条件
 $fliterStatus = $_GET['fliterStatus'] ?? '';
 $fliterPayment = $_GET['fliterPayment'] ?? '';
@@ -155,6 +122,55 @@ $sqlStr .= " $fliterStr ORDER BY {$ordStr}o.created_at DESC";
             border: 1px solid #ccc;
             border-radius: 3px;
         }
+        
+        /* 新的表格样式 */
+        .order-table {
+            width: 100%;
+            border-collapse: collapse;
+            margin-bottom: 20px;
+        }
+        
+        .order-table th {
+            background-color: #f2f2f2;
+            padding: 10px;
+            text-align: left;
+            border-bottom: 2px solid #ddd;
+            font-weight: bold;
+        }
+        
+        .order-table td {
+            padding: 8px 10px;
+            border-bottom: 1px solid #ddd;
+            vertical-align: middle;
+        }
+        
+        .order-table tr:hover {
+            background-color: #f5f5f5;
+        }
+        
+        .pagination {
+            text-align: center;
+            margin: 20px 0;
+        }
+        
+        .pagination a {
+            display: inline-block;
+            padding: 5px 10px;
+            margin: 0 3px;
+            border: 1px solid #ddd;
+            text-decoration: none;
+            color: #337ab7;
+        }
+        
+        .pagination a.current {
+            background-color: #337ab7;
+            color: white;
+            border-color: #337ab7;
+        }
+        
+        .pagination a:hover:not(.current) {
+            background-color: #ddd;
+        }
     </style>
 </head>
 <body>
@@ -214,25 +230,25 @@ $sqlStr .= " $fliterStr ORDER BY {$ordStr}o.created_at DESC";
         </div>
     </div>
 
-    <form id="form1" method="post" action="?act=postchk&Keys=<?= $keys ?>&Page=<?= $page ?>" onSubmit="return false">
-        <div align="right" style="margin-bottom: 10px;">
-            <input type="button" value="新增订单" class="btn1" onClick="location.href='order_add.php'" />
-        </div>
-
-        <div class="table2 em<?= $_SESSION['employee_id'] ?>">
-            <div class="theader">
-                <div class="col1"><input type="checkbox" name="chkall" id="chkall" onClick="chkboxall(this,'chkbox')" /></div>
-                <div class="col2">序号</div>
-                <div class="col3">订单编号</div>
-                <div class="col4">客户</div>
-                <div class="col5">联系人</div>
-                <div class="col6">订单日期</div>
-                <div class="col7">订单状态</div>
-                <div class="col8">付款状态</div>
-                <div class="col9">订单金额</div>
-                <div class="col10">操作</div>
-            </div>
+    <div align="right" style="margin-bottom: 10px;">
+        <input type="button" value="新增订单" class="btn1" onClick="location.href='order_add.php'" />
+    </div>
 
+    <table class="order-table">
+        <thead>
+            <tr>
+                <th>序号</th>
+                <th>订单编号</th>
+                <th>客户</th>
+                <th>联系人</th>
+                <th>订单日期</th>
+                <th>订单状态</th>
+                <th>付款状态</th>
+                <th>订单金额</th>
+                <th>操作</th>
+            </tr>
+        </thead>
+        <tbody>
             <?php
             // 设置每页显示记录数
             $pageSize = 20;
@@ -273,14 +289,13 @@ $sqlStr .= " $fliterStr ORDER BY {$ordStr}o.created_at DESC";
                 while ($row = mysqli_fetch_assoc($result)) {
                     $tempNum++;
                     ?>
-                    <div class="tline">
-                        <div class="col1" align="center"><input type="checkbox" name="chkbox[]" value="<?= $row['id'] ?>" /></div>
-                        <div class="col2"><?= $tempNum ?></div>
-                        <div class="col3"><?= htmlspecialcharsFix($row['order_code']) ?></div>
-                        <div class="col4"><?= htmlspecialcharsFix($row['cs_company']) ?></div>
-                        <div class="col5"><?= htmlspecialcharsFix($row['contact_name']) ?></div>
-                        <div class="col6"><?= date('Y-m-d', strtotime($row['order_date'])) ?></div>
-                        <div class="col7">
+                    <tr>
+                        <td><?= $tempNum ?></td>
+                        <td><?= htmlspecialcharsFix($row['order_code']) ?></td>
+                        <td><?= htmlspecialcharsFix($row['cs_company']) ?></td>
+                        <td><?= htmlspecialcharsFix($row['contact_name']) ?></td>
+                        <td><?= date('Y-m-d', strtotime($row['order_date'])) ?></td>
+                        <td>
                             <?php
                             $statusMap = [
                                 0 => '已取消',
@@ -293,8 +308,8 @@ $sqlStr .= " $fliterStr ORDER BY {$ordStr}o.created_at DESC";
                             echo '<span class="status-badge status-' . $row['order_status'] . '">' .
                                  $statusMap[$row['order_status']] . '</span>';
                             ?>
-                        </div>
-                        <div class="col8">
+                        </td>
+                        <td>
                             <?php
                             $paymentMap = [
                                 0 => '未付款',
@@ -304,86 +319,72 @@ $sqlStr .= " $fliterStr ORDER BY {$ordStr}o.created_at DESC";
                             echo '<span class="payment-badge payment-' . $row['payment_status'] . '">' .
                                  $paymentMap[$row['payment_status']] . '</span>';
                             ?>
-                        </div>
-                        <div class="col9"><?= number_format($row['total_amount'], 2) . ' ' . $row['currency'] ?></div>
-                        <div class="col10">
+                        </td>
+                        <td><?= number_format($row['total_amount'], 2) . ' ' . $row['currency'] ?></td>
+                        <td>
                             <a href="order_edit.php?id=<?= $row['id'] ?>&keys=<?= $keys ?>&page=<?= $page ?>" class="ico_edit ico">修改</a>
                             <a href="order_details.php?id=<?= $row['id'] ?>" class="ico_view ico">查看详情</a>
-                        </div>
-                    </div>
+                        </td>
+                    </tr>
                     <?php
                 }
             } else {
                 if (empty($keys) && empty($fliterStr)) {
-                    echo '<div class="tline"><div align="center" colspan="10">当前暂无订单记录</div></div>';
+                    echo '<tr><td colspan="9" align="center">当前暂无订单记录</td></tr>';
                 } else {
-                    echo '<div class="tline"><div align="center" colspan="10"><a href="?">没有找到匹配的订单记录,点击返回</a></div></div>';
+                    echo '<tr><td colspan="9" align="center"><a href="?">没有找到匹配的订单记录,点击返回</a></td></tr>';
                 }
             }
             ?>
+        </tbody>
+    </table>
+
+    <div class="pagination">
+        <?php
+        if ($totalPages > 1) {
+            $pageName = "?Keys=$keys$urlStr&";
+            $pageLen = 3;
+
+            if ($page > 1) {
+                echo "<a href=\"{$pageName}Page=1\">首页</a>";
+                echo "<a href=\"{$pageName}Page=" . ($page - 1) . "\">上一页</a>";
+            }
 
-            <div colspan="10">
-                <div class="showpagebox">
-                    <?php
-                    if ($totalPages > 1) {
-                        $pageName = "?Keys=$keys$urlStr&";
-                        $pageLen = 3;
-
-                        if ($page > 1) {
-                            echo "<a href=\"{$pageName}Page=1\">首页</a>";
-                            echo "<a href=\"{$pageName}Page=" . ($page - 1) . "\">上一页</a>";
-                        }
-
-                        if ($pageLen * 2 + 1 >= $totalPages) {
-                            $startPage = 1;
-                            $endPage = $totalPages;
-                        } else {
-                            if ($page <= $pageLen + 1) {
-                                $startPage = 1;
-                                $endPage = $pageLen * 2 + 1;
-                            } else {
-                                $startPage = $page - $pageLen;
-                                $endPage = $page + $pageLen;
-                            }
-                            if ($page + $pageLen > $totalPages) {
-                                $startPage = $totalPages - $pageLen * 2;
-                                $endPage = $totalPages;
-                            }
-                        }
-
-                        for ($i = $startPage; $i <= $endPage; $i++) {
-                            if ($i == $page) {
-                                echo "<a class=\"current\">$i</a>";
-                            } else {
-                                echo "<a href=\"{$pageName}Page=$i\">$i</a>";
-                            }
-                        }
-
-                        if ($page < $totalPages) {
-                            if ($totalPages - $page > $pageLen) {
-                                echo "<a href=\"{$pageName}Page=$totalPages\">...$totalPages</a>";
-                            }
-                            echo "<a href=\"{$pageName}Page=" . ($page + 1) . "\">下一页</a>";
-                            echo "<a href=\"{$pageName}Page=$totalPages\">尾页</a>";
-                        }
-                    }
-                    ?>
-                </div>
-                <div class="postchkbox">
-                    <select id="chkact" name="chkact">
-                        <option value="1">待确认</option>
-                        <option value="2">已确认</option>
-                        <option value="3">生产中</option>
-                        <option value="4">已发货</option>
-                        <option value="5">已完成</option>
-                        <option value="0">取消订单</option>
-                        <option value="-1">删除</option>
-                    </select>
-                    <input type="button" value="执行" onClick="postchk(1)" class="btn1" />
-                </div>
-            </div>
-        </div>
-    </form>
+            if ($pageLen * 2 + 1 >= $totalPages) {
+                $startPage = 1;
+                $endPage = $totalPages;
+            } else {
+                if ($page <= $pageLen + 1) {
+                    $startPage = 1;
+                    $endPage = $pageLen * 2 + 1;
+                } else {
+                    $startPage = $page - $pageLen;
+                    $endPage = $page + $pageLen;
+                }
+                if ($page + $pageLen > $totalPages) {
+                    $startPage = $totalPages - $pageLen * 2;
+                    $endPage = $totalPages;
+                }
+            }
+
+            for ($i = $startPage; $i <= $endPage; $i++) {
+                if ($i == $page) {
+                    echo "<a class=\"current\">$i</a>";
+                } else {
+                    echo "<a href=\"{$pageName}Page=$i\">$i</a>";
+                }
+            }
+
+            if ($page < $totalPages) {
+                if ($totalPages - $page > $pageLen) {
+                    echo "<a href=\"{$pageName}Page=$totalPages\">...$totalPages</a>";
+                }
+                echo "<a href=\"{$pageName}Page=" . ($page + 1) . "\">下一页</a>";
+                echo "<a href=\"{$pageName}Page=$totalPages\">尾页</a>";
+            }
+        }
+        ?>
+    </div>
 
     <script>
     $(document).ready(function() {

+ 154 - 108
order_add.php

@@ -58,6 +58,39 @@ $hrefstr = "keys=$keys&Page=$page";
         #product-container {
             margin-bottom: 20px;
         }
+        .productlist {
+            display: none;
+            position: absolute;
+            background: white;
+            border: 1px solid #ccc;
+            max-height: 200px;
+            overflow-y: auto;
+            width: 100%;
+            z-index: 1000;
+            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
+        }
+        .productlist ul {
+            list-style: none;
+            padding: 0;
+            margin: 0;
+        }
+        .productlist li {
+            padding: 8px 10px;
+            cursor: pointer;
+            border-bottom: 1px solid #eee;
+        }
+        .productlist li:hover {
+            background-color: #f5f5f5;
+        }
+        .productinput {
+            position: relative;
+        }
+        .productlist li .category-tag {
+            color: #777;
+            font-size: 12px;
+            font-style: italic;
+            margin-left: 5px;
+        }
     </style>
 </head>
 <body>
@@ -132,8 +165,8 @@ $hrefstr = "keys=$keys&Page=$page";
                 <td>
                     <select id="currency" name="currency">
                         <option value="CNY" selected>人民币 (CNY)</option>
-                        <option value="USD">美元 (USD)</option>
-                        <option value="EUR">欧元 (EUR)</option>
+<!--                        <option value="USD">美元 (USD)</option>-->
+<!--                        <option value="EUR">欧元 (EUR)</option>-->
                     </select>
                 </td>
             </tr>
@@ -146,16 +179,12 @@ $hrefstr = "keys=$keys&Page=$page";
                             <span class="delete-product">×</span>
                             <div>
                                 <label>产品:</label>
-                                <select name="items[0][product_id]" class="product-select" onchange="updateProductInfo(this)">
-                                    <option value="">请选择产品</option>
-                                    <?php
-                                    $productSql = "SELECT id, ProductName FROM products ORDER BY ProductName";
-                                    $productResult = mysqli_query($conn, $productSql);
-                                    while ($productRow = mysqli_fetch_assoc($productResult)) {
-                                        echo "<option value=\"{$productRow['id']}\">" . htmlspecialcharsFix($productRow['ProductName']) . "</option>";
-                                    }
-                                    ?>
-                                </select>
+                                <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>
@@ -168,18 +197,11 @@ $hrefstr = "keys=$keys&Page=$page";
                                 <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][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[0][discount_percent]" value="0" class="discount-percent-input" max="100" min="0">%
-
-                                <label style="margin-left: 10px;">总价:</label>
+                                <label>总价:</label>
                                 <input type="number" step="0.01" name="items[0][total_price]" value="0.00" class="total-price-input" readonly>
-                            </div>
-                            <div style="margin-top: 5px;">
-                                <label>备注:</label>
-                                <input type="text" name="items[0][notes]" value="" style="width: 80%;">
+                                
+                                <label style="margin-left: 10px;">备注:</label>
+                                <input type="text" name="items[0][notes]" value="" style="width: 60%;">
                             </div>
                         </div>
                     </div>
@@ -189,10 +211,6 @@ $hrefstr = "keys=$keys&Page=$page";
                             <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="shipping_fee" id="shipping-fee" value="0.00" onchange="calculateOrderTotal()">
-                        </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()">
@@ -251,33 +269,89 @@ $hrefstr = "keys=$keys&Page=$page";
                 }
             });
 
-            // 更新折扣率和折扣金额之间的关系
-            $(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);
+            // 实时产品搜索功能 - 使用防抖动以减少请求数量
+            var searchTimeout = null;
+            $(document).on('keyup', '.product-search', function() {
+                var $this = $(this);
+                var row = $this.closest('.product-row');
+                var searchTerm = $this.val().trim();
+                var productList = row.find('.productlist');
+                
+                // 清除之前的超时
+                clearTimeout(searchTimeout);
+                
+                // 隐藏之前的结果
+                productList.hide();
+                
+                // 如果搜索词少于2个字符,不执行搜索
+                if (searchTerm.length < 2) {
+                    return;
+                }
+                
+                // 设置一个300毫秒的超时,以减少请求数量
+                searchTimeout = 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();
+                            
+                            if (data && data.products && data.products.length > 0) {
+                                $.each(data.products, function(i, product) {
+                                    ul.append('<li data-id="' + product.id + '">' + 
+                                              product.ProductName + 
+                                              (product.category_name ? ' <span class="category-tag">(' + product.category_name + ')</span>' : '') + 
+                                              '</li>');
+                                });
+                                productList.show();
+                            }
+                        },
+                        error: function() {
+                            console.log('搜索产品失败,请重试');
+                        }
+                    });
+                }, 300);
             });
-
-            $(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);
+            
+            // 点击选择产品
+            $(document).on('click', '.productlist li', function() {
+                var $this = $(this);
+                var productId = $this.data('id');
+                var productName = $this.text(); // 这会包含分类名,需要清理
+                var categoryTag = $this.find('.category-tag').text();
+                
+                // 清理产品名称,移除分类信息
+                if (categoryTag) {
+                    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 displayName = productName;
+                if (categoryTag) {
+                    displayName += ' <span class="category-tag">' + categoryTag + '</span>';
+                }
+                row.find('.selected-product-info').html(displayName);
+                row.find('.productlist').hide();
+                
+                // 获取产品信息
+                getProductInfo(productId, row);
+            });
+            
+            // 点击其他地方隐藏产品列表
+            $(document).on('click', function(e) {
+                if (!$(e.target).closest('.productinput').length) {
+                    $('.productlist').hide();
                 }
-
-                calculateItemTotal(this);
             });
         });
 
@@ -287,10 +361,12 @@ $hrefstr = "keys=$keys&Page=$page";
                 <span class="delete-product">×</span>
                 <div>
                     <label>产品:</label>
-                    <select name="items[${productIndex}][product_id]" class="product-select" onchange="updateProductInfo(this)">
-                        <option value="">请选择产品</option>
-                        ${getProductOptions()}
-                    </select>
+                    <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[${productIndex}][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>
@@ -303,18 +379,11 @@ $hrefstr = "keys=$keys&Page=$page";
                     <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">%
-
-                    <label style="margin-left: 10px;">总价:</label>
+                    <label>总价:</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%;">
+                    
+                    <label style="margin-left: 10px;">备注:</label>
+                    <input type="text" name="items[${productIndex}][notes]" value="" style="width: 60%;">
                 </div>
             </div>
         `;
@@ -323,18 +392,6 @@ $hrefstr = "keys=$keys&Page=$page";
             productIndex++;
         }
 
-        function getProductOptions() {
-            var options = '';
-            <?php
-            $productSql = "SELECT id, ProductName FROM products ORDER BY ProductName";
-            $productResult = mysqli_query($conn, $productSql);
-            while ($productRow = mysqli_fetch_assoc($productResult)) {
-                echo "options += '<option value=\"{$productRow['id']}\">" . addslashes(htmlspecialcharsFix($productRow['ProductName'])) . "</option>';\n";
-            }
-            ?>
-            return options;
-        }
-
         function reindexProductRows() {
             $('.product-row').each(function(index) {
                 $(this).attr('data-index', index);
@@ -349,40 +406,30 @@ $hrefstr = "keys=$keys&Page=$page";
             productIndex = $('.product-row').length;
         }
 
-        function updateProductInfo(selectElement) {
-            var productId = $(selectElement).val();
-            var row = $(selectElement).closest('.product-row');
-
-            if (productId) {
-                // 使用AJAX获取产品信息
-                $.ajax({
-                    url: 'get_product_info.php',
-                    type: 'GET',
-                    data: {id: productId},
-                    dataType: 'json',
-                    success: function(data) {
-                        if (data) {
-                            row.find('.unit-input').val(data.unit);
-                            row.find('.price-input').val(data.price);
-                            calculateItemTotal(selectElement);
-                        }
+        function getProductInfo(productId, row) {
+            if (!productId) return;
+            
+            $.ajax({
+                url: 'get_product_info.php',
+                type: 'GET',
+                data: {id: productId},
+                dataType: 'json',
+                success: function(data) {
+                    if (data) {
+                        row.find('.unit-input').val(data.unit);
+                        row.find('.price-input').val(data.price);
+                        calculateItemTotal(row.find('.price-input')[0]);
                     }
-                });
-            } else {
-                row.find('.unit-input').val('');
-                row.find('.price-input').val('0.00');
-                calculateItemTotal(selectElement);
-            }
+                }
+            });
         }
 
         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 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));
@@ -396,9 +443,8 @@ $hrefstr = "keys=$keys&Page=$page";
                 subtotal += parseFloat($(this).val()) || 0;
             });
 
-            var shippingFee = parseFloat($('#shipping-fee').val()) || 0;
             var orderDiscount = parseFloat($('#order-discount').val()) || 0;
-            var total = subtotal + shippingFee - orderDiscount;
+            var total = subtotal - orderDiscount;
 
             if (total < 0) total = 0;
 
@@ -435,7 +481,7 @@ $hrefstr = "keys=$keys&Page=$page";
             var customerId = $('#customer_id').val();
             var hasProducts = false;
 
-            $('.product-select').each(function() {
+            $('.product-id-input').each(function() {
                 if ($(this).val()) {
                     hasProducts = true;
                     return false; // break the loop

+ 18 - 33
order_save.php

@@ -2,15 +2,6 @@
 require_once 'conn.php';
 checkLogin();
 
-// 辅助函数
-function textEncode($str) {
-    return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
-}
-
-function htmlEncode($str) {
-    return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
-}
-
 $isedit = false;
 $id = $_POST['id'] ?? '';
 if (!empty($id) && is_numeric($id)) {
@@ -18,7 +9,7 @@ if (!empty($id) && is_numeric($id)) {
 }
 
 // 获取表单数据 - 订单基本信息
-$order_code = mysqli_real_escape_string($conn, textEncode($_POST['order_code']));
+$order_code = mysqli_real_escape_string($conn, htmlspecialchars($_POST['order_code'], ENT_QUOTES, 'UTF-8'));
 $customer_id = (int)$_POST['customer_id'];
 $contact_id = !empty($_POST['contact_id']) ? (int)$_POST['contact_id'] : "NULL";
 $employee_id = $_SESSION['employee_id'];
@@ -27,9 +18,9 @@ $delivery_date = !empty($_POST['delivery_date']) ? "'" . mysqli_real_escape_stri
 $actual_delivery_date = !empty($_POST['actual_delivery_date']) ? "'" . mysqli_real_escape_string($conn, $_POST['actual_delivery_date']) . "'" : "NULL";
 $order_status = (int)$_POST['order_status'];
 $payment_status = (int)$_POST['payment_status'];
-$currency = mysqli_real_escape_string($conn, textEncode($_POST['currency']));
-$notes = mysqli_real_escape_string($conn, htmlEncode($_POST['notes']));
-$internal_notes = mysqli_real_escape_string($conn, htmlEncode($_POST['internal_notes']));
+$currency = mysqli_real_escape_string($conn, htmlspecialchars($_POST['currency'], ENT_QUOTES, 'UTF-8'));
+$notes = mysqli_real_escape_string($conn, htmlspecialchars($_POST['notes'], ENT_QUOTES, 'UTF-8'));
+$internal_notes = mysqli_real_escape_string($conn, htmlspecialchars($_POST['internal_notes'], ENT_QUOTES, 'UTF-8'));
 
 // 获取订单项信息
 $items = $_POST['items'] ?? [];
@@ -42,12 +33,11 @@ $discount_amount = !empty($_POST['discount_amount']) ? (float)$_POST['discount_a
 foreach ($items as $item) {
     $quantity = (int)$item['quantity'];
     $unit_price = (float)$item['unit_price'];
-    $item_discount = (float)($item['discount_amount'] ?? 0);
-    $item_total = ($quantity * $unit_price) - $item_discount;
+    $item_total = $quantity * $unit_price;
     $subtotal += $item_total;
 }
 
-$total_amount = $subtotal + $shipping_fee - $discount_amount;
+$total_amount = $subtotal - $discount_amount;
 
 // 验证必填字段
 if (empty($order_code)) {
@@ -80,7 +70,6 @@ if ($isedit) {
             payment_status = $payment_status, 
             currency = '$currency', 
             subtotal = $subtotal, 
-            shipping_fee = $shipping_fee, 
             discount_amount = $discount_amount, 
             total_amount = $total_amount, 
             notes = '$notes', 
@@ -100,20 +89,18 @@ if ($isedit) {
 
         $product_id = (int)$item['product_id'];
         $quantity = (int)$item['quantity'];
-        $unit = mysqli_real_escape_string($conn, textEncode($item['unit']));
+        $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
         $unit_price = (float)$item['unit_price'];
-        $discount_percent = !empty($item['discount_percent']) ? (float)$item['discount_percent'] : 0;
-        $discount_amount = !empty($item['discount_amount']) ? (float)$item['discount_amount'] : 0;
-        $total_price = ($quantity * $unit_price) - $discount_amount;
-        $item_notes = mysqli_real_escape_string($conn, htmlEncode($item['notes'] ?? ''));
+        $total_price = $quantity * $unit_price;
+        $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
 
         $sql = "INSERT INTO order_items (
                 order_id, product_id, quantity, unit, unit_price, 
-                discount_percent, discount_amount, total_price, notes, 
+                total_price, notes, 
                 created_at, updated_at
             ) VALUES (
                 $id, $product_id, $quantity, '$unit', $unit_price, 
-                $discount_percent, $discount_amount, $total_price, '$item_notes', 
+                $total_price, '$item_notes', 
                 NOW(), NOW()
             )";
 
@@ -127,13 +114,13 @@ if ($isedit) {
             order_code, customer_id, contact_id, employee_id, 
             order_date, delivery_date, actual_delivery_date, 
             order_status, payment_status, currency, 
-            subtotal, shipping_fee, discount_amount, total_amount, 
+            subtotal, discount_amount, total_amount, 
             notes, internal_notes, created_at, updated_at
         ) VALUES (
             '$order_code', $customer_id, $contact_id, $employee_id, 
             '$order_date', $delivery_date, $actual_delivery_date, 
             $order_status, $payment_status, '$currency', 
-            $subtotal, $shipping_fee, $discount_amount, $total_amount, 
+            $subtotal, $discount_amount, $total_amount, 
             '$notes', '$internal_notes', NOW(), NOW()
         )";
 
@@ -146,20 +133,18 @@ if ($isedit) {
 
         $product_id = (int)$item['product_id'];
         $quantity = (int)$item['quantity'];
-        $unit = mysqli_real_escape_string($conn, textEncode($item['unit']));
+        $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
         $unit_price = (float)$item['unit_price'];
-        $discount_percent = !empty($item['discount_percent']) ? (float)$item['discount_percent'] : 0;
-        $discount_amount = !empty($item['discount_amount']) ? (float)$item['discount_amount'] : 0;
-        $total_price = ($quantity * $unit_price) - $discount_amount;
-        $item_notes = mysqli_real_escape_string($conn, htmlEncode($item['notes'] ?? ''));
+        $total_price = $quantity * $unit_price;
+        $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
 
         $sql = "INSERT INTO order_items (
                 order_id, product_id, quantity, unit, unit_price, 
-                discount_percent, discount_amount, total_price, notes, 
+                total_price, notes, 
                 created_at, updated_at
             ) VALUES (
                 $order_id, $product_id, $quantity, '$unit', $unit_price, 
-                $discount_percent, $discount_amount, $total_price, '$item_notes', 
+                $total_price, '$item_notes', 
                 NOW(), NOW()
             )";