<?php
class SOFT79_Rule_Helpers {
    /**
     *  Get price incl/excl tax (depends on setting "Prices Entered With Tax" in WooCommerce)
     */
    static function get_taxed_price( $product, $price ) {
        if ( get_option('woocommerce_prices_include_tax') == 'yes' ) {
            return $product->get_price_including_tax(1, $price);
        } else {
            return $product->get_price_excluding_tax(1, $price);
        }
    }    
    /**     
     *  Get price incl/excl tax for cart (depends on setting "woocommerce_tax_display_cart" in WooCommerce)     
     */
    static function get_cart_taxed_price( $product, $price, $cart = null ) {
        if ($cart == null) {
            $cart = WC()->cart;
        }
        if ( $cart->tax_display_cart == 'excl' ) {
            return $product->get_price_excluding_tax(1, $price);
        } else {
            return $product->get_price_including_tax(1, $price);
        }
    }
    
    //New price can be any price or a discount percentage
    static function get_relative_price( $orig_price, $new_price) {
        if ($new_price == '') {
            return $orig_price;
        }
        
        if ( strstr( $new_price, '%' ) ) {
            $percent = str_replace( '%', '', $new_price ) / 100;
            if ( $percent < 0 ) {
                $percent = 1 - abs($percent); //if negative, treat it as a % discount
            }
            $return = $orig_price * $percent;
        } elseif ($new_price < 0) {
            //Negative value; calculate difference
            $return = $orig_price + $new_price;
        } else {
            $return = $new_price;
        }
        //error_log("get_relative_price of " . $orig_price . " , " . $new_price . " is: " . $return);
        
        return $return;
    }
}
