}
            }
        }
        $smarty->assign('tax_rates', $tax_rates);
        $rate_details = array();
        if (!empty($rate_id) && !empty($tax_rates) && is_array($tax_rates)) {
            $rate_formula = "";
            foreach ($tax_rates as $k => $v) {
                if ($v['rate_id'] == $rate_id) {
                    $rate_details = $v;
                    break;
                }
            }
        }
        $smarty->assign('rate_details', $rate_details);
        $zones = cw_query("SELECT * FROM {$tables['zones']} WHERE is_shipping=0 and warehouse_customer_id = '" . cw_get_default_account_for_zones() . "' ORDER BY zone_name");
        $smarty->assign('zones', $zones);
    }
    if (is_array($taxes_units)) {
        #
        # Correct the tax formula units description
        #
        foreach ($taxes_units as $k => $v) {
            $taxes_units[$k] = cw_get_langvar_by_name($v);
        }
        $_taxes = cw_query("SELECT tax_id, tax_name FROM {$tables['taxes']} WHERE tax_id!='{$tax_id}' ORDER BY tax_name");
        $smarty->assign('taxes_units', $taxes_units);
    }
    $smarty->assign('tax_details', $tax_details);
    $smarty->assign('main', "tax_edit");
} else {
function cw_get_product_tax_rates($product, $user_info, $include_in_any_case = false, $special_taxes = false)
{
    global $tables, $config;
    static $saved_tax_rates = array();
    static $saved_tax = array();
    # Define input data
    $is_array = true;
    if (is_int($product)) {
        $is_array = false;
        $_product = array($product => array('product_id' => $product));
    } elseif (isset($product['product_id'])) {
        $is_array = false;
        $_product = array($product['product_id'] => $product);
    } else {
        $_product = array();
        foreach ($product as $k => $p) {
            $_product[$p['product_id']] = $p;
        }
    }
    unset($product);
    $zone_account = $user_info;
    $company_id = $user_info['company_id'];
    if (isset($saved_tax[$company_id])) {
        return $saved_tax[$company_id];
    }
    if ($special_taxes) {
        $_taxes = cw_func_call('cw_taxes_search', array('product_id' => $product['product_id'], 'data' => array('active' => 1, 'use_info' => 1)));
    } else {
        $_taxes = cw_func_call('cw_taxes_search', array('product_id' => $product['product_id'], 'data' => array('active' => 1)));
    }
    $taxes = array();
    if (is_array($_taxes)) {
        foreach ($_taxes as $k => $v) {
            if ($v['address_type'] == 'O') {
                $v['address_type_real'] = 'O';
                $v['address_type'] = 'B';
                $taxes[] = $v;
                $v['address_type'] = 'S';
            }
            $taxes[] = $v;
        }
    }
    if (empty($taxes) || !is_array($taxes)) {
        return array();
    }
    # Define available customer zones
    $tax_rates = $address_zones = $_tax_names = array();
    $display_including_tax = null;
    if ($include_in_any_case) {
        $display_including_tax = 1;
    }
    foreach ($taxes as $k => $v) {
        $_tax_names["tax_" . $v['tax_id']] = true;
        if ($v['address_type_real'] == 'O') {
            $taxes[$k]['tax_name'] = $v['tax_name'] . '_' . $v['address_type'];
        }
        if (isset($display_including_tax)) {
            $taxes[$k]['display_including_tax'] = $display_including_tax;
        }
    }
    # Get tax names
    $_tax_names = cw_get_languages_alt(array_keys($_tax_names));
    if ($config['Taxes']['enable_user_tax_exemption'] == "Y") {
        #
        # Get the 'tax_exempt' feature of customer
        #
        static $_customer_tax_exempt;
        if (empty($_customer_tax_exempt)) {
            $_customer_tax_exempt = cw_query_first_cell("select tax_exempt from {$tables['customers_customer_info']} where customer_id='{$use_customer_id}'");
        }
        if ($_customer_tax_exempt == "Y") {
            $tax_rate['skip'] = true;
        }
    } else {
        $_customer_tax_exempt = "";
    }
    foreach ($_product as $product_id => $product) {
        if ($product['free_tax'] == 'Y') {
            // || !is_array($_taxes[$product_id]) || empty($_taxes[$product_id]))
            continue;
        }
        # kornev, because all of the products are related with admin and we should get the appropriate settings
        $admin_warehouse = cw_get_default_account_for_zones();
        # Generate tax rates array
        foreach ($taxes as $k => $v) {
            $product['warehouse'] = $admin_warehouse;
            if (!isset($address_zones[$product['warehouse']][$v['address_type']])) {
                $address = $v['address_type'] == 'B' ? $zone_account['main_address'] : $zone_account['current_address'];
                $address_zones[$product['warehouse']][$v['address_type']] = array_keys(cw_call('cw_cart_get_zones', array('address' => $address)));
            }
            $zones = $address_zones[$product['warehouse']][$v['address_type']];
            $tax_rate = array();
            # kornev. one rate can be repeated twice. the "both" address mode.
            if (!empty($zones) && is_array($zones)) {
                foreach ($zones as $zone_id) {
                    $saved_key = $v['tax_id'] . '_' . $v['address_type'] . '_' . $zone_id . '_' . $membership_id;
                    if (isset($saved_tax_rates[$saved_key])) {
                        $tax_rate = $saved_tax_rates[$saved_key];
                    } else {
                        $tax_rate = cw_query_first($sql = "SELECT {$tables['tax_rates']}.tax_id, {$tables['tax_rates']}.formula, {$tables['tax_rates']}.rate_value, {$tables['tax_rates']}.rate_type FROM {$tables['tax_rates']} LEFT JOIN {$tables['tax_rate_memberships']} ON {$tables['tax_rate_memberships']}.rate_id = {$tables['tax_rates']}.rate_id WHERE {$tables['tax_rates']}.tax_id = '{$v['tax_id']}' {$warehouse_condition} AND {$tables['tax_rates']}.zone_id = '{$zone_id}' AND ({$tables['tax_rate_memberships']}.membership_id = '{$membership_id}' OR {$tables['tax_rate_memberships']}.membership_id IS NULL) ORDER BY {$tables['tax_rate_memberships']}.membership_id DESC LIMIT 1");
                        if ($tax_rate) {
                            $tax_rate['address_type'] = $v['address_type'];
                            $tax_rate['address_type_real'] = $v['address_type_real'];
                        }
                        $saved_tax_rates[$saved_key] = $tax_rate;
                    }
                    if (!empty($tax_rate)) {
                        break;
                    }
                }
            }
            if (empty($tax_rate) || $_customer_tax_exempt == "Y") {
                if (!$v['price_includes_tax'] != "Y") {
                    continue;
                }
                $tax_rate = cw_query_first("SELECT {$tables['tax_rates']}.tax_id, {$tables['tax_rates']}.formula, {$tables['tax_rates']}.rate_value, {$tables['tax_rates']}.rate_type FROM {$tables['tax_rates']} LEFT JOIN {$tables['tax_rate_memberships']} ON {$tables['tax_rate_memberships']}.rate_id = {$tables['tax_rates']}.rate_id WHERE {$tables['tax_rates']}.tax_id='{$v['tax_id']}' {$warehouse_condition} AND ({$tables['tax_rate_memberships']}.membership_id = '{$membership_id}' OR {$tables['tax_rate_memberships']}.membership_id IS NULL) ORDER BY {$tables['tax_rates']}.rate_value DESC LIMIT 1");
                $tax_rate['skip'] = true;
            }
            if (empty($tax_rate['formula'])) {
                $tax_rate['formula'] = $v['formula'];
            }
            $tax_rate['rate_value'] *= 1;
            $tax_rate['tax_display_name'] = isset($_tax_names["tax_" . $v['tax_id']]) ? $_tax_names["tax_" . $v['tax_id']] . ($v['address_type_real'] == 'O' ? $v['address_type'] == 'B' ? ' ' . cw_get_langvar_by_name('lbl_tax_billing') : ' ' . cw_get_langvar_by_name('lbl_tax_shipping') : '') : $v['tax_name'];
            if ($is_array) {
                $tax_rates[$product_id][$v['tax_name']] = cw_array_merge($v, $tax_rate);
            } else {
                $tax_rates[$v['tax_name']] = cw_array_merge($v, $tax_rate);
            }
        }
    }
    $saved_tax[$company_id] = $tax_rates;
    return $tax_rates;
}