1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Working on getters and setters for invoice attributes

This commit is contained in:
David Bomba 2019-04-05 14:58:26 +11:00
parent 19350f345d
commit 56f05f7122
2 changed files with 72 additions and 23 deletions

View File

@ -2,6 +2,7 @@
namespace App\Helpers\Invoice; namespace App\Helpers\Invoice;
use App\Helpers\Invoice\InvoiceItemCalc;
use App\Models\Invoice; use App\Models\Invoice;
use App\Utils\Traits\NumberFormatter; use App\Utils\Traits\NumberFormatter;
@ -44,22 +45,22 @@ class InvoiceCalc
foreach($this->invoice->line_items as $item) { foreach($this->invoice->line_items as $item) {
$total = $this->formatValue($item->cost) * $this->formatValue($item->qty); $item_calc = new InvoiceItemCalc($item);
$total = $this->setDiscount($total, $item->discount, $item->is_amount_discount); $item_calc->process();
$total = $this->setTaxRate($total, $item->tax_name1, $item->tax_rate1);
$item->line_total = $total;
$new_line_items[] = $item;
$this->setInvoiceTotal($total);
$new_line_items[] = $item_calc->getLineItem();
//set collection of itemised taxes
//set running total of taxes
} }
$this->invoice->line_items = $new_line_items; $this->invoice->line_items = $new_line_items;
} }
/*
private function setDiscount($amount, $discount, $is_amount_discount) private function setDiscount($amount, $discount, $is_amount_discount)
{ {
@ -80,7 +81,7 @@ class InvoiceCalc
$this->invoice_total = $invoice_total; $this->invoice_total = $invoice_total;
} }
*/

View File

@ -18,6 +18,8 @@ class InvoiceItemCalc
protected $total_taxes; protected $total_taxes;
protected $total_dicounts;
protected $tax_collection; protected $tax_collection;
public function __construct(\stdClass $item, int $precision = 2, bool $inclusive_tax) public function __construct(\stdClass $item, int $precision = 2, bool $inclusive_tax)
@ -30,19 +32,33 @@ class InvoiceItemCalc
public function process() public function process()
{ {
$this->setLineTotal($this->formatValue($this->item->cost, $this->precision) * $this->formatValue($this->item->qty, $this->precision));
$this->setDiscount(); $this->setLineTotal($this->formatValue($this->item->cost, $this->precision) * $this->formatValue($this->item->qty, $this->precision))
$this->calcTaxes(); ->setDiscount()
$this->groupTaxes(); ->calcTaxes();
} }
private function setDiscount() private function setDiscount()
{ {
if($this->item->is_amount_discount) if($this->item->is_amount_discount)
$this->setLineTotal($this->getLineTotal() - $this->formatValue($this->item->discount, $this->precision)); {
else $discount = $this->formatValue($this->item->discount, $this->precision);
$this->setLineTotal($this->getLineTotal() - $this->formatValue(($this->getLineTotal() * $this->item->discount / 100), $this->precision));
$this->setLineTotal($this->getLineTotal() - $discount);
$this->setTotalDiscounts($this->getTotalDiscounts() + $discount);
}
else
{
$discount = $this->formatValue(($this->getLineTotal() * $this->item->discount / 100), $this->precision);
$this->setLineTotal($this->getLineTotal() - $discount);
$this->setTotalDiscounts($this->getTotalDiscounts() + $discount);
}
return $this; return $this;
@ -59,7 +75,7 @@ class InvoiceItemCalc
if($this->inclusive_tax) if($this->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->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate1/100))) , $this->precision);
else else
$item_tax_rate1_total += $this->formatValue(($this->getLineTotal() * $tax_rate1/100), $this->precision); $item_tax_rate1_total = $this->formatValue(($this->getLineTotal() * $tax_rate1/100), $this->precision);
$item_tax += $item_tax_rate1_total; $item_tax += $item_tax_rate1_total;
@ -71,23 +87,29 @@ class InvoiceItemCalc
if($tax_rate2 != 0) if($tax_rate2 != 0)
{ {
if($this->inclusive_tax) if($this->inclusive_tax)
$item_tax += $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate2/100))) , $this->precision); $item_tax_rate2_total = $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate2/100))) , $this->precision);
else else
$item_tax += $this->formatValue(($this->getLineTotal() * $tax_rate2/100), $this->precision); $item_tax_rate2_total = $this->formatValue(($this->getLineTotal() * $tax_rate2/100), $this->precision);
$item_tax += $item_tax_rate2_total;
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
} }
$this->setTotalTaxes($item_tax); $this->setTotalTaxes($item_tax);
} }
private function groupTax($tax_name, $tax_rate, $tax_total) : array private function groupTax($tax_name, $tax_rate, $tax_total)
{ {
$group_tax = []; $group_tax = [];
$key = str_replace(" ", "", $tax_name.$tax_rate); $key = str_replace(" ", "", $tax_name.$tax_rate);
$group_tax[$key] = $tax_total
return $group_tax; $group_tax[$key] = ['total' => $tax_total, 'tax_name' => $tax_name . ' ' . $tax_rate];
$this->groupTaxes($group_tax);
} }
@ -99,6 +121,19 @@ class InvoiceItemCalc
return $this; return $this;
} }
/****************
*
* Getters and Setters
*
*
*/
public function getLimeItem()
{
return $this->item;
}
public function getLineTotal() public function getLineTotal()
{ {
return $this->item->line_total; return $this->item->line_total;
@ -123,4 +158,17 @@ class InvoiceItemCalc
return $this; return $this;
} }
public function getTotalDiscounts()
{
return $this->total_dicounts;
}
public function setTotalDiscounts($total)
{
$this->total_dicounts = $total;
return $this;
}
} }