order_add.php 20 KB

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