diff --git a/README.md b/README.md
index d385d02..6e3ab03 100644
--- a/README.md
+++ b/README.md
@@ -12,11 +12,8 @@ use Spaze\VatCalculator\VatCalculator;
$vatRates = new VatRates();
$vatCalculator = new VatCalculator($vatRates);
-$countryCode = $vatCalculator->getIpBasedCountry();
-$vatCalculator->calculate( 24.00, $countryCode );
-$vatCalculator->calculate( 24.00, $countryCode, $postalCode );
-$vatCalculator->calculate( 71.00, 'DE', '41352', $isCompany = true );
-$vatCalculator->getTaxRateForLocation( 'NL' );
+$vatCalculator->calculate(71.00, 'DE' /* $countryCode */, '41352' /* $postalCode or null */, true /* Whether the customer you're calculating the VAT for is a company */);
+$vatCalculator->getTaxRateForLocation('NL');
// Check validity of a VAT number
$vatCalculator->isValidVatNumber('NL123456789B01');
```
@@ -26,7 +23,6 @@ $vatCalculator->isValidVatNumber('NL123456789B01');
- [Standalone](#installation-standalone)
- [Usage](#usage)
- [Calculate the gross price](#calculate-the-gross-price)
- - [Receive more information](#receive-more-information)
- [Validate EU VAT numbers](#validate-eu-vat-numbers)
- [Get EU VAT number details](#vat-number-details)
- [Get the IP based country of your user](#get-ip-based-country)
@@ -55,37 +51,36 @@ use Spaze\VatCalculator\VatCalculator;
$vatRates = new VatRates();
$vatCalculator = new VatCalculator($vatRates);
-$vatCalculator->setBusinessCountryCode('DE');
-$countryCode = $vatCalculator->getIpBasedCountry();
-$grossPrice = $vatCalculator->calculate( 49.99, 'LU' );
+$vatCalculator->setBusinessCountryCode('DE'); // Where your company is based in
+$price = $vatCalculator->calculate(49.99, 'LU', null, false);
+$price->getPrice();
+$price->getNetPrice();
+$price->getTaxValue();
+$price->getTaxRate();
```
## Usage
### Calculate the gross price
-To calculate the gross price use the `calculate` method with a net price and a country code as paremeters.
+To calculate the gross price (price with VAT added) use the `calculate` method with a net price, a country code, a postal code (null if unknow) and whether you're calculating VAT for a customer that's a company as paremeters.
```php
-$grossPrice = $vatCalculator->calculate( 24.00, 'DE' );
+$grossPrice = $vatCalculator->calculate(24.00, 'DE', null, false);
```
-The third parameter is the postal code of the customer.
+The third parameter is the postal code of the customer, pass `null` if unknown.
As a fourth parameter, you can pass in a boolean indicating whether the customer is a company or a private person. If the customer is a company, which you should check by validating the VAT number, the net price gets returned.
+Fifth parameter defines which VAT rate to use if there are more defined for the particular country (`VatRates::HIGH`, `VatRates::LOW`, `VatRates::GENERAL` is the default when just one rate is defined).
+Returns `VatPrice` object:
```php
-$grossPrice = $vatCalculator->calculate( 24.00, 'DE', '12345', $isCompany = true );
-```
-
-### Receive more information
-After calculating the gross price you can extract more information from the VatCalculator.
+$grossPrice->getPrice();
+$grossPrice->getNetPrice();
+$grossPrice->getTaxValue();
+$grossPrice->getTaxRate();
-```php
-$grossPrice = $vatCalculator->calculate( 24.00, 'DE' ); // 28.56
-$taxRate = $vatCalculator->getTaxRate(); // 0.19
-$netPrice = $vatCalculator->getNetPrice(); // 24.00
-$taxValue = $vatCalculator->getTaxValue(); // 4.56
```
@@ -109,7 +104,7 @@ This service relies on a third party SOAP API provided by the EU. If, for whatev
```php
try {
$validVat = $vatCalculator->isValidVatNumber('NL 123456789 B01');
-} catch( VatCheckUnavailableException $e ){
+} catch (VatCheckUnavailableException $e) {
// Please handle me
}
```
@@ -136,7 +131,7 @@ try {
[requestId:VatDetails:private] => FOOBAR338
)
*/
-} catch( VatCheckUnavailableException $e ){
+} catch (VatCheckUnavailableException $e) {
// Please handle me
}
```
diff --git a/UPGRADE.md b/UPGRADE.md
index d4014b7..04732d7 100644
--- a/UPGRADE.md
+++ b/UPGRADE.md
@@ -5,6 +5,8 @@
* Exceptions (`VatCheckUnavailableException`) are always thrown, `forwardSoapFaults` option has been removed (**BC BREAK**)
* Some countries have various VAT rates depending on location resulting in `getTaxRateForCountry()` removal, use `getTaxRateForLocation()` instead (**BC BREAK**)
* Rates have been moved to a separate class `VatRates`, you need to pass the class to `VatCalculator` constructor (**BC BREAK**)
+* `calculate()` & `calculateNet()` methods return `VatPrice` object instead of the calculated price (**BR BREAK**)
+* After running `calculate()` & `calculateNet()`, the `VatCalculator` object keeps its state, `getNetPrice()`, `getTaxRate()`, `getCountryCode()`, `setCountryCode()`, `getPostalCode()`, `setPostalCode()`, `isCompany()`, `setCompany()` removed (**BR BREAK**)
* Norway VAT rate removed, can be manually added back with `VatRates::addRateForCountry()`
* `getIPBasedCountry()` & `getClientIP()` methods have been removed, use some other package (or `CF-IPCountry` HTTP header if you're behind Cloudflare)
* Some methods have been properly *camelCased*: methods like `getClientIP()` -> `getClientIp()` and `shouldCollectVAT` -> `shouldCollectVat` and a few more
diff --git a/src/VatCalculator.php b/src/VatCalculator.php
index 2ac2653..b5417f4 100644
--- a/src/VatCalculator.php
+++ b/src/VatCalculator.php
@@ -23,24 +23,6 @@ class VatCalculator
/** @var VatRates */
private $vatRates;
- /** @var float */
- private $netPrice = 0.0;
-
- /** @var string */
- private $countryCode;
-
- /** @var string */
- private $postalCode;
-
- /** @var float */
- private $taxValue = 0;
-
- /** @var float */
- private $taxRate = 0;
-
- /** @var bool */
- private $company = false;
-
/** @var string */
private $businessCountryCode;
@@ -65,27 +47,17 @@ public function shouldCollectVat(string $countryCode): bool
* customer is a company or not.
*
* @param float $netPrice
- * @param string|null $countryCode
+ * @param string $countryCode
* @param string|null $postalCode
- * @param bool|null $company
- * @param string|null $type
- * @return float
+ * @param bool $company
+ * @param string $type
+ * @return VatPrice
*/
- public function calculate(float $netPrice, ?string $countryCode = null, ?string $postalCode = null, ?bool $company = null, ?string $type = VatRates::GENERAL): float
+ public function calculate(float $netPrice, string $countryCode, ?string $postalCode, bool $company, string $type = VatRates::GENERAL): VatPrice
{
- if ($countryCode) {
- $this->setCountryCode($countryCode);
- }
- if ($postalCode) {
- $this->setPostalCode($postalCode);
- }
- if (!is_null($company) && $company !== $this->isCompany()) {
- $this->setCompany($company);
- }
- $this->netPrice = floatval($netPrice);
- $this->taxRate = $this->getCountryCode() === null ? 0 : $this->getTaxRateForLocation($this->getCountryCode(), $this->getPostalCode(), $this->isCompany(), $type);
- $this->taxValue = $this->taxRate * $this->netPrice;
- return $this->netPrice + $this->taxValue;
+ $taxRate = $this->getTaxRateForLocation($countryCode, $postalCode, $company, $type);
+ $taxValue = $taxRate * $netPrice;
+ return new VatPrice($netPrice, $netPrice + $taxValue, $taxValue, $taxRate);
}
@@ -94,78 +66,17 @@ public function calculate(float $netPrice, ?string $countryCode = null, ?string
* customer is a company or not.
*
* @param float $gross
- * @param string|null $countryCode
+ * @param string $countryCode
* @param string|null $postalCode
- * @param bool|null $company
- * @param string|null $type
- * @return float
+ * @param bool $company
+ * @param string $type
+ * @return VatPrice
*/
- public function calculateNet(float $gross, ?string $countryCode = null, ?string $postalCode = null, ?bool $company = null, ?string $type = VatRates::GENERAL): float
- {
- if ($countryCode) {
- $this->setCountryCode($countryCode);
- }
- if ($postalCode) {
- $this->setPostalCode($postalCode);
- }
- if (!is_null($company) && $company !== $this->isCompany()) {
- $this->setCompany($company);
- }
-
- $value = floatval($gross);
- $this->taxRate = $this->getCountryCode() === null ? 0 : $this->getTaxRateForLocation($this->getCountryCode(), $this->getPostalCode(), $this->isCompany(), $type);
- $this->taxValue = $this->taxRate > 0 ? $value / (1 + $this->taxRate) * $this->taxRate : 0;
- $this->netPrice = $value - $this->taxValue;
-
- return $this->netPrice;
- }
-
-
- public function getNetPrice(): float
- {
- return $this->netPrice;
- }
-
-
- public function getCountryCode(): ?string
- {
- return $this->countryCode ? strtoupper($this->countryCode) : null;
- }
-
-
- public function setCountryCode(string $countryCode): void
- {
- $this->countryCode = $countryCode;
- }
-
-
- public function getPostalCode(): ?string
- {
- return $this->postalCode;
- }
-
-
- public function setPostalCode(string $postalCode): void
- {
- $this->postalCode = $postalCode;
- }
-
-
- public function getTaxRate(): float
- {
- return $this->taxRate;
- }
-
-
- public function isCompany(): bool
+ public function calculateNet(float $gross, string $countryCode, ?string $postalCode, bool $company, string $type = VatRates::GENERAL): VatPrice
{
- return $this->company;
- }
-
-
- public function setCompany(bool $company): void
- {
- $this->company = $company;
+ $taxRate = $this->getTaxRateForLocation($countryCode, $postalCode, $company, $type);
+ $taxValue = $taxRate > 0 ? $gross / (1 + $taxRate) * $taxRate : 0;
+ return new VatPrice($gross - $taxValue, $gross, $taxValue, $taxRate);
}
@@ -192,7 +103,7 @@ public function setBusinessVatNumber(string $businessVatNumber): void
* @param string|null $type
* @return float
*/
- public function getTaxRateForLocation(string $countryCode, ?string $postalCode = null, bool $company = false, ?string $type = null): float
+ public function getTaxRateForLocation(string $countryCode, ?string $postalCode, bool $company, ?string $type = null): float
{
if ($company && strtoupper($countryCode) !== $this->businessCountryCode) {
return 0;
@@ -202,12 +113,6 @@ public function getTaxRateForLocation(string $countryCode, ?string $postalCode =
}
- public function getTaxValue(): float
- {
- return $this->taxValue;
- }
-
-
/**
* @param string $vatNumber
* @return bool
diff --git a/src/VatPrice.php b/src/VatPrice.php
new file mode 100644
index 0000000..319a345
--- /dev/null
+++ b/src/VatPrice.php
@@ -0,0 +1,56 @@
+netPrice = $netPrice;
+ $this->price = $price;
+ $this->taxValue = $taxValue;
+ $this->taxRate = $taxRate;
+ }
+
+
+ public function getNetPrice(): float
+ {
+ return $this->netPrice;
+ }
+
+
+ public function getPrice(): float
+ {
+ return $this->price;
+ }
+
+
+ public function getTaxValue(): float
+ {
+ return $this->taxValue;
+ }
+
+
+ public function getTaxRate(): float
+ {
+ return $this->taxRate;
+ }
+
+}
diff --git a/src/VatRates.php b/src/VatRates.php
index 627e54d..68c0036 100644
--- a/src/VatRates.php
+++ b/src/VatRates.php
@@ -365,7 +365,7 @@ public function shouldCollectVat(string $countryCode): bool
* @param string|null $type
* @return float
*/
- public function getTaxRateForLocation(string $countryCode, ?string $postalCode = null, ?string $type = self::GENERAL): float
+ public function getTaxRateForLocation(string $countryCode, ?string $postalCode, ?string $type = self::GENERAL): float
{
if (isset($this->postalCodeExceptions[$countryCode]) && $postalCode !== null) {
foreach ($this->postalCodeExceptions[$countryCode] as $postalCodeException) {
@@ -380,7 +380,7 @@ public function getTaxRateForLocation(string $countryCode, ?string $postalCode =
}
}
- if ($type !== null) {
+ if ($type !== VatRates::GENERAL) {
return isset($this->taxRules[strtoupper($countryCode)]['rates'][$type]) ? $this->taxRules[strtoupper($countryCode)]['rates'][$type] : 0;
}
diff --git a/tests/VatCalculatorTest.php b/tests/VatCalculatorTest.php
index 72345f1..27d10a1 100644
--- a/tests/VatCalculatorTest.php
+++ b/tests/VatCalculatorTest.php
@@ -26,99 +26,31 @@ protected function setUp()
}
- public function testCalculateVatWithoutCountry()
+ public function testCalculateVat()
{
- $net = 25.00;
-
- $result = $this->vatCalculator->calculate($net);
- $this->assertEquals(25.00, $result);
+ $result = $this->vatCalculator->calculate(24.00, 'DE', null, false);
+ $this->assertEquals(28.56, $result->getPrice());
+ $this->assertEquals(0.19, $result->getTaxRate());
+ $this->assertEquals(4.56, $result->getTaxValue());
+
+ $result = $this->vatCalculator->calculate(24.00, 'DE', null, true);
+ $this->assertEquals(24.00, $result->getPrice());
+ $this->assertEquals(0, $result->getTaxRate());
+ $this->assertEquals(0, $result->getTaxValue());
+
+ $result = $this->vatCalculator->calculate(24.00, 'XXX', null, true);
+ $this->assertEquals(24.00, $result->getPrice());
+ $this->assertEquals(0.00, $result->getTaxRate());
+ $this->assertEquals(0.00, $result->getTaxValue());
}
- public function testCalculateVatWithPredefinedRules()
+ public function testGetTaxRateForLocation()
{
- $net = 24.00;
- $countryCode = 'DE';
-
- $result = $this->vatCalculator->calculate($net, $countryCode);
- $this->assertEquals(28.56, $result);
- $this->assertEquals(0.19, $this->vatCalculator->getTaxRate());
- $this->assertEquals(4.56, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testCalculateVatWithCountryPreviousSet()
- {
- $net = 24.00;
- $countryCode = 'DE';
-
- $this->vatCalculator->setCountryCode($countryCode);
- $result = $this->vatCalculator->calculate($net);
- $this->assertEquals(28.56, $result);
- $this->assertEquals(0.19, $this->vatCalculator->getTaxRate());
- $this->assertEquals(4.56, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testCalculateVatWithCountryAndCompany()
- {
- $net = 24.00;
- $countryCode = 'DE';
- $postalCode = null;
- $company = true;
-
- $result = $this->vatCalculator->calculate($net, $countryCode, $postalCode, $company);
- $this->assertEquals(24.00, $result);
- $this->assertEquals(0, $this->vatCalculator->getTaxRate());
- $this->assertEquals(0, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testCalculateVatWithCountryAndCompanySet()
- {
- $net = 24.00;
- $countryCode = 'DE';
- $company = true;
-
- $this->vatCalculator->setCompany($company);
- $result = $this->vatCalculator->calculate($net, $countryCode);
- $this->assertEquals(24.00, $result);
- $this->assertEquals(24.00, $this->vatCalculator->getNetPrice());
- $this->assertEquals(0, $this->vatCalculator->getTaxRate());
- $this->assertEquals(0, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testCalculateVatWithCountryAndCompanyBothSet()
- {
- $net = 24.00;
- $countryCode = 'DE';
- $company = true;
-
- $this->vatCalculator->setCountryCode($countryCode);
- $this->vatCalculator->setCompany($company);
- $result = $this->vatCalculator->calculate($net);
- $this->assertEquals(24.00, $result);
- $this->assertEquals(0, $this->vatCalculator->getTaxRate());
- $this->assertEquals(0, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testGetTaxRateForLocationWithCountry()
- {
- $countryCode = 'DE';
-
- $result = $this->vatCalculator->getTaxRateForLocation($countryCode);
+ $result = $this->vatCalculator->getTaxRateForLocation('DE', null, false);
$this->assertEquals(0.19, $result);
- }
-
- public function testGetTaxRateForLocationWithCountryAndCompany()
- {
- $countryCode = 'DE';
- $company = true;
-
- $result = $this->vatCalculator->getTaxRateForLocation($countryCode, null, $company);
+ $result = $this->vatCalculator->getTaxRateForLocation('DE', null, true);
$this->assertEquals(0, $result);
}
@@ -258,92 +190,67 @@ public function testCannotValidateVatNumberWhenServiceIsDown()
}
- public function testCompanyInBusinessCountryGetsValidVatRateDirectSet()
+ public function testSetBusinessCountryCode()
{
- $net = 24.00;
- $countryCode = 'DE';
-
$this->vatCalculator->setBusinessCountryCode('DE');
- $result = $this->vatCalculator->calculate($net, $countryCode, null, true);
- $this->assertEquals(28.56, $result);
- $this->assertEquals(0.19, $this->vatCalculator->getTaxRate());
- $this->assertEquals(4.56, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testCompanyOutsideBusinessCountryGetsValidVatRate()
- {
- $net = 24.00;
- $countryCode = 'DE';
+ $result = $this->vatCalculator->calculate(24.00, 'DE', null, true);
+ $this->assertEquals(28.56, $result->getPrice());
+ $this->assertEquals(0.19, $result->getTaxRate());
+ $this->assertEquals(4.56, $result->getTaxValue());
$this->vatCalculator->setBusinessCountryCode('NL');
- $result = $this->vatCalculator->calculate($net, $countryCode, null, true);
- $this->assertEquals(24.00, $result);
- $this->assertEquals(0.00, $this->vatCalculator->getTaxRate());
- $this->assertEquals(0.00, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testReturnsZeroForInvalidCountryCode()
- {
- $net = 24.00;
- $countryCode = 'XXX';
-
- $result = $this->vatCalculator->calculate($net, $countryCode, null, true);
- $this->assertEquals(24.00, $result);
- $this->assertEquals(0.00, $this->vatCalculator->getTaxRate());
- $this->assertEquals(0.00, $this->vatCalculator->getTaxValue());
+ $result = $this->vatCalculator->calculate(24.00, 'DE', null, true);
+ $this->assertEquals(24.00, $result->getPrice());
+ $this->assertEquals(0.00, $result->getTaxRate());
+ $this->assertEquals(0.00, $result->getTaxValue());
}
public function testChecksPostalCodeForVatExceptions()
{
- $net = 24.00;
$postalCode = '27498'; // Heligoland
- $result = $this->vatCalculator->calculate($net, 'DE', $postalCode, false);
- $this->assertEquals(24.00, $result);
- $this->assertEquals(0.00, $this->vatCalculator->getTaxRate());
- $this->assertEquals(0.00, $this->vatCalculator->getTaxValue());
+ $result = $this->vatCalculator->calculate(24.00, 'DE', $postalCode, false);
+ $this->assertEquals(24.00, $result->getPrice());
+ $this->assertEquals(0.00, $result->getTaxRate());
+ $this->assertEquals(0.00, $result->getTaxValue());
$postalCode = '6691'; // Jungholz
- $result = $this->vatCalculator->calculate($net, 'AT', $postalCode, false);
- $this->assertEquals(28.56, $result);
- $this->assertEquals(0.19, $this->vatCalculator->getTaxRate());
- $this->assertEquals(4.56, $this->vatCalculator->getTaxValue());
+ $result = $this->vatCalculator->calculate(24.00, 'AT', $postalCode, false);
+ $this->assertEquals(28.56, $result->getPrice());
+ $this->assertEquals(0.19, $result->getTaxRate());
+ $this->assertEquals(4.56, $result->getTaxValue());
$postalCode = 'BFPO58'; // Dhekelia
- $result = $this->vatCalculator->calculate($net, 'GB', $postalCode, false);
- $this->assertEquals(28.56, $result);
- $this->assertEquals(0.19, $this->vatCalculator->getTaxRate());
- $this->assertEquals(4.56, $this->vatCalculator->getTaxValue());
+ $result = $this->vatCalculator->calculate(24.00, 'GB', $postalCode, false);
+ $this->assertEquals(28.56, $result->getPrice());
+ $this->assertEquals(0.19, $result->getTaxRate());
+ $this->assertEquals(4.56, $result->getTaxValue());
$postalCode = '9122'; // Madeira
- $result = $this->vatCalculator->calculate($net, 'PT', $postalCode, false);
- $this->assertEquals(29.28, $result);
- $this->assertEquals(0.22, $this->vatCalculator->getTaxRate());
- $this->assertEquals(5.28, $this->vatCalculator->getTaxValue());
+ $result = $this->vatCalculator->calculate(24.00, 'PT', $postalCode, false);
+ $this->assertEquals(29.28, $result->getPrice());
+ $this->assertEquals(0.22, $result->getTaxRate());
+ $this->assertEquals(5.28, $result->getTaxValue());
}
public function testPostalCodesWithoutExceptionsGetStandardRate()
{
- $net = 24.00;
-
// Invalid post code
$postalCode = 'IGHJ987ERT35';
- $result = $this->vatCalculator->calculate($net, 'ES', $postalCode, false);
+ $result = $this->vatCalculator->calculate(24.00, 'ES', $postalCode, false);
//Expect standard rate for Spain
- $this->assertEquals(29.04, $result);
- $this->assertEquals(0.21, $this->vatCalculator->getTaxRate());
- $this->assertEquals(5.04, $this->vatCalculator->getTaxValue());
+ $this->assertEquals(29.04, $result->getPrice());
+ $this->assertEquals(0.21, $result->getTaxRate());
+ $this->assertEquals(5.04, $result->getTaxValue());
// Valid UK post code
$postalCode = 'S1A 2AA';
- $result = $this->vatCalculator->calculate($net, 'GB', $postalCode, false);
+ $result = $this->vatCalculator->calculate(24.00, 'GB', $postalCode, false);
//Expect standard rate for UK
- $this->assertEquals(28.80, $result);
- $this->assertEquals(0.20, $this->vatCalculator->getTaxRate());
- $this->assertEquals(4.80, $this->vatCalculator->getTaxValue());
+ $this->assertEquals(28.80, $result->getPrice());
+ $this->assertEquals(0.20, $result->getTaxRate());
+ $this->assertEquals(4.80, $result->getTaxValue());
}
@@ -356,110 +263,31 @@ public function testShouldCollectVat()
}
- public function testCalculateNetPriceWithoutCountry()
+ public function testCalculateNet()
{
- $gross = 25.00;
-
- $result = $this->vatCalculator->calculateNet($gross);
- $this->assertEquals(25.00, $result);
- }
-
-
- public function testCalculateNetPriceWithPredefinedRules()
- {
- $gross = 28.56;
- $countryCode = 'DE';
-
- $result = $this->vatCalculator->calculateNet($gross, $countryCode);
- $this->assertEquals(24.00, $result);
- $this->assertEquals(0.19, $this->vatCalculator->getTaxRate());
- $this->assertEquals(4.56, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testCalculateNetPriceWithCountryPreviousSet()
- {
- $gross = 28.56;
- $countryCode = 'DE';
-
- $this->vatCalculator->setCountryCode($countryCode);
-
- $result = $this->vatCalculator->calculateNet($gross);
- $this->assertEquals(24.00, $result);
- $this->assertEquals(0.19, $this->vatCalculator->getTaxRate());
- $this->assertEquals(4.56, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testCalculateNetPriceWithCountryAndCompany()
- {
- $gross = 28.56;
- $countryCode = 'DE';
- $postalCode = null;
- $company = true;
-
- $result = $this->vatCalculator->calculateNet($gross, $countryCode, $postalCode, $company);
- $this->assertEquals(28.56, $result);
- $this->assertEquals(0, $this->vatCalculator->getTaxRate());
- $this->assertEquals(0, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testCalculateNetPriceWithCountryAndCompanySet()
- {
- $gross = 24.00;
- $countryCode = 'DE';
- $company = true;
-
- $this->vatCalculator->setCompany($company);
- $result = $this->vatCalculator->calculateNet($gross, $countryCode);
- $this->assertEquals(24.00, $result);
- $this->assertEquals(24.00, $this->vatCalculator->getNetPrice());
- $this->assertEquals(0, $this->vatCalculator->getTaxRate());
- $this->assertEquals(0, $this->vatCalculator->getTaxValue());
- }
-
-
- public function testCalculateNetPriceWithCountryAndCompanyBothSet()
- {
- $gross = 24.00;
- $countryCode = 'DE';
- $company = true;
-
- $this->vatCalculator->setCountryCode($countryCode);
- $this->vatCalculator->setCompany($company);
- $result = $this->vatCalculator->calculateNet($gross);
- $this->assertEquals(24.00, $result);
- $this->assertEquals(0, $this->vatCalculator->getTaxRate());
- $this->assertEquals(0, $this->vatCalculator->getTaxValue());
+ $result = $this->vatCalculator->calculateNet(28.56, 'DE', null, false);
+ $this->assertEquals(24.00, $result->getNetPrice());
+ $this->assertEquals(0.19, $result->getTaxRate());
+ $this->assertEquals(4.56, $result->getTaxValue());
+
+ $result = $this->vatCalculator->calculateNet(28.56, 'DE', null, true);
+ $this->assertEquals(28.56, $result->getPrice());
+ $this->assertEquals(0, $result->getTaxRate());
+ $this->assertEquals(0, $result->getTaxValue());
}
public function testCalculateHighVatType()
{
- $gross = 24.00;
- $countryCode = 'NL';
- $company = false;
- $type = 'high';
- $postalCode = null;
-
- $result = $this->vatCalculator->calculate($gross, $countryCode, $postalCode, $company, $type);
-
- $this->assertEquals(29.04, $result);
+ $result = $this->vatCalculator->calculate(24.00, 'NL', null, false, VatRates::HIGH);
+ $this->assertEquals(29.04, $result->getPrice());
}
public function testCalculateLowVatType()
{
- $gross = 24.00;
- $countryCode = 'NL';
- $company = false;
- $type = 'low';
- $postalCode = null;
-
- $result = $this->vatCalculator->calculate($gross, $countryCode, $postalCode, $company, $type);
-
- $this->assertEquals(26.16, $result);
+ $result = $this->vatCalculator->calculate(24.00, 'NL', null, false, VatRates::LOW);
+ $this->assertEquals(26.16, $result->getPrice());
}
}
diff --git a/tests/VatRatesTest.php b/tests/VatRatesTest.php
index ef7eae9..5e620da 100644
--- a/tests/VatRatesTest.php
+++ b/tests/VatRatesTest.php
@@ -22,10 +22,10 @@ public function testAddRateKnownCountry(): void
{
$country = 'nO';
$this->assertFalse($this->vatRates->shouldCollectVat($country));
- $this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country));
+ $this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country, null));
$this->vatRates->addRateForCountry($country);
$this->assertTrue($this->vatRates->shouldCollectVat($country));
- $this->assertEquals(0.25, $this->vatRates->getTaxRateForLocation($country));
+ $this->assertEquals(0.25, $this->vatRates->getTaxRateForLocation($country, null));
}
@@ -33,10 +33,10 @@ public function testAddRateUnknownCountry(): void
{
$country = 'yEs';
$this->assertFalse($this->vatRates->shouldCollectVat($country));
- $this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country));
+ $this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country, null));
$this->vatRates->addRateForCountry($country);
$this->assertFalse($this->vatRates->shouldCollectVat($country));
- $this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country));
+ $this->assertEquals(0, $this->vatRates->getTaxRateForLocation($country, null));
}
}