From f39766d374ed263a0eacca75f4dc9b936a2ef1c1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 27 Apr 2023 13:15:49 +1000 Subject: [PATCH] Improve notifications for reminders --- app/Events/Invoice/InvoiceReminderWasEmailed.php | 2 +- app/Http/Controllers/EmailController.php | 7 ++++--- app/Http/Requests/Email/SendEmailRequest.php | 8 ++++++-- .../Invoice/InvoiceEmailedNotification.php | 16 ++++++++++++++++ app/Mail/Admin/EntitySentObject.php | 6 ++++++ app/Models/BaseModel.php | 3 ++- app/Models/Invoice.php | 15 +++++++++++---- lang/en/texts.php | 1 + lang/fr_CA/texts.php | 6 +++--- 9 files changed, 50 insertions(+), 14 deletions(-) diff --git a/app/Events/Invoice/InvoiceReminderWasEmailed.php b/app/Events/Invoice/InvoiceReminderWasEmailed.php index 4eec242ee0..3a2106f7de 100644 --- a/app/Events/Invoice/InvoiceReminderWasEmailed.php +++ b/app/Events/Invoice/InvoiceReminderWasEmailed.php @@ -22,7 +22,7 @@ class InvoiceReminderWasEmailed { use SerializesModels; - public function __construct(public InvoiceInvitation $invitation, public Company $company, public array $event_vars, public int $reminder) + public function __construct(public InvoiceInvitation $invitation, public Company $company, public array $event_vars, public string $template) { } } diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index a9fdf75d27..8cd59930b6 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -136,7 +136,10 @@ class EmailController extends BaseController $mo->email_template_body = $request->input('template'); $mo->email_template_subject = str_replace("template", "subject", $request->input('template')); - if ($request->has('cc_email') && $request->cc_email) { + /** @var \App\Models\User $user */ + $user = auth()->user(); + + if ($request->has('cc_email') && $request->cc_email && (Ninja::isSelfHost() || $user->account->isPaidHostedClient())) { $mo->cc[] = new Address($request->cc_email); } @@ -144,8 +147,6 @@ class EmailController extends BaseController if (! $invitation->contact->trashed() && $invitation->contact->email) { $entity_obj->service()->markSent()->save(); - // EmailEntity::dispatch($invitation->fresh(), $invitation->company, $template, $data); - $mo->invitation_id = $invitation->id; Email::dispatch($mo, $invitation->company); diff --git a/app/Http/Requests/Email/SendEmailRequest.php b/app/Http/Requests/Email/SendEmailRequest.php index 66a80e3f61..4b9a1b2098 100644 --- a/app/Http/Requests/Email/SendEmailRequest.php +++ b/app/Http/Requests/Email/SendEmailRequest.php @@ -92,7 +92,11 @@ class SendEmailRequest extends Request /*Make sure we have all the require ingredients to send a template*/ if (array_key_exists('entity', $input) && array_key_exists('entity_id', $input) && is_string($input['entity']) && $input['entity_id']) { - $company = auth()->user()->company(); + + /** @var \App\Models\User $user */ + $user = auth()->user(); + + $company = $user->company(); $entity = $input['entity']; @@ -100,7 +104,7 @@ class SendEmailRequest extends Request $entity_obj = $entity::whereId($input['entity_id'])->withTrashed()->company()->first(); /* Check object, check user and company id is same as users, and check user can edit the object */ - if ($entity_obj && ($company->id == $entity_obj->company_id) && auth()->user()->can('edit', $entity_obj)) { + if ($entity_obj && ($company->id == $entity_obj->company_id) && $user->can('edit', $entity_obj)) { return true; } } diff --git a/app/Listeners/Invoice/InvoiceEmailedNotification.php b/app/Listeners/Invoice/InvoiceEmailedNotification.php index 4eb588879f..bf0859e2da 100644 --- a/app/Listeners/Invoice/InvoiceEmailedNotification.php +++ b/app/Listeners/Invoice/InvoiceEmailedNotification.php @@ -37,6 +37,8 @@ class InvoiceEmailedNotification implements ShouldQueue */ public function handle($event) { + nlog($event->template); + MultiDB::setDb($event->company->db); $first_notification_sent = true; @@ -61,6 +63,20 @@ class InvoiceEmailedNotification implements ShouldQueue if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); + // $template = $event->template ?? ''; + + // if(isset($event->reminder)){ + + // $template = match($event->reminder){ + // 63 => 'reminder1', + // 64 => 'reminder2', + // 65 => 'reminder3', + // 66 => 'endless_reminder', + // default => '' + // }; + + // } + $nmo = new NinjaMailerObject; $nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'invoice', $event->template))->build()); $nmo->company = $invoice->company; diff --git a/app/Mail/Admin/EntitySentObject.php b/app/Mail/Admin/EntitySentObject.php index c3bc4eaa50..96c184254f 100644 --- a/app/Mail/Admin/EntitySentObject.php +++ b/app/Mail/Admin/EntitySentObject.php @@ -136,6 +136,12 @@ class EntitySentObject $this->template_subject = 'texts.notification_purchase_order_sent_subject'; $this->template_body = 'texts.notification_purchase_order_sent'; break; + case 'custom1': + case 'custom2': + case 'custom3': + $this->template_subject = 'texts.notification_invoice_custom_sent_subject'; + $this->template_body = 'texts.notification_invoice_sent'; + break; default: $this->template_subject = 'texts.notification_invoice_sent_subject'; $this->template_body = 'texts.notification_invoice_sent'; diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index 52901ea8a2..5670a06957 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -38,7 +38,8 @@ use Illuminate\Support\Str; * @method static \Illuminate\Database\Eloquent\Builder|BaseModel exclude(array $excludeable) * @method static \Illuminate\Database\Eloquent\Builder|BaseModel withTrashed() * @method static \Illuminate\Database\Eloquent\Builder|BaseModel scopeExclude() - * @method static \Illuminate\Database\Eloquent\Builder|BaseModel find() + * @method static \Illuminate\Database\Eloquent\Builder|BaseModel find() + * @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereIn() * @method \App\Models\Company company() * @method int companyId() * @method Builder|static exclude($columns) diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index d0dc1024f5..1e37f0ba7c 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -801,22 +801,29 @@ class Invoice extends BaseModel public function entityEmailEvent($invitation, $reminder_template, $template = '') { + nlog($template); + switch ($reminder_template) { case 'invoice': event(new InvoiceWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template)); break; case 'reminder1': - event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), Activity::INVOICE_REMINDER1_SENT)); + event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template)); break; case 'reminder2': - event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), Activity::INVOICE_REMINDER2_SENT)); + event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template)); break; case 'reminder3': - event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), Activity::INVOICE_REMINDER3_SENT)); + event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template)); break; case 'reminder_endless': case 'endless_reminder': - event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), Activity::INVOICE_REMINDER_ENDLESS_SENT)); + event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template)); + break; + case 'custom1': + case 'custom2': + case 'custom3': + event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template)); break; default: // code... diff --git a/lang/en/texts.php b/lang/en/texts.php index 2589724e3a..24628362fd 100644 --- a/lang/en/texts.php +++ b/lang/en/texts.php @@ -4012,6 +4012,7 @@ $LANG = array( 'notification_invoice_reminder1_sent_subject' => 'Reminder 1 for Invoice :invoice was sent to :client', 'notification_invoice_reminder2_sent_subject' => 'Reminder 2 for Invoice :invoice was sent to :client', 'notification_invoice_reminder3_sent_subject' => 'Reminder 3 for Invoice :invoice was sent to :client', + 'notification_invoice_custom_sent_subject' => 'Custom reminder for Invoice :invoice was sent to :client', 'notification_invoice_reminder_endless_sent_subject' => 'Endless reminder for Invoice :invoice was sent to :client', 'assigned_user' => 'Assigned User', 'setup_steps_notice' => 'To proceed to next step, make sure you test each section.', diff --git a/lang/fr_CA/texts.php b/lang/fr_CA/texts.php index 1478f597cc..691f2496f1 100644 --- a/lang/fr_CA/texts.php +++ b/lang/fr_CA/texts.php @@ -4882,7 +4882,7 @@ Lorsque les montant apparaîtront sur votre relevé, veuillez revenir sur cette 'email_queued' => 'Courriel en file d\'attente', 'clone_to_recurring_invoice' => 'Dupliquer en facture récurrente', 'inventory_threshold' => 'Seuil d\'inventaire', - 'emailed_statement' => 'L\'état de compte a été mis en file d\'attente pour l\'envoi', + 'emailed_statement' => 'Le relevé a été mis en file d\'attente pour l\'envoi', 'show_email_footer' => 'Afficher le pied de page du courriel', 'invoice_task_hours' => 'Facturer les heures de tâches', 'invoice_task_hours_help' => 'Ajouter ces heures aux articles de la facture', @@ -4910,7 +4910,7 @@ Lorsque les montant apparaîtront sur votre relevé, veuillez revenir sur cette 'all_clients' => 'Tous les clients', 'show_aging_table' => 'Afficher la liste des impayés', 'show_payments_table' => 'Afficher la liste des paiements', - 'email_statement' => 'Envoyer par courriel l\'état de compte', + 'email_statement' => 'Envoyer le relevé par courriel', 'once' => 'Une fois', 'schedules' => 'Planifications', 'new_schedule' => 'Nouvelle planification', @@ -5037,7 +5037,7 @@ Lorsque les montant apparaîtront sur votre relevé, veuillez revenir sur cette 'tax_all' => 'Tout taxer', 'tax_selected' => 'Taxe sélectionnée', 'version' => 'version', - 'seller_subregion' => 'Sous-région du vendeur', + 'seller_subregion' => 'Province du vendeur', 'calculate_taxes' => 'Calculer les taxes', 'calculate_taxes_help' => 'Calcul automatique des taxes à la sauvegarde des factures', 'link_expenses' => 'Lier les dépenses',