2023-04-21 07:18:17 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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)
|
2023-04-21 07:18:17 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
2024-03-20 12:54:18 +01:00
|
|
|
namespace App\Services\EDocument\Standards;
|
2023-04-21 07:18:17 +02:00
|
|
|
|
2024-05-15 16:58:21 +02:00
|
|
|
use App\DataMapper\InvoiceItem;
|
2024-03-10 16:02:15 +01:00
|
|
|
use App\Models\Credit;
|
2023-04-21 07:18:17 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Product;
|
2024-03-10 16:02:15 +01:00
|
|
|
use App\Models\Quote;
|
2023-04-21 07:18:17 +02:00
|
|
|
use App\Services\AbstractService;
|
|
|
|
use horstoeko\zugferd\codelists\ZugferdDutyTaxFeeCategories;
|
2023-10-26 04:57:44 +02:00
|
|
|
use horstoeko\zugferd\ZugferdDocumentBuilder;
|
|
|
|
use horstoeko\zugferd\ZugferdProfiles;
|
2023-04-21 07:18:17 +02:00
|
|
|
|
2024-03-10 08:13:34 +01:00
|
|
|
class ZugferdEDokument extends AbstractService
|
2023-04-21 07:18:17 +02:00
|
|
|
{
|
2024-03-10 16:02:15 +01:00
|
|
|
public ZugferdDocumentBuilder $xdocument;
|
2023-04-24 07:50:45 +02:00
|
|
|
|
2024-06-09 07:56:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* __construct
|
|
|
|
*
|
|
|
|
* \App\Models\Invoice | \App\Models\Quote | \App\Models\PurchaseOrder | \App\Models\Credit $document
|
|
|
|
* @param bool $returnObject
|
|
|
|
* @param array $tax_map
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(public \App\Models\Invoice | \App\Models\Quote | \App\Models\PurchaseOrder | \App\Models\Credit $document, 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
|
|
|
|
2024-06-06 04:31:24 +02:00
|
|
|
/** @var \App\Models\Company $company */
|
2024-03-10 16:02:15 +01:00
|
|
|
$company = $this->document->company;
|
2024-06-06 04:31:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
/** @var \App\Models\Client $client */
|
2024-03-10 16:02:15 +01:00
|
|
|
$client = $this->document->client;
|
2024-06-06 04:31:24 +02:00
|
|
|
|
2023-04-21 07:18:17 +02:00
|
|
|
$profile = $client->getSetting('e_invoice_type');
|
|
|
|
|
2023-04-24 07:50:45 +02:00
|
|
|
$profile = match ($profile) {
|
2023-11-27 18:21:19 +01:00
|
|
|
"XInvoice_3_0" => ZugferdProfiles::PROFILE_XRECHNUNG_3,
|
|
|
|
"XInvoice_2_3" => ZugferdProfiles::PROFILE_XRECHNUNG_2_3,
|
2023-04-24 07:50:45 +02:00
|
|
|
"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
|
|
|
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument = ZugferdDocumentBuilder::CreateNew($profile);
|
2023-04-21 07:18:17 +02:00
|
|
|
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument
|
|
|
|
->setDocumentSupplyChainEvent(date_create($this->document->date ?? now()->format('Y-m-d')))
|
2023-04-21 07:18:17 +02:00
|
|
|
->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"))
|
2024-03-10 16:02:15 +01:00
|
|
|
->setDocumentSellerContact($this->document->user->present()->getFullName(), "", $this->document->user->present()->phone(), "", $this->document->user->email)
|
2024-05-15 21:21:21 +02:00
|
|
|
->setDocumentSellerCommunication("EM", $this->document->user->email)
|
2023-12-07 09:32:08 +01:00
|
|
|
->setDocumentBuyer($client->present()->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-12-07 09:32:08 +01:00
|
|
|
->setDocumentBuyerContact($client->present()->primary_contact_name(), "", $client->present()->phone(), "", $client->present()->email())
|
2024-03-10 16:02:15 +01:00
|
|
|
->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]));
|
2023-04-24 07:50:45 +02:00
|
|
|
|
2024-03-10 16:02:15 +01:00
|
|
|
if (!empty($this->document->public_notes)) {
|
|
|
|
$this->xdocument->addDocumentNote($this->document->public_notes ?? '');
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
2024-03-10 16:02:15 +01:00
|
|
|
// 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());
|
2024-05-20 21:25:30 +02:00
|
|
|
$this->xdocument->setIsTestDocument();
|
2024-03-10 16:02:15 +01:00
|
|
|
} else {
|
|
|
|
$this->xdocument->setDocumentInformation($this->document->number, "84", date_create($this->document->date ?? now()->format('Y-m-d')), $client->getCurrencyCode());
|
2024-05-20 21:25:30 +02:00
|
|
|
}
|
2024-03-10 16:02:15 +01:00
|
|
|
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());
|
2024-05-15 21:21:21 +02:00
|
|
|
$this->xdocument->setIsTestDocument();
|
2024-03-10 16:02:15 +01:00
|
|
|
} 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());
|
2024-05-20 21:25:30 +02:00
|
|
|
$this->xdocument->setIsTestDocument();
|
2024-03-10 16:02:15 +01:00
|
|
|
} else {
|
|
|
|
$this->xdocument->setDocumentInformation($this->document->number, "389", date_create($this->document->date ?? now()->format('Y-m-d')), $client->getCurrencyCode());
|
|
|
|
}
|
2023-05-17 19:01:12 +02:00
|
|
|
}
|
2024-03-10 16:02:15 +01:00
|
|
|
if (isset($this->document->po_number)) {
|
|
|
|
$this->xdocument->setDocumentBuyerOrderReferencedDocument($this->document->po_number);
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
|
|
|
if (empty($client->routing_id)) {
|
2024-05-15 21:21:21 +02:00
|
|
|
|
|
|
|
$this->xdocument->setDocumentBuyerReference(ctrans("texts.xinvoice_no_buyers_reference"))
|
|
|
|
->setDocumentSellerCommunication("EM", $client->present()->email());
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
2024-05-15 21:21:21 +02:00
|
|
|
$this->xdocument->setDocumentBuyerReference($client->routing_id)
|
|
|
|
->setDocumentBuyerCommunication("0204", $client->routing_id);
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
2023-12-07 09:32:08 +01:00
|
|
|
if (isset($client->shipping_address1) && $client->shipping_country) {
|
2024-03-10 16:02:15 +01:00
|
|
|
$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);
|
2023-04-24 07:50:45 +02:00
|
|
|
}
|
|
|
|
|
2024-04-25 08:31:15 +02:00
|
|
|
//Payment Means - Switcher
|
|
|
|
if($company->settings->custom_value1 == '42'){
|
|
|
|
$this->xdocument->addDocumentPaymentMean(typecode: 42, payeeIban: $company->settings->custom_value2, payeeAccountName: $company->settings->custom_value4, payeeBic: $company->settings->custom_value3);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$this->xdocument->addDocumentPaymentMean(68, ctrans("texts.xinvoice_online_payment"));
|
2023-04-21 07:18:17 +02:00
|
|
|
|
|
|
|
if (str_contains($company->getSetting('vat_number'), "/")) {
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->addDocumentSellerTaxRegistration("FC", $company->getSetting('vat_number'));
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->addDocumentSellerTaxRegistration("VA", $company->getSetting('vat_number'));
|
2024-05-15 15:56:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
if (!empty($client->vat_number)){
|
|
|
|
$this->xdocument->addDocumentBuyerTaxRegistration("VA", $client->vat_number);
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
|
|
|
|
2024-03-10 16:02:15 +01:00
|
|
|
$invoicing_data = $this->document->calc();
|
2023-04-21 07:18:17 +02:00
|
|
|
//Create line items and calculate taxes
|
2024-03-10 16:02:15 +01:00
|
|
|
foreach ($this->document->line_items as $index => $item) {
|
2024-05-15 16:58:21 +02:00
|
|
|
/** @var InvoiceItem $item **/
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->addNewPosition($index)
|
2024-05-15 16:28:36 +02:00
|
|
|
->setDocumentPositionGrossPrice($item->gross_line_total+$item->discount)
|
2023-04-21 07:18:17 +02:00
|
|
|
->setDocumentPositionNetPrice($item->line_total);
|
2023-10-26 04:57:44 +02:00
|
|
|
if (!empty($item->product_key)) {
|
|
|
|
if (!empty($item->notes)) {
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->setDocumentPositionProductDetails($item->product_key, $item->notes);
|
2023-10-26 04:57:44 +02:00
|
|
|
} else {
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->setDocumentPositionProductDetails($item->product_key);
|
2023-07-03 19:31:54 +02:00
|
|
|
}
|
2023-10-26 04:57:44 +02:00
|
|
|
} else {
|
|
|
|
if (!empty($item->notes)) {
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->setDocumentPositionProductDetails($item->notes);
|
2023-10-26 04:57:44 +02:00
|
|
|
} else {
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->setDocumentPositionProductDetails("no product name defined");
|
2023-04-28 11:27:51 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-19 15:29:52 +01:00
|
|
|
if ($item->type_id == 2) {
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->setDocumentPositionQuantity($item->quantity, "HUR");
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->setDocumentPositionQuantity($item->quantity, "H87");
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
2024-05-15 16:58:21 +02:00
|
|
|
$line_discount = 0.0;
|
|
|
|
if ($item->discount > 0) {
|
2024-03-10 16:02:15 +01:00
|
|
|
if ($this->document->is_amount_discount) {
|
2024-05-15 16:58:21 +02:00
|
|
|
$line_discount -= $item->discount;
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
2024-05-15 16:58:21 +02:00
|
|
|
$line_discount -= $item->line_total * ($item->discount / 100);
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
2024-05-15 21:21:21 +02:00
|
|
|
$this->xdocument->addDocumentPositionGrossPriceAllowanceCharge( abs($line_discount), false);
|
2024-05-15 16:58:21 +02:00
|
|
|
}
|
|
|
|
|
2024-05-16 08:10:07 +02:00
|
|
|
$this->xdocument->setDocumentPositionLineSummation($item->line_total);
|
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)) {
|
2024-05-15 16:28:36 +02:00
|
|
|
if ($taxtype == ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES){
|
2024-05-17 07:59:25 +02:00
|
|
|
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate1, exemptionReason: ctrans('texts.intracommunity_tax_info'));
|
2024-05-15 16:28:36 +02:00
|
|
|
} else {
|
|
|
|
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate1);
|
|
|
|
}
|
2024-05-15 16:58:21 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $item->line_total, $item->tax_rate1);
|
2023-04-21 07:18:17 +02:00
|
|
|
} elseif (!empty($item->tax_name2)) {
|
2024-05-15 16:28:36 +02:00
|
|
|
if ($taxtype == ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES){
|
2024-05-17 07:59:25 +02:00
|
|
|
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate2, exemptionReason: ctrans('texts.intracommunity_tax_info'));
|
2024-05-15 16:28:36 +02:00
|
|
|
} else {
|
|
|
|
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate2);
|
|
|
|
}
|
2024-05-15 16:58:21 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $item->line_total, $item->tax_rate2);
|
2023-04-21 07:18:17 +02:00
|
|
|
} elseif (!empty($item->tax_name3)) {
|
2024-05-15 16:28:36 +02:00
|
|
|
if ($taxtype == ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES){
|
2024-05-17 07:59:25 +02:00
|
|
|
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate3, exemptionReason: ctrans('texts.intracommunity_tax_info'));
|
2024-05-15 16:28:36 +02:00
|
|
|
} else {
|
|
|
|
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $item->tax_rate3);
|
|
|
|
}
|
2024-05-15 16:58:21 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $item->line_total, $item->tax_rate3);
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
2024-05-15 16:58:21 +02:00
|
|
|
nlog("Can't add correct tax position");
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
2024-03-10 16:02:15 +01:00
|
|
|
if (!empty($this->document->tax_name1)) {
|
|
|
|
$taxtype = $this->getTaxType($this->document->tax_name1);
|
|
|
|
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $this->document->tax_rate1);
|
2024-05-15 16:58:21 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $item->line_total, $this->document->tax_rate1);
|
2024-03-10 16:02:15 +01:00
|
|
|
} elseif (!empty($this->document->tax_name2)) {
|
|
|
|
$taxtype = $this->getTaxType($this->document->tax_name2);
|
|
|
|
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $this->document->tax_rate2);
|
2024-05-15 16:58:21 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $item->line_total, $this->document->tax_rate2);
|
2024-03-10 16:02:15 +01:00
|
|
|
} elseif (!empty($this->document->tax_name3)) {
|
|
|
|
$taxtype = $this->getTaxType($this->document->tax_name3);
|
|
|
|
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', $this->document->tax_rate3);
|
2024-05-15 16:58:21 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $item->line_total, $this->document->tax_rate3);
|
2023-04-21 07:18:17 +02:00
|
|
|
} else {
|
2023-05-03 16:41:38 +02:00
|
|
|
$taxtype = ZugferdDutyTaxFeeCategories::ZERO_RATED_GOODS;
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->addDocumentPositionTax($taxtype, 'VAT', 0);
|
2024-05-15 16:58:21 +02:00
|
|
|
$this->addtoTaxMap($taxtype, $item->line_total, 0);
|
2023-10-09 08:49:26 +02:00
|
|
|
// nlog("Can't add correct tax position");
|
2023-04-21 07:18:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-15 21:21:21 +02:00
|
|
|
if ($this->document->is_amount_discount) {
|
2024-05-16 08:10:07 +02:00
|
|
|
$document_discount = abs($this->document->discount);
|
2024-05-15 21:21:21 +02:00
|
|
|
} else {
|
2024-05-16 08:10:07 +02:00
|
|
|
$document_discount = $this->document->amount * $this->document->discount / 100;
|
|
|
|
}
|
2024-05-20 21:25:30 +02:00
|
|
|
|
2024-05-16 08:10:07 +02:00
|
|
|
$this->xdocument->setDocumentSummation($this->document->amount, $this->document->balance, $invoicing_data->getSubTotal(), $invoicing_data->getTotalSurcharges(), $document_discount, $invoicing_data->getSubTotal()-$document_discount, $invoicing_data->getItemTotalTaxes(), 0.0, $this->document->amount - $this->document->balance);
|
2023-10-26 04:57:44 +02:00
|
|
|
foreach ($this->tax_map as $item) {
|
2024-05-16 08:10:07 +02:00
|
|
|
if ($document_discount > 0){
|
|
|
|
if ($item["net_amount"] >= $document_discount) {
|
|
|
|
$item["net_amount"] -= $document_discount;
|
2024-05-20 21:25:30 +02:00
|
|
|
$this->xdocument->addDocumentAllowanceCharge($document_discount, false, $item["tax_type"], "VAT", $item["tax_rate"] * 100);
|
2024-05-16 08:10:07 +02:00
|
|
|
} else {
|
|
|
|
$document_discount -= $item["net_amount"];
|
2024-05-20 21:25:30 +02:00
|
|
|
$this->xdocument->addDocumentAllowanceCharge($item["net_amount"], false, $item["tax_type"], "VAT", $item["tax_rate"] * 100);
|
2024-05-16 08:10:07 +02:00
|
|
|
$item["net_amount"] = 0;
|
2024-05-20 21:25:30 +02:00
|
|
|
|
2024-05-16 08:10:07 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-15 15:56:05 +02:00
|
|
|
if ($item["tax_type"] == ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES){
|
2024-05-17 07:59:25 +02:00
|
|
|
$this->xdocument->addDocumentTax($item["tax_type"], "VAT", $item["net_amount"], $item["tax_rate"] * $item["net_amount"], $item["tax_rate"] * 100, ctrans('texts.intracommunity_tax_info'));
|
2024-05-15 15:56:05 +02:00
|
|
|
} else {
|
2024-03-10 16:02:15 +01:00
|
|
|
$this->xdocument->addDocumentTax($item["tax_type"], "VAT", $item["net_amount"], $item["tax_rate"] * $item["net_amount"], $item["tax_rate"] * 100);
|
2024-05-15 15:56:05 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-16 11:55:35 +02:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-16 11:55:35 +02:00
|
|
|
/**
|
|
|
|
* Returns the XML document
|
|
|
|
* in string format
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getXml(): string
|
|
|
|
{
|
2024-03-10 16:02:15 +01:00
|
|
|
return $this->xdocument->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)) {
|
2024-03-10 16:02:15 +01:00
|
|
|
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) {
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES;
|
2024-03-10 16:02:15 +01:00
|
|
|
} elseif (!in_array($this->document->client->country->iso_3166_2, $eu_states)) {
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::SERVICE_OUTSIDE_SCOPE_OF_TAX;
|
2024-03-10 16:02:15 +01:00
|
|
|
} elseif ($this->document->client->country->iso_3166_2 == "ES-CN") {
|
2023-05-03 16:19:28 +02:00
|
|
|
$tax_type = ZugferdDutyTaxFeeCategories::CANARY_ISLANDS_GENERAL_INDIRECT_TAX;
|
2024-03-10 16:02:15 +01:00
|
|
|
} elseif (in_array($this->document->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-10-26 04:57:44 +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-10-26 04:57:44 +02:00
|
|
|
} else {
|
2023-05-03 16:05:59 +02:00
|
|
|
$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
|
|
|
|
|
|
|
}
|