Mpociot\VatCalculator\VatCalculator::getTaxRateForLocation PHP Méthode

getTaxRateForLocation() public méthode

If a postal code is provided, it will try to lookup the different postal code exceptions that are possible.
public getTaxRateForLocation ( string $countryCode, string | null $postalCode = null, boolean | false $company = false ) : float
$countryCode string
$postalCode string | null
$company boolean | false
Résultat float
    public function getTaxRateForLocation($countryCode, $postalCode = null, $company = false)
    {
        if ($company && strtoupper($countryCode) !== strtoupper($this->businessCountryCode)) {
            return 0;
        }
        $taxKey = 'vat_calculator.rules.' . strtoupper($countryCode);
        if (isset($this->config) && $this->config->has($taxKey)) {
            return $this->config->get($taxKey, 0);
        }
        if (isset($this->postalCodeExceptions[$countryCode]) && $postalCode !== null) {
            foreach ($this->postalCodeExceptions[$countryCode] as $postalCodeException) {
                if (!preg_match($postalCodeException['postalCode'], $postalCode)) {
                    continue;
                }
                if (isset($postalCodeException['name'])) {
                    return $this->taxRules[$postalCodeException['code']]['exceptions'][$postalCodeException['name']];
                }
                return $this->taxRules[$postalCodeException['code']]['rate'];
            }
        }
        return isset($this->taxRules[strtoupper($countryCode)]['rate']) ? $this->taxRules[strtoupper($countryCode)]['rate'] : 0;
    }

Usage Example

 /**
  * Returns the tax rate for the given country code and postal code.
  *
  * @return \Illuminate\Http\Response
  */
 public function getTaxRateForLocation($countryCode = null, $postalCode = null)
 {
     return ['tax_rate' => $this->calculator->getTaxRateForLocation($countryCode, $postalCode)];
 }