123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- <?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;
- }
- </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>
- <select id="customer_id" name="customer_id" onchange="loadCustomerContacts(this.value)">
- <option value="0">请选择客户</option>
- <?php
- $customerSql = "SELECT id, cs_company FROM customer WHERE cs_belong = " . $_SESSION['employee_id'] . " ORDER BY cs_company";
- $customerResult = mysqli_query($conn, $customerSql);
- while ($customerRow = mysqli_fetch_assoc($customerResult)) {
- echo "<option value=\"{$customerRow['id']}\">" . htmlspecialcharsFix($customerRow['cs_company']) . "</option>";
- }
- ?>
- </select>
- </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%">预计交付日期</th>
- <td><input type="date" id="delivery_date" name="delivery_date" value="" class="txt1" /></td>
- </tr>
- <tr>
- <th width="8%">实际交付日期</th>
- <td><input type="date" id="actual_delivery_date" name="actual_delivery_date" value="" class="txt1" /></td>
- </tr>
- <tr>
- <th width="8%">订单状态</th>
- <td>
- <select id="order_status" name="order_status">
- <option value="0">已取消</option>
- <option value="1" selected>待确认</option>
- <option value="2">已确认</option>
- <option value="3">生产中</option>
- <option value="4">已发货</option>
- <option value="5">已完成</option>
- </select>
- </td>
- </tr>
- <tr>
- <th width="8%">付款状态</th>
- <td>
- <select id="payment_status" name="payment_status">
- <option value="0" selected>未付款</option>
- <option value="1">部分付款</option>
- <option value="2">已付清</option>
- </select>
- </td>
- </tr>
- <tr>
- <th width="8%">币种</th>
- <td>
- <select id="currency" name="currency">
- <option value="CNY" selected>人民币 (CNY)</option>
- <option value="USD">美元 (USD)</option>
- <option value="EUR">欧元 (EUR)</option>
- </select>
- </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>
- <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>
- <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][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>
- <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%;">
- </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="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()">
- </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 width="8%">内部备注</th>
- <td>
- <textarea name="internal_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('订单至少需要一个产品');
- }
- });
- // 更新折扣率和折扣金额之间的关系
- $(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() {
- var html = `
- <div class="product-row" data-index="${productIndex}">
- <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>
- <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}][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>
- <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%;">
- </div>
- </div>
- `;
- $('#product-container').append(html);
- 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);
- $(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 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);
- }
- }
- });
- } 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;
- 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 shippingFee = parseFloat($('#shipping-fee').val()) || 0;
- var orderDiscount = parseFloat($('#order-discount').val()) || 0;
- var total = subtotal + shippingFee - 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 hasProducts = false;
- $('.product-select').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_id').focus();
- return false;
- }
- if (!hasProducts) {
- alert('请至少选择一个产品');
- return false;
- }
- return true;
- }
- </script>
- </div>
- </body>
- </html>
|