1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Helpers/Invoice/InvoiceItemSumInclusive.php

279 lines
7.3 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Helpers\Invoice;
2023-04-26 14:17:40 +02:00
use App\Models\Quote;
use App\Models\Credit;
use App\Models\Invoice;
2023-04-26 14:17:40 +02:00
use App\Models\PurchaseOrder;
use App\Models\RecurringQuote;
use App\Models\RecurringInvoice;
use App\Utils\Traits\NumberFormatter;
class InvoiceItemSumInclusive
{
use NumberFormatter;
use Discounter;
use Taxer;
2023-04-26 14:17:40 +02:00
protected RecurringInvoice | Invoice | Quote | Credit | PurchaseOrder | RecurringQuote $invoice;
private $currency;
private $total_taxes;
private $item;
private $line_items;
private $sub_total;
private $tax_collection;
2023-04-26 14:17:40 +02:00
public function __construct(RecurringInvoice | Invoice | Quote | Credit | PurchaseOrder | RecurringQuote $invoice)
{
$this->tax_collection = collect([]);
$this->invoice = $invoice;
if ($this->invoice->client) {
2022-06-07 13:07:14 +02:00
$this->currency = $this->invoice->client->currency();
} else {
2022-06-07 13:07:14 +02:00
$this->currency = $this->invoice->vendor->currency();
}
$this->line_items = [];
}
public function process()
{
if (! $this->invoice->line_items || ! is_array($this->invoice->line_items) || count($this->invoice->line_items) == 0) {
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->item->cost * $this->item->quantity);
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)
if (strlen($this->item->tax_name1) > 1) {
$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 (strlen($this->item->tax_name2) > 1) {
$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 (strlen($this->item->tax_name3) > 1) {
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
}
2022-11-10 11:57:55 +01:00
$this->item->tax_amount = $this->formatValue($item_tax, $this->currency->precision);
2022-11-10 09:16:22 +01:00
$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->gross_line_total = $total;
$this->item->line_total = $total;
return $this;
}
public function getLineTotal()
{
return $this->item->line_total;
}
2021-09-15 03:12:36 +02:00
public function getGrossLineTotal()
{
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;
}
2021-09-15 02:00:29 +02:00
public function getGrossSubTotal()
{
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) {
if ($this->sub_total == 0) {
$amount = $this->item->line_total;
} else {
$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;
2021-02-07 13:35:16 +01:00
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;
2021-02-07 13:35:16 +01:00
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;
2021-02-07 13:35:16 +01:00
if ($item_tax_rate3_total != 0) {
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
}
}
$this->setTotalTaxes($item_tax);
}
}