2023-03-19 10:10:20 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Tax;
|
|
|
|
|
|
|
|
use Tests\TestCase;
|
2023-03-24 03:56:26 +01:00
|
|
|
use App\Models\Client;
|
2023-03-24 06:43:09 +01:00
|
|
|
use App\Models\Invoice;
|
2023-03-24 08:58:59 +01:00
|
|
|
use App\Models\Product;
|
2023-03-19 10:10:20 +01:00
|
|
|
use Tests\MockAccountData;
|
2023-03-24 08:58:59 +01:00
|
|
|
use App\DataMapper\InvoiceItem;
|
2023-03-24 23:26:27 +01:00
|
|
|
use App\DataMapper\Tax\TaxData;
|
2023-03-24 08:58:59 +01:00
|
|
|
use App\Factory\InvoiceFactory;
|
2023-04-03 00:22:07 +02:00
|
|
|
use App\DataMapper\Tax\TaxModel;
|
|
|
|
use App\DataMapper\Tax\ZipTax\Response;
|
2023-03-19 10:10:20 +01:00
|
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
class SumTaxTest extends TestCase
|
|
|
|
{
|
|
|
|
use MockAccountData;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
2023-03-24 06:43:09 +01:00
|
|
|
public Response $response;
|
|
|
|
|
|
|
|
public array $resp = [
|
2023-03-24 03:56:26 +01:00
|
|
|
"geoPostalCode" => "92582",
|
|
|
|
"geoCity" => "SAN JACINTO",
|
|
|
|
"geoCounty" => "RIVERSIDE",
|
|
|
|
"geoState" => "CA",
|
|
|
|
"taxSales" => 0.0875,
|
|
|
|
"taxUse" => 0.0875, // tax amount where destination does not charge sales tax, but origin does
|
|
|
|
"txbService" => "N", // whether services are taxed in this locale
|
|
|
|
"txbFreight" => "N", // whether freight is taxes in this locale
|
|
|
|
"stateSalesTax" => 0.06,
|
|
|
|
"stateUseTax" => 0.06,
|
|
|
|
"citySalesTax" => 0.01,
|
|
|
|
"cityUseTax" => 0.01,
|
|
|
|
"cityTaxCode" => "874",
|
|
|
|
"countySalesTax" => 0.0025,
|
|
|
|
"countyUseTax" => 0.0025,
|
|
|
|
"countyTaxCode" => "",
|
|
|
|
"districtSalesTax" => 0.015,
|
|
|
|
"districtUseTax" => 0.015,
|
|
|
|
"district1Code" => "26",
|
|
|
|
"district1SalesTax" => 0,
|
|
|
|
"district1UseTax" => 0,
|
|
|
|
"district2Code" => "26",
|
|
|
|
"district2SalesTax" => 0.005,
|
|
|
|
"district2UseTax" => 0.005,
|
|
|
|
"district3Code" => "",
|
|
|
|
"district3SalesTax" => 0,
|
|
|
|
"district3UseTax" => 0,
|
|
|
|
"district4Code" => "33",
|
|
|
|
"district4SalesTax" => 0.01,
|
|
|
|
"district4UseTax" => 0.01,
|
|
|
|
"district5Code" => "",
|
|
|
|
"district5SalesTax" => 0,
|
|
|
|
"district5UseTax" => 0, //district1-5 portion of the district tax
|
|
|
|
"originDestination" => "D", //location where this is taxed origin/destination/null
|
2023-03-19 10:10:20 +01:00
|
|
|
];
|
|
|
|
|
2023-03-19 10:30:28 +01:00
|
|
|
|
|
|
|
protected function setUp() :void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->withoutMiddleware(
|
|
|
|
ThrottleRequests::class
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->withoutExceptionHandling();
|
|
|
|
|
2023-03-24 03:56:26 +01:00
|
|
|
$this->makeTestData();
|
2023-03-24 06:43:09 +01:00
|
|
|
|
|
|
|
$this->response = new Response($this->resp);
|
|
|
|
|
2023-03-19 10:10:20 +01:00
|
|
|
}
|
2023-03-19 10:30:28 +01:00
|
|
|
|
2023-03-24 23:26:27 +01:00
|
|
|
/** Proves that we do not charge taxes automatically */
|
2023-03-24 08:58:59 +01:00
|
|
|
public function testCalcInvoiceNoTax()
|
2023-03-24 08:02:34 +01:00
|
|
|
{
|
2023-04-03 00:22:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
$tax_data = new TaxModel();
|
|
|
|
$tax_data->seller_subregion = 'CA';
|
|
|
|
$tax_data->regions->US->has_sales_above_threshold = true;
|
2023-04-08 00:42:04 +02:00
|
|
|
$tax_data->regions->US->tax_all_subregions = true;
|
2023-04-03 00:22:07 +02:00
|
|
|
|
2023-03-24 08:58:59 +01:00
|
|
|
$this->company->calculate_taxes = false;
|
2023-04-03 00:22:07 +02:00
|
|
|
$this->company->tax_data = $tax_data;
|
2023-03-24 08:58:59 +01:00
|
|
|
$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 = [];
|
|
|
|
|
2023-03-24 23:26:27 +01:00
|
|
|
$invoice->tax_data = new TaxData($this->response);
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2023-03-24 23:26:27 +01:00
|
|
|
/** Proves that we do calc taxes automatically */
|
2023-03-24 08:58:59 +01:00
|
|
|
public function testCalcInvoiceTax()
|
|
|
|
{
|
|
|
|
|
2023-04-03 00:22:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
$tax_data = new TaxModel();
|
|
|
|
$tax_data->seller_subregion = 'CA';
|
|
|
|
$tax_data->regions->US->has_sales_above_threshold = true;
|
2023-04-08 00:42:04 +02:00
|
|
|
$tax_data->regions->US->tax_all_subregions = true;
|
2023-04-03 00:22:07 +02:00
|
|
|
|
2023-03-24 08:02:34 +01:00
|
|
|
$this->company->calculate_taxes = true;
|
2023-04-03 00:22:07 +02:00
|
|
|
$this->company->tax_data = $tax_data;
|
2023-03-24 08:58:59 +01:00
|
|
|
$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 = [];
|
|
|
|
|
2023-03-24 23:26:27 +01:00
|
|
|
$invoice->tax_data = new TaxData($this->response);
|
2023-03-24 08:58:59 +01:00
|
|
|
|
|
|
|
$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);
|
2023-03-24 08:02:34 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 06:43:09 +01:00
|
|
|
public function testTaxOnCompany()
|
|
|
|
{
|
|
|
|
|
2023-03-24 23:26:27 +01:00
|
|
|
$tax_class = new TaxData($this->response);
|
2023-03-24 06:43:09 +01:00
|
|
|
|
|
|
|
$this->company->tax_data = $tax_class;
|
|
|
|
$this->company->save();
|
|
|
|
|
|
|
|
$this->assertEquals("92582", $this->company->tax_data->origin->geoPostalCode);
|
|
|
|
$this->assertEquals(0.0875, $this->company->tax_data->origin->taxSales);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTaxOnClient()
|
2023-03-23 21:40:44 +01:00
|
|
|
{
|
2023-03-24 03:56:26 +01:00
|
|
|
$c = Client::factory()->create([
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
]);
|
2023-03-24 06:43:09 +01:00
|
|
|
|
2023-03-24 23:26:27 +01:00
|
|
|
$tax_class = new TaxData($this->response, $this->response);
|
2023-03-24 03:56:26 +01:00
|
|
|
|
2023-03-24 06:43:09 +01:00
|
|
|
$c->tax_data = $tax_class;
|
2023-03-24 03:56:26 +01:00
|
|
|
$c->save();
|
|
|
|
|
2023-03-24 06:43:09 +01:00
|
|
|
$this->assertEquals("92582", $c->tax_data->origin->geoPostalCode);
|
|
|
|
$this->assertEquals(0.0875, $c->tax_data->origin->taxSales);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTaxOnInvoice()
|
|
|
|
{
|
|
|
|
|
|
|
|
$i = Invoice::factory()->create([
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'client_id' => $this->client->id,
|
|
|
|
'user_id' => $this->user->id,
|
|
|
|
]);
|
|
|
|
|
2023-03-24 23:26:27 +01:00
|
|
|
$tax_class = new TaxData($this->response);
|
2023-03-24 06:43:09 +01:00
|
|
|
|
|
|
|
$i->tax_data = $tax_class;
|
|
|
|
$i->save();
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals("92582", $i->tax_data->origin->geoPostalCode);
|
|
|
|
$this->assertEquals(0.0875, $i->tax_data->origin->taxSales);
|
|
|
|
|
|
|
|
|
2023-03-23 21:40:44 +01:00
|
|
|
}
|
|
|
|
|
2023-03-19 10:30:28 +01:00
|
|
|
public function testSumOfInvoice()
|
|
|
|
{
|
|
|
|
|
2023-03-24 06:43:09 +01:00
|
|
|
$this->assertEquals("CA", $this->response->geoState);
|
2023-03-19 10:30:28 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSumOfTaxes()
|
|
|
|
{
|
|
|
|
$sum =
|
2023-03-24 06:43:09 +01:00
|
|
|
$this->response->stateSalesTax +
|
|
|
|
$this->response->citySalesTax +
|
|
|
|
$this->response->countySalesTax +
|
|
|
|
$this->response->districtSalesTax;
|
2023-03-19 10:30:28 +01:00
|
|
|
|
|
|
|
$this->assertEquals(0.0875, $sum);
|
|
|
|
}
|
|
|
|
|
2023-03-19 10:10:20 +01:00
|
|
|
}
|