request = $request; $this->tempDirectory = Tenant::getTempDirectory(); } public function execute() : CreateInvoiceReceiptResponse { $response = new CreateInvoiceReceiptResponse(); $filename = $this->tempDirectory . $this->request->invoice->invoice_number . '.pdf'; if (!Storage::exists($this->tempDirectory)) { Storage::makeDirectory(Tenant::getTempDirectory() . '/' . $this->tempDirectory); } $receipt = $this->request->invoice->document_filename; if ($receipt === null) { Storage::put( $filename, $this->createPdf( $this->createHtml(), 'portrait', $filename, false ) ); $response->fileName = $filename; return $response; } $token = (string)rand(1000000000, 9999999999); $pdf_data = $this->createPdf( $this->createHtml(), 'portrait', $filename, false ); $tmpFileName = $this->tempDirectory . 'tmp-' . $token . '.pdf'; Storage::put($tmpFileName, $pdf_data); try { $merger = new PdfMergeProvider(); $merger ->add( storage_path('app/private/' . $tmpFileName )) ->add(storage_path('app/private/' . $receipt)) ->merge(storage_path('app/private/' .$filename) ); $response->fileName = $filename; } catch ( \Exception $e ) { $zip = new ZipArchive(); $zip->open( storage_path('app/private/' .$filename) . '.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE ); $zip->addFile(storage_path('app/private/' . $tmpFileName ), 'antrag.pdf'); $zip->addFile(storage_path('app/private/' . $receipt), 'beleg.pdf'); $zip->close(); $response->fileName = $filename . '.zip'; } finally { Storage::delete($tmpFileName); } return $response; } private function createHtml() : string { $invoiceReadable = new InvoiceResource($this->request->invoice)->toArray(); $travelPartTemplate = <<Reiseweg:%1\$s Gesamtlänge der Strecke:%2\$s km x %3\$s / km Materialtransport:%4\$s Mitfahrende im PKW:%5\$s HTML; $flatTravelPart = sprintf( $travelPartTemplate, $invoiceReadable['travelDirection'] , $invoiceReadable['distance'], $invoiceReadable['distanceAllowance'], $invoiceReadable['transportation'], $invoiceReadable['passengers'] ); $invoiceTravelPart = 'Kosten für ÖPNV:' . $invoiceReadable['amount'] . '';; $expensePart = 'Auslagenerstattung:' . $invoiceReadable['amount'] . ''; $content = <<

Abrechnungstyp %1\$s



%9\$s %11\$s
Abrechnungsnummer:%2\$s
Name:%3\$s
E-Mail:%4\$s
Telefon:%5\$s
Name der Kostenstelle:%6\$s
Zahlungsgrund:%7\$s
Wird der Betrag gespendet:%8\$s
Gesamtbetrag: %10\$s


Beleg digital eingereicht am:%12\$s
Beleg akzeptiert am:%13\$s
Beleg akzeptiert von:%14\$s
%15\$s HTML; switch ($this->request->invoice->type) { case InvoiceType::INVOICE_TYPE_TRAVELLING: $paymentType = $this->request->invoice->distance !== null ? $flatTravelPart : $invoiceTravelPart; break; default: $paymentType = $expensePart; } if ($this->request->invoice->donation) { $paymentInformation = '' . PageText::where('name', 'CONFIRMATION_DONATE')->first()->content . ''; } else { if ($this->request->invoice->contact_bank_iban === null) { $paymentInformation = ''; } else { $paymentInformationTemplate = <<%1\$s Kontoinhaber*in:%2\$s INAN:%3\$s

HTML; $paymentInformation = sprintf( $paymentInformationTemplate, PageText::where('name', 'CONFIRMATION_PAYMENT')->first()->content, $invoiceReadable['accountOwner'], $invoiceReadable['accountIban'] ); } } $changes = $this->request->invoice->changes !== null ? '

' . $this->request->invoice->changes . '

' : ''; return sprintf( $content, $invoiceReadable['invoiceTypeShort'], $invoiceReadable['invoiceNumber'], $invoiceReadable['contactName'], $invoiceReadable['contactEmail'], $invoiceReadable['contactPhone'], $invoiceReadable['costUnitName'], $invoiceReadable['invoiceType'], $invoiceReadable['donationText'], $paymentType, $invoiceReadable['amount'], $paymentInformation, $invoiceReadable['createdAt'], $invoiceReadable['approvedAt'], $invoiceReadable['approvedBy'], $changes ); } private function createPdf( string $htmlfile, string $orientation, string $filename, bool $download = true ) { $dompdf = new Dompdf(); $dompdf->loadHtml( $htmlfile, 'UTF-8' ); $dompdf->setPaper( 'A4', $orientation ); $dompdf->render(); if ( ! $download ) { return $dompdf->output(); } $dompdf->stream( $filename ); } }