Mpociot\VatCalculator\VatCalculator::calculate PHP Method

calculate() public method

Calculate the VAT based on the net price, country code and indication if the customer is a company or not.
public calculate ( integer | float $netPrice, null | string $countryCode = null, null | string $postalCode = null, null | boolean $company = null ) : float
$netPrice integer | float The net price to use for the calculation
$countryCode null | string The country code to use for the rate lookup
$postalCode null | string The postal code to use for the rate exception lookup
$company null | boolean
return float
    public function calculate($netPrice, $countryCode = null, $postalCode = null, $company = null)
    {
        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->getTaxRateForLocation($this->getCountryCode(), $this->getPostalCode(), $this->isCompany());
        $this->taxValue = $this->taxRate * $this->netPrice;
        $this->value = $this->netPrice + $this->taxValue;
        return $this->value;
    }

Usage Example

示例#1
0
 /**
  * Returns the tax rate for the given country.
  *
  * @param Request $request
  *
  * @return \Illuminate\Http\Response
  */
 public function calculateGrossPrice(Request $request)
 {
     if (!$request->has('netPrice')) {
         return Response::json(['error' => "The 'netPrice' parameter is missing"], 422);
     }
     $valid_vat_id = null;
     $valid_company = false;
     if ($request->has('vat_number')) {
         $valid_company = $this->validateVATID($request->get('vat_number'));
         $valid_company = $valid_company['is_valid'];
         $valid_vat_id = $valid_company;
     }
     return ['gross_price' => $this->calculator->calculate($request->get('netPrice'), $request->get('country'), $request->get('postal_code'), $valid_company), 'net_price' => $this->calculator->getNetPrice(), 'tax_rate' => $this->calculator->getTaxRate(), 'tax_value' => $this->calculator->getTaxValue(), 'valid_vat_id' => $valid_vat_id];
 }