Skip to content

Commit

Permalink
Merge pull request #13 from web-vision/add-support-with-ninja5
Browse files Browse the repository at this point in the history
Add support for Invoice Ninja version 5.8v.
  • Loading branch information
BushraAsif authored Jan 25, 2024
2 parents abee394 + b765639 commit 8ea7517
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
27 changes: 14 additions & 13 deletions src/Jobs/Entity/EmailEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
83 changes: 48 additions & 35 deletions src/Mail/TemplateEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Utils\Ninja;

class TemplateEmail extends Mailable
{
Expand Down Expand Up @@ -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("<br>", "", $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;
Expand Down
4 changes: 2 additions & 2 deletions wiki.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Make sure to register `Webvision\NinjaZugferd\Providers\RouteServiceProvider::class` in the providers section of the root config/app.php file.

0 comments on commit 8ea7517

Please sign in to comment.