2023-03-27 22:47:07 +02:00
|
|
|
<?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\DataMapper\Tax;
|
|
|
|
|
2023-03-29 04:13:50 +02:00
|
|
|
use App\Models\Client;
|
2023-04-22 09:07:22 +02:00
|
|
|
use App\Models\Invoice;
|
2023-04-12 05:59:38 +02:00
|
|
|
use App\Models\Product;
|
2023-04-24 03:47:48 +02:00
|
|
|
use App\DataProviders\USStates;
|
2023-04-29 08:04:42 +02:00
|
|
|
use App\DataMapper\Tax\ZipTax\Response;
|
2023-05-17 06:07:48 +02:00
|
|
|
use App\Services\Tax\Providers\TaxProvider;
|
2023-03-27 22:47:07 +02:00
|
|
|
|
|
|
|
class BaseRule implements RuleInterface
|
|
|
|
{
|
|
|
|
/** EU TAXES */
|
|
|
|
public bool $consumer_tax_exempt = false;
|
2023-03-29 05:23:06 +02:00
|
|
|
|
2023-03-27 22:47:07 +02:00
|
|
|
public bool $business_tax_exempt = true;
|
2023-03-29 05:23:06 +02:00
|
|
|
|
2023-03-27 22:47:07 +02:00
|
|
|
public bool $eu_business_tax_exempt = true;
|
2023-03-29 05:23:06 +02:00
|
|
|
|
2023-03-27 22:47:07 +02:00
|
|
|
public bool $foreign_business_tax_exempt = true;
|
2023-03-29 05:23:06 +02:00
|
|
|
|
2023-03-27 22:47:07 +02:00
|
|
|
public bool $foreign_consumer_tax_exempt = true;
|
2023-03-29 05:23:06 +02:00
|
|
|
|
2023-04-12 05:59:38 +02:00
|
|
|
public string $seller_region = '';
|
2023-04-10 07:51:38 +02:00
|
|
|
|
|
|
|
public string $client_region = '';
|
|
|
|
|
2023-04-10 09:27:59 +02:00
|
|
|
public string $client_subregion = '';
|
2023-04-10 09:33:24 +02:00
|
|
|
|
2023-03-29 05:23:06 +02:00
|
|
|
public array $eu_country_codes = [
|
2023-04-10 07:51:38 +02:00
|
|
|
'AT', // Austria
|
|
|
|
'BE', // Belgium
|
|
|
|
'BG', // Bulgaria
|
|
|
|
'CY', // Cyprus
|
|
|
|
'CZ', // Czech Republic
|
|
|
|
'DE', // Germany
|
|
|
|
'DK', // Denmark
|
|
|
|
'EE', // Estonia
|
|
|
|
'ES', // Spain
|
|
|
|
'FI', // Finland
|
|
|
|
'FR', // France
|
|
|
|
'GR', // Greece
|
|
|
|
'HR', // Croatia
|
|
|
|
'HU', // Hungary
|
|
|
|
'IE', // Ireland
|
|
|
|
'IT', // Italy
|
|
|
|
'LT', // Lithuania
|
|
|
|
'LU', // Luxembourg
|
|
|
|
'LV', // Latvia
|
|
|
|
'MT', // Malta
|
|
|
|
'NL', // Netherlands
|
|
|
|
'PL', // Poland
|
|
|
|
'PT', // Portugal
|
|
|
|
'RO', // Romania
|
|
|
|
'SE', // Sweden
|
|
|
|
'SI', // Slovenia
|
|
|
|
'SK', // Slovakia
|
|
|
|
];
|
|
|
|
|
|
|
|
public array $region_codes = [
|
|
|
|
'AT' => 'EU', // Austria
|
|
|
|
'BE' => 'EU', // Belgium
|
|
|
|
'BG' => 'EU', // Bulgaria
|
|
|
|
'CY' => 'EU', // Cyprus
|
|
|
|
'CZ' => 'EU', // Czech Republic
|
|
|
|
'DE' => 'EU', // Germany
|
|
|
|
'DK' => 'EU', // Denmark
|
|
|
|
'EE' => 'EU', // Estonia
|
|
|
|
'ES' => 'EU', // Spain
|
|
|
|
'FI' => 'EU', // Finland
|
|
|
|
'FR' => 'EU', // France
|
|
|
|
'GR' => 'EU', // Greece
|
|
|
|
'HR' => 'EU', // Croatia
|
|
|
|
'HU' => 'EU', // Hungary
|
|
|
|
'IE' => 'EU', // Ireland
|
|
|
|
'IT' => 'EU', // Italy
|
|
|
|
'LT' => 'EU', // Lithuania
|
|
|
|
'LU' => 'EU', // Luxembourg
|
|
|
|
'LV' => 'EU', // Latvia
|
|
|
|
'MT' => 'EU', // Malta
|
|
|
|
'NL' => 'EU', // Netherlands
|
|
|
|
'PL' => 'EU', // Poland
|
|
|
|
'PT' => 'EU', // Portugal
|
|
|
|
'RO' => 'EU', // Romania
|
|
|
|
'SE' => 'EU', // Sweden
|
|
|
|
'SI' => 'EU', // Slovenia
|
|
|
|
'SK' => 'EU', // Slovakia
|
|
|
|
|
|
|
|
'US' => 'US', // United States
|
2023-04-12 05:59:38 +02:00
|
|
|
|
|
|
|
'AU' => 'AU', // Australia
|
2023-03-29 05:23:06 +02:00
|
|
|
];
|
|
|
|
|
2023-03-29 04:13:50 +02:00
|
|
|
/** EU TAXES */
|
2023-03-27 22:47:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
public string $tax_name1 = '';
|
|
|
|
public float $tax_rate1 = 0;
|
|
|
|
|
|
|
|
public string $tax_name2 = '';
|
|
|
|
public float $tax_rate2 = 0;
|
|
|
|
|
|
|
|
public string $tax_name3 = '';
|
|
|
|
public float $tax_rate3 = 0;
|
|
|
|
|
|
|
|
protected ?Client $client;
|
|
|
|
|
2023-04-24 03:47:48 +02:00
|
|
|
public ?Response $tax_data;
|
2023-03-27 22:47:07 +02:00
|
|
|
|
2023-04-23 08:59:17 +02:00
|
|
|
public mixed $invoice;
|
2023-04-22 09:07:22 +02:00
|
|
|
|
2023-03-27 22:47:07 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-29 05:23:06 +02:00
|
|
|
public function init(): self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
2023-05-17 06:07:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the tax rule for the entity.
|
|
|
|
*
|
|
|
|
* @param mixed $invoice
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-04-24 03:47:48 +02:00
|
|
|
public function setEntity(mixed $invoice): self
|
2023-03-27 22:47:07 +02:00
|
|
|
{
|
2023-04-22 09:07:22 +02:00
|
|
|
$this->invoice = $invoice;
|
2023-04-23 00:16:28 +02:00
|
|
|
|
2023-04-22 09:07:22 +02:00
|
|
|
$this->client = $invoice->client;
|
2023-03-27 22:47:07 +02:00
|
|
|
|
2023-05-17 07:45:06 +02:00
|
|
|
$this->resolveRegions();
|
|
|
|
|
|
|
|
if(!$this->isTaxableRegion())
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
$this->configTaxData();
|
2023-04-10 07:51:38 +02:00
|
|
|
|
2023-04-24 03:47:48 +02:00
|
|
|
$this->tax_data = new Response($this->invoice->tax_data);
|
2023-04-12 05:59:38 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2023-05-17 06:07:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configigures the Tax Data for the entity
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-04-23 00:16:28 +02:00
|
|
|
private function configTaxData(): self
|
|
|
|
{
|
2023-05-17 06:07:48 +02:00
|
|
|
/* We should only apply taxes for configured states */
|
2023-04-24 03:47:48 +02:00
|
|
|
if(!array_key_exists($this->client->country->iso_3166_2, $this->region_codes)) {
|
2023-04-29 08:04:42 +02:00
|
|
|
nlog('Automatic tax calculations not supported for this country - defaulting to company country');
|
2023-05-17 06:07:48 +02:00
|
|
|
nlog("With new logic, we should never see this");
|
2023-04-24 03:47:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
/** Harvest the client_region */
|
2023-04-24 03:47:48 +02:00
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
/** If the tax data is already set and the invoice is marked as sent, do not adjust the rates */
|
2023-04-23 00:16:28 +02:00
|
|
|
if($this->invoice->tax_data && $this->invoice->status_id > 1)
|
|
|
|
return $this;
|
|
|
|
|
2023-05-17 06:07:48 +02:00
|
|
|
/**
|
|
|
|
* Origin - Company Tax Data
|
|
|
|
* Destination - Client Tax Data
|
|
|
|
*
|
|
|
|
*/
|
2023-05-17 08:33:40 +02:00
|
|
|
// $tax_data = new Response([]);
|
|
|
|
$tax_data = false;
|
2023-04-24 03:47:48 +02:00
|
|
|
|
2023-05-17 06:39:59 +02:00
|
|
|
if($this->seller_region == 'US' && $this->client_region == 'US'){
|
2023-05-17 06:07:48 +02:00
|
|
|
|
|
|
|
$company = $this->invoice->company;
|
|
|
|
|
|
|
|
/** If no company tax data has been configured, lets do that now. */
|
|
|
|
if(!$company->origin_tax_data && \DB::transactionLevel() == 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
$tp = new TaxProvider($company);
|
|
|
|
$tp->updateCompanyTaxData();
|
|
|
|
$company->fresh();
|
2023-04-29 08:04:42 +02:00
|
|
|
|
|
|
|
}
|
2023-05-17 06:07:48 +02:00
|
|
|
|
|
|
|
/** If we are in a Origin based state, force the company tax here */
|
|
|
|
if($company->origin_tax_data->originDestination == 'O' && ($company->tax_data->seller_subregion == $this->client_subregion)) {
|
|
|
|
|
|
|
|
$tax_data = $company->origin_tax_data;
|
|
|
|
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
|
|
|
|
/** Ensures the client tax data has been updated */
|
|
|
|
if(!$this->client->tax_data && \DB::transactionLevel() == 0) {
|
|
|
|
|
|
|
|
$tp = new TaxProvider($company, $this->client);
|
|
|
|
$tp->updateClientTaxData();
|
|
|
|
$this->client->fresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
$tax_data = $this->client->tax_data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-24 03:47:48 +02:00
|
|
|
}
|
2023-04-23 00:16:28 +02:00
|
|
|
|
2023-05-17 06:07:48 +02:00
|
|
|
/** Applies the tax data to the invoice */
|
2023-05-17 08:33:40 +02:00
|
|
|
if($this->invoice instanceof Invoice && $tax_data) {
|
2023-05-17 08:02:33 +02:00
|
|
|
|
2023-05-17 08:33:40 +02:00
|
|
|
$this->invoice->tax_data = $tax_data ;
|
2023-05-17 08:02:33 +02:00
|
|
|
|
|
|
|
if(\DB::transactionLevel() == 0)
|
|
|
|
$this->invoice->saveQuietly();
|
2023-05-17 06:07:48 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 00:16:28 +02:00
|
|
|
return $this;
|
2023-05-17 06:07:48 +02:00
|
|
|
|
2023-04-23 00:16:28 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 06:07:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve Regions & Subregions
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-04-10 07:51:38 +02:00
|
|
|
private function resolveRegions(): self
|
|
|
|
{
|
2023-05-17 06:07:48 +02:00
|
|
|
|
|
|
|
$this->client_region = $this->region_codes[$this->client->country->iso_3166_2];
|
2023-04-10 07:51:38 +02:00
|
|
|
|
2023-04-10 09:52:40 +02:00
|
|
|
match($this->client_region){
|
2023-05-17 06:07:48 +02:00
|
|
|
'US' => $this->client_subregion = strlen($this->invoice?->client?->tax_data?->geoState) > 1 ? $this->invoice->client->tax_data->geoState : $this->getUSState(),
|
2023-04-10 12:37:09 +02:00
|
|
|
'EU' => $this->client_subregion = $this->client->country->iso_3166_2,
|
2023-04-24 03:47:48 +02:00
|
|
|
'AU' => $this->client_subregion = 'AU',
|
2023-04-20 05:31:04 +02:00
|
|
|
default => $this->client_subregion = $this->client->country->iso_3166_2,
|
2023-04-10 09:52:40 +02:00
|
|
|
};
|
2023-05-17 06:39:59 +02:00
|
|
|
|
2023-03-27 22:47:07 +02:00
|
|
|
return $this;
|
2023-05-17 06:07:48 +02:00
|
|
|
|
2023-03-27 22:47:07 +02:00
|
|
|
}
|
|
|
|
|
2023-04-24 03:47:48 +02:00
|
|
|
private function getUSState(): string
|
|
|
|
{
|
|
|
|
try {
|
2023-05-17 06:07:48 +02:00
|
|
|
|
|
|
|
$states = USStates::$states;
|
|
|
|
|
|
|
|
if(isset($states[$this->client->state]))
|
|
|
|
return $this->client->state;
|
|
|
|
|
2023-04-24 03:47:48 +02:00
|
|
|
return USStates::getState(strlen($this->client->postal_code) > 1 ? $this->client->postal_code : $this->client->shipping_postal_code);
|
2023-05-17 06:07:48 +02:00
|
|
|
|
2023-04-24 03:47:48 +02:00
|
|
|
} catch (\Exception $e) {
|
2023-04-24 04:07:36 +02:00
|
|
|
return $this->client->company->country()->iso_3166_2 == 'US' ? $this->client->company->tax_data->seller_subregion : 'CA';
|
2023-04-24 03:47:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:04:16 +02:00
|
|
|
public function isTaxableRegion(): bool
|
2023-04-10 09:52:40 +02:00
|
|
|
{
|
|
|
|
return $this->client->company->tax_data->regions->{$this->client_region}->tax_all_subregions || $this->client->company->tax_data->regions->{$this->client_region}->subregions->{$this->client_subregion}->apply_tax;
|
|
|
|
}
|
|
|
|
|
2023-04-12 05:59:38 +02:00
|
|
|
public function defaultForeign(): self
|
2023-04-10 13:11:55 +02:00
|
|
|
{
|
2023-04-12 05:59:38 +02:00
|
|
|
|
2023-05-17 08:33:40 +02:00
|
|
|
if($this->client_region == 'US' && isset($this->tax_data?->taxSales)) {
|
2023-04-12 05:59:38 +02:00
|
|
|
|
2023-04-10 13:11:55 +02:00
|
|
|
$this->tax_rate1 = $this->tax_data->taxSales * 100;
|
|
|
|
$this->tax_name1 = "{$this->tax_data->geoState} Sales Tax";
|
|
|
|
|
2023-04-12 05:59:38 +02:00
|
|
|
return $this;
|
2023-04-10 13:11:55 +02:00
|
|
|
|
|
|
|
}
|
2023-04-22 09:07:22 +02:00
|
|
|
elseif($this->client_region == 'AU'){ //these are defaults and are only stubbed out for now, for AU we can actually remove these
|
2023-04-19 08:51:01 +02:00
|
|
|
|
2023-04-22 09:07:22 +02:00
|
|
|
$this->tax_rate1 = $this->client->company->tax_data->regions->AU->subregions->AU->tax_rate;
|
|
|
|
$this->tax_name1 = $this->client->company->tax_data->regions->AU->subregions->AU->tax_name;
|
2023-04-10 13:11:55 +02:00
|
|
|
|
2023-04-19 08:51:01 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 03:27:33 +02:00
|
|
|
|
2023-04-29 02:07:23 +02:00
|
|
|
if(isset($this->client->company->tax_data->regions->{$this->client_region}->subregions->{$this->client_subregion})) {
|
|
|
|
$this->tax_rate1 = $this->client->company->tax_data->regions->{$this->client_region}->subregions->{$this->client_subregion}->tax_rate;
|
|
|
|
$this->tax_name1 = $this->client->company->tax_data->regions->{$this->client_region}->subregions->{$this->client_subregion}->tax_name;
|
|
|
|
}
|
|
|
|
|
2023-04-12 03:27:33 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-04-12 05:59:38 +02:00
|
|
|
public function tax($item = null): self
|
2023-03-27 22:47:07 +02:00
|
|
|
{
|
2023-04-21 06:40:56 +02:00
|
|
|
|
2023-04-12 05:59:38 +02:00
|
|
|
if ($this->client->is_tax_exempt) {
|
2023-05-15 13:20:47 +02:00
|
|
|
|
|
|
|
return $this->taxExempt($item);
|
|
|
|
|
2023-04-12 05:59:38 +02:00
|
|
|
} elseif($this->client_region == $this->seller_region && $this->isTaxableRegion()) {
|
2023-03-27 22:47:07 +02:00
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
$this->taxByType($item);
|
2023-04-12 05:59:38 +02:00
|
|
|
|
|
|
|
return $this;
|
2023-05-15 13:20:47 +02:00
|
|
|
|
2023-04-12 05:59:38 +02:00
|
|
|
} elseif($this->isTaxableRegion()) { //other regions outside of US
|
|
|
|
|
2023-04-26 02:23:23 +02:00
|
|
|
match(intval($item->tax_id)) {
|
2023-05-15 13:20:47 +02:00
|
|
|
Product::PRODUCT_TYPE_EXEMPT => $this->taxExempt($item),
|
|
|
|
Product::PRODUCT_TYPE_REDUCED_TAX => $this->taxReduced($item),
|
|
|
|
Product::PRODUCT_TYPE_OVERRIDE_TAX => $this->override($item),
|
2023-04-12 05:59:38 +02:00
|
|
|
default => $this->defaultForeign(),
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2023-03-27 22:47:07 +02:00
|
|
|
return $this;
|
|
|
|
|
2023-04-12 05:59:38 +02:00
|
|
|
}
|
|
|
|
|
2023-04-10 09:27:59 +02:00
|
|
|
public function taxByType(mixed $type): self
|
2023-03-27 22:47:07 +02:00
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
public function taxReduced($item): self
|
2023-03-27 22:47:07 +02:00
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
public function taxExempt($item): self
|
2023-03-27 22:47:07 +02:00
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
public function taxDigital($item): self
|
2023-03-27 22:47:07 +02:00
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
public function taxService($item): self
|
2023-03-27 22:47:07 +02:00
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
public function taxShipping($item): self
|
2023-03-27 22:47:07 +02:00
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
public function taxPhysical($item): self
|
2023-03-27 22:47:07 +02:00
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
public function default($item): self
|
2023-03-27 22:47:07 +02:00
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
2023-03-28 23:27:13 +02:00
|
|
|
|
2023-05-15 13:20:47 +02:00
|
|
|
public function override($item): self
|
2023-03-28 23:27:13 +02:00
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
2023-03-29 05:23:06 +02:00
|
|
|
|
|
|
|
public function calculateRates(): self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
2023-03-27 22:47:07 +02:00
|
|
|
}
|