mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-11 05:32:39 +01:00
ba75a44eb8
* Adopt Laravel coding style The Laravel framework adopts the PSR-2 coding style with some additions. Laravel apps *should* adopt this coding style as well. However, Shift allows you to customize the adopted coding style by adding your own [PHP CS Fixer][1] `.php_cs` config to your project. You may use [Shift's .php_cs][2] file as a base. [1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer [2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200 * Shift bindings PHP 5.5.9+ adds the new static `class` property which provides the fully qualified class name. This is preferred over using class name strings as these references are checked by the parser. * Shift core files * Shift to Throwable * Add laravel/ui dependency * Unindent vendor mail templates * Shift config files * Default config files In an effort to make upgrading the constantly changing config files easier, Shift defaulted them so you can review the commit diff for changes. Moving forward, you should use ENV variables or create a separate config file to allow the core config files to remain automatically upgradeable. * Shift Laravel dependencies * Shift cleanup * Upgrade to Laravel 7 Co-authored-by: Laravel Shift <shift@laravelshift.com>
260 lines
6.6 KiB
PHP
260 lines
6.6 KiB
PHP
<?php
|
|
/**
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
*
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
*
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
*
|
|
* @license https://opensource.org/licenses/AAL
|
|
*/
|
|
|
|
namespace App\Helpers\Invoice;
|
|
|
|
use App\Helpers\Invoice\Discounter;
|
|
use App\Helpers\Invoice\Taxer;
|
|
use App\Models\Invoice;
|
|
use App\Utils\Traits\NumberFormatter;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class InvoiceItemSumInclusive
|
|
{
|
|
use NumberFormatter;
|
|
use Discounter;
|
|
use Taxer;
|
|
|
|
protected $invoice;
|
|
|
|
private $items;
|
|
|
|
private $line_total;
|
|
|
|
private $currency;
|
|
|
|
private $total_taxes;
|
|
|
|
private $item;
|
|
|
|
private $line_items;
|
|
|
|
private $sub_total;
|
|
|
|
private $total_discount;
|
|
|
|
private $tax_collection;
|
|
|
|
public function __construct($invoice)
|
|
{
|
|
$this->tax_collection = collect([]);
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
$this->currency = $this->invoice->client->currency();
|
|
|
|
$this->line_items = [];
|
|
}
|
|
|
|
public function process()
|
|
{
|
|
if (! $this->invoice->line_items || ! is_array($this->invoice->line_items) || count($this->invoice->line_items) == 0) {
|
|
$this->items = [];
|
|
|
|
return $this;
|
|
}
|
|
|
|
$this->calcLineItems();
|
|
|
|
return $this;
|
|
}
|
|
|
|
private function calcLineItems()
|
|
{
|
|
foreach ($this->invoice->line_items as $this->item) {
|
|
$this->sumLineItem()
|
|
->setDiscount()
|
|
->calcTaxes()
|
|
->push();
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
private function push()
|
|
{
|
|
$this->sub_total += $this->getLineTotal();
|
|
|
|
$this->line_items[] = $this->item;
|
|
|
|
return $this;
|
|
}
|
|
|
|
private function sumLineItem()
|
|
{
|
|
$this->setLineTotal($this->formatValue($this->item->cost, $this->currency->precision) * $this->formatValue($this->item->quantity, $this->currency->precision));
|
|
|
|
return $this;
|
|
}
|
|
|
|
private function setDiscount()
|
|
{
|
|
if ($this->invoice->is_amount_discount) {
|
|
$this->setLineTotal($this->getLineTotal() - $this->formatValue($this->item->discount, $this->currency->precision));
|
|
} else {
|
|
$this->setLineTotal($this->getLineTotal() - $this->formatValue(($this->item->line_total * ($this->item->discount / 100)), $this->currency->precision));
|
|
}
|
|
|
|
$this->item->is_amount_discount = $this->invoice->is_amount_discount;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Taxes effect the line totals and item costs. we decrement both on
|
|
* application of inclusive tax rates.
|
|
*/
|
|
private function calcTaxes()
|
|
{
|
|
$item_tax = 0;
|
|
|
|
$amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / 100));
|
|
|
|
$item_tax_rate1_total = $this->calcInclusiveLineTax($this->item->tax_rate1, $amount);
|
|
|
|
$item_tax += $this->formatValue($item_tax_rate1_total, $this->currency->precision);
|
|
|
|
if ($item_tax_rate1_total > 0) {
|
|
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
|
|
}
|
|
|
|
$item_tax_rate2_total = $this->calcInclusiveLineTax($this->item->tax_rate2, $amount);
|
|
|
|
$item_tax += $this->formatValue($item_tax_rate2_total, $this->currency->precision);
|
|
|
|
if ($item_tax_rate2_total > 0) {
|
|
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
|
|
}
|
|
|
|
$item_tax_rate3_total = $this->calcInclusiveLineTax($this->item->tax_rate3, $amount);
|
|
|
|
$item_tax += $this->formatValue($item_tax_rate3_total, $this->currency->precision);
|
|
|
|
if ($item_tax_rate3_total > 0) {
|
|
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
|
|
}
|
|
|
|
$this->setTotalTaxes($this->formatValue($item_tax, $this->currency->precision));
|
|
|
|
return $this;
|
|
}
|
|
|
|
private function groupTax($tax_name, $tax_rate, $tax_total)
|
|
{
|
|
$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));
|
|
}
|
|
|
|
public function getTotalTaxes()
|
|
{
|
|
return $this->total_taxes;
|
|
}
|
|
|
|
public function setTotalTaxes($total)
|
|
{
|
|
$this->total_taxes = $total;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setLineTotal($total)
|
|
{
|
|
$this->item->line_total = $total;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getLineTotal()
|
|
{
|
|
return $this->item->line_total;
|
|
}
|
|
|
|
public function getLineItems()
|
|
{
|
|
return $this->line_items;
|
|
}
|
|
|
|
public function getGroupedTaxes()
|
|
{
|
|
return $this->tax_collection;
|
|
}
|
|
|
|
public function setGroupedTaxes($group_taxes)
|
|
{
|
|
$this->tax_collection = $group_taxes;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getSubTotal()
|
|
{
|
|
return $this->sub_total;
|
|
}
|
|
|
|
public function setSubTotal($value)
|
|
{
|
|
$this->sub_total = $value;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Invoice Amount Discount.
|
|
*
|
|
* The problem, when calculating invoice level discounts,
|
|
* the tax collected changes.
|
|
*
|
|
* We need to synthetically reduce the line_total amounts
|
|
* and recalculate the taxes and then pass back
|
|
* the updated map
|
|
*/
|
|
public function calcTaxesWithAmountDiscount()
|
|
{
|
|
$this->setGroupedTaxes(collect([]));
|
|
|
|
$item_tax = 0;
|
|
|
|
foreach ($this->line_items as $this->item) {
|
|
$amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total));
|
|
$item_tax_rate1_total = $this->calcInclusiveLineTax($this->item->tax_rate1, $amount);
|
|
|
|
$item_tax += $item_tax_rate1_total;
|
|
|
|
if ($item_tax_rate1_total > 0) {
|
|
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
|
|
}
|
|
|
|
$item_tax_rate2_total = $this->calcInclusiveLineTax($this->item->tax_rate2, $amount);
|
|
|
|
$item_tax += $item_tax_rate2_total;
|
|
|
|
if ($item_tax_rate2_total > 0) {
|
|
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
|
|
}
|
|
|
|
$item_tax_rate3_total = $this->calcInclusiveLineTax($this->item->tax_rate3, $amount);
|
|
|
|
$item_tax += $item_tax_rate3_total;
|
|
|
|
if ($item_tax_rate3_total > 0) {
|
|
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
|
|
}
|
|
}
|
|
|
|
$this->setTotalTaxes($item_tax);
|
|
}
|
|
}
|