2023-03-12 12:13:59 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Invoice;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
2023-04-06 11:48:32 +02:00
|
|
|
use App\Models\Product;
|
|
|
|
use horstoeko\zugferd\codelists\ZugferdDutyTaxFeeCategories;
|
2023-03-12 12:13:59 +01:00
|
|
|
use horstoeko\zugferd\ZugferdDocumentBuilder;
|
|
|
|
use horstoeko\zugferd\ZugferdDocumentPdfBuilder;
|
|
|
|
use horstoeko\zugferd\ZugferdProfiles;
|
2023-03-12 12:58:48 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
2023-03-12 12:46:10 +01:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2023-03-12 12:58:48 +01:00
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2023-03-13 08:28:46 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2023-03-12 12:13:59 +01:00
|
|
|
|
|
|
|
|
2023-03-13 08:07:54 +01:00
|
|
|
class CreateXInvoice implements ShouldQueue
|
2023-03-12 12:13:59 +01:00
|
|
|
{
|
2023-03-12 12:58:48 +01:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2023-04-05 14:33:23 +02:00
|
|
|
private Invoice $invoice;
|
2023-04-05 11:43:37 +02:00
|
|
|
private bool $alterpdf;
|
|
|
|
private string $custompdfpath;
|
2023-03-12 12:13:59 +01:00
|
|
|
|
2023-04-04 09:18:22 +02:00
|
|
|
public function __construct(Invoice $invoice, bool $alterPDF, string $custompdfpath = "")
|
2023-03-12 12:13:59 +01:00
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
2023-04-04 08:58:01 +02:00
|
|
|
$this->alterpdf = $alterPDF;
|
2023-04-04 09:18:22 +02:00
|
|
|
$this->custompdfpath = $custompdfpath;
|
2023-03-12 12:13:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*
|
2023-03-13 08:28:46 +01:00
|
|
|
* @return string
|
2023-03-12 12:13:59 +01:00
|
|
|
*/
|
2023-03-13 08:28:46 +01:00
|
|
|
public function handle(): string
|
2023-03-12 12:13:59 +01:00
|
|
|
{
|
|
|
|
$invoice = $this->invoice;
|
|
|
|
$company = $invoice->company;
|
|
|
|
$client = $invoice->client;
|
2023-03-13 08:50:37 +01:00
|
|
|
$profile = "";
|
2023-04-17 09:24:16 +02:00
|
|
|
switch ($company->e_invoice_type) {
|
2023-03-13 08:50:37 +01:00
|
|
|
case "EN16931":
|
|
|
|
$profile = ZugferdProfiles::PROFILE_EN16931;
|
|
|
|
break;
|
|
|
|
case "XInvoice_2_2":
|
|
|
|
$profile = ZugferdProfiles::PROFILE_XRECHNUNG_2_2;
|
|
|
|
break;
|
|
|
|
case "XInvoice_2_1":
|
|
|
|
$profile = ZugferdProfiles::PROFILE_XRECHNUNG_2_1;
|
|
|
|
break;
|
|
|
|
case "XInvoice_2_0":
|
|
|
|
$profile = ZugferdProfiles::PROFILE_XRECHNUNG_2;
|
|
|
|
break;
|
|
|
|
case "XInvoice_1_0":
|
|
|
|
$profile = ZugferdProfiles::PROFILE_XRECHNUNG;
|
|
|
|
break;
|
|
|
|
case "XInvoice-Extended":
|
|
|
|
$profile = ZugferdProfiles::PROFILE_EXTENDED;
|
|
|
|
break;
|
|
|
|
case "XInvoice-BasicWL":
|
|
|
|
$profile = ZugferdProfiles::PROFILE_BASICWL;
|
|
|
|
break;
|
|
|
|
case "XInvoice-Basic":
|
|
|
|
$profile = ZugferdProfiles::PROFILE_BASIC;
|
|
|
|
break;
|
|
|
|
}
|
2023-04-05 11:41:26 +02:00
|
|
|
$xrechnung = ZugferdDocumentBuilder::CreateNew($profile);
|
2023-03-12 12:13:59 +01:00
|
|
|
|
|
|
|
$xrechnung
|
|
|
|
->setDocumentInformation($invoice->number, "380", date_create($invoice->date), $invoice->client->getCurrencyCode())
|
|
|
|
->setDocumentSupplyChainEvent(date_create($invoice->date))
|
2023-04-03 21:00:47 +02:00
|
|
|
->setDocumentSeller($company->getSetting('name'))
|
2023-04-05 15:31:40 +02:00
|
|
|
->setDocumentSellerAddress($company->getSetting("address1"), $company->getSetting("address2"), "", $company->getSetting("postal_code"), $company->getSetting("city"), $company->country()->iso_3166_2, $company->getSetting("state"))
|
2023-04-05 14:57:26 +02:00
|
|
|
->setDocumentSellerContact($invoice->user->first_name." ".$invoice->user->last_name, "", $invoice->user->phone, "", $invoice->user->email)
|
2023-03-12 12:13:59 +01:00
|
|
|
->setDocumentBuyer($client->name, $client->number)
|
2023-04-04 11:46:51 +02:00
|
|
|
->setDocumentBuyerAddress($client->address1, "", "", $client->postal_code, $client->city, $client->country->iso_3166_2)
|
2023-04-17 09:24:16 +02:00
|
|
|
->setDocumentBuyerReference($client->routing_id)
|
2023-04-05 11:41:26 +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)
|
2023-04-05 15:31:40 +02:00
|
|
|
->setDocumentShipToAddress($client->shipping_address1, $client->shipping_address2, "", $client->shipping_postal_code, $client->shipping_city, $client->shipping_country->iso_3166_2, $client->shipping_state)
|
2023-03-13 08:01:17 +01:00
|
|
|
->addDocumentPaymentTerm(ctrans("texts.xinvoice_payable", ['payeddue' => date_create($invoice->date)->diff(date_create($invoice->due_date))->format("%d"), 'paydate' => $invoice->due_date]));
|
2023-04-05 11:41:26 +02:00
|
|
|
if (!empty($invoice->public_notes)) {
|
2023-04-05 11:22:41 +02:00
|
|
|
$xrechnung->addDocumentNote($invoice->public_notes);
|
|
|
|
}
|
2023-04-05 11:41:26 +02:00
|
|
|
if (!empty($invoice->po_number)) {
|
2023-04-05 11:22:41 +02:00
|
|
|
$xrechnung->setDocumentBuyerOrderReferencedDocument($invoice->po_number);
|
|
|
|
}
|
2023-04-17 09:24:16 +02:00
|
|
|
if (empty($client->routing_id)){
|
2023-04-05 14:57:26 +02:00
|
|
|
$xrechnung->setDocumentBuyerReference(ctrans("texts.xinvoice_no_buyers_reference"));
|
|
|
|
}
|
2023-04-05 15:43:02 +02:00
|
|
|
$xrechnung->addDocumentPaymentMean(68, ctrans("texts.xinvoice_online_payment"));
|
2023-03-12 12:13:59 +01:00
|
|
|
|
2023-04-05 11:41:26 +02:00
|
|
|
if (str_contains($company->getSetting('vat_number'), "/")) {
|
2023-04-03 21:00:47 +02:00
|
|
|
$xrechnung->addDocumentSellerTaxRegistration("FC", $company->getSetting('vat_number'));
|
2023-04-05 11:41:26 +02:00
|
|
|
} else {
|
2023-04-03 21:00:47 +02:00
|
|
|
$xrechnung->addDocumentSellerTaxRegistration("VA", $company->getSetting('vat_number'));
|
2023-03-12 12:58:48 +01:00
|
|
|
}
|
2023-04-05 11:22:41 +02:00
|
|
|
|
|
|
|
$invoicingdata = $invoice->calc();
|
2023-04-05 14:33:23 +02:00
|
|
|
$globaltax = null;
|
2023-04-05 11:22:41 +02:00
|
|
|
|
|
|
|
//Create line items and calculate taxes
|
2023-04-05 11:41:26 +02:00
|
|
|
foreach ($invoice->line_items as $index => $item) {
|
2023-03-12 12:45:04 +01:00
|
|
|
$xrechnung->addNewPosition($index)
|
2023-04-05 11:41:26 +02:00
|
|
|
->setDocumentPositionProductDetails($item->notes)
|
|
|
|
->setDocumentPositionGrossPrice($item->gross_line_total)
|
|
|
|
->setDocumentPositionNetPrice($item->line_total);
|
|
|
|
if (isset($item->task_id)) {
|
2023-03-12 12:58:48 +01:00
|
|
|
$xrechnung->setDocumentPositionQuantity($item->quantity, "HUR");
|
2023-04-05 11:41:26 +02:00
|
|
|
} else {
|
2023-03-12 12:58:48 +01:00
|
|
|
$xrechnung->setDocumentPositionQuantity($item->quantity, "H87");
|
|
|
|
}
|
2023-04-05 14:57:26 +02:00
|
|
|
$linenetamount = $item->line_total;
|
2023-04-05 14:33:23 +02:00
|
|
|
if ($item->discount > 0){
|
|
|
|
if ($invoice->is_amount_discount){
|
|
|
|
$linenetamount -= $item->discount;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$linenetamount -= $linenetamount * ($item->discount / 100);
|
|
|
|
}
|
2023-04-05 11:39:54 +02:00
|
|
|
}
|
2023-04-05 14:33:23 +02:00
|
|
|
$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))){
|
|
|
|
if (!empty($item->tax_name1)) {
|
2023-04-06 11:48:32 +02:00
|
|
|
$xrechnung->addDocumentPositionTax($this->getTaxType($item->tax_id, $invoice), 'VAT', $item->tax_rate1);
|
2023-04-05 14:33:23 +02:00
|
|
|
} elseif (!empty($item->tax_name2)) {
|
2023-04-06 11:48:32 +02:00
|
|
|
$xrechnung->addDocumentPositionTax($this->getTaxType($item->tax_id, $invoice), 'VAT', $item->tax_rate2);
|
2023-04-05 14:33:23 +02:00
|
|
|
} elseif (!empty($item->tax_name3)) {
|
2023-04-06 11:48:32 +02:00
|
|
|
$xrechnung->addDocumentPositionTax($this->getTaxType($item->tax_id, $invoice), 'VAT', $item->tax_rate3);
|
2023-04-05 14:33:23 +02:00
|
|
|
} else {
|
|
|
|
nlog("Can't add correct tax position");
|
|
|
|
}
|
2023-04-05 11:41:26 +02:00
|
|
|
} else {
|
2023-04-05 14:33:23 +02:00
|
|
|
if (!empty($invoice->tax_name1)) {
|
|
|
|
$globaltax = 0;
|
2023-04-06 11:48:32 +02:00
|
|
|
$xrechnung->addDocumentPositionTax($this->getTaxType($invoice->tax_name1, $invoice), 'VAT', $invoice->tax_rate1);
|
2023-04-05 14:33:23 +02:00
|
|
|
} elseif (!empty($invoice->tax_name2)) {
|
|
|
|
$globaltax = 1;
|
2023-04-06 11:48:32 +02:00
|
|
|
$xrechnung->addDocumentPositionTax($this->getTaxType($invoice->tax_name2, $invoice), 'VAT', $invoice->tax_rate2);
|
2023-04-05 14:33:23 +02:00
|
|
|
} elseif (!empty($invoice->tax_name3)) {
|
|
|
|
$globaltax = 2;
|
2023-04-06 11:48:32 +02:00
|
|
|
$xrechnung->addDocumentPositionTax($this->getTaxType($invoice->tax_name3, $invoice), 'VAT', $item->tax_rate3);
|
2023-04-05 14:33:23 +02:00
|
|
|
} else {
|
|
|
|
nlog("Can't add correct tax position");
|
|
|
|
}
|
2023-03-14 21:26:08 +01:00
|
|
|
}
|
2023-04-05 11:41:26 +02:00
|
|
|
}
|
2023-04-05 11:39:54 +02:00
|
|
|
|
2023-03-12 12:45:04 +01:00
|
|
|
|
2023-04-05 11:41:26 +02:00
|
|
|
if ($invoice->isPartial()) {
|
2023-04-06 15:54:08 +02:00
|
|
|
$xrechnung->setDocumentSummation($invoice->amount, $invoice->balance, $invoicingdata->getSubTotal(), $invoicingdata->getTotalSurcharges(), $invoicingdata->getTotalDiscount(), $invoicingdata->getSubTotal(), $invoicingdata->getItemTotalTaxes(), null, $invoice->partial);
|
2023-04-05 11:22:41 +02:00
|
|
|
} else {
|
2023-04-06 15:54:08 +02:00
|
|
|
$xrechnung->setDocumentSummation($invoice->amount, $invoice->balance, $invoicingdata->getSubTotal(), $invoicingdata->getTotalSurcharges(), $invoicingdata->getTotalDiscount(), $invoicingdata->getSubTotal(), $invoicingdata->getItemTotalTaxes(), null, 0.0);
|
2023-03-12 12:45:04 +01:00
|
|
|
}
|
|
|
|
|
2023-04-05 11:41:26 +02:00
|
|
|
foreach ($invoicingdata->getTaxMap() as $item) {
|
2023-04-05 11:39:54 +02:00
|
|
|
$tax = explode(" ", $item["name"]);
|
2023-04-06 11:48:32 +02:00
|
|
|
$xrechnung->addDocumentTax($this->getTaxType("", $invoice), "VAT", $item["total"] / (explode("%", end($tax))[0] / 100), $item["total"], explode("%", end($tax))[0]);
|
2023-04-05 11:39:54 +02:00
|
|
|
// TODO: Add correct tax type within getTaxType
|
2023-03-12 12:45:04 +01:00
|
|
|
}
|
2023-04-05 14:33:23 +02:00
|
|
|
if (!empty($globaltax)){
|
|
|
|
$tax = explode(" ", $invoicingdata->getTotalTaxMap()[$globaltax]["name"]);
|
2023-04-06 11:48:32 +02:00
|
|
|
$xrechnung->addDocumentTax($this->getTaxType("", $invoice), "VAT", $invoicingdata->getTotalTaxMap()[$globaltax]["total"] / (explode("%", end($tax))[0] / 100), $invoicingdata->getTotalTaxMap()[$globaltax]["total"], explode("%", end($tax))[0]);
|
2023-04-05 11:39:54 +02:00
|
|
|
// TODO: Add correct tax type within getTaxType
|
2023-03-12 12:45:04 +01:00
|
|
|
}
|
|
|
|
|
2023-03-13 08:28:46 +01:00
|
|
|
$disk = config('filesystems.default');
|
2023-04-17 09:24:16 +02:00
|
|
|
if (!Storage::exists($client->e_invoice_filepath($invoice->invitations->first()))) {
|
|
|
|
Storage::makeDirectory($client->e_invoice_filepath($invoice->invitations->first()));
|
2023-04-03 21:00:47 +02:00
|
|
|
}
|
2023-04-17 09:24:16 +02:00
|
|
|
$xrechnung->writeFile(Storage::disk($disk)->path($client->e_invoice_filepath($invoice->invitations->first()) . $invoice->getFileName("xml")));
|
2023-04-05 14:33:23 +02:00
|
|
|
// The validity can be checked using https://portal3.gefeg.com/invoice/validation
|
2023-04-03 21:00:47 +02:00
|
|
|
|
2023-04-05 11:41:26 +02:00
|
|
|
if ($this->alterpdf) {
|
|
|
|
if ($this->custompdfpath != "") {
|
2023-04-04 09:18:22 +02:00
|
|
|
$pdfBuilder = new ZugferdDocumentPdfBuilder($xrechnung, $this->custompdfpath);
|
2023-04-04 08:58:01 +02:00
|
|
|
$pdfBuilder->generateDocument();
|
2023-04-04 09:18:22 +02:00
|
|
|
$pdfBuilder->saveDocument($this->custompdfpath);
|
2023-04-05 11:41:26 +02:00
|
|
|
} else {
|
|
|
|
$filepath_pdf = $client->invoice_filepath($invoice->invitations->first()) . $invoice->getFileName();
|
2023-04-04 09:18:22 +02:00
|
|
|
$file = Storage::disk($disk)->exists($filepath_pdf);
|
|
|
|
if ($file) {
|
|
|
|
$pdfBuilder = new ZugferdDocumentPdfBuilder($xrechnung, Storage::disk($disk)->path($filepath_pdf));
|
|
|
|
$pdfBuilder->generateDocument();
|
|
|
|
$pdfBuilder->saveDocument(Storage::disk($disk)->path($filepath_pdf));
|
|
|
|
}
|
2023-04-04 08:58:01 +02:00
|
|
|
}
|
2023-03-13 08:28:46 +01:00
|
|
|
}
|
2023-04-05 14:33:23 +02:00
|
|
|
|
2023-04-17 09:24:16 +02:00
|
|
|
return $client->e_invoice_filepath($invoice->invitations->first()) . $invoice->getFileName("xml");
|
2023-03-12 12:13:59 +01:00
|
|
|
}
|
2023-04-05 11:39:54 +02:00
|
|
|
|
2023-04-06 11:48:32 +02:00
|
|
|
private function getTaxType($name, Invoice $invoice): string
|
2023-04-05 11:41:26 +02:00
|
|
|
{
|
2023-04-06 11:48:32 +02:00
|
|
|
$taxtype = 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;
|
|
|
|
break;
|
|
|
|
case Product::PRODUCT_TYPE_EXEMPT:
|
|
|
|
$taxtype = ZugferdDutyTaxFeeCategories::EXEMPT_FROM_TAX;
|
|
|
|
break;
|
|
|
|
case Product::PRODUCT_TYPE_ZERO_RATED:
|
|
|
|
$taxtype = ZugferdDutyTaxFeeCategories::ZERO_RATED_GOODS;
|
|
|
|
break;
|
|
|
|
case Product::PRODUCT_TYPE_REVERSE_TAX:
|
|
|
|
$taxtype = 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 (in_array($invoice->company->country()->iso_3166_2, $eu_states) && in_array($invoice->client->country->iso_3166_2, $eu_states)){
|
|
|
|
$taxtype = ZugferdDutyTaxFeeCategories::VAT_EXEMPT_FOR_EEA_INTRACOMMUNITY_SUPPLY_OF_GOODS_AND_SERVICES;
|
|
|
|
}
|
|
|
|
elseif (!in_array($invoice->client->country->iso_3166_2, $eu_states)){
|
|
|
|
$taxtype = ZugferdDutyTaxFeeCategories::SERVICE_OUTSIDE_SCOPE_OF_TAX;
|
|
|
|
}
|
|
|
|
elseif ($invoice->client->country->iso_3166_2 == "ES-CN"){
|
|
|
|
$taxtype = ZugferdDutyTaxFeeCategories::CANARY_ISLANDS_GENERAL_INDIRECT_TAX;
|
|
|
|
}
|
|
|
|
elseif (in_array($invoice->client->country->iso_3166_2, ["ES-CE", "ES-ML"])){
|
|
|
|
$taxtype = ZugferdDutyTaxFeeCategories::TAX_FOR_PRODUCTION_SERVICES_AND_IMPORTATION_IN_CEUTA_AND_MELILLA;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nlog("Unkown tax case for xinvoice");
|
|
|
|
$taxtype = ZugferdDutyTaxFeeCategories::STANDARD_RATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $taxtype;
|
2023-04-05 11:39:54 +02:00
|
|
|
}
|
|
|
|
}
|