Refactor private variables in calc class

This commit is contained in:
David Bomba 2019-04-05 20:08:29 +11:00
parent deface29da
commit 6f264567c7
2 changed files with 61 additions and 45 deletions

View File

@ -12,30 +12,33 @@ class InvoiceItemCalc
protected $item; protected $item;
protected $precision; protected $settings;
protected $inclusive_tax; private $total_taxes;
protected $total_taxes; private $total_discounts;
protected $total_dicounts; private $tax_collection;
protected $tax_collection; private $line_total;
public function __construct(\stdClass $item, int $precision = 2, bool $inclusive_tax) public function __construct(\stdClass $item, \stdClass $settings)
{ {
$this->item = $item; $this->item = $item;
$this->precision = $precision;
$this->inclusive_tax = $inclusive_tax; $this->settings = $settings;
$this->tax_collection = collect(); $this->tax_collection = collect();
} }
public function process() public function process()
{ {
$this->line_total = $this->formatValue($this->item->cost, $this->settings->precision) * $this->formatValue($this->item->qty, $this->settings->precision);
$this->setLineTotal($this->formatValue($this->item->cost, $this->precision) * $this->formatValue($this->item->qty, $this->precision)) $this->setDiscount()
->setDiscount() ->calcTaxes();
->calcTaxes();
} }
@ -44,19 +47,19 @@ class InvoiceItemCalc
if($this->item->is_amount_discount) if($this->item->is_amount_discount)
{ {
$discount = $this->formatValue($this->item->discount, $this->precision); $discount = $this->formatValue($this->item->discount, $this->settings->precision);
$this->setLineTotal($this->getLineTotal() - $discount); $this->line_total -= $discount;
$this->setTotalDiscounts($this->getTotalDiscounts() + $discount); $this->total_discounts += $discount;
} }
else else
{ {
$discount = $this->formatValue(($this->getLineTotal() * $this->item->discount / 100), $this->precision); $discount = $this->formatValue(($this->line_total * $this->item->discount / 100), $this->settings->precision);
$this->setLineTotal($this->getLineTotal() - $discount); $this->line_total -= $discount;
$this->setTotalDiscounts($this->getTotalDiscounts() + $discount); $this->total_discounts += $discount;
} }
@ -68,28 +71,28 @@ class InvoiceItemCalc
{ {
$item_tax = 0; $item_tax = 0;
$tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->precision); $tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->settings->precision);
if($tax_rate1 != 0) if($tax_rate1 != 0)
{ {
if($this->inclusive_tax) if($this->settings->inclusive_tax)
$item_tax_rate1_total = $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate1/100))) , $this->precision); $item_tax_rate1_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate1/100))) , $this->settings->precision);
else else
$item_tax_rate1_total = $this->formatValue(($this->getLineTotal() * $tax_rate1/100), $this->precision); $item_tax_rate1_total = $this->formatValue(($this->line_total * $tax_rate1/100), $this->settings->precision);
$item_tax += $item_tax_rate1_total; $item_tax += $item_tax_rate1_total;
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total); $this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
} }
$tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->precision); $tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->settings->precision);
if($tax_rate2 != 0) if($tax_rate2 != 0)
{ {
if($this->inclusive_tax) if($this->settings->inclusive_tax)
$item_tax_rate2_total = $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate2/100))) , $this->precision); $item_tax_rate2_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate2/100))) , $this->settings->precision);
else else
$item_tax_rate2_total = $this->formatValue(($this->getLineTotal() * $tax_rate2/100), $this->precision); $item_tax_rate2_total = $this->formatValue(($this->line_total * $tax_rate2/100), $this->settings->precision);
$item_tax += $item_tax_rate2_total; $item_tax += $item_tax_rate2_total;
@ -109,7 +112,7 @@ class InvoiceItemCalc
$group_tax[$key] = ['total' => $tax_total, 'tax_name' => $tax_name . ' ' . $tax_rate]; $group_tax[$key] = ['total' => $tax_total, 'tax_name' => $tax_name . ' ' . $tax_rate];
$this->setGroupedTaxes($group_tax); $this->tax_collection->merge(collect($group_tax));
} }
@ -128,12 +131,12 @@ class InvoiceItemCalc
public function getLineTotal() public function getLineTotal()
{ {
return $this->item->line_total; return $this->line_total;
} }
public function setLineTotal($total) public function setLineTotal($total)
{ {
$this->item->line_total = $total; $this->line_total = $total;
return $this; return $this;
} }
@ -167,9 +170,9 @@ class InvoiceItemCalc
return $this->tax_collection; return $this->tax_collection;
} }
public function setGroupedTaxes($group_tax) public function setGroupedTaxes($group_taxes)
{ {
$this->tax_collection->merge(collect($group_tax)); $this->tax_collection = $group_taxes;
return $this; return $this;
} }

