1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00

Doc blocks for Rules

This commit is contained in:
David Bomba 2023-04-12 14:08:56 +10:00
parent b3d9dc3a51
commit 47e35fe4d1
4 changed files with 220 additions and 61 deletions

View File

@ -17,22 +17,35 @@ use App\DataMapper\Tax\RuleInterface;
class Rule extends BaseRule implements RuleInterface
{
/** @var string $seller_region */
public string $seller_region = 'EU';
/** @var bool $consumer_tax_exempt */
public bool $consumer_tax_exempt = false;
/** @var bool $business_tax_exempt */
public bool $business_tax_exempt = false;
/** @var bool $eu_business_tax_exempt */
public bool $eu_business_tax_exempt = true;
/** @var bool $foreign_business_tax_exempt */
public bool $foreign_business_tax_exempt = true;
/** @var bool $foreign_consumer_tax_exempt */
public bool $foreign_consumer_tax_exempt = true;
/** @var float $tax_rate */
public float $tax_rate = 0;
/** @var float $reduced_tax_rate */
public float $reduced_tax_rate = 0;
/**
* Initializes the rules and builds any required data.
*
* @return self
*/
public function init(): self
{
$this->calculateRates();
@ -40,6 +53,12 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Sets the correct tax rate based on the product type.
*
* @param mixed $product_tax_type
* @return self
*/
public function taxByType($product_tax_type): self
{
@ -61,6 +80,11 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rate for a reduced tax product
*
* @return self
*/
public function taxReduced(): self
{
$this->tax_rate1 = $this->reduced_tax_rate;
@ -69,6 +93,11 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rate for a tax exempt product
*
* @return self
*/
public function taxExempt(): self
{
$this->tax_name1 = '';
@ -77,36 +106,55 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rate for a digital product
*
* @return self
*/
public function taxDigital(): self
{
// $this->tax();
return $this;
}
/**
* Calculates the tax rate for a service product
*
* @return self
*/
public function taxService(): self
{
// $this->tax();
return $this;
}
/**
* Calculates the tax rate for a shipping product
*
* @return self
*/
public function taxShipping(): self
{
// $this->tax();
return $this;
}
/**
* Calculates the tax rate for a physical product
*
* @return self
*/
public function taxPhysical(): self
{
// $this->tax();
$this->tax_rate1 = $this->tax_rate;
$this->tax_name1 = 'MwSt.';
return $this;
}
/**
* Calculates the tax rate for a default product
*
* @return self
*/
public function default(): self
{
@ -116,27 +164,37 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rate for an override product
*
* @return self
*/
public function override(): self
{
return $this;
}
/**
* Calculates the tax rates based on the client's location.
*
* @return self
*/
public function calculateRates(): self
{
if ($this->client->is_tax_exempt) {
nlog("tax exempt");
// nlog("tax exempt");
$this->tax_rate = 0;
$this->reduced_tax_rate = 0;
}
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)
{
nlog("euro zone and tax exempt");
// nlog("euro zone and tax exempt");
$this->tax_rate = 0;
$this->reduced_tax_rate = 0;
}
elseif(!in_array($this->client_subregion, $this->eu_country_codes) && ($this->foreign_consumer_tax_exempt || $this->foreign_business_tax_exempt)) //foreign + tax exempt
{
nlog("foreign and tax exempt");
// nlog("foreign and tax exempt");
$this->tax_rate = 0;
$this->reduced_tax_rate = 0;
}
@ -144,18 +202,18 @@ class Rule extends BaseRule implements RuleInterface
{
if(($this->client->company->tax_data->seller_subregion != $this->client_subregion) && $this->client->company->tax_data->regions->EU->has_sales_above_threshold)
{
nlog("eu zone with sales above threshold");
// nlog("eu zone with sales above threshold");
$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;
}
else {
nlog("EU with intra-community supply ie DE to DE");
// nlog("EU with intra-community supply ie DE to DE");
$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;
}
}
else {
nlog("default tax");
// nlog("default tax");
$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;
}

View File

@ -14,12 +14,21 @@ namespace App\DataMapper\Tax;
class TaxModel
{
/** @var mixed $seller_subregion */
public string $seller_subregion = 'CA';
/** @var mixed $version */
public string $version = 'alpha';
/** @var mixed $regions */
public object $regions;
/**
* __construct
*
* @param mixed $model
* @return void
*/
public function __construct(public ?TaxModel $model = null)
{
@ -30,6 +39,11 @@ class TaxModel
}
/**
* Initializes the rules and builds any required data.
*
* @return void
*/
public function init()
{
$this->regions = new \stdClass();
@ -44,6 +58,11 @@ class TaxModel
return $this->regions;
}
/**
* Builds the model for Australian Taxes
*
* @return self
*/
private function auRegion(): self
{
$this->regions->AU = new \stdClass();
@ -55,6 +74,11 @@ class TaxModel
return $this;
}
/**
* Builds the model for Australian Subregions
*
* @return self
*/
private function auSubRegions(): self
{
@ -67,6 +91,11 @@ class TaxModel
return $this;
}
/**
* Builds the model for US Taxes
*
* @return self
*/
private function usRegion(): self
{
$this->regions->US->has_sales_above_threshold = false;
@ -76,6 +105,11 @@ class TaxModel
return $this;
}
/**
* Builds the model for EU Taxes
*
* @return self
*/
private function euRegion(): self
{
@ -87,6 +121,11 @@ class TaxModel
return $this;
}
/**
* Builds the model for US States
*
* @return self
*/
private function usSubRegions(): self
{
$this->regions->US->subregions = new \stdClass();
@ -294,6 +333,11 @@ class TaxModel
return $this;
}
/**
* Create the EU member countries
*
* @return self
*/
private function euSubRegions(): self
{

View File

@ -15,15 +15,22 @@ use App\DataMapper\Tax\BaseRule;
use App\DataMapper\Tax\RuleInterface;
use App\Models\Product;
class Rule extends BaseRule implements RuleInterface
{
public string $seller_region = 'US';
/**
* 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.
*/
class Rule extends BaseRule implements RuleInterface
{
/** @var string $seller_region */
public string $seller_region = 'US';
/**
* Initializes the rules and builds any required data.
*
* @return self
*/
public function init(): self
{
$this->calculateRates();
@ -31,11 +38,22 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Override tax class, we use this when we do not modify the input taxes
*
* @return self
*/
public function override(): self
{
return $this;
}
/**
* Sets the correct tax rate based on the product type.
*
* @param mixed $product_tax_type
* @return self
*/
public function taxByType($product_tax_type): self
{
@ -53,6 +71,11 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Sets the tax as exempt (0)
*
* @return self
*/
public function taxExempt(): self
{
$this->tax_name1 = '';
@ -61,6 +84,11 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rate for a digital product
*
* @return self
*/
public function taxDigital(): self
{
$this->default();
@ -68,6 +96,11 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rate for a service product
*
* @return self
*/
public function taxService(): self
{
if($this->tax_data->txbService == 'Y') {
@ -77,6 +110,11 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rate for a shipping product
*
* @return self
*/
public function taxShipping(): self
{
if($this->tax_data->txbFreight == 'Y') {
@ -86,6 +124,11 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rate for a physical product
*
* @return self
*/
public function taxPhysical(): self
{
$this->default();
@ -93,6 +136,11 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rate for an undefined product uses the default tax rate for the client county
*
* @return self
*/
public function default(): self
{
@ -102,6 +150,11 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rate for a reduced tax product
*
* @return self
*/
public function taxReduced(): self
{
$this->default();
@ -109,11 +162,13 @@ class Rule extends BaseRule implements RuleInterface
return $this;
}
/**
* Calculates the tax rates to be applied
*
* @return self
*/
public function calculateRates(): self
{
if($this->client_region != 'US' && $this->isTaxableRegion())
return $this;
return $this;
}
}

View File

@ -55,6 +55,8 @@ class InvoiceItemSum
'SK', // Slovakia
'US', // USA
'AU', // Australia
];
protected $invoice;