Skip to content

Instantly share code, notes, and snippets.

@koenhendriks
Created April 18, 2015 11:38
Show Gist options
  • Save koenhendriks/76ab34c123c99b5baf10 to your computer and use it in GitHub Desktop.
Save koenhendriks/76ab34c123c99b5baf10 to your computer and use it in GitHub Desktop.
TaxPrice Class
<?php
/**
* TaxPrice.php
* Created by: koen
* Date: 18-4-15
* Time: 12:41
*/
class TaxPrice {
private $withoutTax;
private $withTax;
private $taxPrice;
private $taxRate = 21;
/**
* Construct TaxPrice and set its properties
*
* @param int $price
* @param bool $isTaxPrice whether the price given is including or excluding taxes
* @param null $taxRate sets the tax rate
*/
public function __construct($price, $isTaxPrice = true, $taxRate = null){
if(!is_null($taxRate))
$this->taxRate = $taxRate;
if($isTaxPrice){
$this->withTax = $price;
$this->taxPrice = ($this->withTax / (100 + $this->taxRate) * $this->taxRate);
$this->withoutTax = $this->withTax - $this->taxPrice;
}else {
$this->withoutTax = $price;
$this->taxPrice = $this->withoutTax * ($this->taxRate / 100);
$this->withTax = $this->withoutTax + $this->taxPrice;
}
}
/**
* Getter for price including taxes
*
* @param bool $decimal whether to send full float or rounded
* @return int
*/
public function withTax($decimal = false){
if($decimal)
return $this->withTax;
return round($this->withTax,2);
}
/**
* Getter for price excluding taxes
*
* @param bool $decimal whether to send full float or rounded
* @return int
*/
public function withoutTax($decimal = false){
if($decimal)
return $this->withoutTax;
return round($this->withoutTax,2);
}
/**
* Getter for amount of tax price on the given tax rate
*
* @param bool $decimal whether to send full float or rounded
* @return int
*/
public function getTaxPrice($decimal = false){
if($decimal)
return $this->taxPrice;
return round($this->taxPrice,2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment