Skip to content

Instantly share code, notes, and snippets.

@koenhendriks
Last active August 29, 2015 14:24
Show Gist options
  • Save koenhendriks/0ad92bb0fb7aec739d47 to your computer and use it in GitHub Desktop.
Save koenhendriks/0ad92bb0fb7aec739d47 to your computer and use it in GitHub Desktop.
TaxPrice Object
/**
* TaxPrice
* Created by: Koen
* Date: 6-7-15
* Time: 10:50
*
* @param price
* @param isTaxPrice
* @param taxRate
* @constructor
*/
function TaxPrice(price, isTaxPrice, taxRate) {
var defaultTaxRate = 21;
if(taxRate === undefined)
this.taxRate = defaultTaxRate;
else
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 decimal whether to return full float or rounded
* @returns float
*/
this.getWithTax = function(decimal){
if(decimal)
return this.withTax;
return this.withTax.toFixed(2);
};
/**
* Getter for price excluding taxes
*
* @param decimal whether to return full float or rounded
* @returns float
*/
this.getWithoutTax = function(decimal){
if(decimal)
return this.withoutTax;
return this.withoutTax.toFixed(2);
};
/**
* Getter for amount of tax price on the given tax rate
*
* @param decimal whether to return full float or rounded
* @returns float
*/
this.getTaxPrice = function(decimal) {
if(decimal)
return this.taxPrice;
return this.taxPrice.toFixed(2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment