From 9f3a58129f575bf344e2c11af4f92dadd5cb1431 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 7 Aug 2023 09:59:10 +0200 Subject: [PATCH 01/42] Removed pdf-parameters --- app/Jobs/Entity/CreateEntityPdf.php | 2 +- app/Jobs/Invoice/CreateEInvoice.php | 2 +- app/Jobs/Invoice/ZipInvoices.php | 2 +- app/Services/Invoice/GetInvoiceEInvoice.php | 2 +- app/Services/Invoice/InvoiceService.php | 4 ++-- tests/Unit/EInvoiceTest.php | 6 +++--- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index fe25cd410332..58c60b7cb5ee 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -213,7 +213,7 @@ class CreateEntityPdf implements ShouldQueue } } if ($this->entity_string == "invoice" && $this->client->getSetting('enable_e_invoice')){ - (new CreateEInvoice($this->entity, true))->handle(); + (new CreateEInvoice($this->entity))->handle(); } $this->invitation = null; // $this->entity = null; diff --git a/app/Jobs/Invoice/CreateEInvoice.php b/app/Jobs/Invoice/CreateEInvoice.php index dc2fdfba1d39..e97444705fe3 100644 --- a/app/Jobs/Invoice/CreateEInvoice.php +++ b/app/Jobs/Invoice/CreateEInvoice.php @@ -29,7 +29,7 @@ class CreateEInvoice implements ShouldQueue public $deleteWhenMissingModels = true; - public function __construct(private Invoice $invoice, private bool $alterPDF, private string $custom_pdf_path = "") + public function __construct(private Invoice $invoice) { } diff --git a/app/Jobs/Invoice/ZipInvoices.php b/app/Jobs/Invoice/ZipInvoices.php index 17209446b345..d8c15054343f 100644 --- a/app/Jobs/Invoice/ZipInvoices.php +++ b/app/Jobs/Invoice/ZipInvoices.php @@ -76,7 +76,7 @@ class ZipInvoices implements ShouldQueue $this->invoices->each(function ($invoice) { (new CreateEntityPdf($invoice->invitations()->first()))->handle(); if ($invoice->client->getSetting('enable_e_invoice')){ - (new CreateEInvoice($invoice, false))->handle(); + (new CreateEInvoice($invoice))->handle(); } }); diff --git a/app/Services/Invoice/GetInvoiceEInvoice.php b/app/Services/Invoice/GetInvoiceEInvoice.php index 6b6dac9f544f..b3690eef603c 100644 --- a/app/Services/Invoice/GetInvoiceEInvoice.php +++ b/app/Services/Invoice/GetInvoiceEInvoice.php @@ -43,7 +43,7 @@ class GetInvoiceEInvoice extends AbstractService $file = Storage::disk($disk)->exists($file_path); if (! $file) { - $file_path = (new CreateEInvoice($this->invoice, false))->handle(); + $file_path = (new CreateEInvoice($this->invoice))->handle(); } return $file_path; diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 8558c0128ec6..a024ccaef8ba 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -465,7 +465,7 @@ class InvoiceService if ($invitation->invoice->client->getSetting('enable_e_invoice') && $invitation instanceof InvoiceInvitation) { - (new CreateEInvoice($invitation->invoice, true))->handle(); + (new CreateEInvoice($invitation->invoice))->handle(); } }); @@ -478,7 +478,7 @@ class InvoiceService CreateEntityPdf::dispatch($invitation); if ($invitation->invoice->client->getSetting('enable_e_invoice') && $invitation instanceof InvoiceInvitation) { - CreateEInvoice::dispatch($invitation->invoice, true); + CreateEInvoice::dispatch($invitation->invoice); } }); diff --git a/tests/Unit/EInvoiceTest.php b/tests/Unit/EInvoiceTest.php index 6c31974bf3e9..2a76d5971891 100644 --- a/tests/Unit/EInvoiceTest.php +++ b/tests/Unit/EInvoiceTest.php @@ -42,7 +42,7 @@ class EInvoiceTest extends TestCase $this->company->e_invoice_type = "EN16931"; $this->invoice->client->routing_id = 'DE123456789'; $this->invoice->client->save(); - $xinvoice = (new CreateEInvoice($this->invoice, false))->handle(); + $xinvoice = (new CreateEInvoice($this->invoice))->handle(); $this->assertNotNull($xinvoice); $this->assertTrue(Storage::exists($xinvoice)); } @@ -56,7 +56,7 @@ class EInvoiceTest extends TestCase $this->invoice->client->routing_id = 'DE123456789'; $this->invoice->client->save(); - $xinvoice = (new CreateEInvoice($this->invoice, false))->handle(); + $xinvoice = (new CreateEInvoice($this->invoice))->handle(); nlog(Storage::path($xinvoice)); $document = ZugferdDocumentReader::readAndGuessFromFile(Storage::path($xinvoice)); $document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $documentcurrency, $taxcurrency, $taxname, $documentlangeuage, $rest); @@ -69,7 +69,7 @@ class EInvoiceTest extends TestCase public function checkEmbededPDFFile() { $pdf = (new CreateEntityPdf($this->invoice->invitations()->first()))->handle(); - (new CreateEInvoice($this->invoice, true, $pdf))->handle(); + (new CreateEInvoice($this->invoice))->handle(); $document = ZugferdDocumentReader::readAndGuessFromFile($pdf); $document->getDocumentInformation($documentno, $documenttypecode, $documentdate, $documentcurrency, $taxcurrency, $taxname, $documentlangeuage, $rest); $this->assertEquals($this->invoice->number, $documentno); From 0c83ca51c5b24f1e246c593b9da27065d1e38bc5 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 7 Aug 2023 10:22:11 +0200 Subject: [PATCH 02/42] More adjustments --- app/Jobs/Invoice/MergeEInvoice.php | 61 ++++++++++++++++++++++++++ app/Services/Invoice/GetInvoicePdf.php | 7 ++- app/Services/Invoice/MergeEInvoice.php | 53 ++++++---------------- 3 files changed, 80 insertions(+), 41 deletions(-) create mode 100644 app/Jobs/Invoice/MergeEInvoice.php diff --git a/app/Jobs/Invoice/MergeEInvoice.php b/app/Jobs/Invoice/MergeEInvoice.php new file mode 100644 index 000000000000..671c187e9fd1 --- /dev/null +++ b/app/Jobs/Invoice/MergeEInvoice.php @@ -0,0 +1,61 @@ +invoice->client->getSetting('e_invoice_type'); + switch ($e_invoice_type) { + case "EN16931": + case "XInvoice_2_2": + case "XInvoice_2_1": + case "XInvoice_2_0": + case "XInvoice_1_0": + case "XInvoice-Extended": + case "XInvoice-BasicWL": + case "XInvoice-Basic": + $this->embedEInvoiceZuGFerD(); + //case "Facturae_3.2": + //case "Facturae_3.2.1": + //case "Facturae_3.2.2": + // + default: + $this->embedEInvoiceZuGFerD(); + break; + } + } + + /** + * @throws \Exception + */ + private function embedEInvoiceZuGFerD(): void + { + $filepath_pdf = $this->invoice->client->invoice_filepath($this->invoice->invitations->first()) . $this->invoice->getFileName(); + $e_invoice_path = $this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()) . $this->invoice->getFileName("xml"); + $document = ZugferdDocumentReader::readAndGuessFromFile($e_invoice_path); + $disk = config('filesystems.default'); + + if (!Storage::disk($disk)->exists($this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()))) { + Storage::makeDirectory($this->invoice->client->e_invoice_filepath($this->invoice->invitations->first())); + } + $pdfBuilder = new ZugferdDocumentPdfBuilder($document, Storage::disk($disk)->path($filepath_pdf)); + $pdfBuilder->generateDocument(); + $pdfBuilder->saveDocument(Storage::disk($disk)->path($filepath_pdf)); + } +} diff --git a/app/Services/Invoice/GetInvoicePdf.php b/app/Services/Invoice/GetInvoicePdf.php index 376cdfd95d98..346f707e3b15 100644 --- a/app/Services/Invoice/GetInvoicePdf.php +++ b/app/Services/Invoice/GetInvoicePdf.php @@ -12,6 +12,8 @@ namespace App\Services\Invoice; use App\Jobs\Entity\CreateEntityPdf; +use App\Jobs\Invoice\CreateEInvoice; +use App\Jobs\Invoice\MergeEInvoice; use App\Models\ClientContact; use App\Models\Invoice; use App\Services\AbstractService; @@ -47,7 +49,10 @@ class GetInvoicePdf extends AbstractService if (! $file) { $file_path = (new CreateEntityPdf($invitation))->handle(); } - + if ($this->invoice->client->getSetting('enable_e_invoice')){ + (new CreateEInvoice($this->invoice))->handle(); + (new MergeEInvoice($this->invoice))->handle(); + } return $file_path; } } diff --git a/app/Services/Invoice/MergeEInvoice.php b/app/Services/Invoice/MergeEInvoice.php index d05d61abaf86..ecb30f7a8d69 100644 --- a/app/Services/Invoice/MergeEInvoice.php +++ b/app/Services/Invoice/MergeEInvoice.php @@ -4,58 +4,31 @@ namespace App\Services\Invoice; use App\Models\ClientContact; use App\Models\Invoice; -use App\Services\AbstractService; -use horstoeko\zugferd\ZugferdDocumentPdfBuilder; use Illuminate\Support\Facades\Storage; -use horstoeko\zugferd\ZugferdDocumentReader; -class MergeEInvoice extends AbstractService +class MergeEInvoice { + + /** + * @param Invoice $invoice + * @param mixed|null $contact + */ public function __construct(public Invoice $invoice, public ?ClientContact $contact = null) { } - /** - * @throws \Exception - */ public function run(): void { - $e_invoice_type = $this->invoice->client->getSetting('e_invoice_type'); - switch ($e_invoice_type) { - case "EN16931": - case "XInvoice_2_2": - case "XInvoice_2_1": - case "XInvoice_2_0": - case "XInvoice_1_0": - case "XInvoice-Extended": - case "XInvoice-BasicWL": - case "XInvoice-Basic": - $this->embedEInvoiceZuGFerD(); - //case "Facturae_3.2": - //case "Facturae_3.2.1": - //case "Facturae_3.2.2": - // - default: - $this->embedEInvoiceZuGFerD(); - break; - } - } + $file_path = $this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()). $this->invoice->getFileName("xml"); - /** - * @throws \Exception - */ - private function embedEInvoiceZuGFerD(): void - { - $filepath_pdf = $this->invoice->client->invoice_filepath($this->invoice->invitations->first()) . $this->invoice->getFileName(); - $e_invoice_path = $this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()) . $this->invoice->getFileName("xml"); - $document = ZugferdDocumentReader::readAndGuessFromFile($e_invoice_path); + // $disk = 'public'; $disk = config('filesystems.default'); - if (!Storage::disk($disk)->exists($this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()))) { - Storage::makeDirectory($this->invoice->client->e_invoice_filepath($this->invoice->invitations->first())); + $file = Storage::disk($disk)->exists($file_path); + + if (! $file) { + (new \App\Jobs\Invoice\MergeEInvoice($this->invoice))->handle(); } - $pdfBuilder = new ZugferdDocumentPdfBuilder($document, Storage::disk($disk)->path($filepath_pdf)); - $pdfBuilder->generateDocument(); - $pdfBuilder->saveDocument(Storage::disk($disk)->path($filepath_pdf)); + } } From d8a6578be206eb377321df760a8a383013b37029 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 7 Aug 2023 21:18:50 +0200 Subject: [PATCH 03/42] More fixes --- app/Services/Invoice/InvoiceService.php | 2 ++ app/Services/Invoice/MergeEInvoice.php | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index a024ccaef8ba..d8fc0f47d5ea 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -343,6 +343,7 @@ class InvoiceService })->toArray(); $this->deletePdf(); + $this->deleteEInvoice(); return $this; } @@ -410,6 +411,7 @@ class InvoiceService $this->invoice = $this->invoice->calc()->getInvoice(); $this->deletePdf(); + $this->deleteEInvoice(); /* 24-03-2022 */ $new_balance = $this->invoice->balance; diff --git a/app/Services/Invoice/MergeEInvoice.php b/app/Services/Invoice/MergeEInvoice.php index ecb30f7a8d69..c40cb0bafa58 100644 --- a/app/Services/Invoice/MergeEInvoice.php +++ b/app/Services/Invoice/MergeEInvoice.php @@ -19,14 +19,15 @@ class MergeEInvoice public function run(): void { - $file_path = $this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()). $this->invoice->getFileName("xml"); - + $file_path_xml = $this->invoice->client->e_invoice_filepath($this->invoice->invitations->first()). $this->invoice->getFileName("xml"); + $file_path_pdf = $this->invoice->getFileName(); // $disk = 'public'; $disk = config('filesystems.default'); - $file = Storage::disk($disk)->exists($file_path); + $file_xml = Storage::disk($disk)->exists($file_path_xml); + $file_pdf = Storage::disk($disk)->exists($file_path_pdf); - if (! $file) { + if ($file_xml && $file_pdf) { (new \App\Jobs\Invoice\MergeEInvoice($this->invoice))->handle(); } From dd17119fd60ea9bb8ab2c78b4c3d16167ac98de4 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 7 Aug 2023 21:55:12 +0200 Subject: [PATCH 04/42] Enable E-Invoice for client download --- app/Http/Livewire/PdfSlot.php | 39 +++++++++++++------ app/Jobs/Entity/CreateRawPdf.php | 4 ++ .../components/livewire/pdf-slot.blade.php | 13 ++++++- .../portal/ninja2020/invoices/index.blade.php | 3 +- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/app/Http/Livewire/PdfSlot.php b/app/Http/Livewire/PdfSlot.php index 8c8770eb1205..306fcf961c0e 100644 --- a/app/Http/Livewire/PdfSlot.php +++ b/app/Http/Livewire/PdfSlot.php @@ -12,6 +12,7 @@ namespace App\Http\Livewire; +use App\Services\Invoice\GetInvoiceEInvoice; use App\Utils\Number; use Livewire\Component; use App\Utils\HtmlEngine; @@ -38,7 +39,7 @@ class PdfSlot extends Component public $pdf; public $url; - + private $settings; private $html_variables; @@ -61,7 +62,7 @@ class PdfSlot extends Component } public function getPdf() - { + { // $this->pdf = $this->entity->fullscreenPdfViewer($this->invitation); $blob = [ @@ -74,14 +75,14 @@ class PdfSlot extends Component $hash = Str::random(64); Cache::put($hash, $blob, now()->addMinutes(2)); - + $this->pdf = $hash; } public function downloadPdf() { - + $file_name = $this->entity->numberFormatter().'.pdf'; if($this->entity instanceof \App\Models\PurchaseOrder) @@ -95,11 +96,25 @@ class PdfSlot extends Component echo $file; }, $file_name, $headers); + } + public function downloadEInvoice() + { + + $file_name = $this->entity->numberFormatter().'.xml'; + + $file = (new GetInvoiceEInvoice($this->entity))->run(); + + $headers = ['Content-Type' => 'application/xml']; + + return response()->streamDownload(function () use ($file) { + echo $file; + }, $file_name, $headers); + } public function render() { - + $this->entity_type = $this->resolveEntityType(); $this->settings = $this->entity->client ? $this->entity->client->getMergedSettings() : $this->entity->company->settings; @@ -146,7 +161,7 @@ class PdfSlot extends Component private function getCompanyAddress() { - + $company_address = ""; foreach($this->settings->pdf_variables->company_address as $variable) { @@ -166,7 +181,7 @@ class PdfSlot extends Component } return $this->convertVariables($company_details); - + } private function getEntityDetails() @@ -174,9 +189,9 @@ class PdfSlot extends Component $entity_details = ""; if($this->entity_type == 'invoice' || $this->entity_type == 'recurring_invoice') { - foreach($this->settings->pdf_variables->invoice_details as $variable) + foreach($this->settings->pdf_variables->invoice_details as $variable) $entity_details .= "

