Add Xinvoice to E-Mail

This commit is contained in:
Lars Kusch 2023-04-04 08:58:01 +02:00
parent 55bb506d4e
commit f2c38bb948
6 changed files with 30 additions and 20 deletions

3
.gitignore vendored
View File

@ -37,4 +37,5 @@ public/test.pdf
public/storage/test.pdf public/storage/test.pdf
/Modules /Modules
_ide_helper_models.php _ide_helper_models.php
_ide_helper.php _ide_helper.php
/composer.phar

View File

@ -21,9 +21,10 @@ class CreateXInvoice implements ShouldQueue
public Invoice $invoice; public Invoice $invoice;
public function __construct(Invoice $invoice) public function __construct(Invoice $invoice, bool $alterPDF)
{ {
$this->invoice = $invoice; $this->invoice = $invoice;
$this->alterpdf = $alterPDF;
} }
/** /**
@ -294,12 +295,13 @@ class CreateXInvoice implements ShouldQueue
$xrechnung->writeFile(Storage::disk($disk)->path($client->xinvoice_filepath($invoice->invitations->first()) . $invoice->getFileName("xml"))); $xrechnung->writeFile(Storage::disk($disk)->path($client->xinvoice_filepath($invoice->invitations->first()) . $invoice->getFileName("xml")));
$filepath_pdf = $client->invoice_filepath($invoice->invitations->first()).$invoice->getFileName(); $filepath_pdf = $client->invoice_filepath($invoice->invitations->first()).$invoice->getFileName();
if ($this->alterpdf){
$file = Storage::disk($disk)->exists($filepath_pdf); $file = Storage::disk($disk)->exists($filepath_pdf);
if ($file) { if ($file) {
$pdfBuilder = new ZugferdDocumentPdfBuilder($xrechnung, Storage::disk($disk)->path($filepath_pdf)); $pdfBuilder = new ZugferdDocumentPdfBuilder($xrechnung, Storage::disk($disk)->path($filepath_pdf));
$pdfBuilder->generateDocument(); $pdfBuilder->generateDocument();
$pdfBuilder->saveDocument(Storage::disk($disk)->path($filepath_pdf)); $pdfBuilder->saveDocument(Storage::disk($disk)->path($filepath_pdf));
}
} }
return $client->invoice_filepath($invoice->invitations->first()).$invoice->getFileName("xml"); return $client->invoice_filepath($invoice->invitations->first()).$invoice->getFileName("xml");
} }

View File

@ -79,7 +79,7 @@ class ZipInvoices implements ShouldQueue
$this->invoices->each(function ($invoice) { $this->invoices->each(function ($invoice) {
(new CreateEntityPdf($invoice->invitations()->first()))->handle(); (new CreateEntityPdf($invoice->invitations()->first()))->handle();
if ($this->company->use_xinvoice){ if ($this->company->use_xinvoice){
(new CreateXInvoice($invoice))->handle(); (new CreateXInvoice($invoice, false))->handle();
} }
}); });

View File

@ -11,6 +11,7 @@
namespace App\Services\Email; namespace App\Services\Email;
use App\Jobs\Invoice\CreateXInvoice;
use App\Models\Task; use App\Models\Task;
use App\Utils\Ninja; use App\Utils\Ninja;
use App\Models\Quote; use App\Models\Quote;
@ -55,7 +56,7 @@ class EmailDefaults
public function __construct(protected Email $email) public function __construct(protected Email $email)
{ {
} }
/** /**
* Entry point for generating * Entry point for generating
* the defaults for the email object * the defaults for the email object
@ -66,7 +67,7 @@ class EmailDefaults
{ {
$this->settings = $this->email->email_object->settings; $this->settings = $this->email->email_object->settings;
$this->setLocale() $this->setLocale()
->setFrom() ->setFrom()
->setTo() ->setTo()
->setTemplate() ->setTemplate()
@ -76,7 +77,7 @@ class EmailDefaults
->setBcc() ->setBcc()
->setAttachments() ->setAttachments()
->setVariables(); ->setVariables();
return $this->email->email_object; return $this->email->email_object;
} }
@ -169,7 +170,7 @@ class EmailDefaults
// Default template to be used // Default template to be used
$this->email->email_object->body = EmailTemplateDefaults::getDefaultTemplate($this->email->email_object->email_template_body, $this->locale); $this->email->email_object->body = EmailTemplateDefaults::getDefaultTemplate($this->email->email_object->email_template_body, $this->locale);
} }
if ($this->template == 'email.template.custom') { if ($this->template == 'email.template.custom') {
$this->email->email_object->body = (str_replace('$body', $this->email->email_object->body, $this->email->email_object->settings->email_style_custom)); $this->email->email_object->body = (str_replace('$body', $this->email->email_object->body, $this->email->email_object->settings->email_style_custom));
} }
@ -214,7 +215,7 @@ class EmailDefaults
public function setVariables(): self public function setVariables(): self
{ {
$this->email->email_object->body = strtr($this->email->email_object->body, $this->email->email_object->variables); $this->email->email_object->body = strtr($this->email->email_object->body, $this->email->email_object->variables);
$this->email->email_object->subject = strtr($this->email->email_object->subject, $this->email->email_object->variables); $this->email->email_object->subject = strtr($this->email->email_object->subject, $this->email->email_object->variables);
if ($this->template != 'custom') { if ($this->template != 'custom') {
@ -243,7 +244,7 @@ class EmailDefaults
foreach ($bccs as $bcc) { foreach ($bccs as $bcc) {
$bcc_array[] = new Address($bcc); $bcc_array[] = new Address($bcc);
} }
$this->email->email_object->bcc = array_merge($this->email->email_object->bcc, $bcc_array); $this->email->email_object->bcc = array_merge($this->email->email_object->bcc, $bcc_array);
return $this; return $this;
@ -256,7 +257,7 @@ class EmailDefaults
private function buildCc() private function buildCc()
{ {
return [ return [
]; ];
} }
@ -289,6 +290,7 @@ class EmailDefaults
$this->email->email_object->entity instanceof Quote || $this->email->email_object->entity instanceof Quote ||
$this->email->email_object->entity instanceof Credit)) { $this->email->email_object->entity instanceof Credit)) {
// TODO: Alter this to include XInvoice
$pdf = ((new CreateRawPdf($this->email->email_object->invitation, $this->email->company->db))->handle()); $pdf = ((new CreateRawPdf($this->email->email_object->invitation, $this->email->company->db))->handle());
$this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode($pdf), 'name' => $this->email->email_object->entity->numberFormatter().'.pdf']]); $this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode($pdf), 'name' => $this->email->email_object->entity->numberFormatter().'.pdf']]);
@ -303,6 +305,11 @@ class EmailDefaults
$this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode($ubl_string), 'name' => $this->email->email_object->entity->getFileName('xml')]]); $this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode($ubl_string), 'name' => $this->email->email_object->entity->getFileName('xml')]]);
} }
} }
/** XInvoice xml file */
if ($this->email->email_object->company->use_xinvoice && $this->email->email_object->entity instanceof Invoice) {
$xinvoice_path = (new CreateXInvoice($this->email->email_object->entity, false))->handle();
$this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode(file_get_contents($xinvoice_path)), 'name' => explode(".", $this->email->email_object->entity->getFileName('xml'))[0]."-xinvoice.xml"]]);
}
if(!$this->email->email_object->settings->document_email_attachment) if(!$this->email->email_object->settings->document_email_attachment)
return $this; return $this;
@ -324,7 +331,7 @@ class EmailDefaults
if ($this->email->email_object->entity instanceof Invoice) { if ($this->email->email_object->entity instanceof Invoice) {
$expense_ids = []; $expense_ids = [];
$task_ids = []; $task_ids = [];
foreach ($this->email->email_object->entity->line_items as $item) { foreach ($this->email->email_object->entity->line_items as $item) {
if (property_exists($item, 'expense_id')) { if (property_exists($item, 'expense_id')) {
$expense_ids[] = $item->expense_id; $expense_ids[] = $item->expense_id;

View File

@ -46,7 +46,7 @@ class GetInvoiceXInvoice extends AbstractService
$file = Storage::disk($disk)->exists($file_path); $file = Storage::disk($disk)->exists($file_path);
if (! $file) { if (! $file) {
$file_path = (new CreateXInvoice($this->invoice))->handle(); $file_path = (new CreateXInvoice($this->invoice, false))->handle();
} }
return $file_path; return $file_path;

View File

@ -451,7 +451,7 @@ class InvoiceService
(new CreateEntityPdf($invitation))->handle(); (new CreateEntityPdf($invitation))->handle();
if ($invitation instanceof InvoiceInvitation) if ($invitation instanceof InvoiceInvitation)
{ {
(new CreateXInvoice($invitation->invoice))->handle(); (new CreateXInvoice($invitation->invoice, true))->handle();
} }
}); });
@ -462,7 +462,7 @@ class InvoiceService
CreateEntityPdf::dispatch($invitation); CreateEntityPdf::dispatch($invitation);
if ($invitation instanceof InvoiceInvitation) if ($invitation instanceof InvoiceInvitation)
{ {
CreateXInvoice::dispatch($invitation->invoice); CreateXInvoice::dispatch($invitation->invoice, true);
} }
}); });
} catch (\Exception $e) { } catch (\Exception $e) {