Jump to content

[SOLVED] Markup and Markdown functions


cmgmyr

Recommended Posts

Hey everyone,

I'm having a little bit of trouble with a markup and markdown function.

 

Here is the code:

function priceMarkUp($price, $comp_data, $dir='up'){
	$debug = TRUE;
	if($comp_data->kiosk->markup_cost1 > 0 && $comp_data->kiosk->markup_cost2 > 0){
		//see which markup it is
		if($price >= $comp_data->kiosk->markup_cost2){
			$markup = $comp_data->kiosk->markup_per3;
		}elseif($price <= $comp_data->kiosk->markup_cost1){
			$markup = $comp_data->kiosk->markup_per1;
		}else{
			$markup = $comp_data->kiosk->markup_per2;
		}

		if($debug) echo "<br />markup = $markup <br />";

		$mod = 10;

		if($dir == 'up'){
			if($markup > 0){
				$total = round($price + ($price * ($markup / 100)));
				if($debug) echo "$total = round($price + ($price * ($markup / 100))) <br />";
			}else{
				$total = $price;
			}

			$mod_total = $total % $mod;
			if($debug) echo "$mod_total = $total % $mod <br />";
			$add = $mod - $mod_total;
			if($debug) echo "$add = $mod - $mod_total <br />";
			if($add == $mod) $add = 0;

			$total += $add;
			if($debug) echo "$total += $add <br />";
		}else{
			if($markup > 0){
				$total = round($price - ($price / ($markup / 100)));
				if($debug) echo "$total = round($price - ($price / ($markup / 100))) <br />";
			}else{
				$total = $price;
			}

			$mod_total = $total % $mod;
			if($debug) echo "$mod_total = $total % $mod <br />";
			$add = $mod_total;
			if($debug) echo "$add = $mod_total <br />";
			if($add == $mod) $add = 0;

			$total -= $add;
			if($debug) echo "$total -= $add <br />";
		}

		return $total;
	}else{
		return $price;
	}
}

$comp_data is an array with different customer data in it. It has the start price and end price for different markups and the actual markups for those levels. Pretty much what I need help with is the actual formula for doing the markups or down. Let me know if you have any questions or if something doesn't make sense.

 

Thanks in advance,

-Chris

Link to comment
https://forums.phpfreaks.com/topic/130490-solved-markup-and-markdown-functions/
Share on other sites

Ok, so after a few hours I finally figured it out. Here is the final code. Hope it helps someone out.

 

function priceMarkUp($price, $comp_data, $dir='up'){
	//$debug = TRUE;
	if($comp_data->kiosk->markup_cost1 > 0 && $comp_data->kiosk->markup_cost2 > 0){
		//see which markup it is
		if($price >= $comp_data->kiosk->markup_cost2){
			$markup = $comp_data->kiosk->markup_per3;
		}elseif($price <= $comp_data->kiosk->markup_cost1){
			$markup = $comp_data->kiosk->markup_per1;
		}else{
			$markup = $comp_data->kiosk->markup_per2;
		}

		if($debug) echo "<br />markup = $markup <br />";

		$mod = 10;

		if($dir == 'up'){
			if($markup > 0){
				$total = round((($markup/100) + 1) * $price);
				if($debug) echo "$total = round((($markup/100) + 1) * $price) <br />";
			}else{
				$total = $price;
			}

			$mod_total = $total % $mod;
			if($debug) echo "$mod_total = $total % $mod <br />";
			$add = $mod - $mod_total;
			if($debug) echo "$add = $mod - $mod_total <br />";
			if($add == $mod) $add = 0;

			$total += $add;
			if($debug) echo "$total += $add <br />";
		}else{
			if($markup > 0){
				$total = round($price/(($markup/100) + 1));
				if($debug) echo "$total = round($price/(($markup/100) + 1)) <br />";
			}else{
				$total = $price;
			}

			$mod_total = $total % $mod;
			if($debug) echo "$mod_total = $total % $mod <br />";
			$add = $mod_total;
			if($debug) echo "$add = $mod_total <br />";
			if($add == $mod) $add = 0;

			$total -= $add;
			if($debug) echo "$total -= $add <br />";
		}

		return $total;
	}else{
		return $price;
	}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.