View File

@ -26,9 +26,11 @@ class InvoiceItemTest extends TestCase
$item->cost =10; $item->cost =10;
$item->is_amount_discount = true; $item->is_amount_discount = true;
$inclusive_tax = true; $settings = new \stdClass;
$settings->inclusive_tax = true;
$settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc = new InvoiceItemCalc($item, $settings);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getLineTotal(), 10); $this->assertEquals($item_calc->getLineTotal(), 10);
@ -42,9 +44,10 @@ class InvoiceItemTest extends TestCase
$item->is_amount_discount = true; $item->is_amount_discount = true;
$item->discount = 2; $item->discount = 2;
$inclusive_tax = true; $settings = new \stdClass;
$settings->inclusive_tax = true;
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, $settings);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getLineTotal(), 8); $this->assertEquals($item_calc->getLineTotal(), 8);
@ -58,9 +61,11 @@ class InvoiceItemTest extends TestCase
$item->is_amount_discount = true; $item->is_amount_discount = true;
$item->discount = 2.521254522145214511; $item->discount = 2.521254522145214511;
$inclusive_tax = true; $settings = new \stdClass;
$settings->inclusive_tax = true;
$settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc = new InvoiceItemCalc($item, $settings);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getLineTotal(), 7.48); $this->assertEquals($item_calc->getLineTotal(), 7.48);
@ -75,9 +80,11 @@ class InvoiceItemTest extends TestCase
$item->discount = 2.521254522145214511; $item->discount = 2.521254522145214511;
$item->tax_rate1 = 10; $item->tax_rate1 = 10;
$inclusive_tax = true; $settings = new \stdClass;
$settings->inclusive_tax = true;
$settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc = new InvoiceItemCalc($item, $settings);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getTotalTaxes(), 0.68); $this->assertEquals($item_calc->getTotalTaxes(), 0.68);
@ -92,9 +99,11 @@ class InvoiceItemTest extends TestCase
$item->discount = 2.521254522145214511; $item->discount = 2.521254522145214511;
$item->tax_rate1 = 10; $item->tax_rate1 = 10;
$inclusive_tax = false; $settings = new \stdClass;
$settings->inclusive_tax = false;
$settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc = new InvoiceItemCalc($item, $settings);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getTotalTaxes(), 0.75); $this->assertEquals($item_calc->getTotalTaxes(), 0.75);
@ -110,9 +119,11 @@ class InvoiceItemTest extends TestCase
$item->tax_rate1 = 10; $item->tax_rate1 = 10;
$item->tax_rate2 = 17.5; $item->tax_rate2 = 17.5;
$inclusive_tax = true; $settings = new \stdClass;
$settings->inclusive_tax = true;
$settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc = new InvoiceItemCalc($item, $settings);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getTotalTaxes(), 1.79); $this->assertEquals($item_calc->getTotalTaxes(), 1.79);
@ -128,9 +139,11 @@ class InvoiceItemTest extends TestCase
$item->tax_rate1 = 10; $item->tax_rate1 = 10;
$item->tax_rate2 = 17.5; $item->tax_rate2 = 17.5;
$inclusive_tax = false; $settings = new \stdClass;
$settings->inclusive_tax = false;
$settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, 2, $inclusive_tax); $item_calc = new InvoiceItemCalc($item, $settings);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getTotalTaxes(), 2.06); $this->assertEquals($item_calc->getTotalTaxes(), 2.06);