1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/DataMapper/Tax/DE/Rule.php

265 lines
7.5 KiB
PHP
Raw Normal View History

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-05-17 08:33:40 +02:00
public bool $foreign_business_tax_exempt = false;
2023-04-12 06:08:56 +02:00
/** @var bool $foreign_consumer_tax_exempt */
2023-05-17 08:33:40 +02:00
public bool $foreign_consumer_tax_exempt = false;
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
2023-06-02 09:44:04 +02:00
public string $tax_name1 = 'MwSt.';
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.
*
2023-05-15 13:20:47 +02:00
* @param mixed $item
2023-04-12 06:08:56 +02:00
* @return self
*/
2023-05-15 13:20:47 +02:00
public function taxByType($item): self
2023-03-24 08:58:59 +01:00
{
2023-03-26 22:57:29 +02:00
if ($this->client->is_tax_exempt) {
2023-05-15 13:20:47 +02:00
return $this->taxExempt($item);
2023-03-26 22:57:29 +02:00
}
2023-05-15 13:20:47 +02:00
match(intval($item->tax_id)){
Product::PRODUCT_TYPE_EXEMPT => $this->taxExempt($item),
Product::PRODUCT_TYPE_DIGITAL => $this->taxDigital($item),
Product::PRODUCT_TYPE_SERVICE => $this->taxService($item),
Product::PRODUCT_TYPE_SHIPPING => $this->taxShipping($item),
Product::PRODUCT_TYPE_PHYSICAL => $this->taxPhysical($item),
Product::PRODUCT_TYPE_REDUCED_TAX => $this->taxReduced($item),
Product::PRODUCT_TYPE_OVERRIDE_TAX => $this->override($item),
Product::PRODUCT_TYPE_ZERO_RATED => $this->zeroRated($item),
Product::PRODUCT_TYPE_REVERSE_TAX => $this->reverseTax($item),
default => $this->default($item),
2023-03-24 08:58:59 +01:00
};
return $this;
}
2023-04-12 06:08:56 +02:00
2023-05-15 05:57:00 +02:00
/**
* Calculates the tax rate for a reduced tax product
*
* @return self
*/
2023-05-15 13:20:47 +02:00
public function reverseTax($item): self
2023-05-15 05:57:00 +02:00
{
$this->tax_rate1 = 0;
return $this;
}
2023-04-12 06:08:56 +02:00
/**
* Calculates the tax rate for a reduced tax product
*
* @return self
*/
2023-05-15 13:20:47 +02:00
public function taxReduced($item): self
2023-03-26 22:46:26 +02:00
{
2023-04-10 13:04:16 +02:00
$this->tax_rate1 = $this->reduced_tax_rate;
2023-06-02 09:44:04 +02:00
2023-03-26 22:46:26 +02:00
return $this;
}
2023-04-12 06:08:56 +02:00
2023-05-15 05:57:00 +02:00
/**
* Calculates the tax rate for a zero rated tax product
*
* @return self
*/
2023-05-15 13:20:47 +02:00
public function zeroRated($item): self
2023-05-15 05:57:00 +02:00
{
$this->tax_rate1 = 0;
2023-06-02 09:44:04 +02:00
2023-05-15 05:57:00 +02:00
return $this;
}
2023-04-12 06:08:56 +02:00
/**
* Calculates the tax rate for a tax exempt product
*
* @return self
*/
2023-05-15 13:20:47 +02:00
public function taxExempt($item): self
2023-03-24 08:58:59 +01:00
{
$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-05-15 13:20:47 +02:00
public function taxDigital($item): self
2023-03-24 08:58:59 +01:00
{
2023-04-28 12:18:25 +02:00
$this->tax_rate1 = $this->tax_rate;
2023-06-02 09:44:04 +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 service product
*
* @return self
*/
2023-05-15 13:20:47 +02:00
public function taxService($item): self
2023-03-24 08:58:59 +01:00
{
2023-04-28 12:18:25 +02:00
$this->tax_rate1 = $this->tax_rate;
2023-06-02 09:44:04 +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 shipping product
*
* @return self
*/
2023-05-15 13:20:47 +02:00
public function taxShipping($item): self
2023-03-19 06:14:04 +01:00
{
2023-04-28 12:18:25 +02:00
$this->tax_rate1 = $this->tax_rate;
2023-06-02 09:44:04 +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 physical product
*
* @return self
*/
2023-05-15 13:20:47 +02:00
public function taxPhysical($item): self
2023-03-24 08:58:59 +01:00
{
2023-04-10 13:04:16 +02:00
$this->tax_rate1 = $this->tax_rate;
2023-06-02 09:44:04 +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-05-15 13:20:47 +02:00
public function default($item): self
2023-03-24 08:58:59 +01:00
{
$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-05-15 13:20:47 +02:00
public function override($item): self
2023-03-29 04:13:50 +02:00
{
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-06-07 03:21:26 +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-06-02 09:44:04 +02:00
elseif($this->client_subregion != $this->client->company->tax_data->seller_subregion && in_array($this->client_subregion, $this->eu_country_codes) && $this->client->vat_number && $this->eu_business_tax_exempt)
// 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-06-07 03:21:26 +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-06-07 03:21:26 +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-05-17 08:33:40 +02:00
elseif(!in_array($this->client_subregion, $this->eu_country_codes))
{
$this->defaultForeign();
}
2023-06-02 09:44:04 +02:00
elseif(in_array($this->client_subregion, $this->eu_country_codes) && !$this->client->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-06-07 03:21:26 +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-06-07 03:21:26 +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-06-07 03:21:26 +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
}