invoiceninja/app/Helpers/Invoice/InvoiceItemCalc.php

195 lines
3.9 KiB
PHP
Raw Normal View History

2019-04-04 20:28:53 +11:00
<?php
2019-05-11 13:32:07 +10:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
2019-04-04 20:28:53 +11:00
namespace App\Helpers\Invoice;
use App\Models\Invoice;
use App\Utils\Traits\NumberFormatter;
2019-04-11 10:35:30 +10:00
use Illuminate\Support\Collection;
2019-09-04 22:01:19 +10:00
use Illuminate\Support\Facades\Log;
2019-04-04 20:28:53 +11:00
class InvoiceItemCalc
{
use NumberFormatter;
protected $item;
protected $settings;
2019-04-04 20:28:53 +11:00
private $total_taxes;
2019-04-04 20:28:53 +11:00
private $total_discounts;
2019-04-04 20:28:53 +11:00
private $tax_collection;
private $line_total;
2019-04-05 08:33:12 +11:00
2019-04-24 20:01:40 +10:00
public function __construct(\stdClass $item, $settings)
2019-04-04 20:28:53 +11:00
{
2019-04-04 20:28:53 +11:00
$this->item = $item;
$this->settings = $settings;
$this->tax_collection = collect([]);
2019-04-04 20:28:53 +11:00
}
public function process()
{
$this->line_total = $this->formatValue($this->item->cost, $this->settings->precision) * $this->formatValue($this->item->quantity, $this->settings->precision);
$this->setDiscount()
->calcTaxes();
2019-04-04 20:28:53 +11:00
}
private function setDiscount()
{
2019-05-16 08:26:21 +10:00
if(!isset($this->item->is_amount_discount))
return $this;
2019-04-04 20:28:53 +11:00
if($this->item->is_amount_discount)
{
$discount = $this->formatValue($this->item->discount, $this->settings->precision);
$this->line_total -= $discount;
$this->total_discounts += $discount;
}
else
{
$discount = $this->formatValue(($this->line_total * $this->item->discount / 100), $this->settings->precision);
$this->line_total -= $discount;
$this->total_discounts += $discount;
}
2019-04-04 20:28:53 +11:00
return $this;
}
private function calcTaxes()
{
$item_tax = 0;
2019-05-16 08:26:21 +10:00
if(isset($this->item->tax_rate1) && $this->item->tax_rate1 > 0)
2019-04-04 20:28:53 +11:00
{
2019-05-16 08:26:21 +10:00
$tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->settings->precision);
if($this->settings->inclusive_taxes)
$item_tax_rate1_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate1/100))) , $this->settings->precision);
2019-04-04 20:53:40 +11:00
else
$item_tax_rate1_total = $this->formatValue(($this->line_total * $tax_rate1/100), $this->settings->precision);
2019-04-05 08:33:12 +11:00
$item_tax += $item_tax_rate1_total;
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
2019-04-04 20:28:53 +11:00
}
2019-05-16 08:26:21 +10:00
if(isset($this->item->tax_rate2) && $this->item->tax_rate2 > 0)
2019-04-04 20:28:53 +11:00
{
2019-05-16 08:26:21 +10:00
$tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->settings->precision);
if($this->settings->inclusive_taxes)
$item_tax_rate2_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate2/100))) , $this->settings->precision);
2019-04-04 20:53:40 +11:00
else
$item_tax_rate2_total = $this->formatValue(($this->line_total * $tax_rate2/100), $this->settings->precision);
$item_tax += $item_tax_rate2_total;
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
2019-04-04 20:53:40 +11:00
2019-04-04 20:28:53 +11:00
}
$this->setTotalTaxes($item_tax);
}
private function groupTax($tax_name, $tax_rate, $tax_total)
2019-04-05 08:33:12 +11:00
{
$group_tax = [];
$key = str_replace(" ", "", $tax_name.$tax_rate);
$group_tax = ['key' => $key, 'total' => $tax_total, 'tax_name' => $tax_name . ' ' . $tax_rate.'%'];
$this->tax_collection->push(collect($group_tax));
2019-04-05 08:33:12 +11:00
}
/****************
*
* Getters and Setters
*
*
*/
2019-04-11 10:35:30 +10:00
public function getLineItem()
{
return $this->item;
}
2019-04-04 20:28:53 +11:00
public function getLineTotal()
{
return $this->line_total;
2019-04-04 20:28:53 +11:00
}
public function setLineTotal($total)
{
$this->line_total = $total;
2019-04-04 20:28:53 +11:00
return $this;
}
public function getTotalTaxes()
{
return $this->total_taxes;
}
public function setTotalTaxes($total)
{
$this->total_taxes = $total;
return $this;
}
public function getTotalDiscounts()
{
2019-04-11 10:35:30 +10:00
return $this->total_discounts;
}
public function setTotalDiscounts($total)
{
2019-04-11 10:35:30 +10:00
$this->total_discounts = $total;
return $this;
}
2019-04-05 15:52:30 +11:00
public function getGroupedTaxes()
2019-09-04 22:01:19 +10:00
{
2019-04-05 15:52:30 +11:00
return $this->tax_collection;
}
public function setGroupedTaxes($group_taxes)
2019-04-05 15:52:30 +11:00
{
$this->tax_collection = $group_taxes;
2019-04-05 15:52:30 +11:00
return $this;
}
2019-04-04 20:28:53 +11:00
}