1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Services/Invoice/MergeEInvoice.php

62 lines
2.1 KiB
PHP

<?php
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
{
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;
}
}
/**
* @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));
}
}