driesvints / vat-calculator

Handle all the hard stuff related to EU MOSS tax/vat regulations, the way it should be.
MIT License
1.2k stars 88 forks source link

Adding new countries #162

Closed Spryer closed 1 year ago

Spryer commented 1 year ago

Are there methods to add new countries? For example i want to add externally some countries from the gst like Australian or Hongkong? Any ideas?

Spryer commented 1 year ago

Never mind. I just extended the Service, added the new Countries and then used the new methods. Here in the app/service folder

<?php

namespace App\Services;

use Mpociot\VatCalculator\VatCalculator;

class CustomVatCalculator extends VatCalculator
{
    public function getGstRateForCountry($countryCode)
    {
        // Define your GST rates here
        $gstRates = [
            'NZ' => [
                // New Zealand
                'rate' => 0.15,
            ],
            'AU' => [
                // Australia
                'rate' => 0.10,
            ],
            'CA' => [
                // Canada
                'rate' => 0.05,
            ],
            'SG' => [
                // Singapore
                'rate' => 0.07,
            ],
            'MY' => [
                // Malaysia
                'rate' => 0.06,
            ],
            'IN' => [
                // India
                'rate' => 0.18,
            ],
        ];

        if (array_key_exists($countryCode, $gstRates)) {
            return $gstRates[$countryCode];
        } else {
            // Return a default value if the country code doesn't exist
            return ['rate' => 0];
        }
    }

}

and then in the backend where you need it:

use App\Services\CustomVatCalculator;

class Show extends Component
{
        $vatCalculator = new CustomVatCalculator();
        $gstRate = $vatCalculator->getGstRateForCountry($value)['rate'] ?? 0;
.....
}

Just if someone needed the extension