2019-04-10 11:09:57 +02:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-14 13:11:46 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-04-10 11:09:57 +02:00
|
|
|
namespace Tests\Unit;
|
|
|
|
|
|
|
|
use App\Factory\InvoiceItemFactory;
|
2019-10-16 11:28:52 +02:00
|
|
|
use App\Helpers\Invoice\InvoiceSum;
|
2019-04-24 12:01:40 +02:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2019-10-07 11:56:10 +02:00
|
|
|
use Tests\MockAccountData;
|
2019-04-10 11:09:57 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-10-16 11:28:52 +02:00
|
|
|
* @covers App\Helpers\Invoice\InvoiceSum
|
2019-04-10 11:09:57 +02:00
|
|
|
*/
|
|
|
|
class InvoiceTest extends TestCase
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
use MockAccountData;
|
|
|
|
use DatabaseTransactions;
|
2019-04-10 11:42:19 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public $invoice;
|
2019-04-10 11:42:19 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public $invoice_calc;
|
2019-04-10 11:42:19 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public $settings;
|
2019-04-10 11:42:19 +02:00
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
public function setUp() :void
|
2019-04-10 11:09:57 +02:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
parent::setUp();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->makeTestData();
|
2019-10-07 11:39:22 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
2019-04-10 11:42:19 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice->usesinclusive_taxes = true;
|
2019-04-10 11:42:19 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
}
|
2019-04-24 12:01:40 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
private function buildLineItems()
|
|
|
|
{
|
|
|
|
$line_items = [];
|
2019-04-11 02:57:06 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
2020-09-06 11:38:10 +02:00
|
|
|
$item->cost = 10;
|
2019-04-11 02:57:06 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$line_items[] = $item;
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
2020-09-06 11:38:10 +02:00
|
|
|
$item->cost = 10;
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$line_items[] = $item;
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $line_items;
|
|
|
|
}
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testInvoiceTotals()
|
|
|
|
{
|
|
|
|
$this->invoice_calc->build();
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
$this->assertEquals($this->invoice_calc->getTotal(), 20);
|
|
|
|
}
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testInvoiceTotalsWithDiscount()
|
|
|
|
{
|
|
|
|
$this->invoice->discount = 5;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice_calc->build();
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotal(), 15);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getBalance(), 15);
|
|
|
|
}
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testInvoiceTotalsWithDiscountWithSurcharge()
|
|
|
|
{
|
|
|
|
$this->invoice->discount = 5;
|
|
|
|
$this->invoice->custom_value1 = 5;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice_calc->build();
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotal(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getBalance(), 20);
|
|
|
|
}
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testInvoiceTotalsWithDiscountWithSurchargeWithInclusiveTax()
|
|
|
|
{
|
|
|
|
$this->invoice->discount = 5;
|
|
|
|
$this->invoice->custom_value1 = 5;
|
|
|
|
$this->invoice->tax_name1 = 'GST';
|
|
|
|
$this->invoice->tax_rate1 = 10;
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice_calc->build();
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotal(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getBalance(), 20);
|
|
|
|
}
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testInvoiceTotalsWithDiscountWithSurchargeWithExclusiveTax()
|
|
|
|
{
|
|
|
|
$this->invoice->discount = 5;
|
|
|
|
$this->invoice->custom_value1 = 5;
|
|
|
|
$this->invoice->tax_name1 = 'GST';
|
|
|
|
$this->invoice->tax_rate1 = 10;
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
$this->invoice->is_amount_discount = true;
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
//$this->invoice_calc = new InvoiceSum($this->invoice, $this->settings);
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice_calc->build();
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotal(), 21.5);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getBalance(), 21.5);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotalTaxes(), 1.5);
|
|
|
|
}
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testInvoiceTotalsWithDiscountWithSurchargeWithDoubleExclusiveTax()
|
|
|
|
{
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice->discount = 5;
|
|
|
|
$this->invoice->custom_value1 = 5;
|
|
|
|
$this->invoice->tax_name1 = 'GST';
|
|
|
|
$this->invoice->tax_rate1 = 10;
|
|
|
|
$this->invoice->tax_name2 = 'GST';
|
|
|
|
$this->invoice->tax_rate2 = 10;
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice_calc->build();
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotal(), 23);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getBalance(), 23);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotalTaxes(), 3);
|
|
|
|
}
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testLineItemTaxRatesInclusiveTaxes()
|
|
|
|
{
|
|
|
|
$line_items = [];
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
2020-09-06 11:38:10 +02:00
|
|
|
$item->cost = 10;
|
2020-03-21 06:37:30 +01:00
|
|
|
$item->tax_rate1 = 10;
|
|
|
|
$item->tax_name1 = 10;
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$line_items[] = $item;
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
2020-09-06 11:38:10 +02:00
|
|
|
$item->cost = 10;
|
2020-03-21 06:37:30 +01:00
|
|
|
$item->tax_rate1 = 10;
|
|
|
|
$item->tax_name1 = 10;
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$line_items[] = $item;
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice->line_items = $line_items;
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice->uses_inclusive_taxes = true;
|
|
|
|
$this->invoice->discount = 0;
|
|
|
|
$this->invoice->custom_value1 = 0;
|
2019-04-16 05:28:05 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotal(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getBalance(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotalTaxes(), 1.82);
|
|
|
|
$this->assertEquals(count($this->invoice_calc->getTaxMap()), 1);
|
|
|
|
}
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function testLineItemTaxRatesExclusiveTaxes()
|
|
|
|
{
|
|
|
|
$line_items = [];
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
2020-09-06 11:38:10 +02:00
|
|
|
$item->cost = 10;
|
2020-03-21 06:37:30 +01:00
|
|
|
$item->tax_rate1 = 10;
|
|
|
|
$item->tax_name1 = 10;
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
|
|
|
$item->quantity = 1;
|
2020-09-06 11:38:10 +02:00
|
|
|
$item->cost = 10;
|
2020-03-21 06:37:30 +01:00
|
|
|
$item->tax_rate1 = 10;
|
|
|
|
$item->tax_name1 = 10;
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $line_items;
|
|
|
|
$this->invoice->discount = 0;
|
|
|
|
$this->invoice->tax_name1 = 'GST';
|
|
|
|
$this->invoice->tax_rate1 = 10;
|
|
|
|
$this->invoice->tax_name2 = 'GST';
|
|
|
|
$this->invoice->tax_rate2 = 10;
|
|
|
|
|
|
|
|
$this->invoice->uses_inclusive_taxes = false;
|
|
|
|
$this->invoice_calc = new InvoiceSum($this->invoice);
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotal(), 26);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getBalance(), 26);
|
|
|
|
//$this->assertEquals($this->invoice_calc->getTotalTaxes(), 4);
|
|
|
|
//$this->assertEquals(count($this->invoice_calc->getTaxMap()), 1);
|
|
|
|
}
|
|
|
|
}
|