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\US;
|
2023-03-18 13:06:32 +01:00
|
|
|
|
2023-04-10 07:51:38 +02:00
|
|
|
use App\DataMapper\Tax\BaseRule;
|
2023-03-24 08:02:34 +01:00
|
|
|
use App\DataMapper\Tax\RuleInterface;
|
2023-04-10 09:52:40 +02:00
|
|
|
use App\Models\Product;
|
2023-03-24 08:02:34 +01:00
|
|
|
|
2023-04-12 06:08:56 +02:00
|
|
|
/**
|
|
|
|
* The rules apply US => US taxes using the tax calculator.
|
|
|
|
*
|
|
|
|
* US => Foreign taxes we check the product types still for exemptions, and we all back to the client country tax rate.
|
|
|
|
*/
|
2023-04-10 07:51:38 +02:00
|
|
|
class Rule extends BaseRule implements RuleInterface
|
2023-03-18 13:06:32 +01:00
|
|
|
{
|
2023-04-12 05:59:38 +02:00
|
|
|
|
2023-04-12 06:08:56 +02:00
|
|
|
/** @var string $seller_region */
|
|
|
|
public string $seller_region = 'US';
|
|
|
|
|
2023-04-12 03:27:33 +02:00
|
|
|
/**
|
2023-04-12 06:08:56 +02:00
|
|
|
* Initializes the rules and builds any required data.
|
|
|
|
*
|
|
|
|
* @return self
|
2023-04-12 03:27:33 +02:00
|
|
|
*/
|
2023-04-10 09:52:40 +02:00
|
|
|
public function init(): self
|
|
|
|
{
|
|
|
|
$this->calculateRates();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Override tax class, we use this when we do not modify the input taxes
|
|
|
|
*
|
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 override($item): self
|
2023-04-10 09:52:40 +02:00
|
|
|
{
|
2023-05-15 13:20:47 +02:00
|
|
|
|
|
|
|
$this->tax_rate1 = $item->tax_rate1;
|
|
|
|
|
|
|
|
$this->tax_name1 = $item->tax_name1;
|
|
|
|
|
2023-03-29 04:13:50 +02:00
|
|
|
return $this;
|
2023-05-15 13:20:47 +02:00
|
|
|
|
2023-03-29 04:13:50 +02:00
|
|
|
}
|
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:02:34 +01: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),
|
|
|
|
default => $this->default($item),
|
2023-03-24 08:02:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the tax as exempt (0)
|
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 taxExempt($item): self
|
2023-03-24 08:02:34 +01:00
|
|
|
{
|
|
|
|
$this->tax_name1 = '';
|
|
|
|
$this->tax_rate1 = 0;
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
return $this;
|
2023-03-24 08:02:34 +01:00
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a digital product
|
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 taxDigital($item): self
|
2023-03-24 08:02:34 +01:00
|
|
|
{
|
2023-05-15 13:20:47 +02:00
|
|
|
$this->default($item);
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
return $this;
|
2023-03-24 08:02:34 +01:00
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a service product
|
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 taxService($item): self
|
2023-03-24 08:02:34 +01:00
|
|
|
{
|
2023-04-26 02:56:23 +02:00
|
|
|
if($this->tax_data?->txbService == 'Y') {
|
2023-05-15 13:20:47 +02:00
|
|
|
$this->default($item);
|
2023-04-10 09:52:40 +02:00
|
|
|
}
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
return $this;
|
2023-03-24 08:02:34 +01:00
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a shipping product
|
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 taxShipping($item): self
|
2023-03-24 08:02:34 +01:00
|
|
|
{
|
2023-04-26 02:23:23 +02:00
|
|
|
if($this->tax_data?->txbFreight == 'Y') {
|
2023-05-15 13:20:47 +02:00
|
|
|
$this->default($item);
|
2023-04-10 09:52:40 +02:00
|
|
|
}
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
return $this;
|
2023-03-24 08:02:34 +01:00
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for a physical product
|
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 taxPhysical($item): self
|
2023-03-24 08:02:34 +01:00
|
|
|
{
|
2023-05-15 13:20:47 +02:00
|
|
|
$this->default($item);
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
return $this;
|
2023-03-24 08:02:34 +01:00
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the tax rate for an undefined product uses the default tax rate for the client county
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-05-15 13:20:47 +02:00
|
|
|
public function default($item): self
|
2023-03-24 08:02:34 +01:00
|
|
|
{
|
2023-04-24 04:22:15 +02:00
|
|
|
|
|
|
|
if($this->tax_data?->stateSalesTax == 0) {
|
|
|
|
|
|
|
|
if($this->tax_data->originDestination == "O"){
|
|
|
|
$tax_region = $this->client->company->tax_data->seller_subregion;
|
|
|
|
$this->tax_rate1 = $this->invoice->client->company->tax_data->regions->US->subregions->{$tax_region}->tax_rate;
|
|
|
|
$this->tax_name1 = "{$this->tax_data->geoState} Sales Tax";
|
|
|
|
} else {
|
|
|
|
$this->tax_rate1 = $this->invoice->client->company->tax_data->regions->{$this->client_region}->subregions->{$this->client_subregion}->tax_rate;
|
|
|
|
$this->tax_name1 = $this->invoice->client->company->tax_data->regions->{$this->client_region}->subregions->{$this->client_subregion}->tax_name;
|
2023-04-24 04:50:08 +02:00
|
|
|
|
|
|
|
if($this->client_region == 'US')
|
2023-04-24 04:50:35 +02:00
|
|
|
$this->tax_name1 = "{$this->client_subregion} ".$this->tax_name1;
|
|
|
|
|
2023-04-24 04:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:48:59 +02:00
|
|
|
$this->tax_rate1 = $this->tax_data->taxSales * 100;
|
|
|
|
$this->tax_name1 = "{$this->tax_data->geoState} Sales Tax";
|
2023-03-24 08:58:59 +01:00
|
|
|
|
2023-04-24 04:22:15 +02:00
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
return $this;
|
2023-03-24 08:02:34 +01:00
|
|
|
}
|
2023-04-12 06:08:56 +02:00
|
|
|
|
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;
|
|
|
|
$this->tax_name1 = "{$this->tax_data->geoState} Zero Rated Tax";
|
|
|
|
|
|
|
|
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-05-15 13:20:47 +02:00
|
|
|
$this->default($item);
|
2023-03-26 22:46:26 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2023-05-15 13:20:47 +02:00
|
|
|
|
2023-04-12 06:08:56 +02:00
|
|
|
/**
|
2023-05-15 13:20:47 +02:00
|
|
|
* Calculates the tax rate for a reverse tax product
|
2023-04-12 06:08:56 +02:00
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-05-15 13:20:47 +02:00
|
|
|
public function reverseTax($item): self
|
2023-03-29 05:30:16 +02:00
|
|
|
{
|
2023-05-15 13:20:47 +02:00
|
|
|
$this->default($item);
|
|
|
|
|
2023-03-29 05:30:16 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2023-05-15 05:57:00 +02:00
|
|
|
|
|
|
|
/**
|
2023-05-15 13:20:47 +02:00
|
|
|
* Calculates the tax rates to be applied
|
2023-05-15 05:57:00 +02:00
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2023-05-15 13:20:47 +02:00
|
|
|
public function calculateRates(): self
|
2023-05-15 05:57:00 +02:00
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-03-18 13:06:32 +01:00
|
|
|
}
|