2020-10-27 01:05:42 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-10-27 01:05:42 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-10-27 01:05:42 +01:00
|
|
|
*/
|
|
|
|
|
2020-10-27 12:57:12 +01:00
|
|
|
namespace App\Jobs\Entity;
|
2020-10-27 01:05:42 +01:00
|
|
|
|
2020-11-09 11:17:20 +01:00
|
|
|
use App\Events\Invoice\InvoiceReminderWasEmailed;
|
2020-10-27 01:05:42 +01:00
|
|
|
use App\Events\Invoice\InvoiceWasEmailed;
|
|
|
|
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
|
2021-01-20 02:59:39 +01:00
|
|
|
use App\Jobs\Mail\EntityFailedSendMailer;
|
2021-02-16 13:56:12 +01:00
|
|
|
use App\Jobs\Mail\NinjaMailerJob;
|
|
|
|
use App\Jobs\Mail\NinjaMailerObject;
|
2020-10-27 01:05:42 +01:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Mail\TemplateEmail;
|
2020-11-09 11:17:20 +01:00
|
|
|
use App\Models\Activity;
|
2020-10-27 01:05:42 +01:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\CreditInvitation;
|
|
|
|
use App\Models\InvoiceInvitation;
|
2022-09-15 00:26:52 +02:00
|
|
|
use App\Models\PurchaseOrderInvitation;
|
2020-10-27 01:05:42 +01:00
|
|
|
use App\Models\QuoteInvitation;
|
|
|
|
use App\Models\RecurringInvoiceInvitation;
|
2020-10-27 12:57:12 +01:00
|
|
|
use App\Utils\HtmlEngine;
|
2020-10-27 01:05:42 +01:00
|
|
|
use App\Utils\Ninja;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2022-06-21 11:57:17 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
2020-10-27 01:05:42 +01:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2020-10-27 12:57:12 +01:00
|
|
|
use Illuminate\Support\Str;
|
2020-10-27 01:05:42 +01:00
|
|
|
|
|
|
|
/*Multi Mailer implemented*/
|
|
|
|
|
2021-02-16 14:31:00 +01:00
|
|
|
class EmailEntity implements ShouldQueue
|
2020-10-27 01:05:42 +01:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
public $invitation; //The entity invitation
|
2020-10-27 01:05:42 +01:00
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
public $company; //The company
|
2020-10-27 01:05:42 +01:00
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
public $settings; //The settings object
|
2020-10-27 01:05:42 +01:00
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
public $entity_string; //The entity string ie. invoice, quote, credit
|
2020-10-27 01:05:42 +01:00
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
public $reminder_template; //The base template we are using
|
2020-10-27 12:57:12 +01:00
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
public $entity; //The entity object
|
2020-10-27 12:57:12 +01:00
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
public $html_engine; //The HTMLEngine object
|
2020-10-27 12:57:12 +01:00
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
public $email_entity_builder; //The email builder which merges the template and text
|
2020-10-27 12:57:12 +01:00
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
public $template_data; //The data to be merged into the template
|
2020-12-09 10:52:08 +01:00
|
|
|
|
2021-04-30 01:01:56 +02:00
|
|
|
public $tries = 1;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-10-27 01:05:42 +01:00
|
|
|
/**
|
|
|
|
* EmailEntity constructor.
|
2021-01-20 04:49:22 +01:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
*
|
2020-10-27 01:05:42 +01:00
|
|
|
* @param Invitation $invitation
|
2020-10-27 12:57:12 +01:00
|
|
|
* @param Company $company
|
|
|
|
* @param ?string $reminder_template
|
2021-01-20 04:49:22 +01:00
|
|
|
* @param array $template_data
|
2020-10-27 01:05:42 +01:00
|
|
|
*/
|
2020-11-06 05:43:10 +01:00
|
|
|
public function __construct($invitation, Company $company, ?string $reminder_template = null, $template_data = null)
|
2020-10-27 01:05:42 +01:00
|
|
|
{
|
|
|
|
$this->company = $company;
|
|
|
|
|
|
|
|
$this->invitation = $invitation;
|
|
|
|
|
|
|
|
$this->entity_string = $this->resolveEntityString();
|
2020-10-27 12:57:12 +01:00
|
|
|
|
|
|
|
$this->entity = $invitation->{$this->entity_string};
|
|
|
|
|
2022-09-15 00:52:28 +02:00
|
|
|
$this->settings = $invitation->contact->client->getMergedSettings();
|
2022-09-15 00:26:52 +02:00
|
|
|
|
2020-11-04 10:32:49 +01:00
|
|
|
$this->reminder_template = $reminder_template ?: $this->entity->calculateTemplate($this->entity_string);
|
2020-10-27 12:57:12 +01:00
|
|
|
|
|
|
|
$this->html_engine = new HtmlEngine($invitation);
|
|
|
|
|
2020-11-05 11:14:30 +01:00
|
|
|
$this->template_data = $template_data;
|
|
|
|
|
2020-10-27 01:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-11-27 03:19:50 +01:00
|
|
|
public function handle() :void
|
2020-10-27 01:05:42 +01:00
|
|
|
{
|
2021-01-20 04:49:22 +01:00
|
|
|
/* Don't fire emails if the company is disabled */
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->company->is_disabled) {
|
2022-11-27 05:38:25 +01:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 23:50:55 +01:00
|
|
|
$this->email_entity_builder = $this->resolveEmailBuilder();
|
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
/* Set DB */
|
2020-10-27 01:05:42 +01:00
|
|
|
MultiDB::setDB($this->company->db);
|
|
|
|
|
2021-08-09 23:27:01 +02:00
|
|
|
App::forgetInstance('translator');
|
|
|
|
$t = app('translator');
|
2021-04-06 00:35:27 +02:00
|
|
|
App::setLocale($this->invitation->contact->preferredLocale());
|
2021-08-09 23:27:01 +02:00
|
|
|
$t->replace(Ninja::transformTranslations($this->settings));
|
2021-04-06 00:35:27 +02:00
|
|
|
|
2021-11-30 06:05:15 +01:00
|
|
|
/* Mark entity sent */
|
|
|
|
$this->entity->service()->markSent()->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-02-16 13:56:12 +01:00
|
|
|
$nmo = new NinjaMailerObject;
|
2021-06-10 03:15:21 +02:00
|
|
|
$nmo->mailable = new TemplateEmail($this->email_entity_builder, $this->invitation->contact, $this->invitation);
|
2021-02-16 13:56:12 +01:00
|
|
|
$nmo->company = $this->company;
|
|
|
|
$nmo->settings = $this->settings;
|
|
|
|
$nmo->to_user = $this->invitation->contact;
|
|
|
|
$nmo->entity_string = $this->entity_string;
|
|
|
|
$nmo->invitation = $this->invitation;
|
|
|
|
$nmo->reminder_template = $this->reminder_template;
|
2021-02-18 00:30:31 +01:00
|
|
|
$nmo->entity = $this->entity;
|
2021-11-30 06:05:15 +01:00
|
|
|
|
2023-01-29 04:22:10 +01:00
|
|
|
NinjaMailerJob::dispatch($nmo);
|
2022-11-01 07:10:05 +01:00
|
|
|
|
|
|
|
$nmo = null;
|
|
|
|
$this->invitation = null;
|
2022-11-01 11:20:28 +01:00
|
|
|
$this->company = null;
|
|
|
|
$this->entity_string = null;
|
|
|
|
$this->entity = null;
|
|
|
|
$this->settings = null;
|
|
|
|
$this->reminder_template = null;
|
|
|
|
$this->html_engine = null;
|
|
|
|
$this->template_data = null;
|
|
|
|
$this->email_entity_builder = null;
|
2020-10-27 01:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function resolveEntityString() :string
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($this->invitation instanceof InvoiceInvitation) {
|
2020-10-27 01:05:42 +01:00
|
|
|
return 'invoice';
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($this->invitation instanceof QuoteInvitation) {
|
2020-10-27 01:05:42 +01:00
|
|
|
return 'quote';
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($this->invitation instanceof CreditInvitation) {
|
2020-10-27 01:05:42 +01:00
|
|
|
return 'credit';
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($this->invitation instanceof RecurringInvoiceInvitation) {
|
2020-10-27 01:05:42 +01:00
|
|
|
return 'recurring_invoice';
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-27 01:05:42 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 13:56:12 +01:00
|
|
|
/* Switch statement to handle failure notifications */
|
2020-10-27 01:05:42 +01:00
|
|
|
private function entityEmailFailed($message)
|
|
|
|
{
|
|
|
|
switch ($this->entity_string) {
|
|
|
|
case 'invoice':
|
2021-05-06 23:12:07 +02:00
|
|
|
event(new InvoiceWasEmailedAndFailed($this->invitation, $this->company, $message, $this->reminder_template, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
|
2020-10-27 01:05:42 +01:00
|
|
|
break;
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2020-10-27 01:05:42 +01:00
|
|
|
default:
|
2022-06-21 11:57:17 +02:00
|
|
|
// code...
|
2020-10-27 01:05:42 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 04:49:22 +01:00
|
|
|
/* Builds the email builder object */
|
2020-10-27 12:57:12 +01:00
|
|
|
private function resolveEmailBuilder()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$class = 'App\Mail\Engine\\'.ucfirst(Str::camel($this->entity_string)).'EmailEngine';
|
2020-10-27 12:57:12 +01:00
|
|
|
|
2020-11-05 11:14:30 +01:00
|
|
|
return (new $class($this->invitation, $this->reminder_template, $this->template_data))->build();
|
2020-10-27 12:57:12 +01:00
|
|
|
}
|
2022-06-24 15:49:22 +02:00
|
|
|
|
|
|
|
public function failed($e)
|
|
|
|
{
|
2022-07-05 08:15:46 +02:00
|
|
|
// nlog($e->getMessage());
|
2022-06-24 15:49:22 +02:00
|
|
|
}
|
2020-10-27 01:05:42 +01:00
|
|
|
}
|