return $discount;
    }
    public static function bcmul($data1, $data2, $prec = 0)
    {
        $result = 0;
        if (function_exists('bcmul')) {
            $result = bcmul($data1, $data2, $prec);
        } else {
            $value = $data1 * $data2;
            if ($prec) {
                $result = round($value, $prec);
            }
        }
        return $result;
    }
    public static function bcsub($data1, $data2, $prec = 0)
    {
        $result = 0;
        if (function_exists('bcsub')) {
            $result = bcsub($data1, $data2, $prec);
        } else {
            $value = $data1 - $data2;
            if ($prec) {
                $result = round($value, $prec);
            }
        }
        return $result;
    }
}
WooRolePricing::init();
 /**
  * Calculates the commissions.
  * Order by priority:
  * 1.- Variation values
  * 2.- Product values
  * 3.- Category values
  * 4.- Default value
  * @param unknown $product
  * @param unknown $variation_id
  * @return number
  */
 public static function get_commission($product, $variation_id, $role = null)
 {
     global $post, $woocommerce;
     global $wp_roles;
     global $current_user;
     if (class_exists('WP_Roles')) {
         if (!isset($wp_roles)) {
             $wp_roles = new WP_Roles();
         }
     }
     get_currentuserinfo();
     $user_roles = $current_user->roles;
     $discount = 0;
     if (sizeof($wp_roles) > 0) {
         if (isset($role)) {
             $first_group = $role;
         } else {
             $first_group = self::get_user_role($user_roles, $product, $variation_id);
         }
         // Variation - custom discount ...
         $custom = get_post_meta($variation_id, 'role_pricing_value_' . $first_group, true);
         if ($custom !== "") {
             $discount = $custom;
         } else {
             // Product - custom discount ...
             $custom = get_post_meta($product->id, 'role_pricing_value_' . $first_group, true);
             // Category - custom discount ....
             if ($custom !== "") {
                 $discount = $custom;
             } else {
                 $categories = wp_get_post_terms($product->id, 'product_cat', array('fields' => 'ids'));
                 if (sizeof($categories) > 0) {
                     $max_cat_id = null;
                     $max_cat_discount = 0;
                     foreach ($categories as $cat_id) {
                         $cat_discount = get_woocommerce_term_meta($cat_id, 'role_pricing_value_' . $first_group, true);
                         if ($cat_discount !== "") {
                             if ($cat_discount > $max_cat_discount) {
                                 $max_cat_discount = $cat_discount;
                                 $max_cat_id = $cat_id;
                             }
                         }
                     }
                     if ($max_cat_id !== null) {
                         $custom = $max_cat_discount;
                     }
                 }
             }
         }
         // general discount ....
         if ($custom !== "") {
             $discount = $custom;
         } else {
             if (get_option("wrp-" . $first_group, "-1") !== "-1") {
                 $discount = get_option("wrp-" . $first_group);
             }
         }
     }
     if ($discount) {
         $method = get_option("wrp-method", "rate");
         if ($method == "rate") {
             $discount = WooRolePricing::bcsub(1, $discount, WOO_ROLE_PRICING_DECIMALS);
             // for security reasons, set 0
             if ($discount < 0) {
                 $discount = 0;
             }
         }
     }
     return $discount;
 }