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

404 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-03-23 21:40:44 +01:00
use App\Models\Client;
use App\Models\Invoice;
2023-03-23 21:40:44 +01:00
use App\DataMapper\InvoiceItem;
use App\DataMapper\BaseSettings;
2023-03-24 08:02:34 +01:00
use App\DataMapper\Tax\RuleInterface;
use App\Utils\Traits\NumberFormatter;
2023-03-24 08:58:59 +01:00
use App\DataMapper\Tax\ZipTax\Response;
class InvoiceItemSum
{
use NumberFormatter;
use Discounter;
use Taxer;
2023-03-26 22:57:29 +02:00
private array $tax_jurisdictions = [
'AT', // Austria
'BE', // Belgium
'BG', // Bulgaria
'CY', // Cyprus
'CZ', // Czech Republic
'DE', // Germany
'DK', // Denmark
'EE', // Estonia
'ES', // Spain
'FI', // Finland
'FR', // France
'GR', // Greece
'HR', // Croatia
'HU', // Hungary
'IE', // Ireland
'IT', // Italy
'LT', // Lithuania
'LU', // Luxembourg
'LV', // Latvia
'MT', // Malta
'NL', // Netherlands
'PL', // Poland
'PT', // Portugal
'RO', // Romania
'SE', // Sweden
'SI', // Slovenia
'SK', // Slovakia
2023-04-12 06:08:56 +02:00
'US', // USA
'AU', // Australia
2023-03-26 22:57:29 +02:00
];
protected $invoice;
private $items;
private $line_total;
2021-09-15 01:02:25 +02:00
private $gross_line_total;
2022-11-10 11:57:55 +01:00
private $tax_amount;
2022-11-10 09:16:22 +01:00
private $currency;
private $total_taxes;
private $item;
private $line_items;
private $sub_total;
2021-09-15 01:02:25 +02:00
private $gross_sub_total;
private $total_discount;
private $tax_collection;
2023-03-23 21:40:44 +01:00
private ?Client $client;
private bool $calc_tax = false;
2023-03-24 08:02:34 +01:00
private RuleInterface $rule;
2023-03-24 08:58:59 +01:00
public function __construct($invoice)
{
$this->tax_collection = collect([]);
$this->invoice = $invoice;
if ($this->invoice->client) {
2022-06-06 14:27:17 +02:00
$this->currency = $this->invoice->client->currency();
2023-03-23 21:40:44 +01:00
$this->client = $this->invoice->client;
2023-03-24 08:58:59 +01:00
$this->shouldCalculateTax();
} else {
2022-06-07 13:07:14 +02:00
$this->currency = $this->invoice->vendor->currency();
}
$this->line_items = [];
}
2023-04-26 12:24:10 +02:00
public function process(): self
{
2023-04-26 12:24:10 +02:00
if (!$this->invoice->line_items || !is_array($this->invoice->line_items)) {
$this->items = [];
return $this;
}
$this->calcLineItems();
return $this;
}
2023-04-26 12:24:10 +02:00
private function calcLineItems(): self
{
foreach ($this->invoice->line_items as $this->item) {
$this->cleanLineItem()
->sumLineItem()
->setDiscount()
->calcTaxes()
->push();
}
return $this;
}
2023-03-24 08:58:59 +01:00
private function shouldCalculateTax(): self
2023-03-23 21:40:44 +01:00
{
if (!$this->invoice->company?->calculate_taxes) {
2023-03-24 08:58:59 +01:00
$this->calc_tax = false;
return $this;
}
2023-03-27 05:47:01 +02:00
//should we be filtering by client country here? do we need to reflect at the company <=> client level?
2023-03-26 22:57:29 +02:00
if (in_array($this->client->country->iso_3166_2, $this->tax_jurisdictions)) { //only calculate for supported tax jurisdictions
2023-04-08 00:32:38 +02:00
2023-03-29 11:49:40 +02:00
$class = "App\DataMapper\Tax\\".$this->client->company->country()->iso_3166_2."\\Rule";
2023-03-24 08:02:34 +01:00
2023-03-26 22:46:26 +02:00
$this->rule = new $class();
2023-03-29 11:49:40 +02:00
$this->rule
2023-04-24 03:47:48 +02:00
->setEntity($this->invoice)
2023-03-29 11:49:40 +02:00
->init();
2023-03-24 08:58:59 +01:00
$this->calc_tax = true;
return $this;
}
return $this;
2023-03-23 21:40:44 +01:00
}
2023-04-26 11:25:33 +02:00
private function push(): self
{
$this->sub_total += $this->getLineTotal();
2021-09-15 01:02:25 +02:00
$this->gross_sub_total += $this->getGrossLineTotal();
$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 {
$discount = ($this->item->line_total * ($this->item->discount / 100));
2022-11-10 09:16:22 +01:00
$this->setLineTotal($this->formatValue(($this->getLineTotal() - $discount), $this->currency->precision));
}
$this->item->is_amount_discount = $this->invoice->is_amount_discount;
return $this;
}
2023-03-24 08:58:59 +01:00
2023-03-23 21:40:44 +01:00
/**
2023-03-24 08:58:59 +01:00
* Attempts to calculate taxes based on the clients location
2023-03-23 21:40:44 +01:00
*
* @return self
*/
2023-03-24 08:58:59 +01:00
private function calcTaxesAutomatically(): self
2023-03-23 21:40:44 +01:00
{
2023-04-10 09:33:24 +02:00
$this->rule->tax($this->item);
2023-04-02 23:48:59 +02:00
2023-03-24 08:02:34 +01:00
$this->item->tax_name1 = $this->rule->tax_name1;
$this->item->tax_rate1 = $this->rule->tax_rate1;
$this->item->tax_name2 = $this->rule->tax_name2;
$this->item->tax_rate2 = $this->rule->tax_rate2;
$this->item->tax_name3 = $this->rule->tax_name3;
$this->item->tax_rate3 = $this->rule->tax_rate3;
2023-03-24 03:56:26 +01:00
return $this;
2023-03-23 21:40:44 +01:00
}
private function calcTaxes()
{
2023-03-24 08:58:59 +01:00
if ($this->calc_tax) {
2023-03-24 08:02:34 +01:00
$this->calcTaxesAutomatically();
2023-03-24 08:58:59 +01:00
}
2023-03-24 08:02:34 +01:00
$item_tax = 0;
$amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / 100));
$item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount);
$item_tax += $item_tax_rate1_total;
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->calcAmountLineTax($this->item->tax_rate2, $amount);
$item_tax += $item_tax_rate2_total;
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->calcAmountLineTax($this->item->tax_rate3, $amount);
$item_tax += $item_tax_rate3_total;
if (strlen($this->item->tax_name3) > 1) {
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
}
2019-10-17 07:09:52 +02:00
$this->setTotalTaxes($this->formatValue($item_tax, $this->currency->precision));
2021-09-15 01:02:25 +02:00
$this->item->gross_line_total = $this->getLineTotal() + $item_tax;
2023-03-29 11:49:40 +02:00
2022-11-10 11:57:55 +01:00
$this->item->tax_amount = $item_tax;
2022-11-10 09:16:22 +01:00
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.' '.floatval($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;
}
2021-09-15 01:02:25 +02:00
public function getGrossLineTotal()
{
return $this->item->gross_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 01:02:25 +02:00
public function getGrossSubTotal()
{
2021-09-15 02:00:29 +02:00
return $this->gross_sub_total;
2021-09-15 01:02:25 +02:00
}
public function setSubTotal($value)
{
$this->sub_total = $value;
return $this;
}
2019-10-17 05:14:17 +02:00
/**
* 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->item->line_total == 0) {
continue;
}
2020-10-07 01:16:57 +02:00
//$amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total));
2020-11-25 15:19:52 +01:00
$amount = ($this->sub_total > 0) ? $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total)) : 0;
2020-10-07 01:16:57 +02:00
$item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount);
2019-10-17 05:14:17 +02:00
$item_tax += $item_tax_rate1_total;
2019-10-17 05:14:17 +02:00
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->calcAmountLineTax($this->item->tax_rate2, $amount);
$item_tax += $item_tax_rate2_total;
2019-10-17 05:14:17 +02:00
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);
}
2019-10-17 05:14:17 +02:00
$item_tax_rate3_total = $this->calcAmountLineTax($this->item->tax_rate3, $amount);
2019-10-29 12:44:54 +01:00
$item_tax += $item_tax_rate3_total;
2019-10-29 12:44:54 +01:00
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);
}
}
2019-10-29 12:44:54 +01:00
$this->setTotalTaxes($item_tax);
}
/**
2020-10-07 01:16:57 +02:00
* Sets default casts for the values in the line_items.
2020-11-25 15:19:52 +01:00
*
* @return $this
*/
private function cleanLineItem()
{
$invoice_item = (object) get_class_vars(InvoiceItem::class);
unset($invoice_item->casts);
foreach ($invoice_item as $key => $value) {
if (! property_exists($this->item, $key) || ! isset($this->item->{$key})) {
$this->item->{$key} = $value;
$this->item->{$key} = BaseSettings::castAttribute(InvoiceItem::$casts[$key], $value);
}
}
2019-10-29 12:44:54 +01:00
return $this;
}
}