order_save.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. $price_error = false;
  52. $error_product_name = '';
  53. $error_min_price = 0;
  54. $error_current_price = 0;
  55. foreach ($items as $item) {
  56. if (empty($item['product_id'])) continue;
  57. $product_id = (int)$item['product_id'];
  58. $quantity = (int)$item['quantity'];
  59. $unit_price = (float)$item['unit_price'];
  60. // 查询产品名称,用于错误提示
  61. $product_query = "SELECT ProductName FROM products WHERE id = $product_id";
  62. $product_result = mysqli_query($conn, $product_query);
  63. // 检查产品是否存在
  64. if (mysqli_num_rows($product_result) === 0) {
  65. echo "<script>alert(\"订单中包含不存在的产品(ID: {$product_id}),请检查订单数据\");history.back();</script>";
  66. exit;
  67. }
  68. $product_row = mysqli_fetch_assoc($product_result);
  69. $product_name = $product_row['ProductName'];
  70. // 查询该产品在价格表中的最低价格要求
  71. // 根据产品ID和数量查找最接近但不超过当前订单数量的价格记录
  72. $price_query = "SELECT * FROM price
  73. WHERE productId = $product_id
  74. AND num <= $quantity
  75. ORDER BY num DESC
  76. LIMIT 1";
  77. $price_result = mysqli_query($conn, $price_query);
  78. if (mysqli_num_rows($price_result) > 0) {
  79. $price_row = mysqli_fetch_assoc($price_result);
  80. $min_price = (float)$price_row['price'];
  81. // 如果单价低于指导价,标记错误
  82. if ($unit_price < $min_price) {
  83. $price_error = true;
  84. $error_product_name = $product_name;
  85. $error_min_price = $min_price;
  86. $error_current_price = $unit_price;
  87. break;
  88. }
  89. }
  90. }
  91. // 如果价格低于指导价,显示错误并返回
  92. if ($price_error) {
  93. $error_message = "产品 {$error_product_name} 的价格 ({$error_current_price}) 低于指导价 ({$error_min_price})";
  94. echo "<script>alert(\"{$error_message}\");history.back();</script>";
  95. exit;
  96. }
  97. // 更新订单基本信息
  98. $sql = "UPDATE orders SET
  99. order_code = '$order_code',
  100. customer_id = $customer_id,
  101. contact_id = $contact_id,
  102. employee_id = $employee_id,
  103. order_date = '$order_date',
  104. delivery_date = $delivery_date,
  105. actual_delivery_date = $actual_delivery_date,
  106. order_status = $order_status,
  107. payment_status = $payment_status,
  108. currency = '$currency',
  109. subtotal = $subtotal,
  110. discount_amount = $discount_amount,
  111. total_amount = $total_amount,
  112. notes = '$notes',
  113. internal_notes = '$internal_notes',
  114. updated_at = NOW()
  115. WHERE id = $id";
  116. mysqli_query($conn, $sql);
  117. // 删除旧的订单项
  118. $sql = "DELETE FROM order_items WHERE order_id = $id";
  119. mysqli_query($conn, $sql);
  120. // 添加新的订单项
  121. foreach ($items as $item) {
  122. if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
  123. $product_id = (int)$item['product_id'];
  124. $quantity = (int)$item['quantity'];
  125. $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
  126. $unit_price = (float)$item['unit_price'];
  127. $total_price = $quantity * $unit_price;
  128. $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
  129. $sql = "INSERT INTO order_items (
  130. order_id, product_id, quantity, unit, unit_price,
  131. total_price, notes,
  132. created_at, updated_at
  133. ) VALUES (
  134. $id, $product_id, $quantity, '$unit', $unit_price,
  135. $total_price, '$item_notes',
  136. NOW(), NOW()
  137. )";
  138. mysqli_query($conn, $sql);
  139. }
  140. $message = "订单更新成功!";
  141. } else {
  142. //价格判断,不能低于指导价
  143. $price_error = false;
  144. $error_product_name = '';
  145. $error_min_price = 0;
  146. $error_current_price = 0;
  147. foreach ($items as $item) {
  148. if (empty($item['product_id'])) continue;
  149. $product_id = (int)$item['product_id'];
  150. $quantity = (int)$item['quantity'];
  151. $unit_price = (float)$item['unit_price'];
  152. // 查询产品名称,用于错误提示
  153. $product_query = "SELECT ProductName FROM products WHERE id = $product_id";
  154. $product_result = mysqli_query($conn, $product_query);
  155. // 检查产品是否存在
  156. if (mysqli_num_rows($product_result) === 0) {
  157. echo "<script>alert(\"订单中包含不存在的产品(ID: {$product_id}),请检查订单数据\");history.back();</script>";
  158. exit;
  159. }
  160. $product_row = mysqli_fetch_assoc($product_result);
  161. $product_name = $product_row['ProductName'];
  162. // 查询该产品在价格表中的最低价格要求
  163. // 根据产品ID和数量查找最接近但不超过当前订单数量的价格记录
  164. $price_query = "SELECT * FROM price
  165. WHERE productId = $product_id
  166. AND num <= $quantity
  167. ORDER BY num DESC
  168. LIMIT 1";
  169. $price_result = mysqli_query($conn, $price_query);
  170. if (mysqli_num_rows($price_result) > 0) {
  171. $price_row = mysqli_fetch_assoc($price_result);
  172. $min_price = (float)$price_row['price'];
  173. // 如果单价低于指导价,标记错误
  174. if ($unit_price < $min_price) {
  175. $price_error = true;
  176. $error_product_name = $product_name;
  177. $error_min_price = $min_price;
  178. $error_current_price = $unit_price;
  179. break;
  180. }
  181. }
  182. }
  183. // 如果价格低于指导价,显示错误并返回
  184. if ($price_error) {
  185. $error_message = "产品 {$error_product_name} 的价格 ({$error_current_price}) 低于指导价 ({$error_min_price})";
  186. echo "<script>alert(\"{$error_message}\");history.back();</script>";
  187. exit;
  188. }
  189. // 创建新订单
  190. $sql = "INSERT INTO orders (
  191. order_code, customer_id, contact_id, employee_id,
  192. order_date, delivery_date, actual_delivery_date,
  193. order_status, payment_status, currency,
  194. subtotal, discount_amount, total_amount,
  195. notes, internal_notes, created_at, updated_at
  196. ) VALUES (
  197. '$order_code', $customer_id, $contact_id, $employee_id,
  198. '$order_date', $delivery_date, $actual_delivery_date,
  199. $order_status, $payment_status, '$currency',
  200. $subtotal, $discount_amount, $total_amount,
  201. '$notes', '$internal_notes', NOW(), NOW()
  202. )";
  203. mysqli_query($conn, $sql);
  204. $order_id = mysqli_insert_id($conn);
  205. // 添加订单项
  206. foreach ($items as $item) {
  207. if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
  208. $product_id = (int)$item['product_id'];
  209. $quantity = (int)$item['quantity'];
  210. $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
  211. $unit_price = (float)$item['unit_price'];
  212. $total_price = $quantity * $unit_price;
  213. $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
  214. $sql = "INSERT INTO order_items (
  215. order_id, product_id, quantity, unit, unit_price,
  216. total_price, notes,
  217. created_at, updated_at
  218. ) VALUES (
  219. $order_id, $product_id, $quantity, '$unit', $unit_price,
  220. $total_price, '$item_notes',
  221. NOW(), NOW()
  222. )";
  223. mysqli_query($conn, $sql);
  224. }
  225. $message = "订单创建成功!";
  226. }
  227. // 重定向回订单列表页面
  228. $page = $_GET['Page'] ?? '';
  229. $keys = urlencode($_GET['Keys'] ?? '');
  230. echo "<script>alert('$message');location.href='order.php?keys=$keys&Page=$page';</script>";
  231. exit;
  232. ?>