Refactor ZuGFerd Tax calculation

This commit is contained in:
Lars Kusch 2023-05-03 16:05:59 +02:00
parent 3b7e01db8b
commit 3fe7bb8990

View File

@ -23,7 +23,7 @@ use horstoeko\zugferd\codelists\ZugferdDutyTaxFeeCategories;
class ZugferdEInvoice extends AbstractService 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); $xrechnung->setDocumentPositionLineSummation($linenetamount);
// According to european law, each line item can only have one tax rate // 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))) { if (!(empty($item->tax_name1) && empty($item->tax_name2) && empty($item->tax_name3))) {
$taxtype = $this->getTaxType($item->tax_id);
if (!empty($item->tax_name1)) { 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)) { } 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)) { } 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 { } else {
nlog("Can't add correct tax position"); nlog("Can't add correct tax position");
} }
} else { } else {
if (!empty($this->invoice->tax_name1)) { if (!empty($this->invoice->tax_name1)) {
$globaltax = 0; $taxtype = $this->getTaxType($this->invoice->tax_name1);
$xrechnung->addDocumentPositionTax($this->getTaxType($this->invoice->tax_name1), 'VAT', $this->invoice->tax_rate1); $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate1);
$this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1);
} elseif (!empty($this->invoice->tax_name2)) { } elseif (!empty($this->invoice->tax_name2)) {
$globaltax = 1; $taxtype = $this->getTaxType($this->invoice->tax_name2);
$xrechnung->addDocumentPositionTax($this->getTaxType($this->invoice->tax_name2), 'VAT', $this->invoice->tax_rate2); $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate2);
$this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1);
} elseif (!empty($this->invoice->tax_name3)) { } elseif (!empty($this->invoice->tax_name3)) {
$globaltax = 2; $taxtype = $this->getTaxType($this->invoice->tax_name3);
$xrechnung->addDocumentPositionTax($this->getTaxType($this->invoice->tax_name3), 'VAT', $item->tax_rate3); $xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate3);
$this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1);
} else { } else {
nlog("Can't add correct tax position"); nlog("Can't add correct tax position");
} }
@ -238,5 +245,18 @@ class ZugferdEInvoice extends AbstractService
} }
return $taxtype; 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
];
}
}
} }