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

495 lines
15 KiB
PHP
Raw Normal View History

2019-04-04 11:28:53 +02:00
<?php
2020-09-14 13:11:46 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-09-14 13:11:46 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
2020-09-14 13:11:46 +02:00
*/
2019-04-04 11:28:53 +02:00
namespace Tests\Unit;
2023-10-28 04:57:16 +02:00
use Tests\TestCase;
use Tests\MockAccountData;
use App\DataMapper\InvoiceItem;
use App\Factory\InvoiceFactory;
2019-04-04 11:28:53 +02:00
use App\Factory\InvoiceItemFactory;
use App\Helpers\Invoice\InvoiceItemSum;
2019-04-24 12:01:40 +02:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
2019-04-04 11:28:53 +02:00
/**
* @test
* @covers App\Helpers\Invoice\InvoiceItemSum
2019-04-04 11:28:53 +02:00
*/
class InvoiceItemTest extends TestCase
{
use MockAccountData;
2019-10-07 11:39:22 +02:00
use DatabaseTransactions;
2019-04-24 12:01:40 +02:00
protected function setUp() :void
2019-04-04 11:28:53 +02:00
{
parent::setUp();
$this->makeTestData();
}
2019-04-04 11:28:53 +02:00
2023-10-28 05:11:11 +02:00
public function testDiscountsWithInclusiveTaxes()
{
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
$invoice->client_id = $this->client->id;
$invoice->uses_inclusive_taxes = true;
$invoice->is_amount_discount =true;
$invoice->discount = 10;
$line_items = [];
$line_item = new InvoiceItem;
$line_item->quantity = 1;
$line_item->cost = 100;
$line_item->tax_rate1 = 10;
$line_item->tax_name1 = 'GST';
$line_item->product_key = 'Test';
$line_item->notes = 'Test';
$line_items[] = $line_item;
$invoice->line_items = $line_items;
$invoice->save();
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(90, $invoice->amount);
$this->assertEquals(8.18, $invoice->total_taxes);
}
public function testDiscountsWithInclusiveTaxesNegativeInvoice()
{
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
$invoice->client_id = $this->client->id;
$invoice->uses_inclusive_taxes = true;
$invoice->is_amount_discount =true;
$invoice->discount = -10;
$line_items = [];
$line_item = new InvoiceItem;
$line_item->quantity = -1;
$line_item->cost = 100;
$line_item->tax_rate1 = 10;
$line_item->tax_name1 = 'GST';
$line_item->product_key = 'Test';
$line_item->notes = 'Test';
$line_items[] = $line_item;
$invoice->line_items = $line_items;
$invoice->save();
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(-90, $invoice->amount);
$this->assertEquals(-8.18, $invoice->total_taxes);
}
2023-10-28 04:57:16 +02:00
public function testDicountsWithTaxes()
{
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
$invoice->client_id = $this->client->id;
$invoice->uses_inclusive_taxes = false;
$invoice->is_amount_discount =true;
$invoice->discount = 10;
$line_items = [];
$line_item = new InvoiceItem;
$line_item->quantity = 1;
$line_item->cost = 100;
$line_item->tax_rate1 = 10;
$line_item->tax_name1 = 'GST';
$line_item->product_key = 'Test';
$line_item->notes = 'Test';
$line_items[] = $line_item;
$invoice->line_items = $line_items;
$invoice->save();
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(99, $invoice->amount);
$this->assertEquals(9, $invoice->total_taxes);
}
public function testDicountsWithTaxesNegativeInvoice()
{
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
$invoice->client_id = $this->client->id;
$invoice->uses_inclusive_taxes = false;
$invoice->is_amount_discount =true;
$invoice->discount = -10;
$line_items = [];
$line_item = new InvoiceItem;
$line_item->quantity = -1;
$line_item->cost = 100;
$line_item->tax_rate1 = 10;
$line_item->tax_name1 = 'GST';
$line_item->product_key = 'Test';
$line_item->notes = 'Test';
$line_items[] = $line_item;
$invoice->line_items = $line_items;
$invoice->save();
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(-99, $invoice->amount);
$this->assertEquals(-9, $invoice->total_taxes);
}
2023-10-28 05:11:11 +02:00
public function testDicountsWithTaxesPercentage()
2023-10-28 04:57:16 +02:00
{
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
$invoice->client_id = $this->client->id;
$invoice->uses_inclusive_taxes = false;
$invoice->is_amount_discount =false;
$invoice->discount = 10;
$line_items = [];
$line_item = new InvoiceItem;
$line_item->quantity = 1;
$line_item->cost = 100;
$line_item->tax_rate1 = 10;
$line_item->tax_name1 = 'GST';
$line_item->product_key = 'Test';
$line_item->notes = 'Test';
$line_items[] = $line_item;
$invoice->line_items = $line_items;
$invoice->save();
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(99, $invoice->amount);
$this->assertEquals(9, $invoice->total_taxes);
}
2023-10-28 05:21:07 +02:00
public function testDicountsWithTaxesPercentageOnLine()
{
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
$invoice->client_id = $this->client->id;
$invoice->uses_inclusive_taxes = true;
$invoice->is_amount_discount =false;
$invoice->discount = 10;
$line_items = [];
$line_item = new InvoiceItem;
$line_item->quantity = 1;
$line_item->cost = 100;
$line_item->is_amount_discount = false;
$line_item->discount = 10;
$line_item->tax_rate1 = 10;
$line_item->tax_name1 = 'GST';
$line_item->product_key = 'Test';
$line_item->notes = 'Test';
$line_items[] = $line_item;
$invoice->line_items = $line_items;
$invoice->save();
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(81, $invoice->amount);
$this->assertEquals(7.36, $invoice->total_taxes);
}
public function testDicountsWithExclusiveTaxesPercentageOnLine()
{
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
$invoice->client_id = $this->client->id;
$invoice->uses_inclusive_taxes = false;
$invoice->is_amount_discount =false;
$invoice->discount = -10;
$line_items = [];
$line_item = new InvoiceItem;
$line_item->quantity = -1;
$line_item->cost = 100;
$line_item->is_amount_discount = false;
$line_item->discount = -10;
$line_item->tax_rate1 = 10;
$line_item->tax_name1 = 'GST';
$line_item->product_key = 'Test';
$line_item->notes = 'Test';
$line_items[] = $line_item;
$invoice->line_items = $line_items;
$invoice->save();
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(-133.1, $invoice->amount);
$this->assertEquals(-12.1, $invoice->total_taxes);
}
2023-10-28 04:57:16 +02:00
public function testDicountsWithTaxesNegativeInvoicePercentage()
{
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
$invoice->client_id = $this->client->id;
$invoice->uses_inclusive_taxes = false;
$invoice->is_amount_discount =false;
$invoice->discount = -10;
$line_items = [];
$line_item = new InvoiceItem;
$line_item->quantity = -1;
$line_item->cost = 100;
$line_item->tax_rate1 = 10;
$line_item->tax_name1 = 'GST';
$line_item->product_key = 'Test';
$line_item->notes = 'Test';
$line_items[] = $line_item;
$invoice->line_items = $line_items;
$invoice->save();
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(-121, $invoice->amount);
$this->assertEquals(-10, $invoice->discount);
$this->assertEquals(-11, $invoice->total_taxes);
}
public function testDicountPercentageWithTaxes()
{
$invoice = InvoiceFactory::create($this->company->id, $this->user->id);
$invoice->client_id = $this->client->id;
$invoice->uses_inclusive_taxes = false;
$invoice->is_amount_discount =true;
$invoice->discount = 10;
$line_items = [];
$line_item = new InvoiceItem;
$line_item->quantity = 1;
$line_item->cost = 100;
$line_item->tax_rate1 = 10;
$line_item->tax_name1 = 'GST';
$line_item->product_key = 'Test';
$line_item->notes = 'Test';
$line_items[] = $line_item;
$invoice->line_items = $line_items;
$invoice->save();
$invoice = $invoice->calc()->getInvoice();
$this->assertEquals(99, $invoice->amount);
$this->assertEquals(9, $invoice->total_taxes);
}
public function testInvoiceItemTotalSimple()
{
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$item->is_amount_discount = true;
2019-04-04 11:28:53 +02:00
$settings = new \stdClass;
$settings->inclusive_taxes = true;
$settings->precision = 2;
$this->invoice->line_items = [$item];
2019-04-04 11:28:53 +02:00
$item_calc = new InvoiceItemSum($this->invoice, $settings);
$item_calc->process();
2019-04-04 11:28:53 +02:00
$this->assertEquals($item_calc->getLineTotal(), 10);
}
2019-04-04 11:28:53 +02:00
2021-09-15 01:02:25 +02:00
public function testInvoiceItemTotalSimpleWithGrossTaxes()
{
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$item->is_amount_discount = true;
$item->tax_rate1 = 10;
$settings = new \stdClass;
$settings->inclusive_taxes = false;
$settings->precision = 2;
$this->invoice->line_items = [$item];
$item_calc = new InvoiceItemSum($this->invoice, $settings);
$item_calc->process();
$this->assertEquals($item_calc->getLineTotal(), 10);
$this->assertEquals($item_calc->getGrossLineTotal(), 11);
}
public function testInvoiceItemTotalSimpleWithDiscount()
{
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$item->is_amount_discount = true;
$item->discount = 2;
$this->invoice->line_items = [$item];
$settings = new \stdClass;
$settings->inclusive_taxes = true;
$settings->precision = 2;
2019-04-04 11:28:53 +02:00
$item_calc = new InvoiceItemSum($this->invoice, $settings);
$item_calc->process();
2019-04-04 11:28:53 +02:00
$this->assertEquals($item_calc->getLineTotal(), 8);
}
2019-04-04 11:28:53 +02:00
2021-09-15 01:02:25 +02:00
public function testInvoiceItemTotalSimpleWithDiscountAndGrossLineTotal()
{
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$item->is_amount_discount = true;
$item->discount = 2;
$item->tax_rate1 = 10;
$this->invoice->line_items = [$item];
$settings = new \stdClass;
$settings->inclusive_taxes = false;
$settings->precision = 2;
$item_calc = new InvoiceItemSum($this->invoice, $settings);
$item_calc->process();
$this->assertEquals($item_calc->getLineTotal(), 8);
$this->assertEquals($item_calc->getGrossLineTotal(), 8.8);
}
2021-09-15 01:02:25 +02:00
public function testInvoiceItemTotalSimpleWithDiscountWithPrecision()
{
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$item->is_amount_discount = true;
$item->discount = 2.521254522145214511;
2019-04-04 11:28:53 +02:00
$this->invoice->line_items = [$item];
2019-04-04 11:28:53 +02:00
$settings = new \stdClass;
$settings->inclusive_taxes = true;
$settings->precision = 2;
$item_calc = new InvoiceItemSum($this->invoice, $settings);
$item_calc->process();
$this->assertEquals($item_calc->getLineTotal(), 7.48);
}
2019-04-04 11:28:53 +02:00
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleInclusiveTax()
{
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$item->is_amount_discount = true;
$item->discount = 2;
$item->tax_rate1 = 10;
2019-04-04 11:28:53 +02:00
$settings = new \stdClass;
$settings->inclusive_taxes = false;
$settings->precision = 2;
2019-04-04 11:28:53 +02:00
$this->invoice->line_items = [$item];
$item_calc = new InvoiceItemSum($this->invoice, $settings);
$item_calc->process();
2019-04-04 11:28:53 +02:00
$this->assertEquals($item_calc->getTotalTaxes(), 0.80);
}
2019-04-04 11:28:53 +02:00
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleExclusiveTax()
{
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$item->is_amount_discount = true;
$item->discount = 2.521254522145214511;
$item->tax_rate1 = 10;
2019-04-04 11:53:40 +02:00
$this->invoice->line_items = [$item];
2019-04-04 11:53:40 +02:00
$settings = new \stdClass;
$settings->inclusive_taxes = false;
$settings->precision = 2;
2019-04-04 11:53:40 +02:00
$item_calc = new InvoiceItemSum($this->invoice, $settings);
$item_calc->process();
2019-04-04 11:53:40 +02:00
$this->assertEquals($item_calc->getTotalTaxes(), 0.75);
}
2019-04-04 11:53:40 +02:00
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithDoubleInclusiveTax()
{
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$item->is_amount_discount = true;
$item->discount = 2.521254522145214511;
$item->tax_rate1 = 10;
$item->tax_rate2 = 17.5;
2019-04-04 11:53:40 +02:00
$this->invoice->line_items = [$item];
$settings = new \stdClass;
$settings->inclusive_taxes = true;
$settings->precision = 2;
2019-04-04 11:53:40 +02:00
$item_calc = new InvoiceItemSum($this->invoice, $settings);
$item_calc->process();
2019-04-04 11:53:40 +02:00
$this->assertEquals($item_calc->getTotalTaxes(), 2.06);
}
2019-04-04 11:28:53 +02:00
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithDoubleExclusiveTax()
{
$item = InvoiceItemFactory::create();
$item->quantity = 1;
$item->cost = 10;
$item->is_amount_discount = true;
$item->discount = 2.521254522145214511;
$item->tax_name1 = 'GST';
$item->tax_rate1 = 10;
$item->tax_name2 = 'VAT';
$item->tax_rate2 = 17.5;
$this->invoice->line_items = [$item];
$settings = new \stdClass;
$settings->inclusive_taxes = false;
$settings->precision = 2;
$item_calc = new InvoiceItemSum($this->invoice, $settings);
$item_calc->process();
$this->assertEquals($item_calc->getTotalTaxes(), 2.06);
$this->assertEquals($item_calc->getGroupedTaxes()->count(), 2);
}
2019-04-04 11:28:53 +02:00
}