2023-03-18 13:06:32 +01: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
|
|
|
|
*/
|
|
|
|
|
2023-03-29 05:23:06 +02:00
|
|
|
namespace App\DataMapper\Tax\DE;
|
2023-03-18 13:06:32 +01:00
|
|
|
|
2023-03-26 22:46:26 +02:00
|
|
|
use App\Models\Client;
|
2023-03-24 08:58:59 +01:00
|
|
|
use App\Models\Product;
|
2023-03-29 05:23:06 +02:00
|
|
|
use Illuminate\Support\Str;
|
2023-03-27 22:47:07 +02:00
|
|
|
use App\DataMapper\Tax\BaseRule;
|
2023-03-19 06:14:04 +01:00
|
|
|
use App\DataMapper\Tax\RuleInterface;
|
2023-03-24 08:58:59 +01:00
|
|
|
use App\DataMapper\Tax\ZipTax\Response;
|
2023-03-19 06:14:04 +01:00
|
|
|
|
2023-03-27 22:47:07 +02:00
|
|
|
class Rule extends BaseRule implements RuleInterface
|
2023-03-18 13:06:32 +01:00
|
|
|
{
|
2023-03-29 05:23:06 +02:00
|
|
|
public string $vendor_country_code = 'DE';
|
|
|
|
|
|
|
|
public string $client_country_code = 'DE';
|
2023-03-27 22:47:07 +02:00
|
|
|
|
2023-03-19 06:14:04 +01:00
|
|
|
public bool $consumer_tax_exempt = false;
|
2023-03-18 13:06:32 +01:00
|
|
|
|
2023-03-29 04:13:50 +02:00
|
|
|
public bool $business_tax_exempt = false;
|
2023-03-18 13:06:32 +01:00
|
|
|
|
|
|
|
public bool $eu_business_tax_exempt = true;
|
|
|
|
|
|
|
|
public bool $foreign_business_tax_exempt = true;
|
2023-03-19 06:14:04 +01:00
|
|
|
|
|
|
|
public bool $foreign_consumer_tax_exempt = true;
|
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
public string $tax_name1 = '';
|
2023-03-28 08:37:38 +02:00
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
public float $tax_rate1 = 0;
|
|
|
|
|
|
|
|
public string $tax_name2 = '';
|
2023-03-28 08:37:38 +02:00
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
public float $tax_rate2 = 0;
|
|
|
|
|
|
|
|
public string $tax_name3 = '';
|
2023-03-28 08:37:38 +02:00
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
public float $tax_rate3 = 0;
|
2023-03-29 05:23:06 +02:00
|
|
|
|
|
|
|
public float $vat_rate = 0;
|
|
|
|
|
|
|
|
public float $reduced_vat_rate = 0;
|
|
|
|
|
2023-03-26 22:46:26 +02:00
|
|
|
protected ?Client $client;
|
2023-03-24 08:58:59 +01:00
|
|
|
|
2023-03-26 22:57:29 +02:00
|
|
|
protected ?Response $tax_data;
|
|
|
|
|
2023-03-26 22:46:26 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-29 05:23:06 +02:00
|
|
|
public function init(): self
|
|
|
|
{
|
|
|
|
$this->client_country_code = $this->client->shipping_country ? $this->client->shipping_country->iso_3166_2 : $this->client->country->iso_3166_2;
|
|
|
|
$this->calculateRates();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-03-26 22:46:26 +02:00
|
|
|
public function setClient(Client $client): self
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTaxData(Response $tax_data): self
|
2023-03-24 08:58:59 +01:00
|
|
|
{
|
|
|
|
$this->tax_data = $tax_data;
|
2023-03-26 22:46:26 +02:00
|
|
|
|
|
|
|
return $this;
|
2023-03-24 08:58:59 +01:00
|
|
|
}
|
|
|
|
|
2023-03-27 05:47:01 +02:00
|
|
|
//need to add logic here to capture if
|
2023-04-02 23:48:59 +02:00
|
|
|
public function tax($type): self
|
2023-03-24 08:58:59 +01:00
|
|
|
{
|
2023-04-02 23:48:59 +02:00
|
|
|
|
2023-04-03 00:02:38 +02:00
|
|
|
|
|
|
|
if ($this->client->is_tax_exempt) {
|
2023-04-07 11:51:17 +02:00
|
|
|
|
2023-03-26 22:57:29 +02:00
|
|
|
return $this->taxExempt();
|
2023-04-03 00:02:38 +02:00
|
|
|
|
2023-04-07 11:51:17 +02:00
|
|
|
} elseif ($this->client->company->tax_data->regions->EU->tax_all_subregions || $this->client->company->tax_data->regions->EU->subregions->{$this->client_country_code}->apply_tax) {
|
|
|
|
|
|
|
|
$this->taxByType($type);
|
2023-04-03 00:02:38 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2023-04-02 23:48:59 +02:00
|
|
|
|
2023-03-26 22:57:29 +02:00
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-31 06:25:30 +02:00
|
|
|
public function taxByType($product_tax_type): self
|
2023-03-24 08:58:59 +01:00
|
|
|
{
|
2023-03-26 22:57:29 +02:00
|
|
|
|
|
|
|
if ($this->client->is_tax_exempt) {
|
|
|
|
return $this->taxExempt();
|
|
|
|
}
|
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
match($product_tax_type){
|
2023-03-28 22:53:46 +02:00
|
|
|
Product::PRODUCT_TYPE_EXEMPT => $this->taxExempt(),
|
2023-03-24 08:58:59 +01:00
|
|
|
Product::PRODUCT_TYPE_DIGITAL => $this->taxDigital(),
|
|
|
|
Product::PRODUCT_TYPE_SERVICE => $this->taxService(),
|
|
|
|
Product::PRODUCT_TYPE_SHIPPING => $this->taxShipping(),
|
|
|
|
Product::PRODUCT_TYPE_PHYSICAL => $this->taxPhysical(),
|
2023-03-26 22:46:26 +02:00
|
|
|
Product::PRODUCT_TYPE_REDUCED_TAX => $this->taxReduced(),
|
2023-03-29 04:13:50 +02:00
|
|
|
Product::PRODUCT_TYPE_OVERRIDE_TAX => $this->override(),
|
2023-03-24 08:58:59 +01:00
|
|
|
default => $this->default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-03-26 22:46:26 +02:00
|
|
|
public function taxReduced(): self
|
|
|
|
{
|
2023-03-29 05:23:06 +02:00
|
|
|
$this->tax_rate1 = $this->reduced_vat_rate;
|
2023-03-27 05:47:01 +02:00
|
|
|
$this->tax_name1 = 'ermäßigte MwSt.';
|
2023-03-26 22:46:26 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
public function taxExempt(): self
|
|
|
|
{
|
|
|
|
$this->tax_name1 = '';
|
|
|
|
$this->tax_rate1 = 0;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function taxDigital(): self
|
|
|
|
{
|
2023-04-07 11:51:17 +02:00
|
|
|
// $this->tax();
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function taxService(): self
|
|
|
|
{
|
2023-04-07 11:51:17 +02:00
|
|
|
// $this->tax();
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function taxShipping(): self
|
2023-03-19 06:14:04 +01:00
|
|
|
{
|
2023-04-07 11:51:17 +02:00
|
|
|
// $this->tax();
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function taxPhysical(): self
|
|
|
|
{
|
2023-04-07 11:51:17 +02:00
|
|
|
// $this->tax();
|
2023-03-24 08:58:59 +01:00
|
|
|
|
2023-04-07 11:51:17 +02:00
|
|
|
$this->tax_rate1 = $this->vat_rate;
|
|
|
|
$this->tax_name1 = 'MwSt.';
|
2023-03-24 08:58:59 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function default(): self
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->tax_name1 = '';
|
|
|
|
$this->tax_rate1 = 0;
|
|
|
|
|
2023-03-19 06:14:04 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2023-03-29 04:13:50 +02:00
|
|
|
|
|
|
|
public function override(): self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-03-29 05:23:06 +02:00
|
|
|
public function calculateRates(): self
|
|
|
|
{
|
|
|
|
|
2023-03-29 05:42:08 +02:00
|
|
|
if ($this->client->is_tax_exempt) {
|
|
|
|
$this->vat_rate = 0;
|
|
|
|
$this->reduced_vat_rate = 0;
|
|
|
|
}
|
|
|
|
elseif($this->client_country_code != $this->vendor_country_code && in_array($this->client_country_code, $this->eu_country_codes) && $this->client->has_valid_vat_number && $this->eu_business_tax_exempt)
|
|
|
|
{
|
2023-03-29 05:23:06 +02:00
|
|
|
$this->vat_rate = 0;
|
|
|
|
$this->reduced_vat_rate = 0;
|
2023-03-29 11:49:40 +02:00
|
|
|
// nlog("euro zone and tax exempt");
|
2023-03-29 05:23:06 +02:00
|
|
|
}
|
|
|
|
elseif(!in_array(strtoupper($this->client_country_code), $this->eu_country_codes) && ($this->foreign_consumer_tax_exempt || $this->foreign_business_tax_exempt)) //foreign + tax exempt
|
|
|
|
{
|
|
|
|
$this->vat_rate = 0;
|
|
|
|
$this->reduced_vat_rate = 0;
|
2023-03-29 11:49:40 +02:00
|
|
|
// nlog("foreign and tax exempt");
|
2023-03-29 05:23:06 +02:00
|
|
|
}
|
|
|
|
elseif(in_array(strtoupper($this->client_country_code), $this->eu_country_codes) && !$this->client->has_valid_vat_number) //eu country / no valid vat
|
|
|
|
{
|
|
|
|
if(($this->vendor_country_code != $this->client_country_code) && $this->client->company->tax_data->regions->EU->has_sales_above_threshold)
|
|
|
|
{
|
|
|
|
$this->vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->country->iso_3166_2}->vat_rate;
|
|
|
|
$this->reduced_vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->country->iso_3166_2}->reduced_vat_rate;
|
2023-03-29 11:49:40 +02:00
|
|
|
// nlog("eu zone with sales above threshold");
|
2023-03-29 05:23:06 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->vat_rate;
|
|
|
|
$this->reduced_vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_vat_rate;
|
2023-03-29 11:49:40 +02:00
|
|
|
// nlog("same eu country with");
|
2023-03-29 05:23:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->vat_rate;
|
|
|
|
$this->reduced_vat_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_vat_rate;
|
2023-03-29 11:49:40 +02:00
|
|
|
// nlog("default tax");
|
2023-03-29 05:23:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-18 13:06:32 +01:00
|
|
|
}
|