mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Adjust Zugferd class to new types
This commit is contained in:
parent
99a211b823
commit
bcd6148222
@ -11,6 +11,7 @@
|
||||
|
||||
namespace App\Jobs\EDocument;
|
||||
|
||||
use App\Models\Credit;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Models\Quote;
|
||||
@ -70,16 +71,16 @@ class CreateEDocument implements ShouldQueue
|
||||
case "XInvoice-Extended":
|
||||
case "XInvoice-BasicWL":
|
||||
case "XInvoice-Basic":
|
||||
$zugferd = (new ZugferdEDokument($this->invoice))->run();
|
||||
$zugferd = (new ZugferdEDokument($this->document))->run();
|
||||
|
||||
return $this->returnObject ? $zugferd->xrechnung : $zugferd->getXml();
|
||||
return $this->returnObject ? $zugferd->xdocument : $zugferd->getXml();
|
||||
case "Facturae_3.2":
|
||||
case "Facturae_3.2.1":
|
||||
case "Facturae_3.2.2":
|
||||
return (new FacturaEInvoice($this->invoice, str_replace("Facturae_", "", $e_invoice_type)))->run();
|
||||
return (new FacturaEInvoice($this->document, str_replace("Facturae_", "", $e_document_type)))->run();
|
||||
default:
|
||||
|
||||
$zugferd = (new ZugferdEDokument($this->invoice))->run();
|
||||
$zugferd = (new ZugferdEDokument($this->document))->run();
|
||||
|
||||
return $this->returnObject ? $zugferd : $zugferd->getXml();
|
||||
|
||||
@ -97,15 +98,22 @@ class CreateEDocument implements ShouldQueue
|
||||
case "XInvoice-Extended":
|
||||
case "XInvoice-BasicWL":
|
||||
case "XInvoice-Basic":
|
||||
$zugferd = (new ZugferdEDokument($this->invoice))->run();
|
||||
return $this->returnObject ? $zugferd->xrechnung : $zugferd->getXml();
|
||||
$zugferd = (new ZugferdEDokument($this->document))->run();
|
||||
return $this->returnObject ? $zugferd->xdocument : $zugferd->getXml();
|
||||
default:
|
||||
$zugferd = (new ZugferdEDokument($this->invoice))->run();
|
||||
$zugferd = (new ZugferdEDokument($this->document))->run();
|
||||
return $this->returnObject ? $zugferd : $zugferd->getXml();
|
||||
}
|
||||
}
|
||||
elseif ($this->document instanceof PurchaseOrder){
|
||||
switch ($e_document_type){
|
||||
// No supported implementation yet.
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
elseif ($this->document instanceof Credit) {
|
||||
switch ($e_document_type) {
|
||||
case "EN16931":
|
||||
case "XInvoice_3_0":
|
||||
case "XInvoice_2_3":
|
||||
@ -116,10 +124,10 @@ class CreateEDocument implements ShouldQueue
|
||||
case "XInvoice-Extended":
|
||||
case "XInvoice-BasicWL":
|
||||
case "XInvoice-Basic":
|
||||
$zugferd = (new ZugferdEDokument($this->invoice))->run();
|
||||
return $this->returnObject ? $zugferd->xrechnung : $zugferd->getXml();
|
||||
$zugferd = (new ZugferdEDokument($this->document))->run();
|
||||
return $this->returnObject ? $zugferd->xdocument : $zugferd->getXml();
|
||||
default:
|
||||
$zugferd = (new ZugferdEDokument($this->invoice))->run();
|
||||
$zugferd = (new ZugferdEDokument($this->document))->run();
|
||||
return $this->returnObject ? $zugferd : $zugferd->getXml();
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,11 @@
|
||||
|
||||
namespace App\Services\EInvoicing\Standards;
|
||||
|
||||
use App\Models\Credit;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Product;
|
||||
use App\Models\PurchaseOrder;
|
||||
use App\Models\Quote;
|
||||
use App\Services\AbstractService;
|
||||
use horstoeko\zugferd\codelists\ZugferdDutyTaxFeeCategories;
|
||||
use horstoeko\zugferd\ZugferdDocumentBuilder;
|
||||
@ -20,17 +23,17 @@ use horstoeko\zugferd\ZugferdProfiles;
|
||||
|
||||
class ZugferdEDokument extends AbstractService
|
||||
{
|
||||
public ZugferdDocumentBuilder $xrechnung;
|
||||
public ZugferdDocumentBuilder $xdocument;
|
||||
|
||||
public function __construct(public Invoice $invoice, private readonly bool $returnObject = false, private array $tax_map = [])
|
||||
public function __construct(public object $document, private readonly bool $returnObject = false, private array $tax_map = [])
|
||||
{
|
||||
}
|
||||
|
||||
public function run(): self
|
||||
{
|
||||
|
||||
$company = $this->invoice->company;
|
||||
$client = $this->invoice->client;
|
||||
$company = $this->document->company;
|
||||
$client = $this->document->client;
|
||||
$profile = $client->getSetting('e_invoice_type');
|
||||
|
||||
$profile = match ($profile) {
|
||||
@ -46,123 +49,143 @@ class ZugferdEDokument extends AbstractService
|
||||
default => ZugferdProfiles::PROFILE_EN16931,
|
||||
};
|
||||
|
||||
$this->xrechnung = ZugferdDocumentBuilder::CreateNew($profile);
|
||||
$this->xdocument = ZugferdDocumentBuilder::CreateNew($profile);
|
||||
|
||||
$this->xrechnung
|
||||
->setDocumentSupplyChainEvent(date_create($this->invoice->date ?? now()->format('Y-m-d')))
|
||||
$this->xdocument
|
||||
->setDocumentSupplyChainEvent(date_create($this->document->date ?? now()->format('Y-m-d')))
|
||||
->setDocumentSeller($company->getSetting('name'))
|
||||
->setDocumentSellerAddress($company->getSetting("address1"), $company->getSetting("address2"), "", $company->getSetting("postal_code"), $company->getSetting("city"), $company->country()->iso_3166_2, $company->getSetting("state"))
|
||||
->setDocumentSellerContact($this->invoice->user->present()->getFullName(), "", $this->invoice->user->present()->phone(), "", $this->invoice->user->email)
|
||||
->setDocumentSellerContact($this->document->user->present()->getFullName(), "", $this->document->user->present()->phone(), "", $this->document->user->email)
|
||||
->setDocumentBuyer($client->present()->name(), $client->number)
|
||||
->setDocumentBuyerAddress($client->address1, "", "", $client->postal_code, $client->city, $client->country->iso_3166_2, $client->state)
|
||||
->setDocumentBuyerContact($client->present()->primary_contact_name(), "", $client->present()->phone(), "", $client->present()->email())
|
||||
->addDocumentPaymentTerm(ctrans("texts.xinvoice_payable", ['payeddue' => date_create($this->invoice->date ?? now()->format('Y-m-d'))->diff(date_create($this->invoice->due_date ?? now()->format('Y-m-d')))->format("%d"), 'paydate' => $this->invoice->due_date]));
|
||||
->addDocumentPaymentTerm(ctrans("texts.xinvoice_payable", ['payeddue' => date_create($this->document->date ?? now()->format('Y-m-d'))->diff(date_create($this->document->due_date ?? now()->format('Y-m-d')))->format("%d"), 'paydate' => $this->document->due_date]));
|
||||
|
||||
if (!empty($this->invoice->public_notes)) {
|
||||
$this->xrechnung->addDocumentNote($this->invoice->public_notes ?? '');
|
||||
if (!empty($this->document->public_notes)) {
|
||||
$this->xdocument->addDocumentNote($this->document->public_notes ?? '');
|
||||
}
|
||||
if (empty($this->invoice->number)) {
|
||||
$this->xrechnung->setDocumentInformation("DRAFT", "380", date_create($this->invoice->date ?? now()->format('Y-m-d')), $client->getCurrencyCode());
|
||||
} else {
|
||||
$this->xrechnung->setDocumentInformation($this->invoice->number, "380", date_create($this->invoice->date ?? now()->format('Y-m-d')), $client->getCurrencyCode());
|
||||
// Document type
|
||||
$document_class = get_class($this->document);
|
||||
switch ($document_class){
|
||||
case Quote::class:
|
||||
// Probably wrong file code https://github.com/horstoeko/zugferd/blob/master/src/codelists/ZugferdInvoiceType.php
|
||||
if (empty($this->document->number)) {
|
||||
$this->xdocument->setDocumentInformation("DRAFT", "84", date_create($this->document->date ?? now()->format('Y-m-d')), $client->getCurrencyCode());
|
||||
} else {
|
||||
$this->xdocument->setDocumentInformation($this->document->number, "84", date_create($this->document->date ?? now()->format('Y-m-d')), $client->getCurrencyCode());
|
||||
};
|
||||
break;
|
||||
case Invoice::class:
|
||||
if (empty($this->document->number)) {
|
||||
$this->xdocument->setDocumentInformation("DRAFT", "380", date_create($this->document->date ?? now()->format('Y-m-d')), $client->getCurrencyCode());
|
||||
} else {
|
||||
$this->xdocument->setDocumentInformation($this->document->number, "380", date_create($this->document->date ?? now()->format('Y-m-d')), $client->getCurrencyCode());
|
||||
}
|
||||
break;
|
||||
case Credit::class:
|
||||
if (empty($this->document->number)) {
|
||||
$this->xdocument->setDocumentInformation("DRAFT", "389", date_create($this->document->date ?? now()->format('Y-m-d')), $client->getCurrencyCode());
|
||||
} else {
|
||||
$this->xdocument->setDocumentInformation($this->document->number, "389", date_create($this->document->date ?? now()->format('Y-m-d')), $client->getCurrencyCode());
|
||||
}
|
||||
}
|
||||
if (isset($this->invoice->po_number)) {
|
||||
$this->xrechnung->setDocumentBuyerOrderReferencedDocument($this->invoice->po_number);
|
||||
if (isset($this->document->po_number)) {
|
||||
$this->xdocument->setDocumentBuyerOrderReferencedDocument($this->document->po_number);
|
||||
}
|
||||
|
||||
if (empty($client->routing_id)) {
|
||||
$this->xrechnung->setDocumentBuyerReference(ctrans("texts.xinvoice_no_buyers_reference"));
|
||||
$this->xdocument->setDocumentBuyerReference(ctrans("texts.xinvoice_no_buyers_reference"));
|
||||
} else {
|
||||
$this->xrechnung->setDocumentBuyerReference($client->routing_id);
|
||||
$this->xdocument->setDocumentBuyerReference($client->routing_id);
|
||||
}
|
||||
if (isset($client->shipping_address1) && $client->shipping_country) {
|
||||
$this->xrechnung->setDocumentShipToAddress($client->shipping_address1, $client->shipping_address2, "", $client->shipping_postal_code, $client->shipping_city, $client->shipping_country->iso_3166_2, $client->shipping_state);
|
||||
$this->xdocument->setDocumentShipToAddress($client->shipping_address1, $client->shipping_address2, "", $client->shipping_postal_code, $client->shipping_city, $client->shipping_country->iso_3166_2, $client->shipping_state);
|
||||
}
|
||||
|
||||
$this->xrechnung->addDocumentPaymentMean(68, ctrans("texts.xinvoice_online_payment"));
|
||||
$this->xdocument->addDocumentPaymentMean(68, ctrans("texts.xinvoice_online_payment"));
|
||||
|
||||
if (str_contains($company->getSetting('vat_number'), "/")) {
|
||||
$this->xrechnung->addDocumentSellerTaxRegistration("FC", $company->getSetting('vat_number'));
|
||||
$this->xdocument->addDocumentSellerTaxRegistration("FC", $company->getSetting('vat_number'));
|
||||
} else {
|
||||
$this->xrechnung->addDocumentSellerTaxRegistration("VA", $company->getSetting('vat_number'));
|
||||
$this->xdocument->addDocumentSellerTaxRegistration("VA", $company->getSetting('vat_number'));
|
||||
}
|
||||
|
||||
$invoicing_data = $this->invoice->calc();
|
||||
$invoicing_data = $this->document->calc();
|
||||
|
||||
//Create line items and calculate taxes
|
||||
foreach ($this->invoice->line_items as $index => $item) {
|
||||
foreach ($this->document->line_items as $index => $item) {
|
||||
/** @var \App\DataMapper\InvoiceItem $item **/
|
||||
$this->xrechnung->addNewPosition($index)
|
||||
$this->xdocument->addNewPosition($index)
|
||||
->setDocumentPositionGrossPrice($item->gross_line_total)
|
||||
->setDocumentPositionNetPrice($item->line_total);
|
||||
if (!empty($item->product_key)) {
|
||||
if (!empty($item->notes)) {
|
||||
$this->xrechnung->setDocumentPositionProductDetails($item->product_key, $item->notes);
|
||||
$this->xdocument->setDocumentPositionProductDetails($item->product_key, $item->notes);
|
||||
} else {
|
||||
$this->xrechnung->setDocumentPositionProductDetails($item->product_key);
|
||||
$this->xdocument->setDocumentPositionProductDetails($item->product_key);
|
||||
}
|
||||
} else {
|
||||
if (!empty($item->notes)) {
|
||||
$this->xrechnung->setDocumentPositionProductDetails($item->notes);
|
||||
$this->xdocument->setDocumentPositionProductDetails($item->notes);
|
||||
} else {
|
||||
$this->xrechnung->setDocumentPositionProductDetails("no product name defined");
|
||||
$this->xdocument->setDocumentPositionProductDetails("no product name defined");
|
||||
}
|
||||
}
|
||||
if (isset($item->task_id)) {
|
||||
$this->xrechnung->setDocumentPositionQuantity($item->quantity, "HUR");
|
||||
$this->xdocument->setDocumentPositionQuantity($item->quantity, "HUR");
|
||||
} else {
|
||||
$this->xrechnung->setDocumentPositionQuantity($item->quantity, "H87");
|
||||
$this->xdocument->setDocumentPositionQuantity($item->quantity, "H87");
|
||||
}
|
||||
$linenetamount = $item->line_total;
|
||||
if ($item->discount > 0) {
|
||||
if ($this->invoice->is_amount_discount) {
|
||||
if ($this->document->is_amount_discount) {
|
||||
$linenetamount -= $item->discount;
|
||||
} else {
|
||||
$linenetamount -= $linenetamount * ($item->discount / 100);
|
||||
}
|
||||
}
|
||||
$this->xrechnung->setDocumentPositionLineSummation($linenetamount);
|
||||
$this->xdocument->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)) {
|
||||
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate1);
|
||||
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate1);
|
||||
$this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1);
|
||||
} elseif (!empty($item->tax_name2)) {
|
||||
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate2);
|
||||
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate2);
|
||||
$this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate2);
|
||||
} elseif (!empty($item->tax_name3)) {
|
||||
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate3);
|
||||
$this->xdocument->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)) {
|
||||
$taxtype = $this->getTaxType($this->invoice->tax_name1);
|
||||
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate1);
|
||||
$this->addtoTaxMap($taxtype, $linenetamount, $this->invoice->tax_rate1);
|
||||
} elseif (!empty($this->invoice->tax_name2)) {
|
||||
$taxtype = $this->getTaxType($this->invoice->tax_name2);
|
||||
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate2);
|
||||
$this->addtoTaxMap($taxtype, $linenetamount, $this->invoice->tax_rate2);
|
||||
} elseif (!empty($this->invoice->tax_name3)) {
|
||||
$taxtype = $this->getTaxType($this->invoice->tax_name3);
|
||||
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate3);
|
||||
$this->addtoTaxMap($taxtype, $linenetamount, $this->invoice->tax_rate3);
|
||||
if (!empty($this->document->tax_name1)) {
|
||||
$taxtype = $this->getTaxType($this->document->tax_name1);
|
||||
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $this->document->tax_rate1);
|
||||
$this->addtoTaxMap($taxtype, $linenetamount, $this->document->tax_rate1);
|
||||
} elseif (!empty($this->document->tax_name2)) {
|
||||
$taxtype = $this->getTaxType($this->document->tax_name2);
|
||||
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $this->document->tax_rate2);
|
||||
$this->addtoTaxMap($taxtype, $linenetamount, $this->document->tax_rate2);
|
||||
} elseif (!empty($this->document->tax_name3)) {
|
||||
$taxtype = $this->getTaxType($this->document->tax_name3);
|
||||
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $this->document->tax_rate3);
|
||||
$this->addtoTaxMap($taxtype, $linenetamount, $this->document->tax_rate3);
|
||||
} else {
|
||||
$taxtype = ZugferdDutyTaxFeeCategories::ZERO_RATED_GOODS;
|
||||
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', 0);
|
||||
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', 0);
|
||||
$this->addtoTaxMap($taxtype, $linenetamount, 0);
|
||||
// nlog("Can't add correct tax position");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->xrechnung->setDocumentSummation($this->invoice->amount, $this->invoice->balance, $invoicing_data->getSubTotal(), $invoicing_data->getTotalSurcharges(), $invoicing_data->getTotalDiscount(), $invoicing_data->getSubTotal(), $invoicing_data->getItemTotalTaxes(), 0.0, $this->invoice->amount - $this->invoice->balance);
|
||||
$this->xdocument->setDocumentSummation($this->document->amount, $this->document->balance, $invoicing_data->getSubTotal(), $invoicing_data->getTotalSurcharges(), $invoicing_data->getTotalDiscount(), $invoicing_data->getSubTotal(), $invoicing_data->getItemTotalTaxes(), 0.0, $this->document->amount - $this->document->balance);
|
||||
|
||||
foreach ($this->tax_map as $item) {
|
||||
$this->xrechnung->addDocumentTax($item["tax_type"], "VAT", $item["net_amount"], $item["tax_rate"] * $item["net_amount"], $item["tax_rate"] * 100);
|
||||
$this->xdocument->addDocumentTax($item["tax_type"], "VAT", $item["net_amount"], $item["tax_rate"] * $item["net_amount"], $item["tax_rate"] * 100);
|
||||
}
|
||||
|
||||
// The validity can be checked using https://portal3.gefeg.com/invoice/validation or https://e-rechnung.bayern.de/app/#/upload
|
||||
@ -178,7 +201,7 @@ class ZugferdEDokument extends AbstractService
|
||||
*/
|
||||
public function getXml(): string
|
||||
{
|
||||
return $this->xrechnung->getContent();
|
||||
return $this->xdocument->getContent();
|
||||
}
|
||||
|
||||
private function getTaxType($name): string
|
||||
@ -204,13 +227,13 @@ class ZugferdEDokument extends AbstractService
|
||||
}
|
||||
$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($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) {
|
||||
if ((in_array($this->document->company->country()->iso_3166_2, $eu_states) && in_array($this->document->client->country->iso_3166_2, $eu_states)) && $this->document->company->country()->iso_3166_2 != $this->document->client->country->iso_3166_2) {
|
||||
$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)) {
|
||||
} elseif (!in_array($this->document->client->country->iso_3166_2, $eu_states)) {
|
||||
$tax_type = ZugferdDutyTaxFeeCategories::SERVICE_OUTSIDE_SCOPE_OF_TAX;
|
||||
} elseif ($this->invoice->client->country->iso_3166_2 == "ES-CN") {
|
||||
} elseif ($this->document->client->country->iso_3166_2 == "ES-CN") {
|
||||
$tax_type = ZugferdDutyTaxFeeCategories::CANARY_ISLANDS_GENERAL_INDIRECT_TAX;
|
||||
} elseif (in_array($this->invoice->client->country->iso_3166_2, ["ES-CE", "ES-ML"])) {
|
||||
} elseif (in_array($this->document->client->country->iso_3166_2, ["ES-CE", "ES-ML"])) {
|
||||
$tax_type = ZugferdDutyTaxFeeCategories::TAX_FOR_PRODUCTION_SERVICES_AND_IMPORTATION_IN_CEUTA_AND_MELILLA;
|
||||
} else {
|
||||
nlog("Unkown tax case for xinvoice");
|
||||
|
Loading…
x
Reference in New Issue
Block a user