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-24 08:58:59 +01:00
|
|
|
use App\Models\Product;
|
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-27 22:47:07 +02:00
|
|
|
class Rule extends BaseRule implements RuleInterface
|
2023-04-12 06:08:56 +02:00
|
|
|
{
|
|
|
|
/** @var string $seller_region */
|
2023-04-12 05:59:38 +02:00
|
|
|
public string $seller_region = 'EU';
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/** @var bool $consumer_tax_exempt */
|
2023-03-19 06:14:04 +01:00
|
|
|
public bool $consumer_tax_exempt = false;
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/** @var bool $business_tax_exempt */
|
2023-03-29 04:13:50 +02:00
|
|
|
public bool $business_tax_exempt = false;
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/** @var bool $eu_business_tax_exempt */
|
2023-03-18 13:06:32 +01:00
|
|
|
public bool $eu_business_tax_exempt = true;
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/** @var bool $foreign_business_tax_exempt */
|
2023-03-18 13:06:32 +01:00
|
|
|
public bool $foreign_business_tax_exempt = true;
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/** @var bool $foreign_consumer_tax_exempt */
|
2023-03-19 06:14:04 +01:00
|
|
|
public bool $foreign_consumer_tax_exempt = true;
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/** @var float $tax_rate */
|
2023-04-10 13:04:16 +02:00
|
|
|
public float $tax_rate = 0;
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/** @var float $reduced_tax_rate */
|
2023-04-10 13:04:16 +02:00
|
|
|
public float $reduced_tax_rate = 0;
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the rules and builds any required data.
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-03-29 05:23:06 +02:00
|
|
|
public function init(): self
|
|
|
|
{
|
|
|
|
$this->calculateRates();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the correct tax rate based on the product type.
|
|
|
|
*
|
|
|
|
* @param mixed $product_tax_type
|
|
|
|
* @return self
|
|
|
|
*/
|
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-04-26 02:23:23 +02:00
|
|
|
match(intval($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-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a reduced tax product
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-03-26 22:46:26 +02:00
|
|
|
public function taxReduced(): self
|
|
|
|
{
|
2023-04-10 13:04:16 +02:00
|
|
|
$this->tax_rate1 = $this->reduced_tax_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-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a tax exempt product
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-03-24 08:58:59 +01:00
|
|
|
public function taxExempt(): self
|
|
|
|
{
|
|
|
|
$this->tax_name1 = '';
|
|
|
|
$this->tax_rate1 = 0;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a digital product
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-03-24 08:58:59 +01:00
|
|
|
public function taxDigital(): self
|
|
|
|
{
|
2023-04-28 12:18:25 +02:00
|
|
|
|
|
|
|
$this->tax_rate1 = $this->tax_rate;
|
|
|
|
$this->tax_name1 = 'MwSt.';
|
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a service product
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-03-24 08:58:59 +01:00
|
|
|
public function taxService(): self
|
|
|
|
{
|
2023-04-28 12:18:25 +02:00
|
|
|
|
|
|
|
$this->tax_rate1 = $this->tax_rate;
|
|
|
|
$this->tax_name1 = 'MwSt.';
|
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a shipping product
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-03-24 08:58:59 +01:00
|
|
|
public function taxShipping(): self
|
2023-03-19 06:14:04 +01:00
|
|
|
{
|
2023-04-28 12:18:25 +02:00
|
|
|
|
|
|
|
$this->tax_rate1 = $this->tax_rate;
|
|
|
|
$this->tax_name1 = 'MwSt.';
|
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a physical product
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-03-24 08:58:59 +01:00
|
|
|
public function taxPhysical(): self
|
|
|
|
{
|
|
|
|
|
2023-04-10 13:04:16 +02:00
|
|
|
$this->tax_rate1 = $this->tax_rate;
|
2023-04-07 11:51:17 +02:00
|
|
|
$this->tax_name1 = 'MwSt.';
|
2023-04-12 06:08:56 +02:00
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a default product
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-03-24 08:58:59 +01:00
|
|
|
public function default(): self
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->tax_name1 = '';
|
|
|
|
$this->tax_rate1 = 0;
|
|
|
|
|
2023-03-19 06:14:04 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for an override product
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-03-29 04:13:50 +02:00
|
|
|
public function override(): self
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rates based on the client's location.
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
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) {
|
2023-04-12 06:08:56 +02:00
|
|
|
// nlog("tax exempt");
|
2023-04-10 13:04:16 +02:00
|
|
|
$this->tax_rate = 0;
|
|
|
|
$this->reduced_tax_rate = 0;
|
2023-03-29 05:42:08 +02:00
|
|
|
}
|
2023-04-12 05:59:38 +02:00
|
|
|
elseif($this->client_subregion != $this->client->company->tax_data->seller_subregion && in_array($this->client_subregion, $this->eu_country_codes) && $this->client->has_valid_vat_number && $this->eu_business_tax_exempt)
|
2023-03-29 05:42:08 +02:00
|
|
|
{
|
2023-04-12 06:08:56 +02:00
|
|
|
// nlog("euro zone and tax exempt");
|
2023-04-10 13:04:16 +02:00
|
|
|
$this->tax_rate = 0;
|
|
|
|
$this->reduced_tax_rate = 0;
|
2023-03-29 05:23:06 +02:00
|
|
|
}
|
2023-04-10 12:37:09 +02:00
|
|
|
elseif(!in_array($this->client_subregion, $this->eu_country_codes) && ($this->foreign_consumer_tax_exempt || $this->foreign_business_tax_exempt)) //foreign + tax exempt
|
2023-04-10 12:37:57 +02:00
|
|
|
{
|
2023-04-12 06:08:56 +02:00
|
|
|
// nlog("foreign and tax exempt");
|
2023-04-10 13:04:16 +02:00
|
|
|
$this->tax_rate = 0;
|
|
|
|
$this->reduced_tax_rate = 0;
|
2023-03-29 05:23:06 +02:00
|
|
|
}
|
2023-04-10 12:37:09 +02:00
|
|
|
elseif(in_array($this->client_subregion, $this->eu_country_codes) && !$this->client->has_valid_vat_number) //eu country / no valid vat
|
2023-03-29 05:23:06 +02:00
|
|
|
{
|
2023-04-12 05:59:38 +02:00
|
|
|
if(($this->client->company->tax_data->seller_subregion != $this->client_subregion) && $this->client->company->tax_data->regions->EU->has_sales_above_threshold)
|
2023-03-29 05:23:06 +02:00
|
|
|
{
|
2023-04-12 06:08:56 +02:00
|
|
|
// nlog("eu zone with sales above threshold");
|
2023-04-10 13:04:16 +02:00
|
|
|
$this->tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->country->iso_3166_2}->tax_rate;
|
|
|
|
$this->reduced_tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->country->iso_3166_2}->reduced_tax_rate;
|
2023-03-29 05:23:06 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-04-12 06:08:56 +02:00
|
|
|
// nlog("EU with intra-community supply ie DE to DE");
|
2023-04-10 13:04:16 +02:00
|
|
|
$this->tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->tax_rate;
|
|
|
|
$this->reduced_tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_tax_rate;
|
2023-03-29 05:23:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-04-12 06:08:56 +02:00
|
|
|
// nlog("default tax");
|
2023-04-10 13:04:16 +02:00
|
|
|
$this->tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->tax_rate;
|
|
|
|
$this->reduced_tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_tax_rate;
|
2023-03-29 05:23:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:11:55 +02:00
|
|
|
}
|