Tax Providers

This commit is contained in:
David Bomba 2023-03-27 07:14:10 +11:00
parent 0d38ab1cfb
commit 046a72326e
5 changed files with 131 additions and 3 deletions

View File

@ -108,7 +108,7 @@ class Rule implements RuleInterface
public function tax(): self public function tax(): self
{ {
$this->tax_name1 = 21; $this->tax_name1 = $this->vat_rate;
$this->tax_rate1 = "VAT"; $this->tax_rate1 = "VAT";
return $this; return $this;

View File

@ -0,0 +1,20 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Tax\Providers;
interface TaxProviderInterface
{
public function run();
public function setApiCredentials(mixed $credentials);
}

View File

@ -14,12 +14,14 @@ namespace App\Services\Tax\Providers;
use Illuminate\Http\Client\Response; use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
class ZipTax class ZipTax implements TaxProviderInterface
{ {
private string $endpoint = 'https://api.zip-tax.com/request/v40'; private string $endpoint = 'https://api.zip-tax.com/request/v40';
public function __construct(protected string $api_key, protected string $address, protected ?string $postal_code) private string $api_key = '';
public function __construct(protected array $address)
{ {
} }
@ -43,6 +45,13 @@ class ZipTax
} }
public function setApiCredentials($api_key): self
{
$this->api_key = $api_key;
return $this;
}
/** /**
* callApi * callApi
* *

View File

@ -13,13 +13,106 @@ namespace App\Services\Tax;
use App\Models\Client; use App\Models\Client;
use App\Models\Company; use App\Models\Company;
use App\Services\Tax\Providers\ZipTax;
class TaxService class TaxService
{ {
private string $provider = ZipTax::class;
private mixed $api_credentials;
public function __construct(protected Company $company, protected Client $client) public function __construct(protected Company $company, protected Client $client)
{ {
} }
public function updateCompanyTaxData(): self
{
$this->configureProvider($this->provider); //hard coded for now to one provider, but we'll be able to swap these out later
$company_details = [
'address1' => $this->company->settings->address1,
'address2' => $this->company->settings->address2,
'city' => $this->company->settings->city,
'state' => $this->company->settings->state,
'postal_code' => $this->company->settings->postal_code,
'country_id' => $this->company->settings->country_id,
];
$tax_provider = new $this->provider($company_details);
$tax_provider->setApiCredentials($this->api_credentials);
$tax_data = $tax_provider->run();
$this->company->tax_data = $tax_data;
$this->company->save();
return $this;
}
public function updateClientTaxData(): self
{
$this->configureProvider($this->provider); //hard coded for now to one provider, but we'll be able to swap these out later
$billing_details =[
'address1' => $this->client->address1,
'address2' => $this->client->address2,
'city' => $this->client->city,
'state' => $this->client->state,
'postal_code' => $this->client->postal_code,
'country_id' => $this->client->country_id,
];
$shipping_details =[
'address1' => $this->client->shipping_address1,
'address2' => $this->client->shipping_address2,
'city' => $this->client->shipping_city,
'state' => $this->client->shipping_state,
'postal_code' => $this->client->shipping_postal_code,
'country_id' => $this->client->shipping_country_id,
];
$tax_provider = new $this->provider();
$tax_provider->setApiCredentials($this->api_credentials);
$tax_data = $tax_provider->run();
$this->company->tax_data = $tax_data;
$this->company->save();
return $this;
return $this;
}
private function configureProvider(?string $provider): self
{
match($provider){
ZipTax::class => $this->configureZipTax(),
default => $this->configureZipTax(),
};
return $this;
}
private function configureZipTax(): self
{
$this->provider = ZipTax::class;
$this->api_credentials = config('services.tax.zip_tax.key');
return $this;
}
} }

View File

@ -96,4 +96,10 @@ return [
'redirect' => env('BITBUCKET_OAUTH_REDIRECT'), 'redirect' => env('BITBUCKET_OAUTH_REDIRECT'),
], ],
'tax' => [
'zip_tax' => [
'key' => env('ZIP_TAX_KEY', false),
],
]
]
]; ];