123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- <?php
- require_once 'conn.php';
- checkLogin();
- // 生成一个新的订单编号
- $order_code = date('YmdHis') . rand(100, 999);
- $page = $_GET['Page'] ?? '';
- $keys = urlencode($_GET['Keys'] ?? '');
- $hrefstr = "keys=$keys&Page=$page";
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>新增订单</title>
- <link rel="stylesheet" href="css/common.css" type="text/css" />
- <link rel="stylesheet" href="css/alert.css" type="text/css" />
- <script src="js/jquery-1.7.2.min.js"></script>
- <script src="js/js.js"></script>
- <style>
- body {
- margin: 0;
- padding: 20px;
- background: #fff;
- }
- #man_zone {
- margin-left: 0;
- }
- .product-row {
- border: 1px solid #ddd;
- padding: 10px;
- margin-bottom: 10px;
- background-color: #f9f9f9;
- position: relative;
- }
- .delete-product {
- position: absolute;
- right: 10px;
- top: 10px;
- color: red;
- cursor: pointer;
- }
- .add-product-btn {
- background-color: #4CAF50;
- color: white;
- padding: 8px 12px;
- border: none;
- cursor: pointer;
- margin-bottom: 15px;
- }
- .total-section {
- margin-top: 20px;
- padding: 10px;
- background-color: #eee;
- font-weight: bold;
- }
- #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>
- <div id="man_zone">
- <form name="form1" id="form1" method="post" action="order_save.php?<?= $hrefstr ?>" onsubmit="return validateOrderForm()">
- <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
- <tbody>
- <tr>
- <th width="8%">订单编号</th>
- <td><input type="text" id="order_code" name="order_code" value="<?= $order_code ?>" class="txt1" /></td>
- </tr>
- <tr>
- <th width="8%">客户选择</th>
- <td>
- <div style="display: inline-block; width: 60%;" class="customerinput">
- <input type="text" class="customer-search fastsearch" placeholder="输入客户名称搜索..." style="width: 100%;">
- <div class="customerlist" style="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);">
- <ul style="list-style: none; padding: 0; margin: 0;"></ul>
- </div>
- <input type="hidden" name="customer_id" id="customer_id" value="0">
- <div class="selected-customer-info" style="margin-top: 5px; font-weight: bold;"></div>
- </div>
- </td>
- </tr>
- <tr>
- <th width="8%">联系人</th>
- <td>
- <select id="contact_id" name="contact_id">
- <option value="">请选择联系人</option>
- </select>
- </td>
- </tr>
- <tr>
- <th width="8%">订单日期</th>
- <td><input type="date" id="order_date" name="order_date" value="<?= date('Y-m-d') ?>" class="txt1" /></td>
- </tr>
- <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>
- </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">
- </div>
- </div>
- </td>
- </tr>
- <tr>
- <th width="8%">订单备注</th>
- <td>
- <textarea name="notes" rows="3" style="width: 90%;"></textarea>
- </td>
- </tr>
- <tr>
- <th></th>
- <td>
- <input type="submit" name="save" value="保存订单" class="btn1">
- <input type="button" value="返回" class="btn1" onClick="location.href='order.php?<?= $hrefstr ?>'" />
- </td>
- </tr>
- </tbody>
- </table>
- </form>
- <script>
- var productIndex = 1;
- $(document).ready(function() {
- // 初始化计算
- calculateOrderTotal();
- // 添加产品行
- $('#add-product-btn').click(function() {
- addProductRow();
- });
- // 删除产品行
- $(document).on('click', '.delete-product', function() {
- if ($('.product-row').length > 1) {
- $(this).closest('.product-row').remove();
- reindexProductRows();
- calculateOrderTotal();
- } else {
- alert('订单至少需要一个产品');
- }
- });
-
- // 客户搜索功能
- var customerSearchTimeout = null;
- $(document).on('keyup', '.customer-search', function() {
- var $this = $(this);
- var searchTerm = $this.val().trim();
- var customerList = $('.customerlist');
-
- // 清除之前的超时
- clearTimeout(customerSearchTimeout);
-
- // 隐藏之前的结果
- customerList.hide();
-
- // 如果搜索词少于2个字符,不执行搜索
- if (searchTerm.length < 2) {
- return;
- }
-
- // 设置一个300毫秒的超时,以减少请求数量
- customerSearchTimeout = setTimeout(function() {
- $.ajax({
- url: 'get_customer_search.php',
- type: 'GET',
- data: {search: searchTerm},
- dataType: 'json',
- success: function(data) {
- var ul = customerList.find('ul');
- ul.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;
- }
- ul.append('<li data-id="' + customer.id + '" data-code="' + (customer.cs_code || '') + '">' +
- displayText + '</li>');
- });
- customerList.show();
- }
- },
- error: function() {
- console.log('搜索客户失败,请重试');
- }
- });
- }, 300);
- });
-
- // 点击选择客户
- $(document).on('click', '.customerlist li', function() {
- var $this = $(this);
- var customerId = $this.data('id');
- var customerName = $this.text();
- var customerCode = $this.data('code') || '';
-
- // 设置选中的客户ID和名称
- $('#customer_id').val(customerId);
- $('.customer-search').val(customerName);
- $('.selected-customer-info').text(customerName);
- $('.customerlist').hide();
-
- // 加载客户联系人
- loadCustomerContacts(customerId);
- });
- // 实时产品搜索功能 - 使用防抖动以减少请求数量
- 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('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();
- }
- if (!$(e.target).closest('.customerinput').length) {
- $('.customerlist').hide();
- }
- });
- });
- function addProductRow() {
- 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>
- <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>
- <input type="number" name="items[${productIndex}][quantity]" value="1" min="1" class="quantity-input" onchange="calculateItemTotal(this)">
- <label style="margin-left: 10px;">单位:</label>
- <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}][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%;">
- </div>
- </div>
- `;
- $('#product-container').append(html);
- productIndex++;
- }
- function reindexProductRows() {
- $('.product-row').each(function(index) {
- $(this).attr('data-index', index);
- $(this).find('select, input').each(function() {
- var name = $(this).attr('name');
- if (name) {
- name = name.replace(/items\[\d+\]/, 'items[' + index + ']');
- $(this).attr('name', name);
- }
- });
- });
- productIndex = $('.product-row').length;
- }
- 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]);
- }
- }
- });
- }
- 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 total = quantity * price;
- if (total < 0) total = 0;
- row.find('.total-price-input').val(total.toFixed(2));
- calculateOrderTotal();
- }
- function calculateOrderTotal() {
- var subtotal = 0;
- $('.total-price-input').each(function() {
- subtotal += 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));
- }
- function loadCustomerContacts(customerId) {
- if (customerId) {
- $.ajax({
- url: 'get_customer_contacts.php',
- type: 'GET',
- data: {customer_id: customerId},
- dataType: 'json',
- success: function(data) {
- var options = '<option value="">请选择联系人</option>';
- if (data && data.length > 0) {
- for (var i = 0; i < data.length; i++) {
- options += '<option value="' + data[i].id + '">' + data[i].contact_name + '</option>';
- }
- }
- $('#contact_id').html(options);
- }
- });
- } else {
- $('#contact_id').html('<option value="">请选择联系人</option>');
- }
- }
- function validateOrderForm() {
- var orderCode = $('#order_code').val();
- var customerId = $('#customer_id').val();
- var customerName = $('.customer-search').val();
- var hasProducts = false;
- $('.product-id-input').each(function() {
- if ($(this).val()) {
- hasProducts = true;
- return false; // break the loop
- }
- });
- if (!orderCode) {
- alert('订单编号不能为空');
- $('#order_code').focus();
- return false;
- }
- if (!customerId || customerId == '0') {
- alert('请选择客户');
- $('.customer-search').focus();
- return false;
- }
-
- if (!customerName) {
- alert('请输入并选择有效的客户');
- $('.customer-search').focus();
- return false;
- }
- if (!hasProducts) {
- alert('请至少选择一个产品');
- return false;
- }
- return true;
- }
- </script>
- </div>
- </body>
- </html>
|