Exemplo 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;
}
Exemplo n.º 2
0
 // save new export template
 $r = new VA_Record($table_prefix . "export_templates");
 $r->add_where("template_id", INTEGER);
 $r->add_textbox("template_name", TEXT);
 $r->add_textbox("table_name", TEXT);
 $r->add_textbox("admin_id_added_by", INTEGER);
 $r->add_textbox("date_added", DATETIME);
 if ($db_type == "postgre") {
     $new_template_id = get_db_value(" SELECT NEXTVAL('seq_" . $table_prefix . "export_templates') ");
     $r->change_property("template_id", USE_IN_INSERT, true);
     $r->set_value("template_id", $new_template_id);
 }
 $r->set_value("template_name", $template_name);
 $r->set_value("table_name", $table);
 $r->set_value("admin_id_added_by", get_session("session_admin_id"));
 $r->set_value("date_added", va_time());
 $r->insert_record();
 if ($db_type == "mysql") {
     $new_template_id = get_db_value(" SELECT LAST_INSERT_ID() ");
     $r->set_value("template_id", $new_template_id);
 } elseif ($db_type == "access") {
     $new_template_id = get_db_value(" SELECT @@IDENTITY ");
     $r->set_value("template_id", $new_template_id);
 } elseif ($db_type == "db2") {
     $new_template_id = get_db_value(" SELECT PREVVAL FOR seq_" . $table_prefix . "export_templates FROM " . $table_prefix . "export_templates");
     $r->set_value("template_id", $new_template_id);
 }
 if (strlen($new_template_id)) {
     // start adding fields
     $fld = new VA_Record($table_prefix . "export_fields");
     $fld->add_where("field_id", INTEGER);
    $shipping_city_country = $db->f("delivery_city") . ", " . $db->f("delivery_province") . " " . $db->f("delivery_state_code") . " " . $db->f("delivery_zip") . ", " . $db->f("delivery_country_code");
    $t->set_var("shipping_city_country", $shipping_city_country);
}
// END customization - address data
if (trim($payment_info)) {
    $sql = "SELECT * FROM " . $table_prefix . "orders WHERE order_id=" . $db->tosql($order_id, INTEGER);
    $db->query($sql);
    $db->next_record();
    $t->set_vars($db->Record);
    $t->set_block("payment_info", $payment_info);
    $t->parse("payment_info", false);
    $t->global_parse("payment_info_block", false, false, true);
} else {
    $t->set_var("payment_info_block", "");
}
$current_date = va_time();
$cc_start_years = get_db_values("SELECT start_year AS year_value, start_year AS year_description FROM " . $table_prefix . "cc_start_years", array(array("", YEAR_MSG)));
if (sizeof($cc_start_years) < 2) {
    $cc_start_years = array(array("", YEAR_MSG));
    for ($y = 7; $y >= 0; $y--) {
        $cc_start_years[] = array($current_date[YEAR] - $y, $current_date[YEAR] - $y);
    }
}
$cc_expiry_years = get_db_values("SELECT expiry_year AS year_value, expiry_year AS year_description FROM " . $table_prefix . "cc_expiry_years", array(array("", YEAR_MSG)));
if (sizeof($cc_expiry_years) < 2) {
    $cc_expiry_years = array(array("", YEAR_MSG));
    for ($y = 0; $y <= 7; $y++) {
        $cc_expiry_years[] = array($current_date[YEAR] + $y, $current_date[YEAR] + $y);
    }
}
set_options($cc_start_years, $cc_start_year, "cc_start_year");
Exemplo n.º 4
0
     }
     $r->set_value("email", $r->get_value("login"));
 }
 if ($db_type == "postgre") {
     $user_id = get_db_value(" SELECT NEXTVAL('seq_" . $table_prefix . "users') ");
     $r->change_property("user_id", USE_IN_INSERT, true);
     $r->set_value("user_id", $user_id);
 }
 $registration_date = va_time();
 $r->set_value("registration_date", $registration_date);
 $r->set_value("registration_ip", $user_ip);
 $r->set_value("last_visit_date", $registration_date);
 $r->set_value("last_visit_ip", $user_ip);
 $r->set_value("last_visit_page", get_custom_friendly_url("user_profile.php"));
 if ($is_subscription) {
     $expiry_date = va_time();
     // set expiry_date and suspend_date as yesterday
     $expiry_date_ts = mktime(0, 0, 0, $expiry_date[MONTH], $expiry_date[DAY] - 1, $expiry_date[YEAR]);
     $r->set_value("expiry_date", $expiry_date_ts);
     $r->set_value("suspend_date", $expiry_date_ts);
 }
 $password_encrypt = get_setting_value($settings, "password_encrypt", 0);
 $plain_password = $r->get_value("password");
 set_session("session_plain_password", $plain_password);
 if ($password_encrypt == 1) {
     $r->set_value("password", md5($plain_password));
 }
 if ($r->insert_record()) {
     if ($db_type == "mysql") {
         $user_id = get_db_value(" SELECT LAST_INSERT_ID() ");
         $r->set_value("user_id", $user_id);
Exemplo n.º 5
0
$month_ts = mktime(0, 0, 0, $cmonth, 1, $cyear);
$last_month_ts = mktime(0, 0, 0, $cmonth - 1, 1, $cyear);
$last_month_days = date("t", $last_month_ts);
$last_month_end = mktime(0, 0, 0, $cmonth - 1, $last_month_days, $cyear);
$today_date = va_date($date_edit_format, $today_ts);
$stats = array(array("title" => TODAY_MSG, "date_start" => $today_ts, "date_end" => $today_ts), array("title" => YESTERDAY_MSG, "date_start" => $yesterday_ts, "date_end" => $yesterday_ts), array("title" => LAST_SEVEN_DAYS_MSG, "date_start" => $week_ts, "date_end" => $today_ts), array("title" => THIS_MONTH_MSG, "date_start" => $month_ts, "date_end" => $today_ts), array("title" => LAST_MONTH_MSG, "date_start" => $last_month_ts, "date_end" => $last_month_end));
// get orders stats
for ($i = 0; $i < sizeof($order_statuses); $i++) {
    $status_id = $order_statuses[$i][0];
    $status_name = $order_statuses[$i][1];
    $t->set_var("status_id", $status_id);
    $t->set_var("status_name", get_translation($status_name));
    $t->set_var("stats_periods", "");
    foreach ($stats as $key => $stat_info) {
        $start_date = $stat_info["date_start"];
        $end_date = va_time($stat_info["date_end"]);
        $day_after_end = mktime(0, 0, 0, $end_date[MONTH], $end_date[DAY] + 1, $end_date[YEAR]);
        $sql = " SELECT COUNT(*) FROM " . $table_prefix . "orders ";
        $sql .= " WHERE order_status=" . $db->tosql($status_id, INTEGER);
        $sql .= " AND order_placed_date>=" . $db->tosql($start_date, DATE);
        $sql .= " AND order_placed_date<" . $db->tosql($day_after_end, DATE);
        $period_orders = get_db_value($sql);
        if (isset($stats[$key]["total"])) {
            $stats[$key]["total"] += $period_orders;
        } else {
            $stats[$key]["total"] = $period_orders;
        }
        if ($period_orders > 0) {
            $period_orders = "<a href=\"admin_orders.php?s_os=" . $status_id . "&s_sd=" . va_date($date_edit_format, $start_date) . "&s_ed=" . va_date($date_edit_format, $end_date) . "\"><b>" . $period_orders . "</b></a>";
        }
        $t->set_var("period_orders", $period_orders);
Exemplo n.º 6
0
         }
     }
 }
 if ($generate_serial) {
     for ($sn = $quantity; $sn > 0; $sn--) {
         $serial_number = generate_serial($order_item_id, $sn, $cart_item, $generate_serial);
         if ($serial_number) {
             $ois->set_value("order_item_id", $order_item_id);
             $ois->set_value("item_id", $item_id);
             $ois->set_value("serial_number", $serial_number);
             $ois->set_value("activated", 0);
             $ois->set_value("activations_number", $activations_number);
             $ois->set_value("serial_added", va_time());
             if (strlen($serial_period)) {
                 $serial_expiry = va_timestamp() + intval($serial_period) * 86400;
                 $ois->set_value("serial_expiry", va_time($serial_expiry));
             }
             $ois->insert_record();
         }
     }
 }
 if ($is_gift_voucher) {
     for ($gf = $quantity; $gf > 0; $gf--) {
         $gift_voucher = generate_gift_voucher($order_id, $order_item_id, $item_name, $price_incl_tax);
     }
 }
 // add properties
 if (is_array($properties_info) && sizeof($properties_info) > 0) {
     $oip->set_value("order_item_id", $order_item_id);
     for ($pi = 0; $pi < sizeof($properties_info); $pi++) {
         list($property_id, $control_type, $property_name, $property_value, $pr_add_price, $pr_add_weight, $pr_values, $property_order) = $properties_info[$pi];
Exemplo n.º 7
0
function update_user_status($user_id, $status_id)
{
    global $db, $table_prefix, $settings;
    $current_date = va_time();
    $user_ip = get_ip();
    $admin_id = get_session("session_admin_id");
    // update user status
    $sql = " UPDATE " . $table_prefix . "users SET ";
    $sql .= " is_approved=" . $db->tosql($status_id, INTEGER) . ",";
    if ($admin_id) {
        $sql .= " admin_modified_date=" . $db->tosql($current_date, DATETIME) . ", ";
        $sql .= " admin_modified_ip=" . $db->tosql($user_ip, TEXT);
    } else {
        $sql .= " modified_date=" . $db->tosql($current_date, DATETIME) . ", ";
        $sql .= " modified_ip=" . $db->tosql($user_ip, TEXT);
    }
    $sql .= " WHERE user_id=" . $db->tosql($user_id, INTEGER);
    $db->query($sql);
    // get products settings for user
    $product_settings = array();
    $sql = " SELECT user_type_id ";
    $sql .= " FROM " . $table_prefix . "users ";
    $sql .= " WHERE user_id=" . $db->tosql($user_id, INTEGER);
    $db->query($sql);
    if ($db->next_record()) {
        $type_id = $db->f("user_type_id");
        $setting_type = "user_product_" . $type_id;
        $sql = " SELECT setting_name,setting_value FROM " . $table_prefix . "global_settings ";
        $sql .= " WHERE setting_type=" . $db->tosql($setting_type, TEXT);
        $db->query($sql);
        while ($db->next_record()) {
            $product_settings[$db->f("setting_name")] = $db->f("setting_value");
        }
    }
    $activate_products = get_setting_value($product_settings, "activate_products", 0);
    $deactivate_products = get_setting_value($product_settings, "deactivate_products", 0);
    if ($status_id == 1 && $activate_products == 1) {
        $sql = " UPDATE " . $table_prefix . "items SET is_showing=1 ";
        $sql .= " WHERE user_id=" . $db->tosql($user_id, INTEGER);
        $db->query($sql);
    } elseif ($status_id == 0 && $deactivate_products == 1) {
        $sql = " UPDATE " . $table_prefix . "items SET is_showing=0 ";
        $sql .= " WHERE user_id=" . $db->tosql($user_id, INTEGER);
        $db->query($sql);
    }
}
    }
    $r->validate();
    $password_encrypt = get_setting_value($settings, "password_encrypt", 0);
    if (!$r->is_empty("current_password")) {
        $current_password = $r->get_value("current_password");
        if ($password_encrypt == 1) {
            $password_match = md5($current_password);
        } else {
            $password_match = $current_password;
        }
        $sql = " SELECT password FROM " . $table_prefix . "users WHERE user_id=" . $db->tosql($user_id, INTEGER);
        $sql .= " AND password="******"{field_name}", $r->parameters["current_password"][CONTROL_DESC], INCORRECT_VALUE_MESSAGE);
        }
    }
    if (!strlen($r->errors)) {
        if ($password_encrypt) {
            $r->set_value("password", md5($r->get_value("password")));
        }
        $r->set_value("user_id", $user_id);
        $r->set_value("modified_date", va_time());
        $r->update_record();
        header("Location: " . $return_page . "?updated=password");
        exit;
    }
}
$r->set_parameters();
$block_parsed = true;
$t->parse("block_body", false);