order_save.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. $isAdmin = checkIfAdmin();
  10. if (!$isAdmin) {
  11. // 验证订单所有权
  12. $checkOwnershipQuery = "SELECT id FROM orders WHERE id = $id AND employee_id = " . $_SESSION['employee_id'];
  13. $ownershipResult = mysqli_query($conn, $checkOwnershipQuery);
  14. if (mysqli_num_rows($ownershipResult) === 0) {
  15. echo "<script>alert('您没有权限编辑此订单!');history.back();</script>";
  16. exit;
  17. }
  18. }
  19. }
  20. // 获取表单数据 - 订单基本信息
  21. $order_code = mysqli_real_escape_string($conn, htmlspecialchars($_POST['order_code'], ENT_QUOTES, 'UTF-8'));
  22. $customer_id = (int)$_POST['customer_id'];
  23. $contact_id = !empty($_POST['contact_id']) ? (int)$_POST['contact_id'] : "NULL";
  24. $employee_id = $_SESSION['employee_id'];
  25. $order_date = mysqli_real_escape_string($conn, $_POST['order_date']);
  26. $order_type = (int)$_POST['order_type'];
  27. // 设置已删除字段的默认值
  28. $delivery_date = "NULL";
  29. $actual_delivery_date = "NULL";
  30. $order_status = 1; // 默认为"待确认"
  31. $payment_status = 0; // 默认为"未付款"
  32. $currency = "CNY"; // 默认为人民币
  33. $notes = mysqli_real_escape_string($conn, htmlspecialchars($_POST['notes'], ENT_QUOTES, 'UTF-8'));
  34. $internal_notes = ""; // 默认为空
  35. // 获取订单项信息
  36. $items = $_POST['items'] ?? [];
  37. // 计算订单总额
  38. $subtotal = 0;
  39. $discount_amount = !empty($_POST['discount_amount']) ? (float)$_POST['discount_amount'] : 0;
  40. foreach ($items as $item) {
  41. $quantity = (int)$item['quantity'];
  42. $total_price = (float)$item['total_price']; // 直接使用用户输入的总价
  43. $subtotal += $total_price;
  44. }
  45. $total_amount = $subtotal - $discount_amount;
  46. // 验证必填字段
  47. if (empty($order_code)) {
  48. echo "<script>alert('销售订单号不能为空');history.back();</script>";
  49. exit;
  50. }
  51. if (!in_array($order_type, [1, 2])) {
  52. echo "<script>alert('请选择有效的订单类型');history.back();</script>";
  53. exit;
  54. }
  55. if ($customer_id <= 0) {
  56. echo "<script>alert('请选择客户');history.back();</script>";
  57. exit;
  58. }
  59. if (empty($items)) {
  60. echo "<script>alert('订单必须包含至少一个产品');history.back();</script>";
  61. exit;
  62. }
  63. $customer_country=0;
  64. // 检查客户国家和产品销售限制
  65. $customer_query = "SELECT cs_country FROM customer WHERE id = $customer_id LIMIT 1";
  66. $customer_result = mysqli_query($conn, $customer_query);
  67. if ($customer_result && mysqli_num_rows($customer_result) > 0) {
  68. $customer_data = mysqli_fetch_assoc($customer_result);
  69. $customer_country = $customer_data['cs_country'];
  70. if (!empty($customer_country)) {
  71. $restricted_products = [];
  72. foreach ($items as $item) {
  73. if (empty($item['product_id'])) continue;
  74. $product_id = (int)$item['product_id'];
  75. if($product_id <= 0) continue; // 跳过无效的产品ID
  76. $product_query = "SELECT ProductName, nosale FROM products WHERE id = $product_id LIMIT 1";
  77. $product_result = mysqli_query($conn, $product_query);
  78. if ($product_result && mysqli_num_rows($product_result) > 0) {
  79. $product_data = mysqli_fetch_assoc($product_result);
  80. $nosale_countries = $product_data['nosale'];
  81. // 检查客户所在国家是否在销售限制列表中
  82. if (!empty($nosale_countries)) {
  83. $restricted_countries = explode(',', $nosale_countries);
  84. if (in_array($customer_country, $restricted_countries)) {
  85. $restricted_products[] = $product_data['ProductName'];
  86. }
  87. }
  88. }
  89. }
  90. // 如果有限制销售的产品,显示错误并返回
  91. if (!empty($restricted_products)) {
  92. $restricted_product_names = implode('、', $restricted_products);
  93. echo "<script>alert('以下产品不能销售给所选客户所在的国家/地区: {$restricted_product_names}');history.back();</script>";
  94. exit;
  95. }
  96. }
  97. }
  98. // 处理保存
  99. if ($isedit) {
  100. // 更新订单基本信息
  101. $sql = "UPDATE orders SET
  102. order_code = '$order_code',
  103. order_type = $order_type,
  104. customer_id = $customer_id,
  105. contact_id = $contact_id,
  106. employee_id = $employee_id,
  107. order_date = '$order_date',
  108. delivery_date = $delivery_date,
  109. actual_delivery_date = $actual_delivery_date,
  110. order_status = $order_status,
  111. payment_status = $payment_status,
  112. currency = '$currency',
  113. subtotal = $subtotal,
  114. discount_amount = $discount_amount,
  115. total_amount = $total_amount,
  116. notes = '$notes',
  117. internal_notes = '$internal_notes',
  118. updated_at = NOW()
  119. WHERE id = $id";
  120. mysqli_query($conn, $sql);
  121. // 删除旧的订单项
  122. $sql = "DELETE FROM order_items WHERE order_id = $id";
  123. mysqli_query($conn, $sql);
  124. // 添加新的订单项
  125. foreach ($items as $item) {
  126. if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
  127. $product_id = (int)$item['product_id'];
  128. if($product_id <= 0) continue; // 跳过无效的产品ID
  129. $quantity = (int)$item['quantity'];
  130. $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
  131. $total_price = (float)$item['total_price'];
  132. // 如果数量大于0,计算单价,否则单价为0
  133. $unit_price = ($quantity > 0) ? ($total_price / $quantity) : 0;
  134. $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
  135. $sql = "INSERT INTO order_items (
  136. order_id, product_id, specification_id, quantity, unit, unit_price,
  137. total_price, notes,
  138. created_at, updated_at
  139. ) VALUES (
  140. $id, $product_id, 0, $quantity, '$unit', $unit_price,
  141. $total_price, '$item_notes',
  142. NOW(), NOW()
  143. )";
  144. mysqli_query($conn, $sql);
  145. }
  146. $message = "订单更新成功!";
  147. } else {
  148. // 创建新订单
  149. $sql = "INSERT INTO orders (
  150. order_code, order_type, customer_id, contact_id, employee_id,
  151. order_date, delivery_date, actual_delivery_date,
  152. order_status, payment_status, currency,
  153. subtotal, discount_amount, total_amount,
  154. notes, internal_notes, created_at, updated_at
  155. ) VALUES (
  156. '$order_code', $order_type, $customer_id, $contact_id, $employee_id,
  157. '$order_date', $delivery_date, $actual_delivery_date,
  158. $order_status, $payment_status, '$currency',
  159. $subtotal, $discount_amount, $total_amount,
  160. '$notes', '$internal_notes', NOW(), NOW()
  161. )";
  162. mysqli_query($conn, $sql);
  163. $order_id = mysqli_insert_id($conn);
  164. // 添加订单项
  165. foreach ($items as $item) {
  166. if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
  167. $product_id = (int)$item['product_id'];
  168. if($product_id <= 0) continue; // 跳过无效的产品ID
  169. $quantity = (int)$item['quantity'];
  170. $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
  171. $total_price = (float)$item['total_price'];
  172. // 如果数量大于0,计算单价,否则单价为0
  173. $unit_price = ($quantity > 0) ? ($total_price / $quantity) : 0;
  174. $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
  175. $sql = "INSERT INTO order_items (
  176. order_id, product_id, specification_id, quantity, unit, unit_price,
  177. total_price, notes,
  178. created_at, updated_at
  179. ) VALUES (
  180. $order_id, $product_id, 0, $quantity, '$unit', $unit_price,
  181. $total_price, '$item_notes',
  182. NOW(), NOW()
  183. )";
  184. mysqli_query($conn, $sql);
  185. }
  186. $message = "订单创建成功!";
  187. }
  188. // 重定向回订单列表页面
  189. $page = $_GET['Page'] ?? '';
  190. $keys = urlencode($_GET['Keys'] ?? '');
  191. echo "<script>alert('$message');location.href='order.php?keys=$keys&Page=$page';</script>";
  192. exit;
  193. ?>