mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Working on Invoice Calculator
This commit is contained in:
parent
d28fe81c51
commit
31ce0eaca8
@ -3,13 +3,86 @@
|
||||
namespace App\Helpers\Invoice;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use App\Utils\Traits\NumberFormatter;
|
||||
|
||||
class InvoiceCalc
|
||||
{
|
||||
|
||||
public function __construct(Invoice $invoice)
|
||||
use NumberFormatter;
|
||||
|
||||
protected $invoice;
|
||||
|
||||
protected $balance;
|
||||
|
||||
protected $paid_to_date;
|
||||
|
||||
protected $amount;
|
||||
|
||||
protected $line_items;
|
||||
|
||||
protected $precision;
|
||||
|
||||
protected $invoice_total;
|
||||
|
||||
|
||||
public function __construct(Invoice $invoice, int $precision = 2)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
$this->precision = $precision;
|
||||
}
|
||||
|
||||
public function build()
|
||||
{
|
||||
$this->calcLineItems();
|
||||
}
|
||||
|
||||
|
||||
private function calcLineItems()
|
||||
{
|
||||
|
||||
$new_line_items = [];
|
||||
|
||||
foreach($this->invoice->line_items as $item) {
|
||||
|
||||
$total = $this->formatValue($item->cost) * $this->formatValue($item->qty);
|
||||
$total = $this->setDiscount($total, $item->discount, $item->is_amount_discount);
|
||||
$total = $this->setTaxRate($total, $item->tax_name1, $item->tax_rate1);
|
||||
|
||||
$item->line_total = $total;
|
||||
|
||||
$new_line_items[] = $item;
|
||||
|
||||
$this->setInvoiceTotal($total);
|
||||
}
|
||||
|
||||
$this->invoice->line_items = $new_line_items;
|
||||
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
28
app/Utils/Traits/NumberFormatter.php
Normal file
28
app/Utils/Traits/NumberFormatter.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utils\Traits;
|
||||
|
||||
/**
|
||||
* Class NumberFormatter
|
||||
* @package App\Utils\Traits
|
||||
*/
|
||||
trait NumberFormatter
|
||||
{
|
||||
private function formatValue($value) : string
|
||||
{
|
||||
return number_format($this->parseFloat($value), $this->precision, '.', '');
|
||||
}
|
||||
|
||||
private function parseFloat($value) : float
|
||||
{
|
||||
// check for comma as decimal separator
|
||||
if (preg_match('/,[\d]{1,2}$/', $value)) {
|
||||
$value = str_replace(',', '.', $value);
|
||||
}
|
||||
|
||||
$value = preg_replace('/[^0-9\.\-]/', '', $value);
|
||||
|
||||
return floatval($value);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user