Ejemplo n.º 1
0
function check_add_coupons($auto_apply, $new_coupon_code, &$new_coupon_error)
{
    global $db, $site_id, $table_prefix, $date_show_format;
    global $currency;
    $shopping_cart = get_session("shopping_cart");
    $order_coupons = get_session("session_coupons");
    $user_info = get_session("session_user_info");
    $user_id = get_setting_value($user_info, "user_id", "");
    $user_type_id = get_setting_value($user_info, "user_type_id", "");
    $user_tax_free = get_setting_value($user_info, "tax_free", 0);
    $user_discount_type = get_session("session_discount_type");
    $user_discount_amount = get_session("session_discount_amount");
    if (!is_array($shopping_cart) || sizeof($shopping_cart) < 1) {
        return;
    }
    // check basic product prices before any further checks
    foreach ($shopping_cart as $cart_id => $item) {
        $item_id = $item["ITEM_ID"];
        $properties_more = $item["PROPERTIES_MORE"];
        if (!$item_id || $properties_more > 0) {
            continue;
        }
        $item_type_id = $item["ITEM_TYPE_ID"];
        $properties = $item["PROPERTIES"];
        $quantity = $item["QUANTITY"];
        $tax_id = $item["TAX_ID"];
        $tax_free = $item["TAX_FREE"];
        $discount_applicable = $item["DISCOUNT"];
        $buying_price = $item["BUYING_PRICE"];
        $price = $item["PRICE"];
        $is_price_edit = $item["PRICE_EDIT"];
        $properties_price = $item["PROPERTIES_PRICE"];
        $properties_percentage = $item["PROPERTIES_PERCENTAGE"];
        $properties_buying = $item["PROPERTIES_BUYING"];
        $properties_discount = $item["PROPERTIES_DISCOUNT"];
        $components = $item["COMPONENTS"];
        if ($discount_applicable) {
            if (!$is_price_edit) {
                if ($user_discount_type == 1) {
                    $price -= round($price * $user_discount_amount / 100, 2);
                } else {
                    if ($user_discount_type == 2) {
                        $price -= round($user_discount_amount, 2);
                    } else {
                        if ($user_discount_type == 3) {
                            $price -= round($price * $user_discount_amount / 100, 2);
                        } else {
                            if ($user_discount_type == 4) {
                                $price -= round(($price - $buying_price) * $user_discount_amount / 100, 2);
                            }
                        }
                    }
                }
            }
        }
        if ($properties_percentage && $price) {
            $properties_price += round($price * $properties_percentage / 100, 2);
        }
        if ($properties_discount > 0) {
            $properties_price -= round($properties_price * $properties_discount / 100, 2);
        }
        if ($discount_applicable) {
            if ($user_discount_type == 1) {
                $properties_price -= round($properties_price * $user_discount_amount / 100, 2);
            } else {
                if ($user_discount_type == 4) {
                    $properties_price -= round(($properties_price - $properties_buying) * $user_discount_amount / 100, 2);
                }
            }
        }
        $price += $properties_price;
        // add components prices
        if (is_array($components) && sizeof($components) > 0) {
            foreach ($components as $property_id => $component_values) {
                foreach ($component_values as $property_item_id => $component) {
                    $component_price = $component["price"];
                    $component_tax_id = $component["tax_id"];
                    $component_tax_free = $component["tax_free"];
                    if ($user_tax_free) {
                        $component_tax_free = $user_tax_free;
                    }
                    $sub_item_id = $component["sub_item_id"];
                    $sub_quantity = $component["quantity"];
                    if ($sub_quantity < 1) {
                        $sub_quantity = 1;
                    }
                    $sub_type_id = $component["item_type_id"];
                    if (!strlen($component_price)) {
                        $sub_price = $component["base_price"];
                        $sub_buying = $component["buying"];
                        $sub_user_price = $component["user_price"];
                        $sub_user_action = $component["user_price_action"];
                        $sub_prices = get_product_price($sub_item_id, $sub_price, $sub_buying, 0, 0, $sub_user_price, $sub_user_action, $user_discount_type, $user_discount_amount);
                        $component_price = $sub_prices["base"];
                    }
                    // add to the item price component price
                    $price += $component_price;
                }
            }
        }
        $shopping_cart[$cart_id]["BASIC_PRICE"] = $price;
        // basic price to calculate discount amount for product coupons
        $shopping_cart[$cart_id]["DISCOUNTED_PRICE"] = $price;
        // product price with all coupon discounts
    }
    // end of product prices check
    // check if any product coupons should be removed
    $exclusive_applied = false;
    $new_coupons_total = 0;
    $coupons_total = 0;
    foreach ($shopping_cart as $cart_id => $item) {
        $item_id = $item["ITEM_ID"];
        $properties_more = $item["PROPERTIES_MORE"];
        if (!$item_id || $properties_more > 0) {
            continue;
        }
        $item_type_id = $item["ITEM_TYPE_ID"];
        $basic_price = $item["BASIC_PRICE"];
        $discounted_price = $item["DISCOUNTED_PRICE"];
        $quantity = $item["QUANTITY"];
        // product coupons
        if (isset($item["COUPONS"]) && is_array($item["COUPONS"])) {
            foreach ($item["COUPONS"] as $coupon_id => $coupon_info) {
                if ($auto_apply && $coupon_info["AUTO_APPLY"]) {
                    // always remove auto-apply coupons
                    unset($shopping_cart[$cart_id]["COUPONS"][$coupon_id]);
                } else {
                    $sql = " SELECT * FROM " . $table_prefix . "coupons ";
                    $sql .= " WHERE coupon_id=" . $db->tosql($coupon_id, INTEGER);
                    $db->query($sql);
                    if ($db->next_record()) {
                        $discount_type = $db->f("discount_type");
                        $coupon_discount = $db->f("discount_amount");
                        $min_quantity = $db->f("min_quantity");
                        $max_quantity = $db->f("max_quantity");
                        $minimum_amount = $db->f("minimum_amount");
                        $maximum_amount = $db->f("maximum_amount");
                        $is_exclusive = $db->f("is_exclusive");
                        // check cart fields and total values
                        $min_cart_quantity = $db->f("min_cart_quantity");
                        $max_cart_quantity = $db->f("max_cart_quantity");
                        $min_cart_cost = $db->f("min_cart_cost");
                        $max_cart_cost = $db->f("max_cart_cost");
                        $cart_items_all = $db->f("cart_items_all");
                        $cart_items_ids = $db->f("cart_items_ids");
                        $cart_items_types_ids = $db->f("cart_items_types_ids");
                        check_cart_totals($cart_quantity, $cart_cost, $shopping_cart, $cart_items_all, $cart_items_ids, $cart_items_types_ids);
                        if ($quantity < $min_quantity || $basic_price < $minimum_amount || $max_quantity && $max_quantity < $quantity || $maximum_amount && $maximum_amount < $basic_price || $cart_quantity < $min_cart_quantity || $cart_cost < $min_cart_cost || $max_cart_quantity && $max_cart_quantity < $cart_quantity || $max_cart_cost && $max_cart_cost < $cart_cost) {
                            unset($shopping_cart[$cart_id]["COUPONS"][$coupon_id]);
                        } else {
                            // descrease product price for coupon discount
                            $discount_amount = $coupon_info["DISCOUNT_AMOUNT"];
                            $discounted_price -= $discount_amount;
                            $shopping_cart[$cart_id]["DISCOUNTED_PRICE"] = $discounted_price;
                            if ($is_exclusive) {
                                $exclusive_applied = true;
                            }
                            $coupons_total++;
                        }
                    } else {
                        unset($shopping_cart[$cart_id]["COUPONS"][$coupon_id]);
                    }
                }
            }
        }
    }
    // check if any order coupons should be removed
    // cart_quantity and cart_cost variable is used to check order coupons
    if (is_array($order_coupons)) {
        foreach ($order_coupons as $coupon_id => $coupon_info) {
            if ($auto_apply && $coupon_info["AUTO_APPLY"]) {
                // always remove auto-apply coupons
                unset($order_coupons[$coupon_id]);
            } else {
                $sql = " SELECT c.* FROM ";
                if (isset($site_id)) {
                    $sql .= "(";
                }
                $sql .= $table_prefix . "coupons c";
                if (isset($site_id)) {
                    $sql .= " LEFT JOIN  " . $table_prefix . "coupons_sites s ON s.coupon_id=c.coupon_id)";
                }
                $sql .= " WHERE c.coupon_id=" . $db->tosql($coupon_id, INTEGER);
                if (isset($site_id)) {
                    $sql .= " AND (c.sites_all=1 OR s.site_id=" . $db->tosql($site_id, INTEGER, true, false) . ")";
                } else {
                    $sql .= " AND c.sites_all=1 ";
                }
                $sql .= " ORDER BY c.apply_order ";
                $db->query($sql);
                if ($db->next_record()) {
                    $discount_type = $db->f("discount_type");
                    $coupon_discount = $db->f("discount_amount");
                    $is_exclusive = $db->f("is_exclusive");
                    // check cart fields and cart totals
                    $min_cart_quantity = $db->f("min_cart_quantity");
                    $max_cart_quantity = $db->f("max_cart_quantity");
                    $min_cart_cost = $db->f("min_cart_cost");
                    $max_cart_cost = $db->f("max_cart_cost");
                    check_cart_totals($cart_quantity, $cart_cost, $shopping_cart, 1, "", "");
                    if ($cart_quantity < $min_cart_quantity || $cart_cost < $min_cart_cost || $max_cart_quantity && $max_cart_quantity < $cart_quantity || $max_cart_cost && $max_cart_cost < $cart_cost) {
                        unset($order_coupons[$coupon_id]);
                    } else {
                        if ($is_exclusive) {
                            $exclusive_applied = true;
                        }
                        $coupons_total++;
                    }
                } else {
                    unset($order_coupons[$coupon_id]);
                }
            }
        }
    }
    // check if new coupons could be added
    $new_coupons = array();
    $coupon_title = "";
    if (strlen($new_coupon_code)) {
        $sql = " SELECT c.* FROM (" . $table_prefix . "coupons c";
        if (isset($site_id)) {
            $sql .= " LEFT JOIN  " . $table_prefix . "coupons_sites s ON s.coupon_id=c.coupon_id)";
        } else {
            $sql .= ")";
        }
        $sql .= " WHERE c.coupon_code=" . $db->tosql($new_coupon_code, TEXT);
        if (isset($site_id)) {
            $sql .= " AND (c.sites_all=1 OR s.site_id=" . $db->tosql($site_id, INTEGER, true, false) . ")";
        } else {
            $sql .= " AND c.sites_all=1 ";
        }
        $sql .= " ORDER BY c.apply_order ";
        $db->query($sql);
        if ($db->next_record()) {
            $new_coupon_id = $db->f("coupon_id");
            $start_date_db = $db->f("start_date", DATETIME);
            $expiry_date_db = $db->f("expiry_date", DATETIME);
            $coupon_title = $db->f("coupon_title");
            $new_coupons[$new_coupon_id] = $db->Record;
            $new_coupons[$new_coupon_id]["start_date_db"] = $start_date_db;
            $new_coupons[$new_coupon_id]["expiry_date_db"] = $expiry_date_db;
        }
    }
    $discount_types = array("3,4", "1,2", "5");
    // check products coupons, then order coupons and only then vouchers
    if ($auto_apply) {
        for ($dt = 0; $dt < sizeof($discount_types); $dt++) {
            $sql = " SELECT c.* FROM ";
            if (isset($site_id)) {
                $sql .= " ( ";
            }
            $sql .= $table_prefix . "coupons c";
            if (isset($site_id)) {
                $sql .= " LEFT JOIN  " . $table_prefix . "coupons_sites s ON s.coupon_id=c.coupon_id)";
            }
            $sql .= " WHERE c.is_auto_apply=1 ";
            $sql .= " AND c.discount_type IN (" . $discount_types[$dt] . ") ";
            if (isset($site_id)) {
                $sql .= " AND (c.sites_all=1 OR s.site_id=" . $db->tosql($site_id, INTEGER, true, false) . ")";
            } else {
                $sql .= " AND c.sites_all=1 ";
            }
            $sql .= " ORDER BY c.apply_order ";
            $db->query($sql);
            while ($db->next_record()) {
                $new_coupon_id = $db->f("coupon_id");
                $start_date_db = $db->f("start_date", DATETIME);
                $expiry_date_db = $db->f("expiry_date", DATETIME);
                $new_coupons[$new_coupon_id] = $db->Record;
                $new_coupons[$new_coupon_id]["start_date_db"] = $start_date_db;
                $new_coupons[$new_coupon_id]["expiry_date_db"] = $expiry_date_db;
            }
        }
    }
    // check if new coupons could be added
    if (sizeof($new_coupons) > 0) {
        foreach ($new_coupons as $new_coupon_id => $data) {
            $coupon_error = "";
            $is_active = $data["is_active"];
            $new_coupon_id = $data["coupon_id"];
            $coupon_auto_apply = $data["is_auto_apply"];
            $coupon_code = $data["coupon_code"];
            $coupon_title = $data["coupon_title"];
            $discount_type = $data["discount_type"];
            $discount_quantity = $data["discount_quantity"];
            $coupon_discount = $data["discount_amount"];
            $free_postage = $data["free_postage"];
            $coupon_tax_free = $data["coupon_tax_free"];
            $coupon_order_tax_free = $data["order_tax_free"];
            $items_all = $data["items_all"];
            $items_ids = $data["items_ids"];
            $items_types_ids = $data["items_types_ids"];
            $search_items_ids = explode(",", $items_ids);
            $search_items_types_ids = explode(",", $items_types_ids);
            $cart_items_all = $data["cart_items_all"];
            $cart_items_ids = $data["cart_items_ids"];
            $cart_items_types_ids = $data["cart_items_types_ids"];
            $users_all = $data["users_all"];
            $users_use_limit = $data["users_use_limit"];
            $users_ids = $data["users_ids"];
            $users_types_ids = $data["users_types_ids"];
            $search_users_ids = explode(",", $users_ids);
            $search_users_types_ids = explode(",", $users_types_ids);
            $expiry_date = "";
            $is_expired = false;
            $expiry_date_db = $data["expiry_date_db"];
            if (is_array($expiry_date_db)) {
                $expiry_date = va_date($date_show_format, $expiry_date_db);
                $expiry_date_ts = mktime(0, 0, 0, $expiry_date_db[MONTH], $expiry_date_db[DAY], $expiry_date_db[YEAR]);
                $current_date_ts = va_timestamp();
                if ($current_date_ts > $expiry_date_ts) {
                    $is_expired = true;
                }
            }
            $start_date = "";
            $is_upcoming = false;
            $start_date_db = $data["start_date_db"];
            if (is_array($start_date_db)) {
                $start_date = va_date($date_show_format, $start_date_db);
                $start_date_ts = mktime(0, 0, 0, $start_date_db[MONTH], $start_date_db[DAY], $start_date_db[YEAR]);
                $current_date_ts = va_timestamp();
                if ($current_date_ts < $start_date_ts) {
                    $is_upcoming = true;
                }
            }
            // check number how many times user can use coupon
            $user_not_limited = false;
            if ($users_use_limit && $user_id) {
                if ($discount_type == 3 || $discount_type == 4) {
                    $sql = " SELECT COUNT(*) FROM " . $table_prefix . "orders_items oi ";
                    $sql .= " WHERE oi.user_id=" . $db->tosql($user_id, INTEGER);
                    $sql .= " AND (oi.coupons_ids=" . $db->tosql($new_coupon_id, TEXT);
                    $sql .= " OR oi.coupons_ids LIKE '" . $db->tosql($new_coupon_id, INTEGER) . ",%'";
                    $sql .= " OR oi.coupons_ids LIKE '%," . $db->tosql($new_coupon_id, INTEGER) . "'";
                    $sql .= " OR oi.coupons_ids LIKE '%," . $db->tosql($new_coupon_id, INTEGER) . ",%') ";
                } else {
                    $sql = " SELECT COUNT(*) FROM (" . $table_prefix . "orders o ";
                    $sql .= " INNER JOIN " . $table_prefix . "orders_coupons oc ON o.order_id=oc.order_id) ";
                    $sql .= " WHERE o.user_id=" . $db->tosql($user_id, INTEGER);
                    $sql .= " AND oc.coupon_id=" . $db->tosql($new_coupon_id, INTEGER);
                }
                $user_uses = get_db_value($sql);
                if ($users_use_limit > $user_uses) {
                    $user_not_limited = true;
                }
            }
            // check goods cost limits
            $orders_period = $data["orders_period"];
            $orders_interval = $data["orders_interval"];
            $orders_min_goods = $data["orders_min_goods"];
            $orders_max_goods = $data["orders_max_goods"];
            $orders_goods_coupon = false;
            if ($user_id && ($orders_min_goods || $orders_max_goods)) {
                // check if user buy something in the past
                $sql = " SELECT SUM(o.goods_total) FROM (" . $table_prefix . "orders o ";
                $sql .= " INNER JOIN " . $table_prefix . "order_statuses os ON o.order_status=os.status_id) ";
                $sql .= " WHERE o.user_id=" . $db->tosql($user_id, INTEGER);
                $sql .= " AND os.paid_status=1 ";
                if ($orders_period && $orders_interval) {
                    $cd = va_time();
                    if ($orders_period == 1) {
                        $od = mktime(0, 0, 0, $cd[MONTH], $cd[DAY] - $orders_interval, $cd[YEAR]);
                    } elseif ($orders_period == 2) {
                        $od = mktime(0, 0, 0, $cd[MONTH], $cd[DAY] - $orders_interval * 7, $cd[YEAR]);
                    } elseif ($orders_period == 3) {
                        $od = mktime(0, 0, 0, $cd[MONTH] - $orders_interval, $cd[DAY], $cd[YEAR]);
                    } else {
                        $od = mktime(0, 0, 0, $cd[MONTH], $cd[DAY], $cd[YEAR] - $orders_interval);
                    }
                    $sql .= " AND order_placed_date>=" . $db->tosql($od, DATETIME);
                }
                $user_goods_cost = get_db_value($sql);
                if ($user_goods_cost >= $orders_min_goods && ($user_goods_cost <= $orders_max_goods || !strlen($orders_max_goods))) {
                    $orders_goods_coupon = true;
                }
            }
            // check for friends coupons
            $friends_coupon = false;
            $friends_discount_type = $data["friends_discount_type"];
            $friends_all = $data["friends_all"];
            $friends_ids = $data["friends_ids"];
            $friends_types_ids = $data["friends_types_ids"];
            $friends_period = $data["friends_period"];
            $friends_interval = $data["friends_interval"];
            $friends_min_goods = $data["friends_min_goods"];
            $friends_max_goods = $data["friends_max_goods"];
            $search_friends_ids = explode(",", $friends_ids);
            $search_friends_types_ids = explode(",", $friends_types_ids);
            if ($friends_discount_type == 1) {
                // check if user friends buy something
                $user_friends_goods = 0;
                if ($user_id) {
                    $sql = " SELECT SUM(o.goods_total) FROM (" . $table_prefix . "orders o ";
                    $sql .= " INNER JOIN " . $table_prefix . "order_statuses os ON o.order_status=os.status_id) ";
                    $sql .= " WHERE o.friend_user_id=" . $db->tosql($user_id, INTEGER);
                    $sql .= " AND os.paid_status=1 ";
                    if ($friends_period && $friends_interval) {
                        $cd = va_time();
                        if ($friends_period == 1) {
                            $od = mktime(0, 0, 0, $cd[MONTH], $cd[DAY] - $friends_interval, $cd[YEAR]);
                        } elseif ($friends_period == 2) {
                            $od = mktime(0, 0, 0, $cd[MONTH], $cd[DAY] - $friends_interval * 7, $cd[YEAR]);
                        } elseif ($friends_period == 3) {
                            $od = mktime(0, 0, 0, $cd[MONTH] - $friends_interval, $cd[DAY], $cd[YEAR]);
                        } else {
                            $od = mktime(0, 0, 0, $cd[MONTH], $cd[DAY], $cd[YEAR] - $friends_interval);
                        }
                        $sql .= " AND order_placed_date>=" . $db->tosql($od, DATETIME);
                    }
                    $user_friends_goods = get_db_value($sql);
                }
                if ($user_friends_goods >= $friends_min_goods && ($user_friends_goods <= $friends_max_goods || !strlen($friends_max_goods))) {
                    $friends_coupon = true;
                }
            } elseif ($friends_discount_type == 2) {
                $friend_code = get_session("session_friend");
                $friend_user_id = get_friend_info();
                $friend_type_id = get_session("session_friend_type_id");
                // check whose friends could use coupon
                if ($friends_all && $friend_user_id || $friend_user_id && in_array($friend_user_id, $search_friends_ids) || $friend_type_id && in_array($friend_type_id, $search_friends_types_ids)) {
                    $friends_coupon = true;
                }
            }
            // global options
            $is_exclusive = $data["is_exclusive"];
            $quantity_limit = $data["quantity_limit"];
            $coupon_uses = $data["coupon_uses"];
            // check cart total values
            $min_cart_quantity = $data["min_cart_quantity"];
            $max_cart_quantity = $data["max_cart_quantity"];
            $min_cart_cost = $data["min_cart_cost"];
            $max_cart_cost = $data["max_cart_cost"];
            if ($discount_type <= 2) {
                $cart_items_all = 1;
            }
            // for order coupons always use all cart products to calculate totals
            check_cart_totals($cart_quantity, $cart_cost, $shopping_cart, $cart_items_all, $cart_items_ids, $cart_items_types_ids);
            // product specific fields
            $min_quantity = $data["min_quantity"];
            $max_quantity = $data["max_quantity"];
            $minimum_amount = $data["minimum_amount"];
            $maximum_amount = $data["maximum_amount"];
            // check if coupon can be applied
            if (!$is_active) {
                $coupon_error = COUPON_NON_ACTIVE_MSG;
            } elseif ($quantity_limit > 0 && $coupon_uses >= $quantity_limit) {
                $coupon_error = COUPON_USED_MSG;
            } elseif ($is_expired) {
                $coupon_error = COUPON_EXPIRED_MSG;
            } elseif ($is_upcoming) {
                $coupon_error = COUPON_UPCOMING_MSG;
            } elseif (($exclusive_applied || $is_exclusive && $coupons_total > 0) && $discount_type != 5 && !is_only_gift_certificate()) {
                //Customization by Vital - allow gift cert. with other coupons
                $coupon_error = COUPON_EXCLUSIVE_MSG;
            } elseif ($discount_type <= 4 && $min_cart_cost > $cart_cost) {
                $coupon_error = str_replace("{cart_amount}", currency_format($min_cart_cost), MIN_CART_COST_ERROR);
            } elseif ($discount_type <= 4 && $max_cart_cost && $max_cart_cost < $cart_cost) {
                $coupon_error = str_replace("{cart_amount}", currency_format($max_cart_cost), MAX_CART_COST_ERROR);
            } elseif ($discount_type <= 4 && $min_cart_quantity > $cart_quantity) {
                $coupon_error = str_replace("{min_quantity}", $min_cart_quantity, COUPON_MIN_QTY_ERROR);
            } elseif ($discount_type <= 4 && $max_cart_quantity && $max_cart_quantity < $cart_quantity) {
                $coupon_error = str_replace("{max_quantity}", $max_cart_quantity, COUPON_MAX_QTY_ERROR);
            } elseif (!($users_all || $user_id && in_array($user_id, $search_users_ids) || $user_type_id && in_array($user_type_id, $search_users_types_ids))) {
                $coupon_error = COUPON_CANT_BE_USED_MSG;
                // coupon can't be used for current user
            } elseif ($users_use_limit && !$user_not_limited) {
                // coupon can't be used more times
                if ($users_use_limit == 1) {
                    $coupon_error = COUPON_CAN_BE_USED_ONCE_MSG;
                } else {
                    $coupon_error = str_replace("{use_limit}", $users_use_limit, COUPON_SAME_USE_LIMIT_MSG);
                }
            } elseif ($friends_discount_type > 0 && !$friends_coupon) {
                $coupon_error = COUPON_CANT_BE_USED_MSG;
                // coupon has friends options which can't be used for current user
            } elseif (($orders_min_goods || $orders_max_goods) && !$orders_goods_coupon) {
                $coupon_error = COUPON_CANT_BE_USED_MSG;
                // the sum of user purchased goods doesn't match with goods values for this coupon
            }
            // end coupons checks
            if (!$coupon_error) {
                // check products coupons
                $coupon_items = false;
                foreach ($shopping_cart as $cart_id => $item) {
                    $item_id = $item["ITEM_ID"];
                    $item_type_id = $item["ITEM_TYPE_ID"];
                    $properties_more = $item["PROPERTIES_MORE"];
                    //Customization by Vital
                    $properties_info_array = $item["PROPERTIES_INFO"];
                    $properties_info_array = reset($properties_info_array);
                    $coupon_size_applies = array();
                    if (preg_match('#\\((.*?)\\)#', $coupon_title, $sizes)) {
                        //get all sizes
                        $sizes[1] = strtolower(str_replace(" ", "", $sizes[1]));
                        //remove spaces and lowercase it
                        $coupon_size_applies = explode(",", $sizes[1]);
                        //place them in array
                    }
                    //place them in array
                    $size_does_not_apply = false;
                    $item_size = "";
                    if (count($coupon_size_applies) != 0 && strcasecmp($properties_info_array["NAME"], "size") == 0) {
                        $sql = "SELECT property_value FROM va_items_properties_values WHERE item_property_id=" . $properties_info_array["VALUES"][0];
                        $db->query($sql);
                        if ($db->next_record()) {
                            $item_size = strtolower($db->f("property_value"));
                        }
                        $size_does_not_apply = !in_array($item_size, $coupon_size_applies);
                    }
                    //Check if the coupon applies for the item size
                    if (strcasecmp($properties_info_array["NAME"], "size") == 0 && !$items_all) {
                        $sql = "SELECT COUNT(*) FROM va_coupons_sizes WHERE coupon_id=" . $new_coupon_id . " AND item_id=" . $item_id . " AND item_size_id=" . $properties_info_array["VALUES"][0];
                        $size_is_in = get_db_value($sql);
                        $sql = "SELECT COUNT(*) FROM va_coupons_sizes WHERE coupon_id=" . $new_coupon_id . " AND item_id=" . $item_id;
                        $other_sizes = get_db_value($sql);
                        $size_does_not_apply = $size_is_in == 0 && $other_sizes != 0 ? true : false;
                    }
                    //$coupon_error = $size_does_not_apply."  ".$coupon_size_applies;
                    //if (!$item_id || $properties_more > 0) { //original line
                    if (!$item_id || $properties_more > 0 || $size_does_not_apply) {
                        //EDN customization
                        // ignore the products which has options to be added first
                        continue;
                    }
                    $quantity = $item["QUANTITY"];
                    $basic_price = $item["BASIC_PRICE"];
                    $discounted_price = $item["DISCOUNTED_PRICE"];
                    // add a new coupon
                    if ($discount_type == 3 || $discount_type == 4) {
                        if ($basic_price >= $minimum_amount && $quantity >= $min_quantity && (!$maximum_amount || $basic_price <= $maximum_amount) && (!$max_quantity || $quantity <= $max_quantity) && ($items_all || in_array($item_id, $search_items_ids) || in_array($item_type_id, $search_items_types_ids))) {
                            // add coupon to products
                            $coupon_items = true;
                            if ($discount_type == 3) {
                                $discount_amount = round($basic_price / 100 * $coupon_discount, 2);
                            } else {
                                $discount_amount = $coupon_discount;
                            }
                            if ($discount_amount > $discounted_price) {
                                $discount_amount = $discounted_price;
                            }
                            $shopping_cart[$cart_id]["DISCOUNTED_PRICE"] -= $discount_amount;
                            if (!isset($shopping_cart[$cart_id]["COUPONS"][$new_coupon_id])) {
                                // calculate number of new applied coupons
                                $new_coupons_total++;
                            }
                            $shopping_cart[$cart_id]["COUPONS"][$new_coupon_id] = array("COUPON_ID" => $new_coupon_id, "EXCLUSIVE" => $is_exclusive, "DISCOUNT_QUANTITY" => $discount_quantity, "DISCOUNT_AMOUNT" => $discount_amount, "AUTO_APPLY" => $coupon_auto_apply);
                            if ($is_exclusive) {
                                $exclusive_applied = true;
                            }
                            $coupons_total++;
                        }
                    }
                }
                if (($discount_type == 3 || $discount_type == 4) && !$coupon_items) {
                    $coupon_error = COUPON_PRODUCTS_MSG;
                }
                // end products checks
                // check order coupons
                if ($discount_type <= 2 || $discount_type == 5) {
                    if (!isset($order_coupons[$new_coupon_id])) {
                        $new_coupons_total++;
                    }
                    // add new coupon to system
                    $order_coupons[$new_coupon_id] = array("COUPON_ID" => $new_coupon_id, "DISCOUNT_TYPE" => $discount_type, "EXCLUSIVE" => $is_exclusive, "COUPON_TAX_FREE" => $coupon_tax_free, "MIN_QUANTITY" => $min_cart_quantity, "MAX_QUANTITY" => $max_cart_quantity, "MIN_AMOUNT" => $min_cart_cost, "MAX_AMOUNT" => $max_cart_cost, "ORDER_TAX_FREE" => $coupon_order_tax_free, "AUTO_APPLY" => $coupon_auto_apply);
                    if ($is_exclusive) {
                        $exclusive_applied = true;
                    }
                    $coupons_total++;
                }
                // end order coupons checks
            }
            if (strtolower($coupon_code) == strtolower($new_coupon_code) && $coupon_error) {
                $new_coupon_error = $coupon_error;
            }
        }
    }
    // end check a new coupons and auto-applied coupons
    // update shopping cart and order coupons
    set_session("shopping_cart", $shopping_cart);
    set_session("session_coupons", $order_coupons);
    // return number of applied coupons
    return $new_coupons_total;
}
Ejemplo n.º 2
0
    }
    $sql .= $table_prefix . "users u";
    $sql .= " LEFT JOIN " . $table_prefix . "user_types ut ON ut.type_id=u.user_type_id)";
    if (isset($site_id)) {
        $sql .= " LEFT JOIN " . $table_prefix . "user_types_sites s ON s.type_id=ut.type_id)";
    }
    $sql .= " WHERE u.affiliate_code=" . $db->tosql($affiliate_code, TEXT);
    if (isset($site_id)) {
        $sql .= " AND (ut.sites_all=1 OR s.site_id=" . $db->tosql($site_id, INTEGER, true, false) . ")";
    } else {
        $sql .= " AND ut.sites_all=1";
    }
    $affiliate_user_id = get_db_value($sql);
}
$friend_code = get_session("session_friend");
$friend_user_id = get_friend_info();
$secure_payments = get_setting_value($settings, "secure_payments", 0);
$show_item_code = get_setting_value($settings, "item_code_checkout", 0);
$show_manufacturer_code = get_setting_value($settings, "manufacturer_code_checkout", 0);
$subscribe_block = get_setting_value($order_info, "subscribe_block", 0);
$subcomponents_show_type = get_setting_value($order_info, "subcomponents_show_type", 0);
$phone_code_select = get_setting_value($settings, "phone_code_select", 0);
$item_name_column = get_setting_value($settings, "checkout_item_name", 1);
$item_price_column = get_setting_value($settings, "checkout_item_price", 1);
$item_tax_percent_column = get_setting_value($settings, "checkout_item_tax_percent", 0);
$item_tax_column = get_setting_value($settings, "checkout_item_tax", 0);
$item_price_incl_tax_column = get_setting_value($settings, "checkout_item_price_incl_tax", 0);
$item_quantity_column = get_setting_value($settings, "checkout_item_quantity", 1);
$item_price_total_column = get_setting_value($settings, "checkout_item_price_total", 1);
$item_tax_total_column = get_setting_value($settings, "checkout_item_tax_total", 1);
$item_price_incl_tax_total_column = get_setting_value($settings, "checkout_item_price_incl_tax_total", 1);