From 7f03a1900ea4384482000f4ba975134cb4d77e51 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 13 Jan 2021 18:20:46 +1100 Subject: [PATCH 01/13] Refactor notifications to be more specific, ie. Invoice -> Reminder 1 template --- app/DataMapper/CompanySettings.php | 2 + app/Events/Credit/CreditWasEmailed.php | 4 +- app/Events/Invoice/InvoiceWasEmailed.php | 5 +- app/Events/Quote/QuoteWasEmailed.php | 4 +- app/Http/Controllers/EmailController.php | 8 +-- app/Jobs/Mail/EntitySentMailer.php | 9 ++- app/Jobs/Ninja/SendReminders.php | 2 +- app/Jobs/RecurringInvoice/SendRecurring.php | 5 +- app/Jobs/Util/ReminderJob.php | 2 +- .../Credit/CreditEmailedNotification.php | 2 +- .../Invoice/InvoiceEmailedNotification.php | 2 +- .../Quote/QuoteEmailedNotification.php | 4 +- app/Mail/Admin/EntitySentObject.php | 56 ++++++++++++++++++- app/Models/Invoice.php | 5 +- app/Services/Invoice/TriggeredActions.php | 2 +- 15 files changed, 85 insertions(+), 27 deletions(-) diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 3aadd6cea67f..a537aad97086 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -634,6 +634,7 @@ class CompanySettings extends BaseSettings '$product.description', '$product.unit_cost', '$product.quantity', + '$product.discount', '$product.tax', '$product.line_total', ], @@ -642,6 +643,7 @@ class CompanySettings extends BaseSettings '$task.description', '$task.rate', '$task.hours', + '$task.discount', '$task.tax', '$task.line_total', ], diff --git a/app/Events/Credit/CreditWasEmailed.php b/app/Events/Credit/CreditWasEmailed.php index ddb33ef8ee0c..68b6f1acfc93 100644 --- a/app/Events/Credit/CreditWasEmailed.php +++ b/app/Events/Credit/CreditWasEmailed.php @@ -27,6 +27,7 @@ class CreditWasEmailed public $event_vars; + public $template; /** * Create a new event instance. * @@ -34,10 +35,11 @@ class CreditWasEmailed * @param Company $company * @param array $event_vars */ - public function __construct(CreditInvitation $invitation, Company $company, array $event_vars) + public function __construct(CreditInvitation $invitation, Company $company, array $event_vars, string $template) { $this->invitation = $invitation; $this->company = $company; $this->event_vars = $event_vars; + $this->template = $template; } } diff --git a/app/Events/Invoice/InvoiceWasEmailed.php b/app/Events/Invoice/InvoiceWasEmailed.php index de0329a1028d..ce94a97e8089 100644 --- a/app/Events/Invoice/InvoiceWasEmailed.php +++ b/app/Events/Invoice/InvoiceWasEmailed.php @@ -31,6 +31,8 @@ class InvoiceWasEmailed public $event_vars; + public $template; + /** * Create a new event instance. * @@ -38,10 +40,11 @@ class InvoiceWasEmailed * @param Company $company * @param array $event_vars */ - public function __construct(InvoiceInvitation $invitation, Company $company, array $event_vars) + public function __construct(InvoiceInvitation $invitation, Company $company, array $event_vars, string $template) { $this->invitation = $invitation; $this->company = $company; $this->event_vars = $event_vars; + $this->template = $template; } } diff --git a/app/Events/Quote/QuoteWasEmailed.php b/app/Events/Quote/QuoteWasEmailed.php index 58b9846c9fda..2bbb548f49ab 100644 --- a/app/Events/Quote/QuoteWasEmailed.php +++ b/app/Events/Quote/QuoteWasEmailed.php @@ -29,6 +29,7 @@ class QuoteWasEmailed public $event_vars; + public $template; /** * Create a new event instance. * @@ -37,10 +38,11 @@ class QuoteWasEmailed * @param Company $company * @param array $event_vars */ - public function __construct(QuoteInvitation $invitation, Company $company, array $event_vars) + public function __construct(QuoteInvitation $invitation, Company $company, array $event_vars, string $template) { $this->invitation = $invitation; $this->company = $company; $this->event_vars = $event_vars; + $this->template = $template; } } diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index 97a325f0fa1e..ef1515fc7407 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -140,15 +140,13 @@ class EmailController extends BaseController $entity_obj->save(); /*Only notify the admin ONCE, not once per contact/invite*/ - // $invitation = $entity_obj->invitations->first(); - // EntitySentMailer::dispatch($invitation, $entity_string, $entity_obj->user, $invitation->company); if ($entity_obj instanceof Invoice) { $this->entity_type = Invoice::class; $this->entity_transformer = InvoiceTransformer::class; if ($entity_obj->invitations->count() >= 1) { - $entity_obj->entityEmailEvent($entity_obj->invitations->first(), 'invoice'); + $entity_obj->entityEmailEvent($entity_obj->invitations->first(), 'invoice', $template); } } @@ -157,7 +155,7 @@ class EmailController extends BaseController $this->entity_transformer = QuoteTransformer::class; if ($entity_obj->invitations->count() >= 1) { - event(new QuoteWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars())); + event(new QuoteWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(), 'quote')); } } @@ -166,7 +164,7 @@ class EmailController extends BaseController $this->entity_transformer = CreditTransformer::class; if ($entity_obj->invitations->count() >= 1) { - event(new CreditWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars())); + event(new CreditWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(), 'credit')); } } diff --git a/app/Jobs/Mail/EntitySentMailer.php b/app/Jobs/Mail/EntitySentMailer.php index e36cd521e9c9..b8c5bc988906 100644 --- a/app/Jobs/Mail/EntitySentMailer.php +++ b/app/Jobs/Mail/EntitySentMailer.php @@ -39,6 +39,7 @@ class EntitySentMailer extends BaseMailerJob implements ShouldQueue public $settings; + public $template; /** * Create a new job instance. * @@ -47,7 +48,7 @@ class EntitySentMailer extends BaseMailerJob implements ShouldQueue * @param $user * @param $company */ - public function __construct($invitation, $entity_type, $user, $company) + public function __construct($invitation, $entity_type, $user, $company, $template) { $this->company = $company; @@ -60,6 +61,8 @@ class EntitySentMailer extends BaseMailerJob implements ShouldQueue $this->entity_type = $entity_type; $this->settings = $invitation->contact->client->getMergedSettings(); + + $this->template = $template; } /** @@ -69,6 +72,8 @@ class EntitySentMailer extends BaseMailerJob implements ShouldQueue */ public function handle() { + nlog("entity sent mailer"); + /*If we are migrating data we don't want to fire these notification*/ if ($this->company->is_disabled) { return true; @@ -80,7 +85,7 @@ class EntitySentMailer 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))->build(); + $mail_obj = (new EntitySentObject($this->invitation, $this->entity_type, $this->template))->build(); $mail_obj->from = [config('mail.from.address'), config('mail.from.name')]; try { diff --git a/app/Jobs/Ninja/SendReminders.php b/app/Jobs/Ninja/SendReminders.php index 02c51f9f7693..278cbb9898d9 100644 --- a/app/Jobs/Ninja/SendReminders.php +++ b/app/Jobs/Ninja/SendReminders.php @@ -216,7 +216,7 @@ class SendReminders implements ShouldQueue if ($this->checkSendSetting($invoice, $template)) { - event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars())); + event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars(), $template)); } $invoice->last_sent_date = now(); diff --git a/app/Jobs/RecurringInvoice/SendRecurring.php b/app/Jobs/RecurringInvoice/SendRecurring.php index ea75bd00d5a9..b66f1a164ae1 100644 --- a/app/Jobs/RecurringInvoice/SendRecurring.php +++ b/app/Jobs/RecurringInvoice/SendRecurring.php @@ -94,10 +94,7 @@ class SendRecurring implements ShouldQueue nlog("last send date = " . $this->recurring_invoice->last_sent_date); $this->recurring_invoice->save(); - - //this is duplicated!! - // if ($invoice->invitations->count() > 0) - // event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars())); + } public function failed($exception = null) diff --git a/app/Jobs/Util/ReminderJob.php b/app/Jobs/Util/ReminderJob.php index 70a8cb378888..ea72979d617d 100644 --- a/app/Jobs/Util/ReminderJob.php +++ b/app/Jobs/Util/ReminderJob.php @@ -66,7 +66,7 @@ class ReminderJob implements ShouldQueue }); if ($invoice->invitations->count() > 0) { - event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars())); + event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars(), $reminder_template)); } } else { $invoice->next_send_date = null; diff --git a/app/Listeners/Credit/CreditEmailedNotification.php b/app/Listeners/Credit/CreditEmailedNotification.php index 085f2c6201b5..83d334d98a67 100644 --- a/app/Listeners/Credit/CreditEmailedNotification.php +++ b/app/Listeners/Credit/CreditEmailedNotification.php @@ -51,7 +51,7 @@ class CreditEmailedNotification implements ShouldQueue if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { unset($methods[$key]); - EntitySentMailer::dispatch($event->invitation, 'credit', $user, $event->invitation->company); + EntitySentMailer::dispatch($event->invitation, 'credit', $user, $event->invitation->company, $event->template); $first_notification_sent = false; } diff --git a/app/Listeners/Invoice/InvoiceEmailedNotification.php b/app/Listeners/Invoice/InvoiceEmailedNotification.php index e31356728ab3..08e9377dce1d 100644 --- a/app/Listeners/Invoice/InvoiceEmailedNotification.php +++ b/app/Listeners/Invoice/InvoiceEmailedNotification.php @@ -51,7 +51,7 @@ class InvoiceEmailedNotification 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); + EntitySentMailer::dispatch($event->invitation, 'invoice', $user, $event->invitation->company, $event->template); $first_notification_sent = false; } diff --git a/app/Listeners/Quote/QuoteEmailedNotification.php b/app/Listeners/Quote/QuoteEmailedNotification.php index a04e3595f96f..5098707380cd 100644 --- a/app/Listeners/Quote/QuoteEmailedNotification.php +++ b/app/Listeners/Quote/QuoteEmailedNotification.php @@ -51,7 +51,7 @@ class QuoteEmailedNotification implements ShouldQueue if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { unset($methods[$key]); - EntitySentMailer::dispatch($event->invitation, 'quote', $user, $event->invitation->company); + EntitySentMailer::dispatch($event->invitation, 'quote', $user, $event->invitation->company, $event->template); $first_notification_sent = false; } @@ -60,4 +60,4 @@ class QuoteEmailedNotification implements ShouldQueue $user->notify($notification); } } -} +} \ No newline at end of file diff --git a/app/Mail/Admin/EntitySentObject.php b/app/Mail/Admin/EntitySentObject.php index cf9da0f7dd28..f8cd6a2a8e3b 100644 --- a/app/Mail/Admin/EntitySentObject.php +++ b/app/Mail/Admin/EntitySentObject.php @@ -28,17 +28,26 @@ class EntitySentObject public $settings; - public function __construct($invitation, $entity_type) + public $template; + + private $template_subject; + + private $template_body; + + public function __construct($invitation, $entity_type, $template) { $this->invitation = $invitation; $this->entity_type = $entity_type; $this->entity = $invitation->{$entity_type}; $this->contact = $invitation->contact; $this->company = $invitation->company; + $this->template = $template; } public function build() { + $this->setTemplate(); + $mail_obj = new stdClass; $mail_obj->amount = $this->getAmount(); $mail_obj->subject = $this->getSubject(); @@ -49,6 +58,47 @@ class EntitySentObject return $mail_obj; } + private function setTemplate() + { + nlog($this->template); + + switch ($this->template) { + case 'invoice': + $this->template_subject = "texts.notification_invoice_sent_subject"; + $this->template_body = "texts.notification_invoice_sent"; + break; + case 'reminder1': + $this->template_subject = "texts.notification_invoice_reminder1_sent_subject"; + $this->template_body = "texts.notification_invoice_sent"; + break; + case 'reminder2': + $this->template_subject = "texts.notification_invoice_reminder2_sent_subject"; + $this->template_body = "texts.notification_invoice_sent"; + break; + case 'reminder3': + $this->template_subject = "texts.notification_invoice_reminder3_sent_subject"; + $this->template_body = "texts.notification_invoice_sent"; + break; + case 'reminder_endless': + $this->template_subject = "texts.notification_invoice_reminder_endless_sent_subject"; + $this->template_body = "texts.notification_invoice_sent"; + break; + case 'quote': + $this->template_subject = "texts.notification_quote_sent_subject"; + $this->template_body = "texts.notification_quote_sent"; + break; + case 'credit': + $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"; + break; + } + } + private function getAmount() { return Number::formatMoney($this->entity->amount, $this->entity->client); @@ -58,7 +108,7 @@ class EntitySentObject { return ctrans( - "texts.notification_{$this->entity_type}_sent_subject", + $this->template_subject, [ 'client' => $this->contact->present()->name(), 'invoice' => $this->entity->number, @@ -73,7 +123,7 @@ class EntitySentObject return [ 'title' => $this->getSubject(), 'message' => ctrans( - "texts.notification_{$this->entity_type}_sent", + $this->template_body, [ 'amount' => $this->getAmount(), 'client' => $this->contact->present()->name(), diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index d8982aa2ad70..fda940922f46 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -434,12 +434,11 @@ class Invoice extends BaseModel return $this->calc()->getTotal(); } - - public function entityEmailEvent($invitation, $reminder_template) + public function entityEmailEvent($invitation, $reminder_template, $template) { switch ($reminder_template) { case 'invoice': - event(new InvoiceWasEmailed($invitation, $invitation->company, Ninja::eventVars())); + event(new InvoiceWasEmailed($invitation, $invitation->company, Ninja::eventVars(), $template)); break; case 'reminder1': event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(), Activity::INVOICE_REMINDER1_SENT)); diff --git a/app/Services/Invoice/TriggeredActions.php b/app/Services/Invoice/TriggeredActions.php index 8c3b4a5d5787..74dc9dcbbe7e 100644 --- a/app/Services/Invoice/TriggeredActions.php +++ b/app/Services/Invoice/TriggeredActions.php @@ -66,7 +66,7 @@ class TriggeredActions extends AbstractService }); if ($this->invoice->invitations->count() > 0) { - event(new InvoiceWasEmailed($this->invoice->invitations->first(), $this->invoice->company, Ninja::eventVars())); + event(new InvoiceWasEmailed($this->invoice->invitations->first(), $this->invoice->company, Ninja::eventVars(), 'invoice')); } } } From 75081cf95a8a13b414b2dca6ac6093fa2751f1bb Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 13 Jan 2021 18:32:35 +1100 Subject: [PATCH 02/13] Fix for displaying company logo in portal --- .../ninja2020/components/general/sidebar/desktop.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/portal/ninja2020/components/general/sidebar/desktop.blade.php b/resources/views/portal/ninja2020/components/general/sidebar/desktop.blade.php index acb371424aaf..9887232bd1f7 100644 --- a/resources/views/portal/ninja2020/components/general/sidebar/desktop.blade.php +++ b/resources/views/portal/ninja2020/components/general/sidebar/desktop.blade.php @@ -2,7 +2,7 @@