2019-04-10 11:09:57 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit;
|
|
|
|
|
2019-04-10 11:42:19 +02:00
|
|
|
use App\Factory\InvoiceFactory;
|
2019-04-10 11:09:57 +02:00
|
|
|
use App\Factory\InvoiceItemFactory;
|
2019-04-11 02:35:30 +02:00
|
|
|
use App\Helpers\Invoice\InvoiceCalc;
|
2019-04-24 12:01:40 +02:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2019-04-10 11:09:57 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @covers App\Helpers\Invoice\InvoiceCalc
|
|
|
|
*/
|
|
|
|
class InvoiceTest extends TestCase
|
|
|
|
{
|
2019-04-10 11:42:19 +02:00
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
public $invoice;
|
2019-04-10 11:42:19 +02:00
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
public $invoice_calc;
|
2019-04-10 11:42:19 +02:00
|
|
|
|
2019-04-24 12:01:40 +02: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
|
|
|
{
|
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
parent::setUp();
|
2019-04-10 11:09:57 +02:00
|
|
|
|
2019-04-16 05:28:05 +02:00
|
|
|
$this->invoice = InvoiceFactory::create(1,1);//stub the company and user_id
|
2019-04-24 12:01:40 +02:00
|
|
|
|
2019-04-10 11:42:19 +02:00
|
|
|
$this->invoice->line_items = $this->buildLineItems();
|
2019-04-24 07:18:48 +02:00
|
|
|
|
|
|
|
$this->settings = $this->invoice->settings;
|
2019-04-10 11:42:19 +02:00
|
|
|
|
2019-09-19 08:22:32 +02:00
|
|
|
$this->settings->custom_invoice_taxes1 = true;
|
|
|
|
$this->settings->custom_invoice_taxes2 = true;
|
2019-04-24 07:18:48 +02:00
|
|
|
$this->settings->inclusive_taxes = true;
|
|
|
|
$this->settings->precision = 2;
|
2019-04-10 11:42:19 +02:00
|
|
|
|
|
|
|
|
2019-04-24 07:18:48 +02:00
|
|
|
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
|
2019-04-24 12:01:40 +02:00
|
|
|
|
2019-04-10 11:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function buildLineItems()
|
|
|
|
{
|
|
|
|
$line_items = [];
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
2019-09-04 14:01:19 +02:00
|
|
|
$item->quantity = 1;
|
2019-04-10 11:42:19 +02:00
|
|
|
$item->cost =10;
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
2019-09-04 14:01:19 +02:00
|
|
|
$item->quantity = 1;
|
2019-04-10 11:42:19 +02:00
|
|
|
$item->cost =10;
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
return $line_items;
|
|
|
|
|
|
|
|
}
|
2019-04-11 02:35:30 +02:00
|
|
|
|
|
|
|
public function testInvoiceTotals()
|
|
|
|
{
|
2019-04-24 12:01:40 +02:00
|
|
|
|
2019-04-11 02:35:30 +02:00
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
2019-04-11 02:57:06 +02:00
|
|
|
$this->assertEquals($this->invoice_calc->getTotal(), 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvoiceTotalsWithDiscount()
|
|
|
|
{
|
|
|
|
$this->invoice->discount = 5;
|
|
|
|
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
$this->assertEquals($this->invoice_calc->getTotal(), 15);
|
|
|
|
$this->assertEquals($this->invoice_calc->getBalance(), 15);
|
2019-04-11 02:35:30 +02:00
|
|
|
}
|
2019-04-11 06:40:36 +02:00
|
|
|
|
|
|
|
public function testInvoiceTotalsWithDiscountWithSurcharge()
|
|
|
|
{
|
|
|
|
$this->invoice->discount = 5;
|
|
|
|
$this->invoice->custom_value1 = 5;
|
|
|
|
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
$this->assertEquals($this->invoice_calc->getTotal(), 20);
|
|
|
|
$this->assertEquals($this->invoice_calc->getBalance(), 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvoiceTotalsWithDiscountWithSurchargeWithInclusiveTax()
|
|
|
|
{
|
|
|
|
$this->invoice->discount = 5;
|
|
|
|
$this->invoice->custom_value1 = 5;
|
|
|
|
$this->invoice->tax_name1 = 'GST';
|
|
|
|
$this->invoice->tax_rate1 = 10;
|
|
|
|
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
$this->assertEquals($this->invoice_calc->getTotal(), 20);
|
|
|
|
$this->assertEquals($this->invoice_calc->getBalance(), 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvoiceTotalsWithDiscountWithSurchargeWithExclusiveTax()
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
$this->invoice->discount = 5;
|
|
|
|
$this->invoice->custom_value1 = 5;
|
|
|
|
$this->invoice->tax_name1 = 'GST';
|
|
|
|
$this->invoice->tax_rate1 = 10;
|
2019-04-24 07:18:48 +02:00
|
|
|
$this->settings->inclusive_taxes = false;
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2019-04-24 07:18:48 +02:00
|
|
|
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
|
2019-04-15 01:51:43 +02:00
|
|
|
|
2019-04-11 06:40:36 +02:00
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
$this->assertEquals($this->invoice_calc->getTotal(), 22);
|
|
|
|
$this->assertEquals($this->invoice_calc->getBalance(), 22);
|
|
|
|
$this->assertEquals($this->invoice_calc->getTotalTaxes(), 2);
|
|
|
|
}
|
|
|
|
|
2019-04-15 01:51:43 +02:00
|
|
|
public function testInvoiceTotalsWithDiscountWithSurchargeWithDoubleExclusiveTax()
|
2019-04-11 06:40:36 +02:00
|
|
|
{
|
|
|
|
|
2019-04-24 07:18:48 +02:00
|
|
|
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
|
2019-04-11 06:40:36 +02: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;
|
2019-04-24 07:18:48 +02:00
|
|
|
$this->settings->inclusive_taxes = false;
|
2019-04-11 06:40:36 +02:00
|
|
|
|
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
|
|
|
|
$this->assertEquals($this->invoice_calc->getTotal(), 24);
|
|
|
|
$this->assertEquals($this->invoice_calc->getBalance(), 24);
|
|
|
|
$this->assertEquals($this->invoice_calc->getTotalTaxes(), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-15 01:51:43 +02:00
|
|
|
public function testLineItemTaxRatesInclusiveTaxes()
|
|
|
|
{
|
|
|
|
$line_items = [];
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
2019-09-04 14:01:19 +02:00
|
|
|
$item->quantity = 1;
|
2019-04-15 01:51:43 +02:00
|
|
|
$item->cost =10;
|
|
|
|
$item->tax_rate1 = 10;
|
|
|
|
$item->tax_name1 = 10;
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
2019-09-04 14:01:19 +02:00
|
|
|
$item->quantity = 1;
|
2019-04-15 01:51:43 +02:00
|
|
|
$item->cost =10;
|
|
|
|
$item->tax_rate1 = 10;
|
|
|
|
$item->tax_name1 = 10;
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$this->invoice->line_items = $line_items;
|
2019-04-24 12:01:40 +02:00
|
|
|
|
2019-04-24 07:18:48 +02:00
|
|
|
$this->settings->inclusive_taxes = true;
|
2019-04-15 01:51:43 +02:00
|
|
|
$this->invoice->discount = 0;
|
|
|
|
$this->invoice->custom_value1 = 0;
|
|
|
|
|
2019-04-24 07:18:48 +02:00
|
|
|
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
|
2019-04-15 01:51:43 +02:00
|
|
|
$this->invoice_calc->build();
|
|
|
|
|
|
|
|
$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(), 0);
|
2019-09-04 14:01:19 +02:00
|
|
|
$this->assertEquals(count($this->invoice_calc->getTaxMap()), 1);
|
2019-04-15 01:51:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLineItemTaxRatesExclusiveTaxes()
|
|
|
|
{
|
|
|
|
|
|
|
|
$line_items = [];
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
2019-09-04 14:01:19 +02:00
|
|
|
$item->quantity = 1;
|
2019-04-15 01:51:43 +02:00
|
|
|
$item->cost =10;
|
|
|
|
$item->tax_rate1 = 10;
|
|
|
|
$item->tax_name1 = 10;
|
|
|
|
|
|
|
|
$line_items[] = $item;
|
|
|
|
|
|
|
|
$item = InvoiceItemFactory::create();
|
2019-09-04 14:01:19 +02:00
|
|
|
$item->quantity = 1;
|
2019-04-15 01:51:43 +02:00
|
|
|
$item->cost =10;
|
|
|
|
$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;
|
2019-04-16 05:28:05 +02:00
|
|
|
|
2019-04-24 07:18:48 +02:00
|
|
|
$this->settings->inclusive_taxes = false;
|
|
|
|
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
|
2019-04-15 01:51:43 +02:00
|
|
|
$this->invoice_calc->build();
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2019-04-15 01:51:43 +02:00
|
|
|
$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(), 6);
|
2019-09-04 14:01:19 +02:00
|
|
|
$this->assertEquals(count($this->invoice_calc->getTaxMap()), 1);
|
2019-04-15 01:51:43 +02:00
|
|
|
}
|
2019-04-11 06:40:36 +02:00
|
|
|
|
2019-04-10 11:09:57 +02:00
|
|
|
}
|