invoiceninja/app/Helpers/Invoice/InvoiceCalc.php

143 lines
2.1 KiB
PHP
Raw Normal View History

2019-04-02 16:16:39 +11:00
<?php
namespace App\Helpers\Invoice;
use App\Helpers\Invoice\InvoiceItemCalc;
2019-04-02 16:16:39 +11:00
use App\Models\Invoice;
2019-04-04 15:49:13 +11:00
use App\Utils\Traits\NumberFormatter;
2019-04-02 16:16:39 +11:00
2019-04-03 11:09:22 +11:00
class InvoiceCalc
2019-04-02 16:16:39 +11:00
{
2019-04-03 11:09:22 +11:00
2019-04-04 15:49:13 +11:00
use NumberFormatter;
protected $invoice;
protected $balance;
protected $paid_to_date;
protected $amount;
protected $line_items;
2019-04-05 20:32:59 +11:00
protected $settings;
2019-04-04 15:49:13 +11:00
protected $invoice_total;
2019-04-05 15:52:30 +11:00
protected $tax_map;
protected $total_taxes;
protected $total_discount;
2019-04-04 15:49:13 +11:00
2019-04-05 20:32:59 +11:00
public function __construct(Invoice $invoice, \stdClass $settings)
2019-04-02 16:16:39 +11:00
{
$this->invoice = $invoice;
2019-04-05 20:32:59 +11:00
$this->settings = $settings;
2019-04-02 16:16:39 +11:00
}
2019-04-03 11:09:22 +11:00
2019-04-04 15:49:13 +11:00
public function build()
{
$this->calcLineItems();
}
private function calcLineItems()
{
$new_line_items = [];
foreach($this->invoice->line_items as $item) {
$item_calc = new InvoiceItemCalc($item);
$item_calc->process();
2019-04-04 15:49:13 +11:00
$new_line_items[] = $item_calc->getLineItem();
//set collection of itemised taxes
2019-04-05 20:32:59 +11:00
$this->tax_map->merge($item_calc->getGroupedTaxes());
2019-04-05 15:52:30 +11:00
//set running total of taxes
2019-04-05 20:32:59 +11:00
$this->total_taxes += $item_calc->getTotalTaxes();
2019-04-05 15:52:30 +11:00
//set running total of discounts
2019-04-05 20:32:59 +11:00
$this->total_discount += $item_calc->getTotalDiscounts();
2019-04-04 15:49:13 +11:00
}
$this->invoice->line_items = $new_line_items;
2019-04-05 20:32:59 +11:00
return $this;
2019-04-04 15:49:13 +11:00
}
2019-04-05 15:52:30 +11:00
/**
* Getters and Setters
*/
private function getTaxMap()
{
return $this->tax_map;
}
private function setTaxMap($value)
{
$htis->tax_map = $value;
return $this;
}
private function getTotalDiscount()
{
return $this->total_discount;
}
private function setTotalDiscount($value)
{
$this->total_discount = $value;
return $this;
}
private function getTotalTaxes()
{
return $this->total_taxes;
}
private function setTotalTaxes($value)
{
$this->total_taxes = $value;
return $this;
}
/*
2019-04-04 15:49:13 +11:00
private function setDiscount($amount, $discount, $is_amount_discount)
{
if($is_amount_discount)
return $amount - $this->formatValue($discount);
else
return $amount - $this->formatValue($amount * $discount / 100);
}
private function getInvoiceTotal()
{
return $this->invoice_total;
}
private function setInvoiceTotal($invoice_total)
{
$this->invoice_total = $invoice_total;
}
*/
2019-04-04 15:49:13 +11:00
2019-04-02 16:16:39 +11:00
}