|
@@ -16,15 +16,12 @@ $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;
|
|
|
|
|
|
-
|
|
|
-$spec_name = isset($_POST['spec_name']) ? $_POST['spec_name'] : array();
|
|
|
-$spec_value = isset($_POST['spec_value']) ? $_POST['spec_value'] : array();
|
|
|
-$spec_price = isset($_POST['spec_price']) ? $_POST['spec_price'] : array();
|
|
|
-$spec_moq = isset($_POST['spec_moq']) ? $_POST['spec_moq'] : array();
|
|
|
-$spec_code = isset($_POST['spec_code']) ? $_POST['spec_code'] : array();
|
|
|
-$spec_sort = isset($_POST['spec_sort']) ? $_POST['spec_sort'] : array();
|
|
|
-$spec_id = isset($_POST['spec_id']) ? $_POST['spec_id'] : array();
|
|
|
+
|
|
|
+$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;
|
|
@@ -49,24 +46,64 @@ if (empty($product_name)) {
|
|
|
exit();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-$has_valid_specs = false;
|
|
|
-if (is_array($spec_name) && !empty($spec_name)) {
|
|
|
- foreach ($spec_name as $key => $name) {
|
|
|
- if (!empty($name) && isset($spec_price[$key]) && !empty($spec_price[$key]) &&
|
|
|
- isset($spec_moq[$key]) && !empty($spec_moq[$key])) {
|
|
|
- $has_valid_specs = true;
|
|
|
+
|
|
|
+$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_specs) {
|
|
|
+if (!$has_valid_rebates) {
|
|
|
|
|
|
- header("Location: " . $redirect_url . "&error=missing_specs");
|
|
|
+ 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
|
|
@@ -78,75 +115,66 @@ if ($is_edit) {
|
|
|
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_spec_ids = array();
|
|
|
+
|
|
|
+
|
|
|
+ $kept_rebate_ids = array();
|
|
|
|
|
|
-
|
|
|
- if (is_array($spec_name) && !empty($spec_name)) {
|
|
|
- foreach ($spec_name as $key => $name) {
|
|
|
- if (!empty($name) && isset($spec_price[$key]) && !empty($spec_price[$key])) {
|
|
|
- $spec_price_value = isset($spec_price[$key]) && is_numeric($spec_price[$key]) ? (float)$spec_price[$key] : 0;
|
|
|
- $spec_moq_value = isset($spec_moq[$key]) && is_numeric($spec_moq[$key]) ? (int)$spec_moq[$key] : 1;
|
|
|
- $spec_code_value = isset($spec_code[$key]) ? mysqli_real_escape_string($conn, $spec_code[$key]) : '';
|
|
|
- $spec_sort_value = isset($spec_sort[$key]) && is_numeric($spec_sort[$key]) ? (int)$spec_sort[$key] : 0;
|
|
|
- $spec_value_value = isset($spec_value[$key]) ? mysqli_real_escape_string($conn, $spec_value[$key]) : '';
|
|
|
- $spec_id_value = isset($spec_id[$key]) && is_numeric($spec_id[$key]) ? (int)$spec_id[$key] : 0;
|
|
|
+
|
|
|
+ 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 ($spec_id_value > 0) {
|
|
|
-
|
|
|
- $sql = "UPDATE product_specifications SET
|
|
|
- spec_name = '" . mysqli_real_escape_string($conn, $name) . "',
|
|
|
- spec_value = '" . $spec_value_value . "',
|
|
|
- price = " . $spec_price_value . ",
|
|
|
- min_order_quantity = " . $spec_moq_value . ",
|
|
|
- spec_code = '" . $spec_code_value . "',
|
|
|
- sort_order = " . $spec_sort_value . "
|
|
|
- WHERE id = " . $spec_id_value . " AND product_id = " . (int)$id;
|
|
|
+ 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_spec_ids[] = $spec_id_value;
|
|
|
+ $kept_rebate_ids[] = $rebate_id_value;
|
|
|
} else {
|
|
|
-
|
|
|
- $sql = "INSERT INTO product_specifications
|
|
|
- (product_id, spec_name, spec_value, price, min_order_quantity, spec_code, addtime, sort_order)
|
|
|
+
|
|
|
+ $sql = "INSERT INTO rebate_rules
|
|
|
+ (product_id, min_quantity, rebate_amount, addtime)
|
|
|
VALUES (
|
|
|
" . (int)$id . ",
|
|
|
- '" . mysqli_real_escape_string($conn, $name) . "',
|
|
|
- '" . $spec_value_value . "',
|
|
|
- " . $spec_price_value . ",
|
|
|
- " . $spec_moq_value . ",
|
|
|
- '" . $spec_code_value . "',
|
|
|
- NOW(),
|
|
|
- " . $spec_sort_value . "
|
|
|
+ " . $quantity_value . ",
|
|
|
+ " . $rebate_value . ",
|
|
|
+ NOW()
|
|
|
)";
|
|
|
mysqli_query($conn, $sql);
|
|
|
|
|
|
|
|
|
- $kept_spec_ids[] = mysqli_insert_id($conn);
|
|
|
+ $kept_rebate_ids[] = mysqli_insert_id($conn);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- if (!empty($kept_spec_ids)) {
|
|
|
- $delete_sql = "DELETE FROM product_specifications WHERE product_id = " . (int)$id;
|
|
|
- if (count($kept_spec_ids) > 0) {
|
|
|
- $delete_sql .= " AND id NOT IN (" . implode(',', $kept_spec_ids) . ")";
|
|
|
+
|
|
|
+ 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 product_specifications WHERE product_id = " . (int)$id);
|
|
|
+
|
|
|
+ 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, category_id)
|
|
|
+ $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) . "',
|
|
@@ -156,32 +184,26 @@ if ($is_edit) {
|
|
|
'" . $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($spec_name) && !empty($spec_name)) {
|
|
|
- foreach ($spec_name as $key => $name) {
|
|
|
- if (!empty($name) && isset($spec_price[$key]) && !empty($spec_price[$key])) {
|
|
|
- $spec_price_value = isset($spec_price[$key]) && is_numeric($spec_price[$key]) ? (float)$spec_price[$key] : 0;
|
|
|
- $spec_moq_value = isset($spec_moq[$key]) && is_numeric($spec_moq[$key]) ? (int)$spec_moq[$key] : 1;
|
|
|
- $spec_code_value = isset($spec_code[$key]) ? mysqli_real_escape_string($conn, $spec_code[$key]) : '';
|
|
|
- $spec_sort_value = isset($spec_sort[$key]) && is_numeric($spec_sort[$key]) ? (int)$spec_sort[$key] : 0;
|
|
|
- $spec_value_value = isset($spec_value[$key]) ? mysqli_real_escape_string($conn, $spec_value[$key]) : '';
|
|
|
+
|
|
|
+ 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 product_specifications
|
|
|
- (product_id, spec_name, spec_value, price, min_order_quantity, spec_code, addtime, sort_order)
|
|
|
+ $sql = "INSERT INTO rebate_rules
|
|
|
+ (product_id, min_quantity, rebate_amount, addtime)
|
|
|
VALUES (
|
|
|
" . (int)$id . ",
|
|
|
- '" . mysqli_real_escape_string($conn, $name) . "',
|
|
|
- '" . $spec_value_value . "',
|
|
|
- " . $spec_price_value . ",
|
|
|
- " . $spec_moq_value . ",
|
|
|
- '" . $spec_code_value . "',
|
|
|
- NOW(),
|
|
|
- " . $spec_sort_value . "
|
|
|
+ " . $quantity_value . ",
|
|
|
+ " . $rebate_value . ",
|
|
|
+ NOW()
|
|
|
)";
|
|
|
mysqli_query($conn, $sql);
|
|
|
}
|