From b7656392514fb154cf4ba92082ac989d1ec7c3e8 Mon Sep 17 00:00:00 2001 From: Bushra Asif Date: Thu, 25 Jan 2024 12:50:50 +0500 Subject: [PATCH] Add support for Invoice Ninja version 5.8v. --- CHANGELOG.md | 4 ++ composer.json | 2 +- src/Jobs/Entity/EmailEntity.php | 27 +++++------ src/Mail/TemplateEmail.php | 83 +++++++++++++++++++-------------- wiki.md | 4 +- 5 files changed, 69 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de3202a..b22a4e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All notable changes to this project will be documented in this file. +## [1.0.3] +### Added +- Add support for Invoice Ninja version 5.8v. + ## [1.0.2] ### Added - Update installation steps in the documentation diff --git a/composer.json b/composer.json index 9091e1a..dafc823 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "webvision/invoice-ninja-xrechnung", - "version": "1.0.2", + "version": "1.0.3", "description": "Add-on module for Invoice Ninja 5.x to create electronic invoices for Germany's xRechnung format", "type": "library", "license": "GPL-3.0-only", diff --git a/src/Jobs/Entity/EmailEntity.php b/src/Jobs/Entity/EmailEntity.php index 851d9e4..cf99820 100644 --- a/src/Jobs/Entity/EmailEntity.php +++ b/src/Jobs/Entity/EmailEntity.php @@ -103,8 +103,11 @@ public function __construct($invitation, Company $company, ?string $reminder_tem public function handle() { /* Don't fire emails if the company is disabled */ - if ($this->company->is_disabled) + if ($this->company->is_disabled){ return true; + } + + $this->email_entity_builder = $this->resolveEmailBuilder(); /* Set DB */ MultiDB::setDB($this->company->db); @@ -114,26 +117,24 @@ public function handle() App::setLocale($this->invitation->contact->preferredLocale()); $t->replace(Ninja::transformTranslations($this->settings)); + /* Mark entity sent */ + $this->entity->service()->markSent()->save(); + $nmo = new NinjaMailerObject; - $nmo->mailable = new TemplateEmail($this->email_entity_builder, $this->invitation->contact, $this->invitation); - $nmo->company = $this->company; + $nmo->mailable = new TemplateEmail($this->email_entity_builder, $this->invitation->contact->withoutRelations(), $this->invitation->withoutRelations()); + $nmo->company = $this->company->withoutRelations(); $nmo->settings = $this->settings; - $nmo->to_user = $this->invitation->contact; + $nmo->to_user = $this->invitation->contact->withoutRelations(); $nmo->entity_string = $this->entity_string; - $nmo->invitation = $this->invitation; + $nmo->invitation = $this->invitation->withoutRelations(); $nmo->reminder_template = $this->reminder_template; - $nmo->entity = $this->entity; - + $nmo->entity = $this->entity->withoutRelations(); + /** * Append file */ //$nmo->mailable->attachData('Test', $this->invitation->invoice->getFileName('xml')); - - - NinjaMailerJob::dispatchNow($nmo); - - /* Mark entity sent */ - $this->entity->service()->markSent()->save(); + NinjaMailerJob::dispatch($nmo); } private function resolveEntityString() :string diff --git a/src/Mail/TemplateEmail.php b/src/Mail/TemplateEmail.php index 70ba98f..181af72 100644 --- a/src/Mail/TemplateEmail.php +++ b/src/Mail/TemplateEmail.php @@ -23,6 +23,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; +use App\Utils\Ninja; class TemplateEmail extends Mailable { @@ -87,52 +88,64 @@ public function build() $this->from(config('mail.from.address'), $email_from_name); - if (strlen($settings->bcc_email) > 1) - $this->bcc(explode(",",str_replace(" ", "", $settings->bcc_email)));//remove whitespace if any has been inserted. - - $this->subject($this->build_email->getSubject()) - ->text('email.template.plain', [ - 'body' => $this->build_email->getBody(), - 'footer' => $this->build_email->getFooter(), - 'whitelabel' => $this->client->user->account->isPaid() ? true : false, - 'settings' => $settings, - ]) - ->view($template_name, [ - 'greeting' => ctrans('texts.email_salutation', ['name' => $this->contact->present()->name()]), - 'body' => $this->build_email->getBody(), - 'footer' => $this->build_email->getFooter(), - 'view_link' => $this->build_email->getViewLink(), - 'view_text' => $this->build_email->getViewText(), - 'title' => '', - 'signature' => $signature, - 'settings' => $settings, - 'company' => $company, - 'whitelabel' => $this->client->user->account->isPaid() ? true : false, - 'logo' => $this->company->present()->logo(), - ]) - ->withSwiftMessage(function ($message) use($company){ - $message->getHeaders()->addTextHeader('Tag', $company->company_key); - $message->invitation = $this->invitation; - }); + if (strlen($settings->bcc_email) > 1) { + if (Ninja::isHosted()) { - foreach ($this->build_email->getAttachments() as $file) { - - if(is_string($file)) - $this->attach($file); - elseif(is_array($file)) - $this->attach($file['path'], ['as' => $file['name'], 'mime' => $file['mime']]); + if($company->account->isPaid()) { + $bccs = explode(',', str_replace(' ', '', $settings->bcc_email)); + $this->bcc(array_slice($bccs, 0, 5)); + } + } else { + $this->bcc(explode(',', str_replace(' ', '', $settings->bcc_email))); + } } - if($this->invitation && $this->invitation->invoice && $settings->ubl_email_attachment && $this->company->account->hasFeature(Account::FEATURE_DOCUMENTS)){ + $this->subject(str_replace("
", "", $this->build_email->getSubject())) + ->text('email.template.text', [ + 'text_body' => $this->build_email->getTextBody(), + 'whitelabel' => $this->client->user->account->isPaid() ? true : false, + 'settings' => $settings, + ]) + ->view($template_name, [ + 'greeting' => ctrans('texts.email_salutation', ['name' => $this->contact->present()->name()]), + 'body' => $this->build_email->getBody(), + 'footer' => $this->build_email->getFooter(), + 'view_link' => $this->build_email->getViewLink(), + 'view_text' => $this->build_email->getViewText(), + 'title' => '', + 'signature' => $signature, + 'settings' => $settings, + 'company' => $company, + 'whitelabel' => $this->client->user->account->isPaid() ? true : false, + 'logo' => $this->company->present()->logo($settings), + 'links' => $this->build_email->getAttachmentLinks(), + ]); - $ubl_string = CreateZugferd::dispatchNow($this->invitation->invoice); + foreach ($this->build_email->getAttachments() as $file) { + if (array_key_exists('file', $file)) { + $this->attachData(base64_decode($file['file']), $file['name']); + } else { + $this->attach($file['path'], ['as' => $file['name'], 'mime' => null]); + } + } + + if($this->invitation && $this->invitation->invoice && $settings->ubl_email_attachment && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)){ + $ubl_string = (new CreateZugferd($this->invitation->invoice))->handle(); nlog($ubl_string); if($ubl_string) $this->attachData($ubl_string, $this->invitation->invoice->getFileName('xml')); + } + if ($this->invitation && $this->invitation->invoice && $this->invitation->invoice->client->getSetting('enable_e_invoice') && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) { + $xml_string = $this->invitation->invoice->service()->getEInvoice($this->invitation->contact); + + if($xml_string) { + $this->attachData($xml_string, $this->invitation->invoice->getEFileName("xml")); + } + } return $this; diff --git a/wiki.md b/wiki.md index 49802d6..1b17a36 100644 --- a/wiki.md +++ b/wiki.md @@ -54,8 +54,8 @@ Ensure that you added all the highlighted fields in the Invoice. Navigate to **I ## Installation In the root directory run the following commands using the command line -`composer update webvision/ninja-xrechnung --no-cache` +`composer require webvision/invoice-ninja-xrechnung --no-cache` `php artisan cache:clear && php artisan config:clear && php artisan route:clear` -Make sure to register `Webvision\NinjaZugferd\Providers\RouteServiceProvider::class` in the providers section of the root config/app.php file. \ No newline at end of file +Make sure to register `Webvision\NinjaZugferd\Providers\RouteServiceProvider::class` in the providers section of the root config/app.php file.