order_save.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // 检查订单号是否已存在
  52. $check_order_code_sql = "SELECT id FROM orders WHERE order_code = '$order_code'";
  53. if ($isedit) {
  54. $check_order_code_sql .= " AND id != $id";
  55. }
  56. $check_result = mysqli_query($conn, $check_order_code_sql);
  57. if (mysqli_num_rows($check_result) > 0) {
  58. echo "<script>alert('销售开单号已存在,请使用其他开单号');history.back();</script>";
  59. exit;
  60. }
  61. if (!in_array($order_type, [1, 2])) {
  62. echo "<script>alert('请选择有效的订单类型');history.back();</script>";
  63. exit;
  64. }
  65. if ($customer_id <= 0) {
  66. echo "<script>alert('请选择客户');history.back();</script>";
  67. exit;
  68. }
  69. if (empty($items)) {
  70. echo "<script>alert('订单必须包含至少一个产品');history.back();</script>";
  71. exit;
  72. }
  73. $customer_country=0;
  74. // 检查客户国家和产品销售限制
  75. $customer_query = "SELECT cs_country FROM customer WHERE id = $customer_id LIMIT 1";
  76. $customer_result = mysqli_query($conn, $customer_query);
  77. if ($customer_result && mysqli_num_rows($customer_result) > 0) {
  78. $customer_data = mysqli_fetch_assoc($customer_result);
  79. $customer_country = $customer_data['cs_country'];
  80. if (!empty($customer_country)) {
  81. $restricted_products = [];
  82. foreach ($items as $item) {
  83. if (empty($item['product_id'])) continue;
  84. $product_id = (int)$item['product_id'];
  85. if($product_id <= 0) continue; // 跳过无效的产品ID
  86. $product_query = "SELECT ProductName, nosale FROM products WHERE id = $product_id LIMIT 1";
  87. $product_result = mysqli_query($conn, $product_query);
  88. if ($product_result && mysqli_num_rows($product_result) > 0) {
  89. $product_data = mysqli_fetch_assoc($product_result);
  90. $nosale_countries = $product_data['nosale'];
  91. // 检查客户所在国家是否在销售限制列表中
  92. if (!empty($nosale_countries)) {
  93. $restricted_countries = explode(',', $nosale_countries);
  94. if (in_array($customer_country, $restricted_countries)) {
  95. $restricted_products[] = $product_data['ProductName'];
  96. }
  97. }
  98. }
  99. }
  100. // 如果有限制销售的产品,显示错误并返回
  101. if (!empty($restricted_products)) {
  102. $restricted_product_names = implode('、', $restricted_products);
  103. echo "<script>alert('以下产品不能销售给所选客户所在的国家/地区: {$restricted_product_names}');history.back();</script>";
  104. exit;
  105. }
  106. }
  107. }
  108. // 处理保存
  109. if ($isedit) {
  110. // 更新订单基本信息
  111. $sql = "UPDATE orders SET
  112. order_code = '$order_code',
  113. order_type = $order_type,
  114. customer_id = $customer_id,
  115. contact_id = $contact_id,
  116. employee_id = $employee_id,
  117. order_date = '$order_date',
  118. delivery_date = $delivery_date,
  119. actual_delivery_date = $actual_delivery_date,
  120. order_status = $order_status,
  121. payment_status = $payment_status,
  122. currency = '$currency',
  123. subtotal = $subtotal,
  124. discount_amount = $discount_amount,
  125. total_amount = $total_amount,
  126. notes = '$notes',
  127. internal_notes = '$internal_notes',
  128. updated_at = NOW()
  129. WHERE id = $id";
  130. mysqli_query($conn, $sql);
  131. // 删除旧的订单项
  132. $sql = "DELETE FROM order_items WHERE order_id = $id";
  133. mysqli_query($conn, $sql);
  134. // 添加新的订单项
  135. foreach ($items as $item) {
  136. if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
  137. $product_id = (int)$item['product_id'];
  138. if($product_id <= 0) continue; // 跳过无效的产品ID
  139. $quantity = (int)$item['quantity'];
  140. $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
  141. $total_price = (float)$item['total_price'];
  142. // 如果数量大于0,计算单价,否则单价为0
  143. $unit_price = ($quantity > 0) ? ($total_price / $quantity) : 0;
  144. $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
  145. $sql = "INSERT INTO order_items (
  146. order_id, product_id, specification_id, quantity, unit, unit_price,
  147. total_price, notes,
  148. created_at, updated_at
  149. ) VALUES (
  150. $id, $product_id, 0, $quantity, '$unit', $unit_price,
  151. $total_price, '$item_notes',
  152. NOW(), NOW()
  153. )";
  154. mysqli_query($conn, $sql);
  155. }
  156. $message = "订单更新成功!";
  157. } else {
  158. // 创建新订单
  159. $sql = "INSERT INTO orders (
  160. order_code, order_type, customer_id, contact_id, employee_id,
  161. order_date, delivery_date, actual_delivery_date,
  162. order_status, payment_status, currency,
  163. subtotal, discount_amount, total_amount,
  164. notes, internal_notes, created_at, updated_at
  165. ) VALUES (
  166. '$order_code', $order_type, $customer_id, $contact_id, $employee_id,
  167. '$order_date', $delivery_date, $actual_delivery_date,
  168. $order_status, $payment_status, '$currency',
  169. $subtotal, $discount_amount, $total_amount,
  170. '$notes', '$internal_notes', NOW(), NOW()
  171. )";
  172. mysqli_query($conn, $sql);
  173. $order_id = mysqli_insert_id($conn);
  174. // 添加订单项
  175. foreach ($items as $item) {
  176. if (empty($item['product_id'])) continue; // 跳过没有选择产品的行
  177. $product_id = (int)$item['product_id'];
  178. if($product_id <= 0) continue; // 跳过无效的产品ID
  179. $quantity = (int)$item['quantity'];
  180. $unit = mysqli_real_escape_string($conn, htmlspecialchars($item['unit'], ENT_QUOTES, 'UTF-8'));
  181. $total_price = (float)$item['total_price'];
  182. // 如果数量大于0,计算单价,否则单价为0
  183. $unit_price = ($quantity > 0) ? ($total_price / $quantity) : 0;
  184. $item_notes = mysqli_real_escape_string($conn, htmlspecialchars($item['notes'] ?? '', ENT_QUOTES, 'UTF-8'));
  185. $sql = "INSERT INTO order_items (
  186. order_id, product_id, specification_id, quantity, unit, unit_price,
  187. total_price, notes,
  188. created_at, updated_at
  189. ) VALUES (
  190. $order_id, $product_id, 0, $quantity, '$unit', $unit_price,
  191. $total_price, '$item_notes',
  192. NOW(), NOW()
  193. )";
  194. mysqli_query($conn, $sql);
  195. }
  196. $message = "订单创建成功!";
  197. }
  198. // 重定向回订单列表页面
  199. $page = $_GET['Page'] ?? '';
  200. $keys = urlencode($_GET['Keys'] ?? '');
  201. echo "<script>alert('$message');location.href='order.php?keys=$keys&Page=$page';</script>";
  202. exit;
  203. ?>