mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-13 22:54:25 +01:00
Fixes for auto calculating taxes
This commit is contained in:
parent
aa51299a10
commit
680277f062
@ -24,5 +24,8 @@ class InvoiceTaxData
|
|||||||
|
|
||||||
public function __construct(public Response $origin)
|
public function __construct(public Response $origin)
|
||||||
{
|
{
|
||||||
|
foreach($origin as $key => $value) {
|
||||||
|
$this->{$key} = $value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ namespace App\DataMapper\Tax;
|
|||||||
|
|
||||||
interface RuleInterface
|
interface RuleInterface
|
||||||
{
|
{
|
||||||
public function run();
|
public function tax();
|
||||||
|
|
||||||
public function taxByType(?int $type);
|
public function taxByType(?int $type);
|
||||||
|
|
||||||
|
@ -93,7 +93,7 @@ class Response
|
|||||||
public float $district5UseTax = 0;
|
public float $district5UseTax = 0;
|
||||||
public string $originDestination = "";
|
public string $originDestination = "";
|
||||||
|
|
||||||
public function __construct(?array $data)
|
public function __construct($data)
|
||||||
{
|
{
|
||||||
|
|
||||||
foreach($data as $key => $value){
|
foreach($data as $key => $value){
|
||||||
|
@ -11,7 +11,9 @@
|
|||||||
|
|
||||||
namespace App\DataMapper\Tax\de;
|
namespace App\DataMapper\Tax\de;
|
||||||
|
|
||||||
|
use App\Models\Product;
|
||||||
use App\DataMapper\Tax\RuleInterface;
|
use App\DataMapper\Tax\RuleInterface;
|
||||||
|
use App\DataMapper\Tax\ZipTax\Response;
|
||||||
|
|
||||||
class Rule implements RuleInterface
|
class Rule implements RuleInterface
|
||||||
{
|
{
|
||||||
@ -89,8 +91,91 @@ class Rule implements RuleInterface
|
|||||||
|
|
||||||
public bool $foreign_consumer_tax_exempt = true;
|
public bool $foreign_consumer_tax_exempt = true;
|
||||||
|
|
||||||
public function run()
|
public string $tax_name1 = '';
|
||||||
|
public float $tax_rate1 = 0;
|
||||||
|
|
||||||
|
public string $tax_name2 = '';
|
||||||
|
public float $tax_rate2 = 0;
|
||||||
|
|
||||||
|
public string $tax_name3 = '';
|
||||||
|
public float $tax_rate3 = 0;
|
||||||
|
|
||||||
|
|
||||||
|
public function __construct(public Response $tax_data)
|
||||||
{
|
{
|
||||||
|
$this->tax_data = $tax_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tax(): self
|
||||||
|
{
|
||||||
|
$this->tax_name1 = 21;
|
||||||
|
$this->tax_rate1 = "VAT";
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function taxByType(?int $product_tax_type): self
|
||||||
|
{
|
||||||
|
if(!$product_tax_type)
|
||||||
|
return $this;
|
||||||
|
|
||||||
|
match($product_tax_type){
|
||||||
|
Product::PRODUCT_TAX_EXEMPT => $this->taxExempt(),
|
||||||
|
Product::PRODUCT_TYPE_DIGITAL => $this->taxDigital(),
|
||||||
|
Product::PRODUCT_TYPE_SERVICE => $this->taxService(),
|
||||||
|
Product::PRODUCT_TYPE_SHIPPING => $this->taxShipping(),
|
||||||
|
Product::PRODUCT_TYPE_PHYSICAL => $this->taxPhysical(),
|
||||||
|
default => $this->default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function taxExempt(): self
|
||||||
|
{
|
||||||
|
$this->tax_name1 = '';
|
||||||
|
$this->tax_rate1 = 0;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function taxDigital(): self
|
||||||
|
{
|
||||||
|
$this->tax();
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function taxService(): self
|
||||||
|
{
|
||||||
|
if($this->tax_data->txbService == 'Y')
|
||||||
|
$this->tax();
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function taxShipping(): self
|
||||||
|
{
|
||||||
|
if($this->tax_data->txbFreight == 'Y')
|
||||||
|
$this->tax();
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function taxPhysical(): self
|
||||||
|
{
|
||||||
|
$this->tax();
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function default(): self
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->tax_name1 = '';
|
||||||
|
$this->tax_rate1 = 0;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ namespace App\DataMapper\Tax\us;
|
|||||||
|
|
||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
use App\DataMapper\Tax\RuleInterface;
|
use App\DataMapper\Tax\RuleInterface;
|
||||||
|
use App\DataMapper\Tax\ZipTax\Response;
|
||||||
|
|
||||||
class Rule implements RuleInterface
|
class Rule implements RuleInterface
|
||||||
{
|
{
|
||||||
@ -79,22 +80,25 @@ class Rule implements RuleInterface
|
|||||||
public string $tax_name3 = '';
|
public string $tax_name3 = '';
|
||||||
public float $tax_rate3 = 0;
|
public float $tax_rate3 = 0;
|
||||||
|
|
||||||
public function __construct(public RuleInterface $tax_data)
|
public function __construct(public Response $tax_data)
|
||||||
{
|
{
|
||||||
$this->tax_data = $tax_data;
|
$this->tax_data = $tax_data;
|
||||||
|
nlog($tax_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run()
|
public function tax(): self
|
||||||
{
|
{
|
||||||
$this->tax_name1 = $this->tax_data->taxSales * 100;
|
$this->tax_rate1 = $this->tax_data->taxSales * 100;
|
||||||
$this->tax_rate1 = "{$this->tax_data->geoState} Sales Tax";
|
$this->tax_name1 = "{$this->tax_data->geoState} Sales Tax";
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function taxByType(?int $product_tax_type)
|
public function taxByType(?int $product_tax_type): self
|
||||||
{
|
{
|
||||||
if(!$product_tax_type)
|
if(!$product_tax_type)
|
||||||
return;
|
return $this;
|
||||||
|
|
||||||
match($product_tax_type){
|
match($product_tax_type){
|
||||||
Product::PRODUCT_TAX_EXEMPT => $this->taxExempt(),
|
Product::PRODUCT_TAX_EXEMPT => $this->taxExempt(),
|
||||||
@ -108,39 +112,50 @@ class Rule implements RuleInterface
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function taxExempt()
|
public function taxExempt(): self
|
||||||
{
|
{
|
||||||
$this->tax_name1 = '';
|
$this->tax_name1 = '';
|
||||||
$this->tax_rate1 = 0;
|
$this->tax_rate1 = 0;
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function taxDigital()
|
public function taxDigital(): self
|
||||||
{
|
{
|
||||||
$this->tax_name1 = '';
|
$this->tax();
|
||||||
$this->tax_rate1 = 0;
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function taxService()
|
public function taxService(): self
|
||||||
{
|
{
|
||||||
if($this->tax_data->txbService == 'Y')
|
if($this->tax_data->txbService == 'Y')
|
||||||
$this->run();
|
$this->tax();
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function taxShipping()
|
public function taxShipping(): self
|
||||||
{
|
{
|
||||||
if($this->tax_data->txbFreight == 'N')
|
if($this->tax_data->txbFreight == 'Y')
|
||||||
$this->run();
|
$this->tax();
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function taxPhysical()
|
public function taxPhysical(): self
|
||||||
{
|
{
|
||||||
|
$this->tax();
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function default(): self
|
||||||
|
{
|
||||||
|
|
||||||
$this->tax_name1 = '';
|
$this->tax_name1 = '';
|
||||||
$this->tax_rate1 = 0;
|
$this->tax_rate1 = 0;
|
||||||
}
|
|
||||||
|
|
||||||
public function default()
|
return $this;
|
||||||
{
|
|
||||||
$this->tax_name1 = '';
|
|
||||||
$this->tax_rate1 = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ use App\DataMapper\InvoiceItem;
|
|||||||
use App\DataMapper\BaseSettings;
|
use App\DataMapper\BaseSettings;
|
||||||
use App\DataMapper\Tax\RuleInterface;
|
use App\DataMapper\Tax\RuleInterface;
|
||||||
use App\Utils\Traits\NumberFormatter;
|
use App\Utils\Traits\NumberFormatter;
|
||||||
|
use App\DataMapper\Tax\ZipTax\Response;
|
||||||
|
|
||||||
class InvoiceItemSum
|
class InvoiceItemSum
|
||||||
{
|
{
|
||||||
@ -65,7 +66,7 @@ class InvoiceItemSum
|
|||||||
if ($this->invoice->client) {
|
if ($this->invoice->client) {
|
||||||
$this->currency = $this->invoice->client->currency();
|
$this->currency = $this->invoice->client->currency();
|
||||||
$this->client = $this->invoice->client;
|
$this->client = $this->invoice->client;
|
||||||
$this->calc_tax = $this->shouldCalculateTax();
|
$this->shouldCalculateTax();
|
||||||
} else {
|
} else {
|
||||||
$this->currency = $this->invoice->vendor->currency();
|
$this->currency = $this->invoice->vendor->currency();
|
||||||
}
|
}
|
||||||
@ -99,22 +100,29 @@ class InvoiceItemSum
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function shouldCalculateTax(): bool
|
private function shouldCalculateTax(): self
|
||||||
{
|
{
|
||||||
if(!$this->invoice->company->calculate_taxes || $this->client->is_tax_exempt)
|
if (!$this->invoice->company->calculate_taxes || $this->client->is_tax_exempt) {
|
||||||
return false;
|
$this->calc_tax = false;
|
||||||
|
nlog("returning false");
|
||||||
if(in_array($this->client->country->iso_3166_2, ['US'])){ //only calculate for USA
|
return $this;
|
||||||
|
|
||||||
$class = "App\DataMapper\Tax\\".strtolower($this->client->country->iso_3166_2)."\\Rule";
|
|
||||||
|
|
||||||
$this->rule = new $class($this->invoice->tax_data);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
if (in_array($this->client->country->iso_3166_2, ['US'])) { //only calculate for USA
|
||||||
|
$class = "App\DataMapper\Tax\\".strtolower($this->client->country->iso_3166_2)."\\Rule";
|
||||||
|
|
||||||
|
$tax_data = new Response($this->invoice->tax_data);
|
||||||
|
|
||||||
|
$this->rule = new $class($tax_data);
|
||||||
|
|
||||||
|
nlog("returning true");
|
||||||
|
|
||||||
|
$this->calc_tax = true;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function push()
|
private function push()
|
||||||
@ -157,12 +165,9 @@ class InvoiceItemSum
|
|||||||
*/
|
*/
|
||||||
private function calcTaxesAutomatically(): self
|
private function calcTaxesAutomatically(): self
|
||||||
{
|
{
|
||||||
if($this->invoice->company->tax_all_products || $this->item->tax_id != ''){
|
if ($this->invoice->company->tax_all_products || $this->item->tax_id != '') {
|
||||||
$this->rule->run();
|
$this->rule->tax();
|
||||||
|
} else {
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
$this->rule->taxByType($this->item->tax_id);
|
$this->rule->taxByType($this->item->tax_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,8 +185,9 @@ class InvoiceItemSum
|
|||||||
|
|
||||||
private function calcTaxes()
|
private function calcTaxes()
|
||||||
{
|
{
|
||||||
if($this->calc_tax)
|
if ($this->calc_tax) {
|
||||||
$this->calcTaxesAutomatically();
|
$this->calcTaxesAutomatically();
|
||||||
|
}
|
||||||
|
|
||||||
$item_tax = 0;
|
$item_tax = 0;
|
||||||
|
|
||||||
@ -190,7 +196,6 @@ class InvoiceItemSum
|
|||||||
|
|
||||||
$item_tax += $item_tax_rate1_total;
|
$item_tax += $item_tax_rate1_total;
|
||||||
|
|
||||||
// if($item_tax_rate1_total != 0)
|
|
||||||
if (strlen($this->item->tax_name1) > 1) {
|
if (strlen($this->item->tax_name1) > 1) {
|
||||||
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
|
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
|
||||||
}
|
}
|
||||||
|
@ -2,4 +2,5 @@ parameters:
|
|||||||
level: 2
|
level: 2
|
||||||
paths:
|
paths:
|
||||||
- app
|
- app
|
||||||
# - tests
|
universalObjectCratesClasses:
|
||||||
|
- App\DataMapper\Tax\RuleInterface
|
@ -14,11 +14,14 @@ namespace Tests\Unit\Tax;
|
|||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
|
use App\Models\Product;
|
||||||
use Tests\MockAccountData;
|
use Tests\MockAccountData;
|
||||||
|
use App\DataMapper\InvoiceItem;
|
||||||
use App\DataMapper\Tax\ClientTaxData;
|
use App\DataMapper\Tax\ClientTaxData;
|
||||||
use App\DataMapper\Tax\CompanyTaxData;
|
use App\DataMapper\Tax\CompanyTaxData;
|
||||||
use App\DataMapper\Tax\InvoiceTaxData;
|
use App\DataMapper\Tax\InvoiceTaxData;
|
||||||
use App\DataMapper\Tax\ZipTax\Response;
|
use App\DataMapper\Tax\ZipTax\Response;
|
||||||
|
use App\Factory\InvoiceFactory;
|
||||||
use Illuminate\Routing\Middleware\ThrottleRequests;
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
|
||||||
@ -86,10 +89,89 @@ class SumTaxTest extends TestCase
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCalcLogic()
|
|
||||||
|
public function testCalcInvoiceNoTax()
|
||||||
{
|
{
|
||||||
|
$this->company->calculate_taxes = false;
|
||||||
|
$this->company->tax_all_products = true;
|
||||||
|
$this->company->save();
|
||||||
|
|
||||||
|
$client = Client::factory()->create([
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'country_id' => 840,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
|
||||||
|
$invoice->client_id = $client->id;
|
||||||
|
$invoice->uses_inclusive_taxes = false;
|
||||||
|
|
||||||
|
$line_items = [];
|
||||||
|
|
||||||
|
$invoice->tax_data = new InvoiceTaxData($this->response);
|
||||||
|
|
||||||
|
$line_item = new InvoiceItem();
|
||||||
|
$line_item->quantity = 1;
|
||||||
|
$line_item->cost = 10;
|
||||||
|
$line_item->product_key = 'Test';
|
||||||
|
$line_item->notes = 'Test';
|
||||||
|
$line_item->tax_id = Product::PRODUCT_TYPE_PHYSICAL;
|
||||||
|
$line_items[] = $line_item;
|
||||||
|
|
||||||
|
$invoice->line_items = $line_items;
|
||||||
|
$invoice->save();
|
||||||
|
|
||||||
|
$invoice = $invoice->calc()->getInvoice();
|
||||||
|
|
||||||
|
$line_items = $invoice->line_items;
|
||||||
|
|
||||||
|
|
||||||
|
$this->assertEquals(10, $invoice->amount);
|
||||||
|
$this->assertEquals("", $line_items[0]->tax_name1);
|
||||||
|
$this->assertEquals(0, $line_items[0]->tax_rate1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testCalcInvoiceTax()
|
||||||
|
{
|
||||||
|
|
||||||
$this->company->calculate_taxes = true;
|
$this->company->calculate_taxes = true;
|
||||||
$this->company->tax_all_products = true;
|
$this->company->tax_all_products = true;
|
||||||
|
$this->company->save();
|
||||||
|
|
||||||
|
$client = Client::factory()->create([
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'country_id' => 840,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
|
||||||
|
$invoice->client_id = $client->id;
|
||||||
|
$invoice->uses_inclusive_taxes = false;
|
||||||
|
|
||||||
|
$line_items = [];
|
||||||
|
|
||||||
|
$invoice->tax_data = new InvoiceTaxData($this->response);
|
||||||
|
|
||||||
|
$line_item = new InvoiceItem;
|
||||||
|
$line_item->quantity = 1;
|
||||||
|
$line_item->cost = 10;
|
||||||
|
$line_item->product_key = 'Test';
|
||||||
|
$line_item->notes = 'Test';
|
||||||
|
$line_item->tax_id = Product::PRODUCT_TYPE_PHYSICAL;
|
||||||
|
$line_items[] = $line_item;
|
||||||
|
|
||||||
|
$invoice->line_items = $line_items;
|
||||||
|
$invoice->save();
|
||||||
|
|
||||||
|
$invoice = $invoice->calc()->getInvoice();
|
||||||
|
|
||||||
|
$line_items = $invoice->line_items;
|
||||||
|
|
||||||
|
|
||||||
|
$this->assertEquals(10.88, $invoice->amount);
|
||||||
|
$this->assertEquals("CA Sales Tax", $line_items[0]->tax_name1);
|
||||||
|
$this->assertEquals(8.75, $line_items[0]->tax_rate1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTaxOnCompany()
|
public function testTaxOnCompany()
|
||||||
|
Loading…
Reference in New Issue
Block a user