order_save.php 9.4 KB

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