mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 05:02:36 +01:00
Exclusive tax rate calculations (#3005)
* clean up invoice calculations - exclusive taxes * Fixes for tests * Clean up and fixes for tests
This commit is contained in:
parent
03eb26a6db
commit
02fadc8272
@ -67,12 +67,12 @@ class InvoiceItemSum
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->buildLineItems();
|
||||
$this->calcLineItems();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function buildLineItems()
|
||||
private function calcLineItems()
|
||||
{
|
||||
foreach($this->invoice->line_items as $this->item)
|
||||
{
|
||||
@ -104,7 +104,7 @@ class InvoiceItemSum
|
||||
private function setDiscount()
|
||||
{
|
||||
|
||||
if($this->item->is_amount_discount)
|
||||
if($this->invoice->is_amount_discount)
|
||||
{
|
||||
$this->setLineTotal($this->getLineTotal() - $this->formatValue($this->item->discount, $this->currency->precision));
|
||||
}
|
||||
@ -113,6 +113,8 @@ class InvoiceItemSum
|
||||
$this->setLineTotal($this->getLineTotal() - $this->formatValue(round($this->item->line_total * ($this->item->discount / 100),2), $this->currency->precision));
|
||||
}
|
||||
|
||||
$this->item->is_amount_discount = $this->invoice->is_amount_discount;
|
||||
|
||||
return $this;
|
||||
|
||||
}
|
||||
@ -122,22 +124,22 @@ class InvoiceItemSum
|
||||
|
||||
$item_tax = 0;
|
||||
|
||||
|
||||
$item_tax_rate1_total = $this->calcLineTax($this->item->tax_rate1, $this->item);
|
||||
$amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount/100));
|
||||
$item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount);
|
||||
|
||||
$item_tax += $item_tax_rate1_total;
|
||||
|
||||
if($item_tax_rate1_total > 0)
|
||||
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
|
||||
|
||||
$item_tax_rate2_total = $this->calcLineTax($this->item->tax_rate2, $this->item);
|
||||
$item_tax_rate2_total = $this->calcAmountLineTax($this->item->tax_rate2, $amount);
|
||||
|
||||
$item_tax += $item_tax_rate2_total;
|
||||
|
||||
if($item_tax_rate2_total > 0)
|
||||
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
|
||||
|
||||
$item_tax_rate3_total = $this->calcLineTax($this->item->tax_rate3, $this->item);
|
||||
$item_tax_rate3_total = $this->calcAmountLineTax($this->item->tax_rate3, $amount);
|
||||
|
||||
$item_tax += $item_tax_rate3_total;
|
||||
|
||||
@ -230,10 +232,11 @@ class InvoiceItemSum
|
||||
{
|
||||
$this->setGroupedTaxes(collect([]));
|
||||
|
||||
$item_tax = 0;
|
||||
|
||||
foreach($this->line_items as $this->item)
|
||||
{
|
||||
$item_tax = 0;
|
||||
|
||||
|
||||
$amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount/$this->sub_total));
|
||||
$item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount);
|
||||
|
||||
@ -256,7 +259,7 @@ class InvoiceItemSum
|
||||
if($item_tax_rate3_total > 0)
|
||||
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
|
||||
|
||||
//\Log::error($this->item->tax_name1. " ". $this->item->line_total." ". $this->invoice->discount." ". $this->sub_total. " ".$amount. " ". $item_tax);
|
||||
|
||||
}
|
||||
|
||||
$this->setTotalTaxes($item_tax);
|
||||
|
@ -22,37 +22,9 @@ trait Taxer
|
||||
return round($amount * (($tax_rate ? $tax_rate : 0) / 100), 2);
|
||||
}
|
||||
|
||||
public function calcLineTax($tax_rate, $item)
|
||||
{
|
||||
if(!isset($tax_rate) || $tax_rate == 0)
|
||||
return 0;
|
||||
|
||||
if($this->settings->inclusive_taxes)
|
||||
return $this->inclusiveTax($tax_rate, $item);
|
||||
|
||||
return $this->exclusiveTax($tax_rate, $item);
|
||||
}
|
||||
|
||||
public function exclusiveTax($tax_rate, $item)
|
||||
{
|
||||
|
||||
$tax_rate = $this->formatValue($tax_rate, 4);
|
||||
|
||||
return $this->formatValue(($item->line_total * $tax_rate/100), 4);
|
||||
|
||||
}
|
||||
|
||||
public function calcAmountLineTax($tax_rate, $amount)
|
||||
{
|
||||
return $this->formatValue(($amount * $tax_rate/100), 4);
|
||||
}
|
||||
|
||||
public function inclusiveTax($tax_rate, $item)
|
||||
{
|
||||
|
||||
$tax_rate = $this->formatValue($tax_rate, 4);
|
||||
|
||||
return $this->formatValue(($item->line_total - ($item->line_total / (1+$tax_rate/100))) , 4);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -62,6 +62,8 @@ class MarkInvoicePaid implements ShouldQueue
|
||||
$payment->invoices()->save($this->invoice);
|
||||
$payment->save();
|
||||
|
||||
$this->invoice->updateBalance($payment->amount*-1);
|
||||
|
||||
/* Update Invoice balance */
|
||||
event(new PaymentWasCreated($payment));
|
||||
|
||||
|
@ -10,7 +10,7 @@ $factory->define(App\Models\Invoice::class, function (Faker $faker) {
|
||||
'status_id' => App\Models\Invoice::STATUS_SENT,
|
||||
'invoice_number' => $faker->ean13(),
|
||||
'discount' => $faker->numberBetween(1,10),
|
||||
'is_amount_discount' => true,
|
||||
'is_amount_discount' => (bool)random_int(0,1),
|
||||
'tax_name1' => 'GST',
|
||||
'tax_rate1' => 10,
|
||||
'tax_name2' => 'VAT',
|
||||
|
@ -36,10 +36,15 @@ class MarkInvoicePaidTest extends TestCase
|
||||
|
||||
public function testMarkInvoicePaidInvoice()
|
||||
{
|
||||
\Log::error($this->invoice->amount);
|
||||
\Log::error($this->invoice->balance);
|
||||
MarkInvoicePaid::dispatchNow($this->invoice);
|
||||
|
||||
$invoice = Invoice::find($this->invoice->id);
|
||||
|
||||
|
||||
\Log::error($invoice->amount);
|
||||
\Log::error($invoice->balance);
|
||||
|
||||
$this->assertEquals(0.00, $invoice->balance);
|
||||
|
||||
$this->assertEquals(1, count($invoice->payments));
|
||||
|
@ -230,7 +230,7 @@ trait MockAccountData
|
||||
$line_items = [];
|
||||
|
||||
$item = InvoiceItemFactory::create();
|
||||
$item->qty = 1;
|
||||
$item->quantity = 1;
|
||||
$item->cost =10;
|
||||
|
||||
$line_items[] = $item;
|
||||
|
@ -174,19 +174,19 @@ class InvoiceItemV2Test extends TestCase
|
||||
|
||||
}
|
||||
|
||||
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleInclusiveTax()
|
||||
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleExcTax()
|
||||
{
|
||||
$item = InvoiceItemFactory::create();
|
||||
$item->quantity = 1;
|
||||
$item->cost =10;
|
||||
$item->is_amount_discount = true;
|
||||
$item->discount = 2.521254522145214511;
|
||||
$item->is_amount_discount = $this->invoice->is_amount_discount;
|
||||
$item->discount = 2;
|
||||
$item->tax_rate1 = 10;
|
||||
|
||||
$this->invoice->line_items = [$item];
|
||||
|
||||
$settings = new \stdClass;
|
||||
$settings->inclusive_taxes = true;
|
||||
$settings->inclusive_taxes = false;
|
||||
$settings->precision = 2;
|
||||
|
||||
$item_calc = new InvoiceItemSum($this->invoice, $settings);
|
||||
@ -194,7 +194,7 @@ class InvoiceItemV2Test extends TestCase
|
||||
|
||||
$line_items = $item_calc->getLineItems();
|
||||
|
||||
$this->assertEquals($item_calc->getTotalTaxes(), 0.68);
|
||||
$this->assertEquals($item_calc->getTotalTaxes(), 0.80);
|
||||
}
|
||||
|
||||
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithSingleExclusiveTax()
|
||||
@ -218,26 +218,26 @@ class InvoiceItemV2Test extends TestCase
|
||||
$this->assertEquals($item_calc->getTotalTaxes(), 0.75);
|
||||
}
|
||||
|
||||
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithDoubleInclusiveTax()
|
||||
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithDoubleExcTax()
|
||||
{
|
||||
$item = InvoiceItemFactory::create();
|
||||
$item->quantity = 1;
|
||||
$item->cost =10;
|
||||
$item->is_amount_discount = true;
|
||||
$item->discount = 2.521254522145214511;
|
||||
$item->discount = 2;
|
||||
$item->tax_rate1 = 10;
|
||||
$item->tax_rate2 = 17.5;
|
||||
|
||||
$this->invoice->line_items = [$item];
|
||||
|
||||
$settings = new \stdClass;
|
||||
$settings->inclusive_taxes = true;
|
||||
$settings->inclusive_taxes = false;
|
||||
$settings->precision = 2;
|
||||
|
||||
$item_calc = new InvoiceItemSum($this->invoice, $settings);
|
||||
$item_calc->process();
|
||||
|
||||
$this->assertEquals($item_calc->getTotalTaxes(), 1.79);
|
||||
$this->assertEquals($item_calc->getTotalTaxes(), 2.20);
|
||||
}
|
||||
|
||||
public function testInvoiceItemTotalSimpleWithDiscountWithPrecisionWithDoubleExclusiveTax()
|
||||
|
Loading…
Reference in New Issue
Block a user