diff --git a/app/Events/Invoice/InvoiceWasEmailedAndFailed.php b/app/Events/Invoice/InvoiceWasEmailedAndFailed.php index b999d027a2f2..6fd9baad561e 100644 --- a/app/Events/Invoice/InvoiceWasEmailedAndFailed.php +++ b/app/Events/Invoice/InvoiceWasEmailedAndFailed.php @@ -12,7 +12,7 @@ namespace App\Events\Invoice; use App\Models\Company; -use App\Models\Invoice; +use App\Models\InvoiceInvitation; use Illuminate\Queue\SerializesModels; /** @@ -22,36 +22,34 @@ class InvoiceWasEmailedAndFailed { use SerializesModels; - /** - * @var Invoice - */ - public $invoice; + public $invitation; - /** - * @var array - */ - public $errors; + public $message; public $company; public $event_vars; + public $template; + /** * Create a new event instance. * - * @param Invoice $invoice + * @param InvoiceInvitation $invitation * @param Company $company * @param string $errors * @param array $event_vars */ - public function __construct(Invoice $invoice, Company $company, string $errors, array $event_vars) + public function __construct(InvoiceInvitation $invitation, Company $company, string $message, string $template, array $event_vars) { - $this->invoice = $invoice; + $this->invitation = $invitation; $this->company = $company; - $this->errors = $errors; + $this->message = $message; $this->event_vars = $event_vars; + + $this->template = $template; } } diff --git a/app/Helpers/Generic.php b/app/Helpers/Generic.php index 4ea60eeb8de7..ace00ff6f711 100644 --- a/app/Helpers/Generic.php +++ b/app/Helpers/Generic.php @@ -30,7 +30,7 @@ function nlog($output, $context = []): void } $trace = debug_backtrace(); - \Illuminate\Support\Facades\Log::channel('invoiceninja')->info(print_r($trace[1]['class'],1), []); + // \Illuminate\Support\Facades\Log::channel('invoiceninja')->info(print_r($trace[1]['class'],1), []); \Illuminate\Support\Facades\Log::channel('invoiceninja')->info($output, $context); } diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index ef1515fc7407..eac013a5b7c4 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -129,9 +129,6 @@ class EmailController extends BaseController ]; $entity_obj->service()->markSent()->save(); - - //@TODO why is this dispatchNow instead of just dispatch? - //update - changing to dispatch and see if something breaks. EmailEntity::dispatch($invitation, $invitation->company, $template, $data)->delay(now()->addSeconds(5)); } }); diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index ba5f07d7cab3..1af1b10a01ca 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -711,6 +711,7 @@ class InvoiceController extends BaseController break; case 'email': //check query parameter for email_type and set the template else use calculateTemplate + if (request()->has('email_type') && property_exists($invoice->company->settings, request()->input('email_type'))) { $this->reminder_template = $invoice->client->getSetting(request()->input('email_type')); } else { @@ -725,7 +726,7 @@ class InvoiceController extends BaseController }); if ($invoice->invitations->count() >= 1) { - $invoice->entityEmailEvent($invoice->invitations->first(), $this->reminder_template); + $invoice->entityEmailEvent($invoice->invitations->first(), 'invoice', $this->reminder_template); } if (! $bulk) { diff --git a/app/Jobs/Entity/EmailEntity.php b/app/Jobs/Entity/EmailEntity.php index 0021aad84b88..44a6eafd2f5d 100644 --- a/app/Jobs/Entity/EmailEntity.php +++ b/app/Jobs/Entity/EmailEntity.php @@ -15,6 +15,7 @@ use App\Events\Invoice\InvoiceReminderWasEmailed; use App\Events\Invoice\InvoiceWasEmailed; use App\Events\Invoice\InvoiceWasEmailedAndFailed; use App\Jobs\Mail\BaseMailerJob; +use App\Jobs\Mail\EntityFailedSendMailer; use App\Libraries\MultiDB; use App\Mail\TemplateEmail; use App\Models\Activity; @@ -110,7 +111,6 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue ) ); } catch (\Exception $e) { - $this->entityEmailFailed($e->getMessage()); $this->logMailError($e->getMessage(), $this->entity->client); } @@ -136,7 +136,7 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue { switch ($this->entity_string) { case 'invoice': - event(new InvoiceWasEmailedAndFailed($this->invitation->invoice, $this->company, $message, Ninja::eventVars())); + event(new InvoiceWasEmailedAndFailed($this->invitation, $this->company, $message, $this->reminder_template, Ninja::eventVars())); break; default: diff --git a/app/Jobs/Mail/EntityFailedSendMailer.php b/app/Jobs/Mail/EntityFailedSendMailer.php index 4f4b5f7a2354..0f330f6f5c9b 100644 --- a/app/Jobs/Mail/EntityFailedSendMailer.php +++ b/app/Jobs/Mail/EntityFailedSendMailer.php @@ -12,6 +12,7 @@ namespace App\Jobs\Mail; use App\Libraries\MultiDB; +use App\Mail\Admin\EntityFailedSendObject; use App\Mail\Admin\EntityNotificationMailer; use App\Mail\Admin\EntitySentObject; use Illuminate\Bus\Queueable; @@ -40,6 +41,8 @@ class EntityFailedSendMailer extends BaseMailerJob implements ShouldQueue public $settings; public $template; + + public $message; /** * Create a new job instance. * @@ -48,7 +51,7 @@ class EntityFailedSendMailer extends BaseMailerJob implements ShouldQueue * @param $user * @param $company */ - public function __construct($invitation, $entity_type, $user, $company, $template) + public function __construct($invitation, $entity_type, $user, $company, $template, $message) { $this->company = $company; @@ -63,6 +66,8 @@ class EntityFailedSendMailer extends BaseMailerJob implements ShouldQueue $this->settings = $invitation->contact->client->getMergedSettings(); $this->template = $template; + + $this->message = $message; } /** @@ -72,12 +77,11 @@ class EntityFailedSendMailer extends BaseMailerJob implements ShouldQueue */ public function handle() { - nlog("entity sent mailer"); + nlog("failed entity sent mailer"); /*If we are migrating data we don't want to fire these notification*/ - if ($this->company->is_disabled) { + if ($this->company->is_disabled) return true; - } //Set DB MultiDB::setDb($this->company->db); @@ -85,13 +89,14 @@ class EntityFailedSendMailer extends BaseMailerJob implements ShouldQueue //if we need to set an email driver do it now $this->setMailDriver(); - $mail_obj = (new EntitySentObject($this->invitation, $this->entity_type, $this->template))->build(); + $mail_obj = (new EntityFailedSendObject($this->invitation, $this->entity_type, $this->template, $this->message))->build(); $mail_obj->from = [config('mail.from.address'), config('mail.from.name')]; try { Mail::to($this->user->email) ->send(new EntityNotificationMailer($mail_obj)); } catch (\Exception $e) { + nlog("failing in EntityFailedSendMailer"); $this->failed($e); $this->logMailError($e->getMessage(), $this->entity->client); } diff --git a/app/Listeners/Invoice/InvoiceEmailFailedActivity.php b/app/Listeners/Invoice/InvoiceEmailFailedActivity.php index cec37c1794ab..feebbfd923f6 100644 --- a/app/Listeners/Invoice/InvoiceEmailFailedActivity.php +++ b/app/Listeners/Invoice/InvoiceEmailFailedActivity.php @@ -39,17 +39,19 @@ class InvoiceEmailFailedActivity implements ShouldQueue */ public function handle($event) { + nlog("inside activity_repo"); + MultiDB::setDb($event->company->db); $fields = new stdClass; - $fields->invoice_id = $event->invoice->id; - $fields->client_id = $event->invoice->client_id; - $fields->user_id = $event->invoice->user_id; - $fields->company_id = $event->invoice->company_id; + $fields->invoice_id = $event->invitation->invoice->id; + $fields->client_id = $event->invitation->invoice->client_id; + $fields->user_id = $event->invitation->invoice->user_id; + $fields->company_id = $event->invitation->invoice->company_id; $fields->activity_type_id = Activity::EMAIL_INVOICE_FAILED; - $fields->notes = $event->errors; + $fields->notes = $event->message; - $this->activity_repo->save($fields, $event->invoice, $event->event_vars); + $this->activity_repo->save($fields, $event->invitation->invoice, $event->event_vars); } } diff --git a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php index 8c55ce5520b8..3ce6f7ce73b4 100644 --- a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php +++ b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php @@ -11,6 +11,7 @@ namespace App\Listeners\Invoice; +use App\Jobs\Mail\EntityFailedSendMailer; use App\Jobs\Mail\EntitySentMailer; use App\Libraries\MultiDB; use App\Notifications\Admin\EntitySentNotification; @@ -33,6 +34,8 @@ class InvoiceFailedEmailNotification implements ShouldQueue */ public function handle($event) { + nlog("inside a failed notification"); + MultiDB::setDb($event->company->db); $first_notification_sent = true; @@ -51,7 +54,7 @@ class InvoiceFailedEmailNotification implements ShouldQueue if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { unset($methods[$key]); - EntitySentMailer::dispatch($event->invitation, 'invoice', $user, $event->invitation->company, $event->template); + EntityFailedSendMailer::dispatch($event->invitation, 'invoice', $user, $event->invitation->company, $event->template, $event->message); $first_notification_sent = false; } diff --git a/app/Mail/Admin/EntityFailedSendObject.php b/app/Mail/Admin/EntityFailedSendObject.php index 7ef6ee23f9b3..e1b74dc736f1 100644 --- a/app/Mail/Admin/EntityFailedSendObject.php +++ b/app/Mail/Admin/EntityFailedSendObject.php @@ -34,7 +34,9 @@ class EntityFailedSendObject private $template_body; - public function __construct($invitation, $entity_type, $template) + private $message; + + public function __construct($invitation, $entity_type, $template, $message) { $this->invitation = $invitation; $this->entity_type = $entity_type; @@ -42,6 +44,7 @@ class EntityFailedSendObject $this->contact = $invitation->contact; $this->company = $invitation->company; $this->template = $template; + $this->message = $message; } public function build() @@ -127,6 +130,7 @@ class EntityFailedSendObject 'amount' => $this->getAmount(), 'client' => $this->contact->present()->name(), 'invoice' => $this->entity->number, + 'error' => $this->message, ] ), 'url' => $this->invitation->getAdminLink(), diff --git a/app/Mail/Admin/EntityNotificationMailer.php b/app/Mail/Admin/EntityNotificationMailer.php index 94d409c6bf6f..c41eb30cac7b 100644 --- a/app/Mail/Admin/EntityNotificationMailer.php +++ b/app/Mail/Admin/EntityNotificationMailer.php @@ -34,6 +34,7 @@ class EntityNotificationMailer extends Mailable */ public function build() { + return $this->from(config('mail.from.address'), config('mail.from.name')) ->subject($this->mail_obj->subject) ->markdown($this->mail_obj->markdown, $this->mail_obj->data) diff --git a/app/Mail/Admin/EntitySentObject.php b/app/Mail/Admin/EntitySentObject.php index ad7da9877e60..b493061cbe2c 100644 --- a/app/Mail/Admin/EntitySentObject.php +++ b/app/Mail/Admin/EntitySentObject.php @@ -14,7 +14,7 @@ namespace App\Mail\Admin; use App\Utils\Number; use stdClass; -class EntityFailedSendObject +class EntitySentObject { public $invitation; @@ -91,6 +91,7 @@ class EntityFailedSendObject $this->template_subject = "texts.notification_credit_sent_subject"; $this->template_body = "texts.notification_credit_sent"; break; + default: $this->template_subject = "texts.notification_invoice_sent_subject"; $this->template_body = "texts.notification_invoice_sent"; @@ -115,20 +116,25 @@ class EntityFailedSendObject ); } - private function getData() + private function getMessage() { - $settings = $this->entity->client->getMergedSettings(); - - return [ - 'title' => $this->getSubject(), - 'message' => ctrans( + return ctrans( $this->template_body, [ 'amount' => $this->getAmount(), 'client' => $this->contact->present()->name(), 'invoice' => $this->entity->number, ] - ), + ); + } + + private function getData() + { + $settings = $this->entity->client->getMergedSettings(); + + return [ + 'title' => $this->getSubject(), + 'message' => $this->getMessage(), 'url' => $this->invitation->getAdminLink(), 'button' => ctrans("texts.view_{$this->entity_type}"), 'signature' => $settings->email_signature, diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index b77a47db3c4f..8a5209f6ff49 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -42,6 +42,7 @@ use App\Events\Invoice\InvoiceWasCancelled; use App\Events\Invoice\InvoiceWasCreated; use App\Events\Invoice\InvoiceWasDeleted; use App\Events\Invoice\InvoiceWasEmailed; +use App\Events\Invoice\InvoiceWasEmailedAndFailed; use App\Events\Invoice\InvoiceWasMarkedSent; use App\Events\Invoice\InvoiceWasPaid; use App\Events\Invoice\InvoiceWasRestored; @@ -128,6 +129,7 @@ use App\Listeners\Invoice\InvoiceDeletedActivity; use App\Listeners\Invoice\InvoiceEmailActivity; use App\Listeners\Invoice\InvoiceEmailFailedActivity; use App\Listeners\Invoice\InvoiceEmailedNotification; +use App\Listeners\Invoice\InvoiceFailedEmailNotification; use App\Listeners\Invoice\InvoicePaidActivity; use App\Listeners\Invoice\InvoiceReminderEmailActivity; use App\Listeners\Invoice\InvoiceRestoredActivity; @@ -310,6 +312,7 @@ class EventServiceProvider extends ServiceProvider ], InvoiceWasEmailedAndFailed::class => [ InvoiceEmailFailedActivity::class, + InvoiceFailedEmailNotification::class, ], InvoiceReminderWasEmailed::class => [ InvoiceReminderEmailActivity::class, diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index a7be2323013e..3ef41ab64de3 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -728,9 +728,9 @@ return [ 'disable' => 'Disable', 'invoice_quote_number' => 'Invoice and Quote Numbers', 'invoice_charges' => 'Invoice Surcharges', - 'notification_invoice_bounced' => 'We were unable to deliver Invoice :invoice to :contact.', + 'notification_invoice_bounced' => 'We were unable to deliver Invoice :invoice to :contact. \n :error', 'notification_invoice_bounced_subject' => 'Unable to deliver Invoice :invoice', - 'notification_quote_bounced' => 'We were unable to deliver Quote :invoice to :contact.', + 'notification_quote_bounced' => 'We were unable to deliver Quote :invoice to :contact. \n :error', 'notification_quote_bounced_subject' => 'Unable to deliver Quote :invoice', 'custom_invoice_link' => 'Custom Invoice Link', 'total_invoiced' => 'Total Invoiced', @@ -3372,6 +3372,6 @@ return [ 'required_payment_information_more' => 'To complete a payment we need more details about you.', 'required_client_info_save_label' => 'We will save this, so you don\'t have to enter it next time.', - 'notification_credit_bounced' => 'We were unable to deliver Credit :invoice to :contact.', + 'notification_credit_bounced' => 'We were unable to deliver Credit :invoice to :contact. \n :error', 'notification_credit_bounced_subject' => 'Unable to deliver Credit :invoice', ];