Price Calculation with php depending on tax and discount price and percentage


function priceCalc($price, $discount_price, $discount_percent, $id_tax = -1)
{
    // $baseTax = _s("shop-base-tax");
    // $priceType = _s("shop-base-price");

    $baseTax = 21;
    $priceType = 1;

    // $ci = & get_instance();
    // $ci->load->database();

    // if( $id_tax != -1 ){
    //     $ci->db->select('t.*')
    //         ->from("tax as t")        
    //         ->where("id_tax", $id_tax);    
    //     $query = $ci->db->get();
    //     $baseTax = $query->row()->value;
    // }

    // return var_dump($baseTax);

    // $baseTax = 15;

    $tax = $baseTax; /* == $data_tax->value ? $baseTax : $data_tax->value; */

    $result = new stdClass();
    $result->oldPrice =  $price;

    // return $result->oldPrice;

    if ($discount_price > 0) {
        //samotná hodnota daně
        $taxvalue = ($discount_price / 100) * $tax;

        if ($priceType) {
            $result->newPrice = $discount_price + $taxvalue;
        } else {
            $result->newPrice = $discount_price;
        }
        $result->switch = 1;
    } elseif ($discount_percent > 0) {
        //samotná hodnota daně
        // $taxvalue = ($price / 100) * $tax;
        $taxvalue = ($result->oldPrice / 100) * $tax;


        if ($priceType) {
        	$result->oldPrice = (( $tax + 100 ) / 100 ) * $price;
            // $result->newPrice = $price - ((($price + $taxvalue) / 100) * $discount_percent);
            $result->newPrice = $result->oldPrice - ( ($result->oldPrice / 100) * $discount_percent);
        } else {
            $result->newPrice = $price - (($price / 100) * $discount_percent);
        }

        $result->switch = 1;
    } else {
        //samotná hodnota daně
        // $taxvalue = ($price / 100) * $tax;
        $taxvalue = ($result->oldPrice / 100) * $tax;
        

        if ($priceType) {
        	$result->oldPrice = (( $tax + 100 ) / 100 ) * $price;
            // $result->newPrice = $price + $taxvalue;
            $result->newPrice = $result->oldPrice;
        } else {
            $result->newPrice = $price;
        }
        //        $result->newPrice = $price;
        $result->switch = 0;
    }
    //    $priceFormat = number_format(, 2);
    $priceRound = (int) round($result->newPrice);
    //    $priceReplace = str_replace(",", " ", $priceRound);
    $result->newPrice = $priceRound;

    return $result;
}

var_dump(priceCalc(3469,0,30));

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s