From 3fe7bb8990ce6f55653035b1a0fe44807039560a Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Wed, 3 May 2023 16:05:59 +0200 Subject: [PATCH 1/6] Refactor ZuGFerd Tax calculation --- .../Invoice/EInvoice/ZugferdEInvoice.php | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php index b0f26a54583f..a8de0f11c685 100644 --- a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php +++ b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php @@ -23,7 +23,7 @@ use horstoeko\zugferd\codelists\ZugferdDutyTaxFeeCategories; class ZugferdEInvoice extends AbstractService { - public function __construct(public Invoice $invoice, private bool $alterPDF, private string $custom_pdf_path = "") + public function __construct(public Invoice $invoice, private bool $alterPDF, private string $custom_pdf_path = "", private array $tax_map = []) { } @@ -122,25 +122,32 @@ class ZugferdEInvoice extends AbstractService $xrechnung->setDocumentPositionLineSummation($linenetamount); // According to european law, each line item can only have one tax rate if (!(empty($item->tax_name1) && empty($item->tax_name2) && empty($item->tax_name3))) { + $taxtype = $this->getTaxType($item->tax_id); if (!empty($item->tax_name1)) { - $xrechnung->addDocumentPositionTax($this->getTaxType($item->tax_id), 'VAT', $item->tax_rate1); + $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate1); + $this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1); } elseif (!empty($item->tax_name2)) { - $xrechnung->addDocumentPositionTax($this->getTaxType($item->tax_id), 'VAT', $item->tax_rate2); + $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate2); + $this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate2); } elseif (!empty($item->tax_name3)) { - $xrechnung->addDocumentPositionTax($this->getTaxType($item->tax_id), 'VAT', $item->tax_rate3); + $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate3); + $this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate3); } else { nlog("Can't add correct tax position"); } } else { if (!empty($this->invoice->tax_name1)) { - $globaltax = 0; - $xrechnung->addDocumentPositionTax($this->getTaxType($this->invoice->tax_name1), 'VAT', $this->invoice->tax_rate1); + $taxtype = $this->getTaxType($this->invoice->tax_name1); + $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate1); + $this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1); } elseif (!empty($this->invoice->tax_name2)) { - $globaltax = 1; - $xrechnung->addDocumentPositionTax($this->getTaxType($this->invoice->tax_name2), 'VAT', $this->invoice->tax_rate2); + $taxtype = $this->getTaxType($this->invoice->tax_name2); + $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate2); + $this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1); } elseif (!empty($this->invoice->tax_name3)) { - $globaltax = 2; - $xrechnung->addDocumentPositionTax($this->getTaxType($this->invoice->tax_name3), 'VAT', $item->tax_rate3); + $taxtype = $this->getTaxType($this->invoice->tax_name3); + $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate3); + $this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1); } else { nlog("Can't add correct tax position"); } @@ -238,5 +245,18 @@ class ZugferdEInvoice extends AbstractService } return $taxtype; } + private function addtoTaxMap(ZugferdDutyTaxFeeCategories $taxtype, float $netamount, float $taxrate){ + $hash = hash("md5", sprintf("%s%s", $taxtype, $taxrate), true); + if (array_key_exists($hash, $this->tax_map)){ + $this->tax_map[$hash]["netamount"] += $netamount; + } + else{ + $this->tax_map[$hash] = [ + "taxtype" => $taxtype, + "netamount" => $netamount, + "taxrate" => $taxrate + ]; + } + } } From 9634c99fb413212a9009b745459f8a6bbf040138 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Wed, 3 May 2023 16:11:24 +0200 Subject: [PATCH 2/6] Refactor ZuGFerd Tax calculation --- .../Invoice/EInvoice/ZugferdEInvoice.php | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php index a8de0f11c685..f45bbc604343 100644 --- a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php +++ b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php @@ -162,23 +162,9 @@ class ZugferdEInvoice extends AbstractService } - foreach ($invoicing_data->getTaxMap() as $item) { - - $tax_name = explode(" ", $item["name"]); - $tax_rate = (explode("%", end($tax_name))[0] / 100); - - $total_tax = $tax_rate == 0 ? 0 : $item["total"] / $tax_rate; - - $xrechnung->addDocumentTax($this->getTaxType(""), "VAT", $total_tax, $item["total"], explode("%", end($tax_name))[0]); - // TODO: Add correct tax type within getTaxType + foreach ($this->tax_map as $item){ + $xrechnung->addDocumentTax($item["tax_type"], "VAT", $item["net_amount"], $item["tax_rate"]*$item["net_amount"], $item["tax_rate"]); } - - if (!empty($globaltax && isset($invoicing_data->getTotalTaxMap()[$globaltax]["name"]))) { - $tax_name = explode(" ", $invoicing_data->getTotalTaxMap()[$globaltax]["name"]); - $xrechnung->addDocumentTax($this->getTaxType(""), "VAT", $invoicing_data->getTotalTaxMap()[$globaltax]["total"] / (explode("%", end($tax_name))[0] / 100), $invoicing_data->getTotalTaxMap()[$globaltax]["total"], explode("%", end($tax_name))[0]); - // TODO: Add correct tax type within getTaxType - } - $disk = config('filesystems.default'); if (!Storage::disk($disk)->exists($client->e_invoice_filepath($this->invoice->invitations->first()))) { @@ -248,13 +234,13 @@ class ZugferdEInvoice extends AbstractService private function addtoTaxMap(ZugferdDutyTaxFeeCategories $taxtype, float $netamount, float $taxrate){ $hash = hash("md5", sprintf("%s%s", $taxtype, $taxrate), true); if (array_key_exists($hash, $this->tax_map)){ - $this->tax_map[$hash]["netamount"] += $netamount; + $this->tax_map[$hash]["net_amount"] += $netamount; } else{ $this->tax_map[$hash] = [ - "taxtype" => $taxtype, - "netamount" => $netamount, - "taxrate" => $taxrate + "tax_type" => $taxtype, + "net_amount" => $netamount, + "tax_rate" => $taxrate ]; } } From 937036ddb36c30b3fec7d62090d1d118deb6c766 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Wed, 3 May 2023 16:19:28 +0200 Subject: [PATCH 3/6] Minor fixes --- .../Invoice/EInvoice/ZugferdEInvoice.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php index f45bbc604343..82faedd61864 100644 --- a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php +++ b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php @@ -195,44 +195,44 @@ class ZugferdEInvoice extends AbstractService private function getTaxType($name): string { - $taxtype = null; + $tax_type = null; switch ($name) { case Product::PRODUCT_TYPE_SERVICE: case Product::PRODUCT_TYPE_DIGITAL: case Product::PRODUCT_TYPE_PHYSICAL: case Product::PRODUCT_TYPE_SHIPPING: case Product::PRODUCT_TYPE_REDUCED_TAX: - $taxtype = ZugferdDutyTaxFeeCategories::STANDARD_RATE; + $tax_type = ZugferdDutyTaxFeeCategories::STANDARD_RATE; break; case Product::PRODUCT_TYPE_EXEMPT: - $taxtype = ZugferdDutyTaxFeeCategories::EXEMPT_FROM_TAX; + $tax_type = ZugferdDutyTaxFeeCategories::EXEMPT_FROM_TAX; break; case Product::PRODUCT_TYPE_ZERO_RATED: - $taxtype = ZugferdDutyTaxFeeCategories::ZERO_RATED_GOODS; + $tax_type = ZugferdDutyTaxFeeCategories::ZERO_RATED_GOODS; break; case Product::PRODUCT_TYPE_REVERSE_TAX: - $taxtype = ZugferdDutyTaxFeeCategories::VAT_REVERSE_CHARGE; + $tax_type = ZugferdDutyTaxFeeCategories::VAT_REVERSE_CHARGE; break; } $eu_states = ["AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "EL", "GR", "HU", "IE", "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "IS", "LI", "NO", "CH"]; - if (empty($taxtype)) { + if (empty($tax_type)) { if ((in_array($this->invoice->company->country()->iso_3166_2, $eu_states) && in_array($this->invoice->client->country->iso_3166_2, $eu_states)) && $this->invoice->company->country()->iso_3166_2 != $this->invoice->client->country->iso_3166_2) { - $taxtype = ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES; + $tax_type = ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES; } elseif (!in_array($this->invoice->client->country->iso_3166_2, $eu_states)) { - $taxtype = ZugferdDutyTaxFeeCategories::SERVICE_OUTSIDE_SCOPE_OF_TAX; + $tax_type = ZugferdDutyTaxFeeCategories::SERVICE_OUTSIDE_SCOPE_OF_TAX; } elseif ($this->invoice->client->country->iso_3166_2 == "ES-CN") { - $taxtype = ZugferdDutyTaxFeeCategories::CANARY_ISLANDS_GENERAL_INDIRECT_TAX; + $tax_type = ZugferdDutyTaxFeeCategories::CANARY_ISLANDS_GENERAL_INDIRECT_TAX; } elseif (in_array($this->invoice->client->country->iso_3166_2, ["ES-CE", "ES-ML"])) { - $taxtype = ZugferdDutyTaxFeeCategories::TAX_FOR_PRODUCTION_SERVICES_AND_IMPORTATION_IN_CEUTA_AND_MELILLA; + $tax_type = ZugferdDutyTaxFeeCategories::TAX_FOR_PRODUCTION_SERVICES_AND_IMPORTATION_IN_CEUTA_AND_MELILLA; } else { nlog("Unkown tax case for xinvoice"); - $taxtype = ZugferdDutyTaxFeeCategories::STANDARD_RATE; + $tax_type = ZugferdDutyTaxFeeCategories::STANDARD_RATE; } } - return $taxtype; + return $tax_type; } - private function addtoTaxMap(ZugferdDutyTaxFeeCategories $taxtype, float $netamount, float $taxrate){ - $hash = hash("md5", sprintf("%s%s", $taxtype, $taxrate), true); + private function addtoTaxMap(string $taxtype, float $netamount, float $taxrate){ + $hash = hash("md5", $taxtype."-".$taxrate, true); if (array_key_exists($hash, $this->tax_map)){ $this->tax_map[$hash]["net_amount"] += $netamount; } From cbf441adec6014ee18360b9b1ebc1b96e5aad243 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Wed, 3 May 2023 16:32:53 +0200 Subject: [PATCH 4/6] More fixes --- .../Invoice/EInvoice/ZugferdEInvoice.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php index 82faedd61864..51d8791ab713 100644 --- a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php +++ b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php @@ -85,7 +85,6 @@ class ZugferdEInvoice extends AbstractService } $invoicing_data = $this->invoice->calc(); - $globaltax = null; //Create line items and calculate taxes foreach ($this->invoice->line_items as $index => $item) { @@ -139,15 +138,15 @@ class ZugferdEInvoice extends AbstractService if (!empty($this->invoice->tax_name1)) { $taxtype = $this->getTaxType($this->invoice->tax_name1); $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate1); - $this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1); + $this->addtoTaxMap($taxtype, $linenetamount, $this->invoice->tax_rate1); } elseif (!empty($this->invoice->tax_name2)) { $taxtype = $this->getTaxType($this->invoice->tax_name2); $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate2); - $this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1); + $this->addtoTaxMap($taxtype, $linenetamount, $this->invoice->tax_rate2); } elseif (!empty($this->invoice->tax_name3)) { $taxtype = $this->getTaxType($this->invoice->tax_name3); - $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate3); - $this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1); + $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate3); + $this->addtoTaxMap($taxtype, $linenetamount, $this->invoice->tax_rate3); } else { nlog("Can't add correct tax position"); } @@ -156,14 +155,14 @@ class ZugferdEInvoice extends AbstractService if ($this->invoice->isPartial()) { - $xrechnung->setDocumentSummation($this->invoice->amount, $this->invoice->amount-$this->invoice->balance, $invoicing_data->getSubTotal(), $invoicing_data->getTotalSurcharges(), $invoicing_data->getTotalDiscount(), $invoicing_data->getSubTotal(), $invoicing_data->getItemTotalTaxes(), null, $this->invoice->partial); + $xrechnung->setDocumentSummation($this->invoice->amount, $this->invoice->balance, $invoicing_data->getSubTotal(), $invoicing_data->getTotalSurcharges(), $invoicing_data->getTotalDiscount(), $invoicing_data->getSubTotal(), $invoicing_data->getItemTotalTaxes(), null, $this->invoice->partial); } else { - $xrechnung->setDocumentSummation($this->invoice->amount, $this->invoice->amount-$this->invoice->balance, $invoicing_data->getSubTotal(), $invoicing_data->getTotalSurcharges(), $invoicing_data->getTotalDiscount(), $invoicing_data->getSubTotal(), $invoicing_data->getItemTotalTaxes(), null, 0.0); + $xrechnung->setDocumentSummation($this->invoice->amount, $this->invoice->balance, $invoicing_data->getSubTotal(), $invoicing_data->getTotalSurcharges(), $invoicing_data->getTotalDiscount(), $invoicing_data->getSubTotal(), $invoicing_data->getItemTotalTaxes(), null, 0.0); } foreach ($this->tax_map as $item){ - $xrechnung->addDocumentTax($item["tax_type"], "VAT", $item["net_amount"], $item["tax_rate"]*$item["net_amount"], $item["tax_rate"]); + $xrechnung->addDocumentTax($item["tax_type"], "VAT", $item["net_amount"], $item["tax_rate"]*$item["net_amount"], $item["tax_rate"]*100); } $disk = config('filesystems.default'); @@ -240,7 +239,7 @@ class ZugferdEInvoice extends AbstractService $this->tax_map[$hash] = [ "tax_type" => $taxtype, "net_amount" => $netamount, - "tax_rate" => $taxrate + "tax_rate" => $taxrate / 100 ]; } } From f74450765411f342b69be25294d7fa68720f79ea Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Wed, 3 May 2023 16:38:42 +0200 Subject: [PATCH 5/6] Speed improvements --- app/Services/Invoice/EInvoice/ZugferdEInvoice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php index 51d8791ab713..af01c174ea15 100644 --- a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php +++ b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php @@ -231,7 +231,7 @@ class ZugferdEInvoice extends AbstractService return $tax_type; } private function addtoTaxMap(string $taxtype, float $netamount, float $taxrate){ - $hash = hash("md5", $taxtype."-".$taxrate, true); + $hash = hash("md5", $taxtype."-".$taxrate); if (array_key_exists($hash, $this->tax_map)){ $this->tax_map[$hash]["net_amount"] += $netamount; } From 1573cb7a25063d1f598e73b32c3e9935bc5c181e Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Wed, 3 May 2023 16:41:38 +0200 Subject: [PATCH 6/6] Minor fix --- app/Services/Invoice/EInvoice/ZugferdEInvoice.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php index af01c174ea15..1dcb7b7cb514 100644 --- a/app/Services/Invoice/EInvoice/ZugferdEInvoice.php +++ b/app/Services/Invoice/EInvoice/ZugferdEInvoice.php @@ -148,6 +148,9 @@ class ZugferdEInvoice extends AbstractService $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate3); $this->addtoTaxMap($taxtype, $linenetamount, $this->invoice->tax_rate3); } else { + $taxtype = ZugferdDutyTaxFeeCategories::ZERO_RATED_GOODS; + $xrechnung->addDocumentPositionTax($taxtype, 'VAT', 0); + $this->addtoTaxMap($taxtype, $linenetamount, 0); nlog("Can't add correct tax position"); } } @@ -230,16 +233,16 @@ class ZugferdEInvoice extends AbstractService } return $tax_type; } - private function addtoTaxMap(string $taxtype, float $netamount, float $taxrate){ - $hash = hash("md5", $taxtype."-".$taxrate); + private function addtoTaxMap(string $tax_type, float $net_amount, float $tax_rate){ + $hash = hash("md5", $tax_type."-".$tax_rate); if (array_key_exists($hash, $this->tax_map)){ - $this->tax_map[$hash]["net_amount"] += $netamount; + $this->tax_map[$hash]["net_amount"] += $net_amount; } else{ $this->tax_map[$hash] = [ - "tax_type" => $taxtype, - "net_amount" => $netamount, - "tax_rate" => $taxrate / 100 + "tax_type" => $tax_type, + "net_amount" => $net_amount, + "tax_rate" => $tax_rate / 100 ]; } }