invoiceninja/app/Helpers/Invoice/InvoiceCalc.php

236 lines
3.6 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-08 14:28:28 +10:00
/**
* Class for invoice calculations.
*/
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;
2019-04-08 14:28:28 +10:00
protected $settings;
2019-04-04 15:49:13 +11:00
2019-04-08 14:28:28 +10:00
private $balance;
2019-04-04 15:49:13 +11:00
2019-04-08 14:28:28 +10:00
private $paid_to_date;
2019-04-04 15:49:13 +11:00
2019-04-08 14:28:28 +10:00
private $amount;
2019-04-04 15:49:13 +11:00
2019-04-08 14:28:28 +10:00
private $sub_total;
private $line_items;
2019-04-04 15:49:13 +11:00
2019-04-08 14:28:28 +10:00
private $invoice_total;
2019-04-04 15:49:13 +11:00
2019-04-08 14:28:28 +10:00
private $tax_map;
2019-04-05 15:52:30 +11:00
2019-04-08 14:28:28 +10:00
private $total_taxes;
2019-04-05 15:52:30 +11:00
2019-04-08 14:28:28 +10:00
private $total_discount;
2019-04-04 15:49:13 +11:00
2019-04-08 14:28:28 +10:00
/**
* Constructs the object with Invoice and Settings object
*
* @param \App\Models\Invoice $invoice The invoice
* @param \stdClass $settings The settings
*/
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-08 14:28:28 +10:00
/**
* Builds the invoice values
*/
2019-04-04 15:49:13 +11:00
public function build()
{
$this->calcLineItems()->calcTaxes();
2019-04-04 15:49:13 +11:00
}
/**
* Calculates the Invoice Level taxes.
*/
private function calcTaxes()
{
}
2019-04-08 14:28:28 +10:00
/**
* Calculates the line items.
*
* @return self The line items.
*/
2019-04-04 15:49:13 +11:00
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-08 14:28:28 +10:00
//set running subtotal
$this->sub_total += $item_calc->getLineTotal();
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
2019-04-08 14:28:28 +10:00
/**
* Getters and Setters
*/
2019-04-05 15:52:30 +11:00
2019-04-08 14:28:28 +10:00
/**
* Gets the sub total.
*
* @return float The sub total.
*/
private function getSubTotal()
{
return $this->subtotal;
}
2019-04-05 15:52:30 +11:00
/**
2019-04-08 14:28:28 +10:00
* Sets the sub total.
*
* @param float $value The value
*
* @return self $this
2019-04-05 15:52:30 +11:00
*/
2019-04-08 14:28:28 +10:00
private function setSubTotal($value)
{
$this->sub_total = $value;
return $this;
}
2019-04-05 15:52:30 +11:00
2019-04-08 14:28:28 +10:00
/**
* Gets the tax map.
*
* @return Collection The tax map.
*/
2019-04-05 15:52:30 +11:00
private function getTaxMap()
{
return $this->tax_map;
}
2019-04-08 14:28:28 +10:00
/**
* Sets the tax map.
*
* @param Collection $value Collection of mapped taxes
*
* @return self $this
*/
2019-04-05 15:52:30 +11:00
private function setTaxMap($value)
{
$htis->tax_map = $value;
return $this;
}
2019-04-08 14:28:28 +10:00
/**
* Gets the total discount.
*
* @return float The total discount.
*/
2019-04-05 15:52:30 +11:00
private function getTotalDiscount()
{
return $this->total_discount;
}
2019-04-08 14:28:28 +10:00
/**
* Sets the total discount.
*
* @param float $value The value
*
* @return self $this
*/
2019-04-05 15:52:30 +11:00
private function setTotalDiscount($value)
{
$this->total_discount = $value;
return $this;
}
2019-04-08 14:28:28 +10:00
/**
* Gets the total taxes.
*
* @return float The total taxes.
*/
2019-04-05 15:52:30 +11:00
private function getTotalTaxes()
{
return $this->total_taxes;
}
2019-04-08 14:28:28 +10:00
/**
* Sets the total taxes.
*
* @param float $value The value
*
* @return self ( $this )
*/
2019-04-05 15:52:30 +11:00
private function setTotalTaxes($value)
{
$this->total_taxes = $value;
return $this;
}
2019-04-08 14:28:28 +10:00
/*
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
}