Fixes for invoice calculations

This commit is contained in:
David Bomba 2019-04-10 17:57:02 +10:00
parent 3c5ccf2da3
commit 1ce11fcb36
3 changed files with 73 additions and 6 deletions

View File

@ -18,6 +18,8 @@ class InvoiceCalc
protected $settings;
private $line_items;
private $balance;
private $paid_to_date;
@ -26,9 +28,7 @@ class InvoiceCalc
private $sub_total;
private $line_items;
private $invoice_total;
private $total;
private $tax_map;
@ -54,17 +54,83 @@ class InvoiceCalc
*/
public function build()
{
$this->calcLineItems()->calcTaxes();
$this->calcLineItems()
->calcDiscount()
->sumCustomValues()
->calcBalance();
return $this;
}
private function calcDiscount()
{
if ($this->invoice->discount != 0) {
if ($this->invoice->is_amount_discount) {
$this->total -= $this->invoice->discount;
} else {
$this->total -= round($this->total * ($this->invoice->discount / 100), 2);
}
}
return $this;
}
private function sumBalance()
{
if(isset($this->invoice->id) && $this->invoice->id >= 1)
{
$this->invoice->balance = round($this->total - ($this->invoice->amount - $this->invoice->balance), 2);
} else {
$this->invoice->balance = $this->total;
}
return $this;
}
private function sumCustomValues()
{
$this->total += $this->getSubTotal();
// custom fields charged taxes
if ($this->invoice->custom_value1 && $this->settings->custom_taxes1) {
$this->total += $invoice->custom_value1;
}
if ($invoice->custom_value2 && $invoice->custom_taxes2) {
$this->total += $invoice->custom_value2;
}
$this->calcTaxes();
// custom fields not charged taxes
if ($invoice->custom_value1 && ! $this->settings->custom_taxes1) {
$this->total += $invoice->custom_value1;
}
if ($invoice->custom_value2 && ! $this->settings->custom_taxes2) {
$this->total += $invoice->custom_value2;
}
}
/**
* Calculates the Invoice Level taxes.
*/
private function calcTaxes()
{
if (! $this->settings->inclusive_taxes) {
$taxAmount1 = round($this->total * ($this->invoice->tax_rate1 ? $this->invoice->tax_rate1 : 0) / 100, 2);
$taxAmount2 = round($this->total * ($this->invoice->tax_rate2 ? $this->invoice->tax_rate2 : 0) / 100, 2);
$this->total = round($this->total + $taxAmount1 + $taxAmount2, 2);
$this->total += $this->total_taxes;
}
}
/**

View File

@ -369,6 +369,7 @@ class CreateUsersTable extends Migration
$t->string('custom_value3')->nullable();
$t->string('custom_value4')->nullable();
$t->decimal('total', 13,2);
$t->decimal('amount', 13, 2);
$t->decimal('balance', 13, 2);
$t->decimal('partial', 13, 2)->nullable();

View File

@ -26,7 +26,7 @@ class BaseSettingsTest extends TestCase
{
$blank_object = new \stdClass;
$this->assertEquals(count(get_object_vars($this->migrate($blank_object))), 14);
$this->assertEquals(count(get_object_vars($this->migrate($blank_object))), 15);
}
public function testPropertyNamesExist()