Skip to content

Instantly share code, notes, and snippets.

@keltanas
Last active November 21, 2016 14:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keltanas/44a26483460268923d1f to your computer and use it in GitHub Desktop.
Save keltanas/44a26483460268923d1f to your computer and use it in GitHub Desktop.
Vat calculating
<?php
use Money\Money;
/**
* Class VatCalculatingService
* Calculating VAT
*
* @see: http://www.achievertalk.com/achievertalkwp/2011/06/vat-in-your-young-enterprise-company/
*/
class VatCalculatingService
{
protected $vat;
protected $accuracy;
/**
* @param $vat
* @param $accuracy
*/
public function __construct($vat, $accuracy = 4)
{
$this->vat = $vat;
$this->accuracy = $accuracy;
}
/**
* @param $amount
* @return float
*/
protected function resolvePrice($amount)
{
if (is_numeric($amount)) {
return (float) $amount;
} elseif ($amount instanceof Money) {
return $amount->getAmount() / 100;
} else {
throw new \InvalidArgumentException('Amount must have float or Money type');
}
}
/**
* Calculate the price inclusive of VAT
*
* @param $amount
*
* @return float
*/
public function addingVatToPrice($amount)
{
$amount = $this->resolvePrice($amount);
return round($amount * (1 + ($this->vat / 100)), $this->accuracy);
}
/**
* Calculate how much VAT there is on the price of something that already includes VAT
*
* @param $amountInclusiveVat
*
* @return float
*/
public function calculatingVatElement($amountInclusiveVat)
{
$amount = $this->resolvePrice($amountInclusiveVat);
return round($amount / ($this->vat + 100) * $this->vat, $this->accuracy);
}
/**
* Calculate the price of amount exclusive of VAT
*
* @param $amountInclusiveVat
*
* @return float
*/
public function removingVat($amountInclusiveVat)
{
$amount = $this->resolvePrice($amountInclusiveVat);
return round($amount / ($this->vat + 100) * 100 , $this->accuracy);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment