1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/DataMapper/Tax/BaseRule.php

263 lines
6.5 KiB
PHP
Raw Normal View History

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-12 05:59:38 +02:00
use App\Models\Product;
use App\DataMapper\Tax\ZipTax\Response;
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
2023-03-29 04:13:50 +02:00
/** US TAXES */
/** US 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;
protected ?Response $tax_data;
public function __construct()
{
}
2023-03-29 05:23:06 +02:00
public function init(): self
{
return $this;
}
2023-03-27 22:47:07 +02:00
public function setClient(Client $client): self
{
$this->client = $client;
2023-04-10 07:51:38 +02:00
$this->resolveRegions();
return $this;
}
2023-04-12 05:59:38 +02:00
public function setTaxData(Response $tax_data): self
{
$this->tax_data = $tax_data;
return $this;
}
2023-04-10 09:52:40 +02:00
// Refactor to support switching between shipping / billing country / region / subregion
2023-04-10 07:51:38 +02:00
private function resolveRegions(): self
{
if(!array_key_exists($this->client->country->iso_3166_2, $this->region_codes))
2023-04-12 05:59:38 +02:00
throw new \Exception('Automatic tax calculations not supported for this country');
2023-04-10 09:27:59 +02:00
2023-04-10 13:04:16 +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){
'US' => $this->client_subregion = $this->tax_data->geoState,
2023-04-10 12:37:09 +02:00
'EU' => $this->client_subregion = $this->client->country->iso_3166_2,
2023-04-12 05:59:38 +02:00
default => $this->client->country->iso_3166_2,
2023-04-10 09:52:40 +02:00
};
2023-03-27 22:47:07 +02:00
return $this;
}
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-04-10 13:11:55 +02:00
if($this->client_region == 'US') {
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-12 03:27:33 +02:00
$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;
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-12 05:59:38 +02:00
nlog($this->client_region);
nlog($this->seller_region);
2023-03-27 22:47:07 +02:00
2023-04-12 05:59:38 +02:00
if ($this->client->is_tax_exempt) {
return $this->taxExempt();
} elseif($this->client_region == $this->seller_region && $this->isTaxableRegion()) {
2023-03-27 22:47:07 +02:00
2023-04-12 05:59:38 +02:00
$this->taxByType($item->tax_id);
return $this;
} elseif($this->isTaxableRegion()) { //other regions outside of US
match($item->tax_id) {
Product::PRODUCT_TYPE_EXEMPT => $this->taxExempt(),
Product::PRODUCT_TYPE_REDUCED_TAX => $this->taxReduced(),
Product::PRODUCT_TYPE_OVERRIDE_TAX => $this->override(),
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;
}
public function taxReduced(): self
{
return $this;
}
public function taxExempt(): self
{
return $this;
}
public function taxDigital(): self
{
return $this;
}
public function taxService(): self
{
return $this;
}
public function taxShipping(): self
{
return $this;
}
public function taxPhysical(): self
{
return $this;
}
public function default(): self
{
return $this;
}
2023-03-28 23:27:13 +02:00
public function override(): self
{
return $this;
}
2023-03-29 05:23:06 +02:00
public function calculateRates(): self
{
return $this;
}
2023-03-27 22:47:07 +02:00
}