order_save.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. $sql = "UPDATE orders SET
  52. order_code = '$order_code',
  53. customer_id = $customer_id,
  54. contact_id = $contact_id,
  55. employee_id = $employee_id,
  56. order_date = '$order_date',
  57. delivery_date = $delivery_date,
  58. actual_delivery_date = $actual_delivery_date,
  59. order_status = $order_status,
  60. payment_status = $payment_status,
  61. currency = '$currency',
  62. subtotal = $subtotal,
  63. discount_amount = $discount_amount,
  64. total_amount = $total_amount,
  65. notes = '$notes',
  66. internal_notes = '$internal_notes',
  67. updated_at = NOW()
  68. WHERE id = $id";
  69. mysqli_query($conn, $sql);
  70. // 删除旧的订单项
  71. $sql = "DELETE FROM order_items WHERE order_id = $id";
  72. mysqli_query($conn, $sql);
  73. // 添加新的订单项
  74. foreach ($items as $item) {
  75. if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
  76. $product_id = (int)$item['product_id'];
  77. $quantity = (int)$item['quantity'];
  78. $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
  79. $unit_price = (float)$item['unit_price'];
  80. $total_price = $quantity * $unit_price;
  81. $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
  82. $sql = "INSERT INTO order_items (
  83. order_id, product_id, quantity, unit, unit_price,
  84. total_price, notes,
  85. created_at, updated_at
  86. ) VALUES (
  87. $id, $product_id, $quantity, '$unit', $unit_price,
  88. $total_price, '$item_notes',
  89. NOW(), NOW()
  90. )";
  91. mysqli_query($conn, $sql);
  92. }
  93. $message = "订单更新成功!";
  94. } else {
  95. // 创建新订单
  96. $sql = "INSERT INTO orders (
  97. order_code, customer_id, contact_id, employee_id,
  98. order_date, delivery_date, actual_delivery_date,
  99. order_status, payment_status, currency,
  100. subtotal, discount_amount, total_amount,
  101. notes, internal_notes, created_at, updated_at
  102. ) VALUES (
  103. '$order_code', $customer_id, $contact_id, $employee_id,
  104. '$order_date', $delivery_date, $actual_delivery_date,
  105. $order_status, $payment_status, '$currency',
  106. $subtotal, $discount_amount, $total_amount,
  107. '$notes', '$internal_notes', NOW(), NOW()
  108. )";
  109. mysqli_query($conn, $sql);
  110. $order_id = mysqli_insert_id($conn);
  111. // 添加订单项
  112. foreach ($items as $item) {
  113. if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
  114. $product_id = (int)$item['product_id'];
  115. $quantity = (int)$item['quantity'];
  116. $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
  117. $unit_price = (float)$item['unit_price'];
  118. $total_price = $quantity * $unit_price;
  119. $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
  120. $sql = "INSERT INTO order_items (
  121. order_id, product_id, quantity, unit, unit_price,
  122. total_price, notes,
  123. created_at, updated_at
  124. ) VALUES (
  125. $order_id, $product_id, $quantity, '$unit', $unit_price,
  126. $total_price, '$item_notes',
  127. NOW(), NOW()
  128. )";
  129. mysqli_query($conn, $sql);
  130. }
  131. $message = "订单创建成功!";
  132. }
  133. // 重定向回订单列表页面
  134. $page = $_GET['Page'] ?? '';
  135. $keys = urlencode($_GET['Keys'] ?? '');
  136. echo "<script>alert('$message');location.href='order.php?keys=$keys&Page=$page';</script>";
  137. exit;
  138. ?>