2019-10-22 04:07:00 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-10-22 04:07:00 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-10-22 04:07:00 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-10-22 04:07:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Helpers\Invoice;
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\DataMapper\Tax\RuleInterface;
|
2023-07-31 03:28:30 +02:00
|
|
|
use App\Models\Client;
|
2023-04-26 14:17:40 +02:00
|
|
|
use App\Models\Credit;
|
2019-10-22 04:07:00 +02:00
|
|
|
use App\Models\Invoice;
|
2023-04-26 14:17:40 +02:00
|
|
|
use App\Models\PurchaseOrder;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\Quote;
|
2023-04-26 14:17:40 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\RecurringQuote;
|
2019-10-22 04:07:00 +02:00
|
|
|
use App\Utils\Traits\NumberFormatter;
|
|
|
|
|
|
|
|
class InvoiceItemSumInclusive
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
use NumberFormatter;
|
|
|
|
use Discounter;
|
|
|
|
use Taxer;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2023-07-31 03:28:30 +02:00
|
|
|
|
|
|
|
private array $eu_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
|
|
|
|
];
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
'US', // USA
|
|
|
|
|
|
|
|
'AU', // Australia
|
|
|
|
];
|
|
|
|
|
2023-04-26 14:17:40 +02:00
|
|
|
protected RecurringInvoice | Invoice | Quote | Credit | PurchaseOrder | RecurringQuote $invoice;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2023-08-07 10:29:07 +02:00
|
|
|
private \App\Models\Currency $currency;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
private $total_taxes;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
private $item;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
private $line_items;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
private $sub_total;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
private $tax_collection;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2023-07-31 03:28:30 +02:00
|
|
|
private bool $calc_tax = false;
|
|
|
|
|
|
|
|
private ?Client $client;
|
|
|
|
|
|
|
|
private RuleInterface $rule;
|
|
|
|
|
2023-04-26 14:17:40 +02:00
|
|
|
public function __construct(RecurringInvoice | Invoice | Quote | Credit | PurchaseOrder | RecurringQuote $invoice)
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
|
|
|
$this->tax_collection = collect([]);
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->invoice = $invoice;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->invoice->client) {
|
2022-06-07 13:07:14 +02:00
|
|
|
$this->currency = $this->invoice->client->currency();
|
2023-08-01 11:44:16 +02:00
|
|
|
$this->client = $this->invoice->client;
|
2023-07-31 03:28:30 +02:00
|
|
|
$this->shouldCalculateTax();
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2022-06-07 13:07:14 +02:00
|
|
|
$this->currency = $this->invoice->vendor->currency();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->line_items = [];
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function process()
|
|
|
|
{
|
2023-10-11 05:18:56 +02:00
|
|
|
if (!$this->invoice->line_items || ! is_iterable($this->invoice->line_items) || count($this->invoice->line_items) == 0) {
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->calcLineItems();
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
private function calcLineItems()
|
|
|
|
{
|
|
|
|
foreach ($this->invoice->line_items as $this->item) {
|
|
|
|
$this->sumLineItem()
|
|
|
|
->setDiscount()
|
|
|
|
->calcTaxes()
|
|
|
|
->push();
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
private function push()
|
|
|
|
{
|
|
|
|
$this->sub_total += $this->getLineTotal();
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->line_items[] = $this->item;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
private function sumLineItem()
|
|
|
|
{
|
2022-02-11 21:35:08 +01:00
|
|
|
$this->setLineTotal($this->item->cost * $this->item->quantity);
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
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));
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->item->is_amount_discount = $this->invoice->is_amount_discount;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2023-07-31 03:28:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to calculate taxes based on the clients location
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
private function calcTaxesAutomatically(): self
|
|
|
|
{
|
|
|
|
$this->rule->tax($this->item);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-07-31 03:28:30 +02:00
|
|
|
$precision = strlen(substr(strrchr($this->rule->tax_rate1, "."), 1));
|
|
|
|
|
|
|
|
$this->item->tax_name1 = $this->rule->tax_name1;
|
|
|
|
$this->item->tax_rate1 = round($this->rule->tax_rate1, $precision);
|
|
|
|
|
|
|
|
$precision = strlen(substr(strrchr($this->rule->tax_rate2, "."), 1));
|
|
|
|
|
|
|
|
$this->item->tax_name2 = $this->rule->tax_name2;
|
|
|
|
$this->item->tax_rate2 = round($this->rule->tax_rate2, $precision);
|
|
|
|
|
|
|
|
$precision = strlen(substr(strrchr($this->rule->tax_rate3, "."), 1));
|
|
|
|
|
|
|
|
$this->item->tax_name3 = $this->rule->tax_name3;
|
|
|
|
$this->item->tax_rate3 = round($this->rule->tax_rate3, $precision);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-07-31 03:28:30 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
|
|
|
* Taxes effect the line totals and item costs. we decrement both on
|
|
|
|
* application of inclusive tax rates.
|
|
|
|
*/
|
|
|
|
private function calcTaxes()
|
|
|
|
{
|
2023-07-31 03:28:30 +02:00
|
|
|
|
|
|
|
if ($this->calc_tax) {
|
|
|
|
$this->calcTaxesAutomatically();
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax = 0;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / 100));
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2023-08-07 07:07:52 +02:00
|
|
|
/** @var float $item_tax_rate1_total */
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax_rate1_total = $this->calcInclusiveLineTax($this->item->tax_rate1, $amount);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-07 07:07:52 +02:00
|
|
|
/** @var float $item_tax */
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax += $this->formatValue($item_tax_rate1_total, $this->currency->precision);
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2024-02-29 02:01:02 +01:00
|
|
|
if (strlen($this->item->tax_name1) > 1) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax_rate2_total = $this->calcInclusiveLineTax($this->item->tax_rate2, $amount);
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax += $this->formatValue($item_tax_rate2_total, $this->currency->precision);
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2024-02-29 02:01:02 +01:00
|
|
|
if (strlen($this->item->tax_name2) > 1) {
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax_rate3_total = $this->calcInclusiveLineTax($this->item->tax_rate3, $amount);
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax += $this->formatValue($item_tax_rate3_total, $this->currency->precision);
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2024-02-29 02:01:02 +01:00
|
|
|
if (strlen($this->item->tax_name3) > 1) {
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2022-11-10 11:57:55 +01:00
|
|
|
$this->item->tax_amount = $this->formatValue($item_tax, $this->currency->precision);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->setTotalTaxes($this->formatValue($item_tax, $this->currency->precision));
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
private function groupTax($tax_name, $tax_rate, $tax_total)
|
|
|
|
{
|
|
|
|
$group_tax = [];
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$key = str_replace(' ', '', $tax_name.$tax_rate);
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$group_tax = ['key' => $key, 'total' => $tax_total, 'tax_name' => $tax_name.' '.$tax_rate.'%'];
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->tax_collection->push(collect($group_tax));
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function getTotalTaxes()
|
|
|
|
{
|
|
|
|
return $this->total_taxes;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function setTotalTaxes($total)
|
|
|
|
{
|
|
|
|
$this->total_taxes = $total;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function setLineTotal($total)
|
|
|
|
{
|
2022-03-11 21:52:51 +01:00
|
|
|
$this->item->gross_line_total = $total;
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->item->line_total = $total;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function getLineTotal()
|
|
|
|
{
|
|
|
|
return $this->item->line_total;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2021-09-15 03:12:36 +02:00
|
|
|
public function getGrossLineTotal()
|
|
|
|
{
|
|
|
|
return $this->item->line_total;
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function getLineItems()
|
|
|
|
{
|
|
|
|
return $this->line_items;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function getGroupedTaxes()
|
|
|
|
{
|
|
|
|
return $this->tax_collection;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function setGroupedTaxes($group_taxes)
|
|
|
|
{
|
|
|
|
$this->tax_collection = $group_taxes;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function getSubTotal()
|
|
|
|
{
|
|
|
|
return $this->sub_total;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2021-09-15 02:00:29 +02:00
|
|
|
public function getGrossSubTotal()
|
|
|
|
{
|
|
|
|
return $this->sub_total;
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function setSubTotal($value)
|
|
|
|
{
|
|
|
|
$this->sub_total = $value;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Amount Discount.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* 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([]));
|
2019-10-22 04:07:00 +02:00
|
|
|
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach ($this->line_items as $this->item) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->sub_total == 0) {
|
2021-02-05 23:31:26 +01:00
|
|
|
$amount = $this->item->line_total;
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2023-10-28 05:11:11 +02:00
|
|
|
$amount = $this->item->line_total - ($this->invoice->discount * ($this->item->line_total / $this->sub_total));
|
2023-09-12 23:26:22 +02:00
|
|
|
// $amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total));
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-09-12 23:26:22 +02:00
|
|
|
$item_tax = 0;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax_rate1_total = $this->calcInclusiveLineTax($this->item->tax_rate1, $amount);
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax += $item_tax_rate1_total;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2021-02-07 13:35:16 +01:00
|
|
|
if ($item_tax_rate1_total != 0) {
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax_rate2_total = $this->calcInclusiveLineTax($this->item->tax_rate2, $amount);
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax += $item_tax_rate2_total;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2021-02-07 13:35:16 +01:00
|
|
|
if ($item_tax_rate2_total != 0) {
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
|
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax_rate3_total = $this->calcInclusiveLineTax($this->item->tax_rate3, $amount);
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$item_tax += $item_tax_rate3_total;
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2021-02-07 13:35:16 +01:00
|
|
|
if ($item_tax_rate3_total != 0) {
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
|
|
|
|
}
|
2023-09-12 23:26:22 +02:00
|
|
|
|
|
|
|
$this->setTotalTaxes($this->getTotalTaxes() + $item_tax);
|
2023-09-13 06:31:06 +02:00
|
|
|
$this->item->gross_line_total = $this->getLineTotal();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-09-12 23:26:22 +02:00
|
|
|
$this->item->tax_amount = $item_tax;
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-10-22 04:07:00 +02:00
|
|
|
|
2023-09-12 23:26:22 +02:00
|
|
|
return $this;
|
|
|
|
|
|
|
|
// $this->setTotalTaxes($item_tax);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2023-07-31 03:28:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
private function shouldCalculateTax(): self
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-07-31 03:28:30 +02:00
|
|
|
if (!$this->invoice->company?->calculate_taxes || $this->invoice->company->account->isFreeHostedClient()) {
|
|
|
|
$this->calc_tax = false;
|
|
|
|
return $this;
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if (in_array($this->client->company->country()->iso_3166_2, $this->tax_jurisdictions)) { //only calculate for supported tax jurisdictions
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-07-31 03:28:30 +02:00
|
|
|
$class = "App\DataMapper\Tax\\".$this->client->company->country()->iso_3166_2."\\Rule";
|
|
|
|
|
|
|
|
$this->rule = new $class();
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if($this->rule->regionWithNoTaxCoverage($this->client->country->iso_3166_2)) {
|
|
|
|
return $this;
|
|
|
|
}
|
2023-07-31 03:28:30 +02:00
|
|
|
|
|
|
|
$this->rule
|
|
|
|
->setEntity($this->invoice)
|
|
|
|
->init();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-07-31 03:28:30 +02:00
|
|
|
$this->calc_tax = $this->rule->shouldCalcTax();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-07-31 03:28:30 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|