1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/tests/Unit/InvoiceTest.php

235 lines
6.0 KiB
PHP
Raw Normal View History

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-10-07 11:39:22 +02:00
//append a client here - otherwise tests are broken.
$this->invoice = InvoiceFactory::create(1,1);//stub the company and user_id
2019-04-24 12:01:40 +02:00
2019-10-07 11:39:22 +02:00
$client = factory(\App\Models\Client::class)->create([
'user_id' => 1,
'company_id' => 1,
]);
$this->invoice->client_id = $client->id;
2019-04-10 11:42:19 +02:00
$this->invoice->line_items = $this->buildLineItems();
$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;
$this->settings->inclusive_taxes = true;
$this->settings->precision = 2;
2019-04-10 11:42:19 +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;
$this->settings->inclusive_taxes = false;
2019-04-11 06:40:36 +02:00
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
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);
}
public function testInvoiceTotalsWithDiscountWithSurchargeWithDoubleExclusiveTax()
2019-04-11 06:40:36 +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;
$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);
}
public function testLineItemTaxRatesInclusiveTaxes()
{
$line_items = [];
$item = InvoiceItemFactory::create();
2019-09-04 14:01:19 +02:00
$item->quantity = 1;
$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;
$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
$this->settings->inclusive_taxes = true;
$this->invoice->discount = 0;
$this->invoice->custom_value1 = 0;
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
$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);
}
public function testLineItemTaxRatesExclusiveTaxes()
{
$line_items = [];
$item = InvoiceItemFactory::create();
2019-09-04 14:01:19 +02:00
$item->quantity = 1;
$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;
$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;
$this->settings->inclusive_taxes = false;
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
$this->invoice_calc->build();
2019-04-11 06:40:36 +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-11 06:40:36 +02:00
2019-04-10 11:09:57 +02:00
}