order_save.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. $isedit = false;
  5. $id = $_POST['id'] ?? '';
  6. if (!empty($id) && is_numeric($id)) {
  7. $isedit = true;
  8. }
  9. // 获取表单数据 - 订单基本信息
  10. $order_code = mysqli_real_escape_string($conn, htmlspecialchars($_POST['order_code'], ENT_QUOTES, 'UTF-8'));
  11. $customer_id = (int)$_POST['customer_id'];
  12. $contact_id = !empty($_POST['contact_id']) ? (int)$_POST['contact_id'] : "NULL";
  13. $employee_id = $_SESSION['employee_id'];
  14. $order_date = mysqli_real_escape_string($conn, $_POST['order_date']);
  15. // 设置已删除字段的默认值
  16. $delivery_date = "NULL";
  17. $actual_delivery_date = "NULL";
  18. $order_status = 1; // 默认为"待确认"
  19. $payment_status = 0; // 默认为"未付款"
  20. $currency = "CNY"; // 默认为人民币
  21. $notes = mysqli_real_escape_string($conn, htmlspecialchars($_POST['notes'], ENT_QUOTES, 'UTF-8'));
  22. $internal_notes = ""; // 默认为空
  23. // 获取订单项信息
  24. $items = $_POST['items'] ?? [];
  25. // 计算订单总额
  26. $subtotal = 0;
  27. $discount_amount = !empty($_POST['discount_amount']) ? (float)$_POST['discount_amount'] : 0;
  28. foreach ($items as $item) {
  29. $quantity = (int)$item['quantity'];
  30. $unit_price = (float)$item['unit_price'];
  31. $item_total = $quantity * $unit_price;
  32. $subtotal += $item_total;
  33. }
  34. $total_amount = $subtotal - $discount_amount;
  35. // 验证必填字段
  36. if (empty($order_code)) {
  37. echo "<script>alert('订单编号不能为空');history.back();</script>";
  38. exit;
  39. }
  40. if ($customer_id <= 0) {
  41. echo "<script>alert('请选择客户');history.back();</script>";
  42. exit;
  43. }
  44. if (empty($items)) {
  45. echo "<script>alert('订单必须包含至少一个产品');history.back();</script>";
  46. exit;
  47. }
  48. // 处理保存
  49. if ($isedit) {
  50. //价格判断,不能低于指导价
  51. // 更新订单基本信息
  52. $sql = "UPDATE orders SET
  53. order_code = '$order_code',
  54. customer_id = $customer_id,
  55. contact_id = $contact_id,
  56. employee_id = $employee_id,
  57. order_date = '$order_date',
  58. delivery_date = $delivery_date,
  59. actual_delivery_date = $actual_delivery_date,
  60. order_status = $order_status,
  61. payment_status = $payment_status,
  62. currency = '$currency',
  63. subtotal = $subtotal,
  64. discount_amount = $discount_amount,
  65. total_amount = $total_amount,
  66. notes = '$notes',
  67. internal_notes = '$internal_notes',
  68. updated_at = NOW()
  69. WHERE id = $id";
  70. mysqli_query($conn, $sql);
  71. // 删除旧的订单项
  72. $sql = "DELETE FROM order_items WHERE order_id = $id";
  73. mysqli_query($conn, $sql);
  74. // 添加新的订单项
  75. foreach ($items as $item) {
  76. if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
  77. $product_id = (int)$item['product_id'];
  78. $quantity = (int)$item['quantity'];
  79. $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
  80. $unit_price = (float)$item['unit_price'];
  81. $total_price = $quantity * $unit_price;
  82. $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
  83. $sql = "INSERT INTO order_items (
  84. order_id, product_id, quantity, unit, unit_price,
  85. total_price, notes,
  86. created_at, updated_at
  87. ) VALUES (
  88. $id, $product_id, $quantity, '$unit', $unit_price,
  89. $total_price, '$item_notes',
  90. NOW(), NOW()
  91. )";
  92. mysqli_query($conn, $sql);
  93. }
  94. $message = "订单更新成功!";
  95. } else {
  96. //价格判断,不能低于指导价
  97. foreach ($items as $item) {
  98. }
  99. // 创建新订单
  100. $sql = "INSERT INTO orders (
  101. order_code, customer_id, contact_id, employee_id,
  102. order_date, delivery_date, actual_delivery_date,
  103. order_status, payment_status, currency,
  104. subtotal, discount_amount, total_amount,
  105. notes, internal_notes, created_at, updated_at
  106. ) VALUES (
  107. '$order_code', $customer_id, $contact_id, $employee_id,
  108. '$order_date', $delivery_date, $actual_delivery_date,
  109. $order_status, $payment_status, '$currency',
  110. $subtotal, $discount_amount, $total_amount,
  111. '$notes', '$internal_notes', NOW(), NOW()
  112. )";
  113. mysqli_query($conn, $sql);
  114. $order_id = mysqli_insert_id($conn);
  115. // 添加订单项
  116. foreach ($items as $item) {
  117. if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
  118. $product_id = (int)$item['product_id'];
  119. $quantity = (int)$item['quantity'];
  120. $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
  121. $unit_price = (float)$item['unit_price'];
  122. $total_price = $quantity * $unit_price;
  123. $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
  124. $sql = "INSERT INTO order_items (
  125. order_id, product_id, quantity, unit, unit_price,
  126. total_price, notes,
  127. created_at, updated_at
  128. ) VALUES (
  129. $order_id, $product_id, $quantity, '$unit', $unit_price,
  130. $total_price, '$item_notes',
  131. NOW(), NOW()
  132. )";
  133. mysqli_query($conn, $sql);
  134. }
  135. $message = "订单创建成功!";
  136. }
  137. // 重定向回订单列表页面
  138. $page = $_GET['Page'] ?? '';
  139. $keys = urlencode($_GET['Keys'] ?? '');
  140. echo "<script>alert('$message');location.href='order.php?keys=$keys&Page=$page';</script>";
  141. exit;
  142. ?>