1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Helpers/Invoice/InvoiceSumInclusive.php

367 lines
10 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 13:18:01 +02:00
use App\Models\Quote;
use App\Models\Credit;
2020-10-28 11:10:49 +01:00
use App\Models\Invoice;
2023-04-26 13:18:01 +02:00
use App\Models\PurchaseOrder;
use App\Models\RecurringQuote;
use App\Models\RecurringInvoice;
use Illuminate\Support\Collection;
2023-04-26 13:18:01 +02:00
use App\Utils\Traits\NumberFormatter;
use App\Helpers\Invoice\InvoiceItemSumInclusive;
class InvoiceSumInclusive
{
use Taxer;
use CustomValuer;
use Discounter;
use NumberFormatter;
2023-04-26 13:18:01 +02:00
protected RecurringInvoice | Invoice | Quote | Credit | PurchaseOrder | RecurringQuote $invoice;
public $tax_map;
public $invoice_item;
public $total_taxes;
private $total;
private $total_discount;
private $total_custom_values;
private $total_tax_map;
private $sub_total;
2022-06-07 13:07:14 +02:00
private $precision;
2023-04-26 11:25:33 +02:00
public InvoiceItemSumInclusive $invoice_items;
/**
* Constructs the object with Invoice and Settings object.
*
2023-04-26 13:18:01 +02:00
* @param RecurringInvoice | Invoice | Quote | Credit | PurchaseOrder | RecurringQuote $invoice;
*/
public function __construct($invoice)
{
$this->invoice = $invoice;
if ($this->invoice->client) {
2022-06-07 13:07:14 +02:00
$this->precision = $this->invoice->client->currency()->precision;
} else {
2022-06-07 13:07:14 +02:00
$this->precision = $this->invoice->vendor->currency()->precision;
}
2022-06-07 13:07:14 +02:00
$this->tax_map = new Collection;
}
public function build()
{
$this->calculateLineItems()
->calculateDiscount()
->calculateCustomValues()
->calculateInvoiceTaxes()
->setTaxMap()
2020-11-25 11:30:00 +01:00
->calculateTotals() //just don't add the taxes!!
->calculateBalance()
->calculatePartial();
return $this;
}
private function calculateLineItems()
{
$this->invoice_items = new InvoiceItemSumInclusive($this->invoice);
$this->invoice_items->process();
$this->invoice->line_items = $this->invoice_items->getLineItems();
$this->total = $this->invoice_items->getSubTotal();
$this->setSubTotal($this->invoice_items->getSubTotal());
return $this;
}
private function calculateDiscount()
{
$this->total_discount = $this->discount($this->invoice_items->getSubTotal());
$this->total -= $this->total_discount;
return $this;
}
private function calculateCustomValues()
{
$this->total_taxes += $this->valuerTax($this->invoice->custom_surcharge1, $this->invoice->custom_surcharge_tax1);
2019-10-29 12:12:59 +01:00
$this->total_custom_values += $this->valuer($this->invoice->custom_surcharge1);
$this->total_taxes += $this->valuerTax($this->invoice->custom_surcharge2, $this->invoice->custom_surcharge_tax2);
2019-10-29 12:12:59 +01:00
$this->total_custom_values += $this->valuer($this->invoice->custom_surcharge2);
$this->total_taxes += $this->valuerTax($this->invoice->custom_surcharge3, $this->invoice->custom_surcharge_tax3);
2019-10-29 12:12:59 +01:00
$this->total_custom_values += $this->valuer($this->invoice->custom_surcharge3);
$this->total_taxes += $this->valuerTax($this->invoice->custom_surcharge4, $this->invoice->custom_surcharge_tax4);
2019-10-29 12:12:59 +01:00
$this->total_custom_values += $this->valuer($this->invoice->custom_surcharge4);
$this->total += $this->total_custom_values;
return $this;
}
private function calculateInvoiceTaxes()
{
$amount = $this->total;
if ($this->invoice->discount > 0 && $this->invoice->is_amount_discount) {
$amount = $this->formatValue(($this->sub_total - $this->invoice->discount), 2);
}
if ($this->invoice->discount > 0 && ! $this->invoice->is_amount_discount) {
$amount = $this->formatValue(($this->sub_total - ($this->sub_total * ($this->invoice->discount / 100))), 2);
}
if ($this->invoice->tax_rate1 > 0) {
$tax = $this->calcInclusiveLineTax($this->invoice->tax_rate1, $amount);
$this->total_taxes += $tax;
$this->total_tax_map[] = ['name' => $this->invoice->tax_name1.' '.floatval($this->invoice->tax_rate1).'%', 'total' => $tax];
}
if ($this->invoice->tax_rate2 > 0) {
$tax = $this->calcInclusiveLineTax($this->invoice->tax_rate2, $amount);
$this->total_taxes += $tax;
$this->total_tax_map[] = ['name' => $this->invoice->tax_name2.' '.floatval($this->invoice->tax_rate2).'%', 'total' => $tax];
}
if ($this->invoice->tax_rate3 > 0) {
$tax = $this->calcInclusiveLineTax($this->invoice->tax_rate3, $amount);
$this->total_taxes += $tax;
$this->total_tax_map[] = ['name' => $this->invoice->tax_name3.' '.floatval($this->invoice->tax_rate3).'%', 'total' => $tax];
}
return $this;
}
/**
* Calculates the balance.
*
* @return self The balance.
*/
private function calculateBalance()
{
$this->setCalculatedAttributes();
return $this;
}
private function calculatePartial()
{
if (! isset($this->invoice->id) && isset($this->invoice->partial)) {
$this->invoice->partial = max(0, min($this->formatValue($this->invoice->partial, 2), $this->invoice->balance));
}
return $this;
}
private function calculateTotals()
{
return $this;
}
public function getTotalSurcharges()
{
return $this->total_custom_values;
}
2020-10-20 03:30:55 +02:00
public function getRecurringInvoice()
{
2022-06-07 13:07:14 +02:00
$this->invoice->amount = $this->formatValue($this->getTotal(), $this->precision);
2020-10-20 03:30:55 +02:00
$this->invoice->total_taxes = $this->getTotalTaxes();
2022-06-07 13:07:14 +02:00
$this->invoice->balance = $this->formatValue($this->getTotal(), $this->precision);
2020-10-20 03:30:55 +02:00
2021-10-14 08:54:38 +02:00
$this->invoice->saveQuietly();
2020-10-20 03:30:55 +02:00
return $this->invoice;
}
public function getTempEntity()
{
$this->setCalculatedAttributes();
return $this->invoice;
}
/**
* @return Invoice | RecurringInvoice | Quote | Credit | PurchaseOrder
*/
public function getInvoice()
{
//Build invoice values here and return Invoice
$this->setCalculatedAttributes();
2021-10-14 08:54:38 +02:00
$this->invoice->saveQuietly();
return $this->invoice;
}
/**
* @return Invoice | RecurringInvoice | Quote | Credit | PurchaseOrder
*/
2020-02-24 22:12:55 +01:00
public function getQuote()
{
//Build invoice values here and return Invoice
$this->setCalculatedAttributes();
2021-10-14 08:54:38 +02:00
$this->invoice->saveQuietly();
2020-02-24 22:12:55 +01:00
return $this->invoice;
}
/**
* @return Invoice | RecurringInvoice | Quote | Credit | PurchaseOrder
*/
2020-02-24 22:12:55 +01:00
public function getCredit()
{
//Build invoice values here and return Invoice
$this->setCalculatedAttributes();
2022-05-31 00:28:32 +02:00
$this->invoice->saveQuietly();
return $this->invoice;
}
/**
* @return Invoice | RecurringInvoice | Quote | Credit | PurchaseOrder
*/
2022-05-31 00:28:32 +02:00
public function getPurchaseOrder()
{
//Build invoice values here and return Invoice
$this->setCalculatedAttributes();
2021-10-14 08:54:38 +02:00
$this->invoice->saveQuietly();
2020-02-24 22:12:55 +01:00
return $this->invoice;
}
/**
* Build $this->invoice variables after
* calculations have been performed.
*/
private function setCalculatedAttributes()
{
/* If amount != balance then some money has been paid on the invoice, need to subtract this difference from the total to set the new balance */
if ($this->invoice->status_id != Invoice::STATUS_DRAFT) {
if ($this->invoice->amount != $this->invoice->balance) {
$paid_to_date = $this->invoice->amount - $this->invoice->balance;
2022-06-07 13:07:14 +02:00
$this->invoice->balance = $this->formatValue($this->getTotal(), $this->precision) - $paid_to_date;
} else {
2022-06-07 13:07:14 +02:00
$this->invoice->balance = $this->formatValue($this->getTotal(), $this->precision);
}
}
/* Set new calculated total */
2022-06-07 13:07:14 +02:00
$this->invoice->amount = $this->formatValue($this->getTotal(), $this->precision);
$this->invoice->total_taxes = $this->getTotalTaxes();
return $this;
}
public function getSubTotal()
{
return $this->sub_total;
}
2021-09-15 03:12:36 +02:00
public function getGrossSubTotal()
{
return $this->sub_total;
}
public function setSubTotal($value)
{
$this->sub_total = $value;
return $this;
}
public function getTotalDiscount()
{
return $this->total_discount;
}
public function getTotalTaxes()
{
return $this->total_taxes;
}
public function getTotalTaxMap()
{
return $this->total_tax_map;
}
public function getTotal()
{
return $this->total;
}
public function setTaxMap()
{
if ($this->invoice->is_amount_discount == true) {
$this->invoice_items->calcTaxesWithAmountDiscount();
}
$this->tax_map = collect();
$keys = $this->invoice_items->getGroupedTaxes()->pluck('key')->unique();
$values = $this->invoice_items->getGroupedTaxes();
foreach ($keys as $key) {
$tax_name = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->pluck('tax_name')->first();
$total_line_tax = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->sum('total');
//$total_line_tax -= $this->discount($total_line_tax);
$this->tax_map[] = ['name' => $tax_name, 'total' => $total_line_tax];
$this->total_taxes += $total_line_tax;
}
return $this;
}
public function getTaxMap()
{
return $this->tax_map;
}
public function getBalance()
{
return $this->invoice->balance;
}
public function getItemTotalTaxes()
{
return $this->getTotalTaxes();
}
2020-10-10 12:57:28 +02:00
2020-10-15 11:41:59 +02:00
public function purgeTaxes()
{
return $this;
}
}