2023-04-21 07:18:17 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice\EInvoice;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Product;
|
|
|
|
use App\Services\AbstractService;
|
|
|
|
use horstoeko\zugferd\ZugferdProfiles;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use horstoeko\zugferd\ZugferdDocumentBuilder;
|
|
|
|
use horstoeko\zugferd\ZugferdDocumentPdfBuilder;
|
|
|
|
use horstoeko\zugferd\codelists\ZugferdDutyTaxFeeCategories;
|
|
|
|
|
|
|
|
class ZugferdEInvoice extends AbstractService
|
|
|
|
{
|
2023-08-16 11:55:35 +02:00
|
|
|
public ZugferdDocumentBuilder $xrechnung;
|
2023-04-24 07:50:45 +02:00
|
|
|
|
2023-08-14 10:22:54 +02:00
|
|
|
public function __construct(public Invoice $invoice, private readonly bool $returnObject = false, private array $tax_map = [])
|
2023-04-21 07:18:17 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-08-16 11:55:35 +02:00
|
|
|
public function run(): self
|
2023-04-21 07:18:17 +02:00
|
|
|
{
|
2023-04-24 07:50:45 +02:00
|
|
|
|
2023-04-21 07:18:17 +02:00
|
|
|
$company = $this->invoice->company;
|
|
|
|
$client = $this->invoice->client;
|
|
|
|
$profile = $client->getSetting('e_invoice_type');
|
|
|
|
|
2023-04-24 07:50:45 +02:00
|
|
|
$profile = match ($profile) {
|
|
|
|
"XInvoice_2_2" => ZugferdProfiles::PROFILE_XRECHNUNG_2_2,
|
|
|
|
"XInvoice_2_1" => ZugferdProfiles::PROFILE_XRECHNUNG_2_1,
|
|
|
|
"XInvoice_2_0" => ZugferdProfiles::PROFILE_XRECHNUNG_2,
|
|
|
|
"XInvoice_1_0" => ZugferdProfiles::PROFILE_XRECHNUNG,
|
|
|
|
"XInvoice-Extended" => ZugferdProfiles::PROFILE_EXTENDED,
|
|
|
|
"XInvoice-BasicWL" => ZugferdProfiles::PROFILE_BASICWL,
|
|
|
|
"XInvoice-Basic" => ZugferdProfiles::PROFILE_BASIC,
|
|
|
|
default => ZugferdProfiles::PROFILE_EN16931,
|
|
|
|
};
|
2023-04-21 07:18:17 +02:00
|
|
|
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung = ZugferdDocumentBuilder::CreateNew($profile);
|
2023-04-21 07:18:17 +02:00
|
|
|
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung
|
2023-04-21 07:18:17 +02:00
|
|
|
->setDocumentSupplyChainEvent(date_create($this->invoice->date))
|
|
|
|
->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->first_name." ".$this->invoice->user->last_name, "", $this->invoice->user->phone, "", $this->invoice->user->email)
|
|
|
|
->setDocumentBuyer($client->name, $client->number)
|
2023-05-17 19:01:12 +02:00
|
|
|
->setDocumentBuyerAddress($client->address1, "", "", $client->postal_code, $client->city, $client->country->iso_3166_2, $client->state)
|
2023-04-21 07:18:17 +02:00
|
|
|
->setDocumentBuyerContact($client->primary_contact()->first()->first_name . " " . $client->primary_contact()->first()->last_name, "", $client->primary_contact()->first()->phone, "", $client->primary_contact()->first()->email)
|
|
|
|
->addDocumentPaymentTerm(ctrans("texts.xinvoice_payable", ['payeddue' => date_create($this->invoice->date)->diff(date_create($this->invoice->due_date))->format("%d"), 'paydate' => $this->invoice->due_date]));
|
2023-04-24 07:50:45 +02:00
|
|
|
|
2023-04-21 07:44:11 +02:00
|
|
|
if (!empty($this->invoice->public_notes)) {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentNote($this->invoice->public_notes);
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
2023-05-17 19:01:12 +02:00
|
|
|
if (empty($this->invoice->number)){
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentInformation("DRAFT", "380", date_create($this->invoice->date), $this->invoice->client->getCurrencyCode());
|
2023-05-17 19:01:12 +02:00
|
|
|
} else {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentInformation($this->invoice->number, "380", date_create($this->invoice->date), $this->invoice->client->getCurrencyCode());
|
2023-05-17 19:01:12 +02:00
|
|
|
}
|
2023-04-21 07:18:17 +02:00
|
|
|
if (!empty($this->invoice->po_number)) {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentBuyerOrderReferencedDocument($this->invoice->po_number);
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
2023-04-24 07:50:45 +02:00
|
|
|
|
2023-04-21 07:18:17 +02:00
|
|
|
if (empty($client->routing_id)) {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentBuyerReference(ctrans("texts.xinvoice_no_buyers_reference"));
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentBuyerReference($client->routing_id);
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
2023-04-24 07:50:45 +02:00
|
|
|
if (!empty($client->shipping_address1)){
|
2023-08-16 11:55:35 +02:00
|
|
|
$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);
|
2023-04-24 07:50:45 +02:00
|
|
|
}
|
|
|
|
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentPaymentMean(68, ctrans("texts.xinvoice_online_payment"));
|
2023-04-21 07:18:17 +02:00
|
|
|
|
|
|
|
if (str_contains($company->getSetting('vat_number'), "/")) {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentSellerTaxRegistration("FC", $company->getSetting('vat_number'));
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentSellerTaxRegistration("VA", $company->getSetting('vat_number'));
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$invoicing_data = $this->invoice->calc();
|
|
|
|
|
|
|
|
//Create line items and calculate taxes
|
|
|
|
foreach ($this->invoice->line_items as $index => $item) {
|
2023-08-06 03:44:52 +02:00
|
|
|
/** @var \App\DataMapper\InvoiceItem $item **/
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addNewPosition($index)
|
2023-04-21 07:18:17 +02:00
|
|
|
->setDocumentPositionGrossPrice($item->gross_line_total)
|
|
|
|
->setDocumentPositionNetPrice($item->line_total);
|
2023-04-28 11:27:51 +02:00
|
|
|
if (!empty($item->product_key)){
|
|
|
|
if (!empty($item->notes)){
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentPositionProductDetails($item->product_key, $item->notes);
|
2023-04-28 11:27:51 +02:00
|
|
|
}
|
2023-07-03 19:31:54 +02:00
|
|
|
else {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentPositionProductDetails($item->product_key);
|
2023-07-03 19:31:54 +02:00
|
|
|
}
|
2023-04-28 11:27:51 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!empty($item->notes)){
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentPositionProductDetails($item->notes);
|
2023-04-28 11:27:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentPositionProductDetails("no product name defined");
|
2023-04-28 11:27:51 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-21 07:18:17 +02:00
|
|
|
if (isset($item->task_id)) {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentPositionQuantity($item->quantity, "HUR");
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentPositionQuantity($item->quantity, "H87");
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
|
|
|
$linenetamount = $item->line_total;
|
|
|
|
if ($item->discount > 0) {
|
|
|
|
if ($this->invoice->is_amount_discount) {
|
|
|
|
$linenetamount -= $item->discount;
|
|
|
|
} else {
|
|
|
|
$linenetamount -= $linenetamount * ($item->discount / 100);
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->setDocumentPositionLineSummation($linenetamount);
|
2023-04-21 07:18:17 +02:00
|
|
|
// 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))) {
|
2023-05-03 16:05:59 +02:00
|
|
|
$taxtype = $this->getTaxType($item->tax_id);
|
2023-04-21 07:18:17 +02:00
|
|
|
if (!empty($item->tax_name1)) {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate1);
|
2023-05-03 16:05:59 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate1);
|
2023-04-21 07:18:17 +02:00
|
|
|
} elseif (!empty($item->tax_name2)) {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate2);
|
2023-05-03 16:05:59 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate2);
|
2023-04-21 07:18:17 +02:00
|
|
|
} elseif (!empty($item->tax_name3)) {
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate3);
|
2023-05-03 16:05:59 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $linenetamount, $item->tax_rate3);
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
|
|
|
nlog("Can't add correct tax position");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!empty($this->invoice->tax_name1)) {
|
2023-05-03 16:05:59 +02:00
|
|
|
$taxtype = $this->getTaxType($this->invoice->tax_name1);
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate1);
|
2023-05-03 16:32:53 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $linenetamount, $this->invoice->tax_rate1);
|
2023-04-21 07:18:17 +02:00
|
|
|
} elseif (!empty($this->invoice->tax_name2)) {
|
2023-05-03 16:05:59 +02:00
|
|
|
$taxtype = $this->getTaxType($this->invoice->tax_name2);
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate2);
|
2023-05-03 16:32:53 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $linenetamount, $this->invoice->tax_rate2);
|
2023-04-21 07:18:17 +02:00
|
|
|
} elseif (!empty($this->invoice->tax_name3)) {
|
2023-05-03 16:05:59 +02:00
|
|
|
$taxtype = $this->getTaxType($this->invoice->tax_name3);
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', $this->invoice->tax_rate3);
|
2023-05-03 16:32:53 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $linenetamount, $this->invoice->tax_rate3);
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
2023-05-03 16:41:38 +02:00
|
|
|
$taxtype = ZugferdDutyTaxFeeCategories::ZERO_RATED_GOODS;
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentPositionTax($taxtype, 'VAT', 0);
|
2023-05-03 16:41:38 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $linenetamount, 0);
|
2023-04-21 07:18:17 +02:00
|
|
|
nlog("Can't add correct tax position");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 11:55:35 +02:00
|
|
|
$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);
|
2023-04-24 00:22:49 +02:00
|
|
|
|
2023-05-03 16:11:24 +02:00
|
|
|
foreach ($this->tax_map as $item){
|
2023-08-16 11:55:35 +02:00
|
|
|
$this->xrechnung->addDocumentTax($item["tax_type"], "VAT", $item["net_amount"], $item["tax_rate"]*$item["net_amount"], $item["tax_rate"]*100);
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
2023-04-24 07:50:45 +02:00
|
|
|
|
2023-04-28 11:27:51 +02:00
|
|
|
// The validity can be checked using https://portal3.gefeg.com/invoice/validation or https://e-rechnung.bayern.de/app/#/upload
|
2023-08-16 11:55:35 +02:00
|
|
|
return $this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the XML document
|
|
|
|
* in string format
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getXml(): string
|
|
|
|
{
|
|
|
|
return $this->xrechnung->getContent();
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getTaxType($name): string
|
|
|
|
{
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = null;
|
2023-04-21 07:18:17 +02:00
|
|
|
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:
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::STANDARD_RATE;
|
2023-04-21 07:18:17 +02:00
|
|
|
break;
|
|
|
|
case Product::PRODUCT_TYPE_EXEMPT:
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::EXEMPT_FROM_TAX;
|
2023-04-21 07:18:17 +02:00
|
|
|
break;
|
|
|
|
case Product::PRODUCT_TYPE_ZERO_RATED:
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::ZERO_RATED_GOODS;
|
2023-04-21 07:18:17 +02:00
|
|
|
break;
|
|
|
|
case Product::PRODUCT_TYPE_REVERSE_TAX:
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::VAT_REVERSE_CHARGE;
|
2023-04-21 07:18:17 +02:00
|
|
|
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"];
|
2023-05-03 16:19:28 +02:00
|
|
|
if (empty($tax_type)) {
|
2023-04-28 11:27:51 +02:00
|
|
|
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) {
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES;
|
2023-04-21 07:18:17 +02:00
|
|
|
} elseif (!in_array($this->invoice->client->country->iso_3166_2, $eu_states)) {
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::SERVICE_OUTSIDE_SCOPE_OF_TAX;
|
2023-04-21 07:18:17 +02:00
|
|
|
} elseif ($this->invoice->client->country->iso_3166_2 == "ES-CN") {
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::CANARY_ISLANDS_GENERAL_INDIRECT_TAX;
|
2023-04-21 07:18:17 +02:00
|
|
|
} elseif (in_array($this->invoice->client->country->iso_3166_2, ["ES-CE", "ES-ML"])) {
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::TAX_FOR_PRODUCTION_SERVICES_AND_IMPORTATION_IN_CEUTA_AND_MELILLA;
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
|
|
|
nlog("Unkown tax case for xinvoice");
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::STANDARD_RATE;
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-03 16:19:28 +02:00
|
|
|
return $tax_type;
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
2023-07-03 19:31:54 +02:00
|
|
|
private function addtoTaxMap(string $tax_type, float $net_amount, float $tax_rate): void
|
|
|
|
{
|
2023-05-03 16:41:38 +02:00
|
|
|
$hash = hash("md5", $tax_type."-".$tax_rate);
|
2023-05-03 16:05:59 +02:00
|
|
|
if (array_key_exists($hash, $this->tax_map)){
|
2023-05-03 16:41:38 +02:00
|
|
|
$this->tax_map[$hash]["net_amount"] += $net_amount;
|
2023-05-03 16:05:59 +02:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
$this->tax_map[$hash] = [
|
2023-05-03 16:41:38 +02:00
|
|
|
"tax_type" => $tax_type,
|
|
|
|
"net_amount" => $net_amount,
|
|
|
|
"tax_rate" => $tax_rate / 100
|
2023-05-03 16:05:59 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2023-04-21 07:18:17 +02:00
|
|
|
|
|
|
|
}
|