From 0c83ca51c5b24f1e246c593b9da27065d1e38bc5 Mon Sep 17 00:00:00 2001 From: Lars Kusch Date: Mon, 7 Aug 2023 10:22:11 +0200 Subject: [PATCH] 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)); + } }