{$variable}_label

{$variable}

"; - + } elseif($this->entity_type == 'quote'){ foreach($this->settings->pdf_variables->quote_details as $variable) @@ -190,7 +205,7 @@ class PdfSlot extends Component foreach($this->settings->pdf_variables->purchase_order_details as $variable) $entity_details .= "

{$variable}_label

{$variable}

"; } - + return $this->convertVariables($entity_details); } @@ -206,7 +221,7 @@ class PdfSlot extends Component $name = $this->settings->pdf_variables->client_details[0]; } - + return $this->convertVariables($name); } @@ -225,7 +240,7 @@ class PdfSlot extends Component $user_details .= "

{$variable}

"; } } - + return $this->convertVariables($user_details); } diff --git a/app/Jobs/Entity/CreateRawPdf.php b/app/Jobs/Entity/CreateRawPdf.php index 4231b6595b44..e4297068d40d 100644 --- a/app/Jobs/Entity/CreateRawPdf.php +++ b/app/Jobs/Entity/CreateRawPdf.php @@ -12,6 +12,7 @@ namespace App\Jobs\Entity; use App\Exceptions\FilePermissionsFailure; +use App\Jobs\Invoice\MergeEInvoice; use App\Libraries\MultiDB; use App\Models\Credit; use App\Models\CreditInvitation; @@ -202,6 +203,9 @@ class CreateRawPdf implements ShouldQueue if ($pdf) { $maker =null; $state = null; + if ($this->invitation->invoice->client->getSetting('enable_e_invoice') && $this->entity == "invoice"){ + (new \App\Services\Invoice\MergeEInvoice($this->invitation->invoice))->run(); + }; return $pdf; } diff --git a/resources/views/portal/ninja2020/components/livewire/pdf-slot.blade.php b/resources/views/portal/ninja2020/components/livewire/pdf-slot.blade.php index 992c75e86947..8d6154df9b94 100644 --- a/resources/views/portal/ninja2020/components/livewire/pdf-slot.blade.php +++ b/resources/views/portal/ninja2020/components/livewire/pdf-slot.blade.php @@ -9,6 +9,15 @@ +