1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 09:21:34 +02:00
invoiceninja/app/DataMapper/Tax/DE/Rule.php

195 lines
5.7 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-26 22:46:26 +02:00
use App\Models\Client;
2023-03-24 08:58:59 +01:00
use App\Models\Product;
2023-04-10 09:27:59 +02:00
use App\DataMapper\InvoiceItem;
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-04-10 07:51:38 +02:00
public string $vendor_iso_3166_2 = 'DE';
2023-03-29 05:23:06 +02:00
2023-04-10 07:51:38 +02:00
public string $client_iso_3166_2 = '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-29 05:23:06 +02:00
public float $vat_rate = 0;
public float $reduced_vat_rate = 0;
public function init(): self
{
2023-04-10 07:51:38 +02:00
$this->client_iso_3166_2 = $this->client->shipping_country ? $this->client->shipping_country->iso_3166_2 : $this->client->country->iso_3166_2;
2023-03-29 05:23:06 +02:00
$this->calculateRates();
return $this;
}
2023-04-10 09:27:59 +02:00
public function tax(mixed $type, ?InvoiceItem $item = null): 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-10 07:51:38 +02:00
} elseif ($this->client->company->tax_data->regions->EU->tax_all_subregions || $this->client->company->tax_data->regions->EU->subregions->{$this->client_iso_3166_2}->apply_tax) {
2023-04-07 11:51:17 +02:00
$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;
}
2023-04-10 07:51:38 +02:00
elseif($this->client_iso_3166_2 != $this->vendor_iso_3166_2 && in_array($this->client_iso_3166_2, $this->eu_country_codes) && $this->client->has_valid_vat_number && $this->eu_business_tax_exempt)
2023-03-29 05:42:08 +02:00
{
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
}
2023-04-10 07:51:38 +02:00
elseif(!in_array(strtoupper($this->client_iso_3166_2), $this->eu_country_codes) && ($this->foreign_consumer_tax_exempt || $this->foreign_business_tax_exempt)) //foreign + 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("foreign and tax exempt");
2023-03-29 05:23:06 +02:00
}
2023-04-10 07:51:38 +02:00
elseif(in_array(strtoupper($this->client_iso_3166_2), $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-10 07:51:38 +02:00
if(($this->vendor_iso_3166_2 != $this->client_iso_3166_2) && $this->client->company->tax_data->regions->EU->has_sales_above_threshold)
2023-03-29 05:23:06 +02:00
{
$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
}