order_edit.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. $id = $_GET['id'] ?? '';
  5. $page = $_GET['Page'] ?? '';
  6. $keys = urlencode($_GET['Keys'] ?? '');
  7. $hrefstr = "keys=$keys&Page=$page";
  8. // 验证并获取订单数据
  9. if (!empty($id) && is_numeric($id)) {
  10. // 获取订单基本信息
  11. $employee_id = $_SESSION['employee_id'];
  12. $sql = "SELECT o.*, c.cs_company, cc.contact_name
  13. FROM orders o
  14. LEFT JOIN customer c ON o.customer_id = c.id
  15. LEFT JOIN customer_contact cc ON o.contact_id = cc.id
  16. WHERE o.id = $id AND o.employee_id = $employee_id";
  17. $result = mysqli_query($conn, $sql);
  18. if ($row = mysqli_fetch_assoc($result)) {
  19. $order = $row;
  20. } else {
  21. echo "<script>alert('订单不存在或您没有权限查看');history.back();</script>";
  22. exit;
  23. }
  24. // 获取订单项信息
  25. $sql = "SELECT oi.*, p.ProductName
  26. FROM order_items oi
  27. LEFT JOIN products p ON oi.product_id = p.id
  28. WHERE oi.order_id = $id";
  29. $itemsResult = mysqli_query($conn, $sql);
  30. $orderItems = [];
  31. while ($itemRow = mysqli_fetch_assoc($itemsResult)) {
  32. $orderItems[] = $itemRow;
  33. }
  34. } else {
  35. echo "<script>alert('订单不存在!');history.back();</script>";
  36. exit;
  37. }
  38. ?>
  39. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  40. <html xmlns="http://www.w3.org/1999/xhtml">
  41. <head>
  42. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  43. <title>编辑订单</title>
  44. <link rel="stylesheet" href="css/common.css" type="text/css" />
  45. <link rel="stylesheet" href="css/alert.css" type="text/css" />
  46. <script src="js/jquery-1.7.2.min.js"></script>
  47. <script src="js/js.js"></script>
  48. <style>
  49. body {
  50. margin: 0;
  51. padding: 20px;
  52. background: #fff;
  53. }
  54. #man_zone {
  55. margin-left: 0;
  56. }
  57. .product-row {
  58. border: 1px solid #ddd;
  59. padding: 10px;
  60. margin-bottom: 10px;
  61. background-color: #f9f9f9;
  62. position: relative;
  63. }
  64. .delete-product {
  65. position: absolute;
  66. right: 10px;
  67. top: 10px;
  68. color: red;
  69. cursor: pointer;
  70. }
  71. .add-product-btn {
  72. background-color: #4CAF50;
  73. color: white;
  74. padding: 8px 12px;
  75. border: none;
  76. cursor: pointer;
  77. margin-bottom: 15px;
  78. }
  79. .total-section {
  80. margin-top: 20px;
  81. padding: 10px;
  82. background-color: #eee;
  83. font-weight: bold;
  84. }
  85. #product-container {
  86. margin-bottom: 20px;
  87. }
  88. </style>
  89. </head>
  90. <body>
  91. <div id="man_zone">
  92. <form name="form1" id="form1" method="post" action="order_save.php?<?= $hrefstr ?>" onsubmit="return validateOrderForm()">
  93. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  94. <tbody>
  95. <tr>
  96. <th width="8%">订单编号</th>
  97. <td>
  98. <input type="text" id="order_code" name="order_code" value="<?= htmlspecialcharsFix($order['order_code']) ?>" class="txt1" />
  99. <input type="hidden" name="id" value="<?= $id ?>" />
  100. </td>
  101. </tr>
  102. <tr>
  103. <th width="8%">客户选择</th>
  104. <td>
  105. <select id="customer_id" name="customer_id" onchange="loadCustomerContacts(this.value)">
  106. <option value="0">请选择客户</option>
  107. <?php
  108. $customerSql = "SELECT id, cs_company FROM customer WHERE cs_belong = " . $_SESSION['employee_id'] . " ORDER BY cs_company";
  109. $customerResult = mysqli_query($conn, $customerSql);
  110. while ($customerRow = mysqli_fetch_assoc($customerResult)) {
  111. $selected = ($order['customer_id'] == $customerRow['id']) ? ' selected' : '';
  112. echo "<option value=\"{$customerRow['id']}\"$selected>" . htmlspecialcharsFix($customerRow['cs_company']) . "</option>";
  113. }
  114. ?>
  115. </select>
  116. </td>
  117. </tr>
  118. <tr>
  119. <th width="8%">联系人</th>
  120. <td>
  121. <select id="contact_id" name="contact_id">
  122. <option value="">请选择联系人</option>
  123. <?php
  124. if ($order['customer_id']) {
  125. $contactSql = "SELECT id, contact_name FROM customer_contact WHERE customer_id = " . $order['customer_id'];
  126. $contactResult = mysqli_query($conn, $contactSql);
  127. while ($contactRow = mysqli_fetch_assoc($contactResult)) {
  128. $selected = ($order['contact_id'] == $contactRow['id']) ? ' selected' : '';
  129. echo "<option value=\"{$contactRow['id']}\"$selected>" . htmlspecialcharsFix($contactRow['contact_name']) . "</option>";
  130. }
  131. }
  132. ?>
  133. </select>
  134. </td>
  135. </tr>
  136. <tr>
  137. <th width="8%">订单日期</th>
  138. <td>
  139. <input type="date" id="order_date" name="order_date" value="<?= substr($order['order_date'], 0, 10) ?>" class="txt1" />
  140. </td>
  141. </tr>
  142. <tr>
  143. <th width="8%">预计交付日期</th>
  144. <td>
  145. <input type="date" id="delivery_date" name="delivery_date" value="<?= $order['delivery_date'] ? substr($order['delivery_date'], 0, 10) : '' ?>" class="txt1" />
  146. </td>
  147. </tr>
  148. <tr>
  149. <th width="8%">实际交付日期</th>
  150. <td>
  151. <input type="date" id="actual_delivery_date" name="actual_delivery_date" value="<?= $order['actual_delivery_date'] ? substr($order['actual_delivery_date'], 0, 10) : '' ?>" class="txt1" />
  152. </td>
  153. </tr>
  154. <tr>
  155. <th width="8%">订单状态</th>
  156. <td>
  157. <select id="order_status" name="order_status">
  158. <option value="0" <?= $order['order_status'] == 0 ? 'selected' : '' ?>>已取消</option>
  159. <option value="1" <?= $order['order_status'] == 1 ? 'selected' : '' ?>>待确认</option>
  160. <option value="2" <?= $order['order_status'] == 2 ? 'selected' : '' ?>>已确认</option>
  161. <option value="3" <?= $order['order_status'] == 3 ? 'selected' : '' ?>>生产中</option>
  162. <option value="4" <?= $order['order_status'] == 4 ? 'selected' : '' ?>>已发货</option>
  163. <option value="5" <?= $order['order_status'] == 5 ? 'selected' : '' ?>>已完成</option>
  164. </select>
  165. </td>
  166. </tr>
  167. <tr>
  168. <th width="8%">付款状态</th>
  169. <td>
  170. <select id="payment_status" name="payment_status">
  171. <option value="0" <?= $order['payment_status'] == 0 ? 'selected' : '' ?>>未付款</option>
  172. <option value="1" <?= $order['payment_status'] == 1 ? 'selected' : '' ?>>部分付款</option>
  173. <option value="2" <?= $order['payment_status'] == 2 ? 'selected' : '' ?>>已付清</option>
  174. </select>
  175. </td>
  176. </tr>
  177. <tr>
  178. <th width="8%">币种</th>
  179. <td>
  180. <select id="currency" name="currency">
  181. <option value="CNY" <?= $order['currency'] == 'CNY' ? 'selected' : '' ?>>人民币 (CNY)</option>
  182. <option value="USD" <?= $order['currency'] == 'USD' ? 'selected' : '' ?>>美元 (USD)</option>
  183. <option value="EUR" <?= $order['currency'] == 'EUR' ? 'selected' : '' ?>>欧元 (EUR)</option>
  184. </select>
  185. </td>
  186. </tr>
  187. <tr>
  188. <th width="8%" valign="top">产品列表</th>
  189. <td>
  190. <button type="button" id="add-product-btn" class="add-product-btn">添加产品</button>
  191. <div id="product-container">
  192. <?php foreach ($orderItems as $index => $item): ?>
  193. <div class="product-row" data-index="<?= $index ?>">
  194. <span class="delete-product">×</span>
  195. <div>
  196. <label>产品:</label>
  197. <select name="items[<?= $index ?>][product_id]" class="product-select" onchange="updateProductInfo(this)">
  198. <option value="">请选择产品</option>
  199. <?php
  200. $productSql = "SELECT id, ProductName FROM products ORDER BY ProductName";
  201. $productResult = mysqli_query($conn, $productSql);
  202. while ($productRow = mysqli_fetch_assoc($productResult)) {
  203. $selected = ($item['product_id'] == $productRow['id']) ? ' selected' : '';
  204. echo "<option value=\"{$productRow['id']}\"$selected>" . htmlspecialcharsFix($productRow['ProductName']) . "</option>";
  205. }
  206. ?>
  207. </select>
  208. </div>
  209. <div style="margin-top: 5px;">
  210. <label>数量:</label>
  211. <input type="number" name="items[<?= $index ?>][quantity]" value="<?= $item['quantity'] ?>" min="1" class="quantity-input" onchange="calculateItemTotal(this)">
  212. <label style="margin-left: 10px;">单位:</label>
  213. <input type="text" name="items[<?= $index ?>][unit]" value="<?= htmlspecialcharsFix($item['unit']) ?>" class="unit-input">
  214. <label style="margin-left: 10px;">单价:</label>
  215. <input type="number" step="0.01" name="items[<?= $index ?>][unit_price]" value="<?= $item['unit_price'] ?>" class="price-input" onchange="calculateItemTotal(this)">
  216. </div>
  217. <div style="margin-top: 5px;">
  218. <label>折扣金额:</label>
  219. <input type="number" step="0.01" name="items[<?= $index ?>][discount_amount]" value="<?= $item['discount_amount'] ?>" class="discount-amount-input" onchange="calculateItemTotal(this)">
  220. <label style="margin-left: 10px;">折扣率:</label>
  221. <input type="number" step="0.01" name="items[<?= $index ?>][discount_percent]" value="<?= $item['discount_percent'] ?>" class="discount-percent-input" max="100" min="0">%
  222. <label style="margin-left: 10px;">总价:</label>
  223. <input type="number" step="0.01" name="items[<?= $index ?>][total_price]" value="<?= $item['total_price'] ?>" class="total-price-input" readonly>
  224. </div>
  225. <div style="margin-top: 5px;">
  226. <label>备注:</label>
  227. <input type="text" name="items[<?= $index ?>][notes]" value="<?= htmlspecialcharsFix($item['notes']) ?>" style="width: 80%;">
  228. </div>
  229. </div>
  230. <?php endforeach; ?>
  231. </div>
  232. <div class="total-section">
  233. <div>
  234. <label>订单小计:</label>
  235. <span id="subtotal"><?= number_format($order['subtotal'], 2) ?></span>
  236. <input type="hidden" name="subtotal" id="subtotal-input" value="<?= $order['subtotal'] ?>">
  237. </div>
  238. <div style="margin-top: 5px;">
  239. <label>运费:</label>
  240. <input type="number" step="0.01" name="shipping_fee" id="shipping-fee" value="<?= $order['shipping_fee'] ?>" onchange="calculateOrderTotal()">
  241. </div>
  242. <div style="margin-top: 5px;">
  243. <label>订单折扣:</label>
  244. <input type="number" step="0.01" name="discount_amount" id="order-discount" value="<?= $order['discount_amount'] ?>" onchange="calculateOrderTotal()">
  245. </div>
  246. <div style="margin-top: 5px;">
  247. <label>订单总额:</label>
  248. <span id="total-amount"><?= number_format($order['total_amount'], 2) ?></span>
  249. <input type="hidden" name="total_amount" id="total-amount-input" value="<?= $order['total_amount'] ?>">
  250. </div>
  251. </div>
  252. </td>
  253. </tr>
  254. <tr>
  255. <th width="8%">订单备注</th>
  256. <td>
  257. <textarea name="notes" rows="3" style="width: 90%;"><?= htmlspecialcharsFix($order['notes']) ?></textarea>
  258. </td>
  259. </tr>
  260. <tr>
  261. <th width="8%">内部备注</th>
  262. <td>
  263. <textarea name="internal_notes" rows="3" style="width: 90%;"><?= htmlspecialcharsFix($order['internal_notes']) ?></textarea>
  264. </td>
  265. </tr>
  266. <tr>
  267. <th></th>
  268. <td>
  269. <input type="submit" name="save" value="保存订单" class="btn1">
  270. <input type="button" value="返回" class="btn1" onClick="location.href='order.php?<?= $hrefstr ?>'" />
  271. </td>
  272. </tr>
  273. </tbody>
  274. </table>
  275. </form>
  276. <script>
  277. var productIndex = <?= count($orderItems) ?>;
  278. $(document).ready(function() {
  279. // 初始化计算
  280. calculateOrderTotal();
  281. // 添加产品行
  282. $('#add-product-btn').click(function() {
  283. addProductRow();
  284. });
  285. // 删除产品行
  286. $(document).on('click', '.delete-product', function() {
  287. if ($('.product-row').length > 1) {
  288. $(this).closest('.product-row').remove();
  289. reindexProductRows();
  290. calculateOrderTotal();
  291. } else {
  292. alert('订单至少需要一个产品');
  293. }
  294. });
  295. // 更新折扣率和折扣金额之间的关系
  296. $(document).on('change', '.discount-percent-input', function() {
  297. var row = $(this).closest('.product-row');
  298. var percent = parseFloat($(this).val()) || 0;
  299. var quantity = parseInt(row.find('.quantity-input').val()) || 0;
  300. var price = parseFloat(row.find('.price-input').val()) || 0;
  301. var subtotal = quantity * price;
  302. var discountAmount = (subtotal * percent / 100).toFixed(2);
  303. row.find('.discount-amount-input').val(discountAmount);
  304. calculateItemTotal(this);
  305. });
  306. $(document).on('change', '.discount-amount-input', function() {
  307. var row = $(this).closest('.product-row');
  308. var amount = parseFloat($(this).val()) || 0;
  309. var quantity = parseInt(row.find('.quantity-input').val()) || 0;
  310. var price = parseFloat(row.find('.price-input').val()) || 0;
  311. var subtotal = quantity * price;
  312. if (subtotal > 0) {
  313. var percent = ((amount / subtotal) * 100).toFixed(2);
  314. row.find('.discount-percent-input').val(percent);
  315. }
  316. calculateItemTotal(this);
  317. });
  318. });
  319. function addProductRow() {
  320. var html = `
  321. <div class="product-row" data-index="${productIndex}">
  322. <span class="delete-product">×</span>
  323. <div>
  324. <label>产品:</label>
  325. <select name="items[${productIndex}][product_id]" class="product-select" onchange="updateProductInfo(this)">
  326. <option value="">请选择产品</option>
  327. ${getProductOptions()}
  328. </select>
  329. </div>
  330. <div style="margin-top: 5px;">
  331. <label>数量:</label>
  332. <input type="number" name="items[${productIndex}][quantity]" value="1" min="1" class="quantity-input" onchange="calculateItemTotal(this)">
  333. <label style="margin-left: 10px;">单位:</label>
  334. <input type="text" name="items[${productIndex}][unit]" value="" class="unit-input">
  335. <label style="margin-left: 10px;">单价:</label>
  336. <input type="number" step="0.01" name="items[${productIndex}][unit_price]" value="0.00" class="price-input" onchange="calculateItemTotal(this)">
  337. </div>
  338. <div style="margin-top: 5px;">
  339. <label>折扣金额:</label>
  340. <input type="number" step="0.01" name="items[${productIndex}][discount_amount]" value="0.00" class="discount-amount-input" onchange="calculateItemTotal(this)">
  341. <label style="margin-left: 10px;">折扣率:</label>
  342. <input type="number" step="0.01" name="items[${productIndex}][discount_percent]" value="0" class="discount-percent-input" max="100" min="0">%
  343. <label style="margin-left: 10px;">总价:</label>
  344. <input type="number" step="0.01" name="items[${productIndex}][total_price]" value="0.00" class="total-price-input" readonly>
  345. </div>
  346. <div style="margin-top: 5px;">
  347. <label>备注:</label>
  348. <input type="text" name="items[${productIndex}][notes]" value="" style="width: 80%;">
  349. </div>
  350. </div>
  351. `;
  352. $('#product-container').append(html);
  353. productIndex++;
  354. }
  355. function getProductOptions() {
  356. var options = '';
  357. <?php
  358. $productSql = "SELECT id, ProductName FROM products ORDER BY ProductName";
  359. $productResult = mysqli_query($conn, $productSql);
  360. while ($productRow = mysqli_fetch_assoc($productResult)) {
  361. echo "options += '<option value=\"{$productRow['id']}\">" . addslashes(htmlspecialcharsFix($productRow['ProductName'])) . "</option>';\n";
  362. }
  363. ?>
  364. return options;
  365. }
  366. function reindexProductRows() {
  367. $('.product-row').each(function(index) {
  368. $(this).attr('data-index', index);
  369. $(this).find('select, input').each(function() {
  370. var name = $(this).attr('name');
  371. if (name) {
  372. name = name.replace(/items\[\d+\]/, 'items[' + index + ']');
  373. $(this).attr('name', name);
  374. }
  375. });
  376. });
  377. productIndex = $('.product-row').length;
  378. }
  379. function updateProductInfo(selectElement) {
  380. var productId = $(selectElement).val();
  381. var row = $(selectElement).closest('.product-row');
  382. if (productId) {
  383. // 使用AJAX获取产品信息
  384. $.ajax({
  385. url: 'get_product_info.php',
  386. type: 'GET',
  387. data: {id: productId},
  388. dataType: 'json',
  389. success: function(data) {
  390. if (data) {
  391. row.find('.unit-input').val(data.unit);
  392. row.find('.price-input').val(data.price);
  393. calculateItemTotal(selectElement);
  394. }
  395. }
  396. });
  397. } else {
  398. row.find('.unit-input').val('');
  399. row.find('.price-input').val('0.00');
  400. calculateItemTotal(selectElement);
  401. }
  402. }
  403. function calculateItemTotal(element) {
  404. var row = $(element).closest('.product-row');
  405. var quantity = parseInt(row.find('.quantity-input').val()) || 0;
  406. var price = parseFloat(row.find('.price-input').val()) || 0;
  407. var discountAmount = parseFloat(row.find('.discount-amount-input').val()) || 0;
  408. var subtotal = quantity * price;
  409. var total = subtotal - discountAmount;
  410. if (total < 0) total = 0;
  411. row.find('.total-price-input').val(total.toFixed(2));
  412. calculateOrderTotal();
  413. }
  414. function calculateOrderTotal() {
  415. var subtotal = 0;
  416. $('.total-price-input').each(function() {
  417. subtotal += parseFloat($(this).val()) || 0;
  418. });
  419. var shippingFee = parseFloat($('#shipping-fee').val()) || 0;
  420. var orderDiscount = parseFloat($('#order-discount').val()) || 0;
  421. var total = subtotal + shippingFee - orderDiscount;
  422. if (total < 0) total = 0;
  423. $('#subtotal').text(subtotal.toFixed(2));
  424. $('#subtotal-input').val(subtotal.toFixed(2));
  425. $('#total-amount').text(total.toFixed(2));
  426. $('#total-amount-input').val(total.toFixed(2));
  427. }
  428. function loadCustomerContacts(customerId) {
  429. if (customerId) {
  430. $.ajax({
  431. url: 'get_customer_contacts.php',
  432. type: 'GET',
  433. data: {customer_id: customerId},
  434. dataType: 'json',
  435. success: function(data) {
  436. var options = '<option value="">请选择联系人</option>';
  437. if (data && data.length > 0) {
  438. for (var i = 0; i < data.length; i++) {
  439. options += '<option value="' + data[i].id + '">' + data[i].contact_name + '</option>';
  440. }
  441. }
  442. $('#contact_id').html(options);
  443. }
  444. });
  445. } else {
  446. $('#contact_id').html('<option value="">请选择联系人</option>');
  447. }
  448. }
  449. function validateOrderForm() {
  450. var orderCode = $('#order_code').val();
  451. var customerId = $('#customer_id').val();
  452. var hasProducts = false;
  453. $('.product-select').each(function() {
  454. if ($(this).val()) {
  455. hasProducts = true;
  456. return false; // break the loop
  457. }
  458. });
  459. if (!orderCode) {
  460. alert('订单编号不能为空');
  461. $('#order_code').focus();
  462. return false;
  463. }
  464. if (!customerId || customerId == '0') {
  465. alert('请选择客户');
  466. $('#customer_id').focus();
  467. return false;
  468. }
  469. if (!hasProducts) {
  470. alert('请至少选择一个产品');
  471. return false;
  472. }
  473. return true;
  474. }
  475. </script>
  476. </div>
  477. </body>
  478. </html>