Refactor notifications to be more specific, ie. Invoice -> Reminder 1 template

This commit is contained in:
David Bomba 2021-01-13 18:20:46 +11:00
parent ff203c251f
commit 7f03a1900e
15 changed files with 85 additions and 27 deletions

View File

@ -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',
],

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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'));
}
}

View File

@ -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 {

View File

@ -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();

View File

@ -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)

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
}
}
}
}

View File

@ -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(),

View File

@ -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));

View File

@ -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'));
}
}
}