123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- require_once('conn.php');
- checkLogin("信息管理");
- $id = isset($_POST['id']) ? $_POST['id'] : '';
- $product_name = isset($_POST['ProductName']) ? htmlspecialcharsFix($_POST['ProductName']) : '';
- $product_img = isset($_POST['ProductImg']) ? htmlspecialcharsFix($_POST['ProductImg']) : '';
- $unit = isset($_POST['unit']) ? htmlspecialcharsFix($_POST['unit']) : '';
- $moq = isset($_POST['moq']) ? htmlspecialcharsFix($_POST['moq']) : '';
- $category_id = isset($_POST['category_id']) ? intval($_POST['category_id']) : 0;
- $nosale = isset($_POST['nosale']) ? $_POST['nosale'] : array();
- $note = isset($_POST['note']) ? htmlspecialcharsFix($_POST['note']) : '';
- $tips = isset($_POST['tips']) ? htmlspecialcharsFix($_POST['tips']) : '';
- $keys = isset($_POST['keys']) ? $_POST['keys'] : '';
- $page = isset($_POST['page']) ? $_POST['page'] : 1;
- $rebate = isset($_POST['rebate']) ? intval($_POST['rebate']) : 0;
- $min_quantity = isset($_POST['min_quantity']) ? $_POST['min_quantity'] : array();
- $rebate_amount = isset($_POST['rebate_amount']) ? $_POST['rebate_amount'] : array();
- $rebate_id = isset($_POST['rebate_id']) ? $_POST['rebate_id'] : array();
- $redirect_url = "products.php?Keys=" . $keys . "&Page=" . $page;
- if ($category_id) {
- $redirect_url .= "&category_id=" . $category_id;
- }
- $is_edit = (!empty($id) && is_numeric($id));
- $nosale_str = '';
- if (is_array($nosale) && !empty($nosale)) {
- $nosale_clean = array_map('intval', $nosale);
- $nosale_str = implode(',', $nosale_clean);
- }
- if (empty($product_name)) {
-
- header("Location: " . $redirect_url);
- exit();
- }
- $has_valid_rebates = false;
- if (is_array($min_quantity) && !empty($min_quantity)) {
- foreach ($min_quantity as $key => $quantity) {
- if (!empty($quantity) && isset($rebate_amount[$key]) && $rebate_amount[$key] !== '') {
- $has_valid_rebates = true;
- break;
- }
- }
- }
- if (!$has_valid_rebates) {
-
- header("Location: " . $redirect_url . "&error=missing_rebates");
- exit();
- }
- $quantity_values = array();
- $rebate_rules = array();
- if (is_array($min_quantity) && !empty($min_quantity)) {
- foreach ($min_quantity as $key => $quantity) {
- if (!empty($quantity) && isset($rebate_amount[$key]) && $rebate_amount[$key] !== '') {
- $quantity_value = (int)$quantity;
- $rebate_value = (float)$rebate_amount[$key];
-
-
- if (in_array($quantity_value, $quantity_values)) {
-
- header("Location: " . $redirect_url . "&error=duplicate_quantity");
- exit();
- }
-
- $quantity_values[] = $quantity_value;
- $rebate_rules[] = array(
- 'quantity' => $quantity_value,
- 'amount' => $rebate_value
- );
- }
- }
- }
- usort($rebate_rules, function($a, $b) {
- return $a['quantity'] - $b['quantity'];
- });
- for ($i = 1; $i < count($rebate_rules); $i++) {
- if ($rebate_rules[$i]['amount'] < $rebate_rules[$i-1]['amount']) {
-
- header("Location: " . $redirect_url . "&error=invalid_rebate_amount");
- exit();
- }
- }
- if ($is_edit) {
-
- $sql = "UPDATE products SET
- ProductName = '" . mysqli_real_escape_string($conn, $product_name) . "',
- ProductImg = '" . mysqli_real_escape_string($conn, $product_img) . "',
- Addtime = NOW(),
- moq = '" . mysqli_real_escape_string($conn, $moq) . "',
- unit = '" . mysqli_real_escape_string($conn, $unit) . "',
- nosale = '" . $nosale_str . "',
- note = '" . mysqli_real_escape_string($conn, $note) . "',
- tips = '" . mysqli_real_escape_string($conn, $tips) . "',
- rebate = " . $rebate . ",
- category_id = " . $category_id . "
- WHERE id = " . (int)$id;
- mysqli_query($conn, $sql);
-
-
-
- $kept_rebate_ids = array();
-
-
- if (is_array($min_quantity) && !empty($min_quantity)) {
- foreach ($min_quantity as $key => $quantity) {
- if (!empty($quantity) && isset($rebate_amount[$key]) && $rebate_amount[$key] !== '') {
- $quantity_value = isset($min_quantity[$key]) && is_numeric($min_quantity[$key]) ? (int)$min_quantity[$key] : 1;
- $rebate_value = isset($rebate_amount[$key]) && is_numeric($rebate_amount[$key]) ? (float)$rebate_amount[$key] : 0;
- $rebate_id_value = isset($rebate_id[$key]) && is_numeric($rebate_id[$key]) ? (int)$rebate_id[$key] : 0;
-
- if ($rebate_id_value > 0) {
-
- $sql = "UPDATE rebate_rules SET
- min_quantity = " . $quantity_value . ",
- rebate_amount = " . $rebate_value . "
- WHERE id = " . $rebate_id_value . " AND product_id = " . (int)$id;
- mysqli_query($conn, $sql);
-
-
- $kept_rebate_ids[] = $rebate_id_value;
- } else {
-
- $sql = "INSERT INTO rebate_rules
- (product_id, min_quantity, rebate_amount, addtime)
- VALUES (
- " . (int)$id . ",
- " . $quantity_value . ",
- " . $rebate_value . ",
- NOW()
- )";
- mysqli_query($conn, $sql);
-
-
- $kept_rebate_ids[] = mysqli_insert_id($conn);
- }
- }
- }
- }
-
-
- if (!empty($kept_rebate_ids)) {
- $delete_sql = "DELETE FROM rebate_rules WHERE product_id = " . (int)$id;
- if (count($kept_rebate_ids) > 0) {
- $delete_sql .= " AND id NOT IN (" . implode(',', $kept_rebate_ids) . ")";
- }
- mysqli_query($conn, $delete_sql);
- } else {
-
- mysqli_query($conn, "DELETE FROM rebate_rules WHERE product_id = " . (int)$id);
- }
- } else {
-
- $sql = "INSERT INTO products (ProductName, ProductImg, Addtime, moq, unit, nosale, note, tips, rebate, category_id)
- VALUES (
- '" . mysqli_real_escape_string($conn, $product_name) . "',
- '" . mysqli_real_escape_string($conn, $product_img) . "',
- NOW(),
- '" . mysqli_real_escape_string($conn, $moq) . "',
- '" . mysqli_real_escape_string($conn, $unit) . "',
- '" . $nosale_str . "',
- '" . mysqli_real_escape_string($conn, $note) . "',
- '" . mysqli_real_escape_string($conn, $tips) . "',
- " . $rebate . ",
- " . $category_id . "
- )";
- mysqli_query($conn, $sql);
- $id = mysqli_insert_id($conn);
-
-
- if (is_array($min_quantity) && !empty($min_quantity)) {
- foreach ($min_quantity as $key => $quantity) {
- if (!empty($quantity) && isset($rebate_amount[$key]) && $rebate_amount[$key] !== '') {
- $quantity_value = isset($min_quantity[$key]) && is_numeric($min_quantity[$key]) ? (int)$min_quantity[$key] : 1;
- $rebate_value = isset($rebate_amount[$key]) && is_numeric($rebate_amount[$key]) ? (float)$rebate_amount[$key] : 0;
-
- $sql = "INSERT INTO rebate_rules
- (product_id, min_quantity, rebate_amount, addtime)
- VALUES (
- " . (int)$id . ",
- " . $quantity_value . ",
- " . $rebate_value . ",
- NOW()
- )";
- mysqli_query($conn, $sql);
- }
- }
- }
- }
- mysqli_close($conn);
- header("Location: " . $redirect_url);
- exit();
|