diff --git a/VERSION.txt b/VERSION.txt index 4ea01d15c8f5..bdab8caeb267 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -5.0.33 \ No newline at end of file +5.0.34 \ No newline at end of file diff --git a/app/Events/Credit/CreditWasEmailed.php b/app/Events/Credit/CreditWasEmailed.php index fb47bcfa5283..a631b73e68eb 100644 --- a/app/Events/Credit/CreditWasEmailed.php +++ b/app/Events/Credit/CreditWasEmailed.php @@ -11,7 +11,8 @@ namespace App\Events\Credit; -use App\Models\Credit; +use App\Models\Company; +use App\Models\CreditInvitation; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Foundation\Events\Dispatchable; use Illuminate\Queue\SerializesModels; @@ -20,7 +21,7 @@ class CreditWasEmailed { use Dispatchable, InteractsWithSockets, SerializesModels; - public $credit; + public $invitation; public $company; @@ -33,9 +34,9 @@ class CreditWasEmailed * @param Company $company * @param array $event_vars */ - public function __construct(Credit $credit, Company $company, array $event_vars) + public function __construct(CreditInvitation $invitation, Company $company, array $event_vars) { - $this->credit = $credit; + $this->invitation = $invitation; $this->company = $company; $this->event_vars = $event_vars; } diff --git a/app/Events/Quote/QuoteWasEmailed.php b/app/Events/Quote/QuoteWasEmailed.php index 259a1a53ed97..b9ab80ba52ac 100644 --- a/app/Events/Quote/QuoteWasEmailed.php +++ b/app/Events/Quote/QuoteWasEmailed.php @@ -13,6 +13,7 @@ namespace App\Events\Quote; use App\Models\Company; use App\Models\Quote; +use App\Models\QuoteInvitation; use Illuminate\Queue\SerializesModels; /** @@ -22,12 +23,10 @@ class QuoteWasEmailed { use SerializesModels; - public $quote; + public $invitation; public $company; - public $notes; - public $event_vars; /** @@ -38,10 +37,9 @@ class QuoteWasEmailed * @param Company $company * @param array $event_vars */ - public function __construct(Quote $quote, string $notes, Company $company, array $event_vars) + public function __construct(QuoteInvitation $invitation, Company $company, array $event_vars) { - $this->quote = $quote; - $this->notes = $notes; + $this->invitation = $invitation; $this->company = $company; $this->event_vars = $event_vars; } diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index a255a44cf0ba..7bbbc959bf9b 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -11,6 +11,8 @@ namespace App\Http\Controllers; +use App\Events\Credit\CreditWasEmailed; +use App\Events\Quote\QuoteWasEmailed; use App\Http\Requests\Email\SendEmailRequest; use App\Jobs\Entity\EmailEntity; use App\Jobs\Mail\EntitySentMailer; @@ -22,6 +24,7 @@ use App\Transformers\CreditTransformer; use App\Transformers\InvoiceTransformer; use App\Transformers\QuoteTransformer; use App\Transformers\RecurringInvoiceTransformer; +use App\Utils\Ninja; use App\Utils\Traits\MakesHash; use Illuminate\Http\Response; @@ -117,6 +120,7 @@ class EmailController extends BaseController $template = $request->input('template'); $template = str_replace("email_template_", "", $template); + $entity_obj->invitations->each(function ($invitation) use ($subject, $body, $entity_string, $entity_obj, $template) { if ($invitation->contact->send_email && $invitation->contact->email) { $data = [ @@ -128,6 +132,8 @@ class EmailController extends BaseController } }); + $entity_obj->service()->markSent()->save(); + $entity_obj->last_sent_date = now(); $entity_obj->save(); @@ -138,16 +144,27 @@ class EmailController extends BaseController 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'); + } if ($entity_obj instanceof Quote) { $this->entity_type = Quote::class; $this->entity_transformer = QuoteTransformer::class; + + if($entity_obj->invitations->count() >= 1) + event(new QuoteWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars())); + } if ($entity_obj instanceof Credit) { $this->entity_type = Credit::class; $this->entity_transformer = CreditTransformer::class; + + if($entity_obj->invitations->count() >= 1) + event(new CreditWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars())); } if ($entity_obj instanceof RecurringInvoice) { @@ -155,7 +172,6 @@ class EmailController extends BaseController $this->entity_transformer = RecurringInvoiceTransformer::class; } - $entity_obj->service()->markSent()->save(); return $this->itemResponse($entity_obj); diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 16db0a89b093..8b8bb6c0e312 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -12,7 +12,9 @@ namespace App\Http\Controllers; +use App\Events\Invoice\InvoiceReminderWasEmailed; use App\Events\Invoice\InvoiceWasCreated; +use App\Events\Invoice\InvoiceWasEmailed; use App\Events\Invoice\InvoiceWasUpdated; use App\Factory\CloneInvoiceFactory; use App\Factory\CloneInvoiceToQuoteFactory; @@ -29,6 +31,7 @@ use App\Jobs\Entity\EmailEntity; use App\Jobs\Invoice\StoreInvoice; use App\Jobs\Invoice\ZipInvoices; use App\Jobs\Util\UnlinkFile; +use App\Models\Activity; use App\Models\Client; use App\Models\Invoice; use App\Models\Quote; @@ -721,14 +724,15 @@ class InvoiceController extends BaseController } //touch reminder1,2,3_sent + last_sent here if the email is a reminder. - $invoice->service()->touchReminder($this->reminder_template)->save(); $invoice->invitations->load('contact.client.country', 'invoice.client.country', 'invoice.company')->each(function ($invitation) use ($invoice) { - info("firing email"); EmailEntity::dispatch($invitation, $invoice->company, $this->reminder_template); }); + if($invoice->invitations->count() >= 1) + $invoice->entityEmailEvent($invoice->invitations->first(), $this->reminder_template); + if (! $bulk) { return response()->json(['message' => 'email sent'], 200); } diff --git a/app/Jobs/Entity/EmailEntity.php b/app/Jobs/Entity/EmailEntity.php index 6b7a5c0ccaa2..63e232b07b6c 100644 --- a/app/Jobs/Entity/EmailEntity.php +++ b/app/Jobs/Entity/EmailEntity.php @@ -56,6 +56,7 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue public $email_entity_builder; public $template_data; + /** * EmailEntity constructor. * @param Invitation $invitation @@ -100,7 +101,7 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue $this->setMailDriver(); try { - /** @noinspection PhpMethodParametersCountMismatchInspection */ + Mail::to($this->invitation->contact->email, $this->invitation->contact->present()->name()) ->send( new TemplateEmail( @@ -115,9 +116,9 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue $this->logMailError($e->getMessage(), $this->entity->client); } - if (count(Mail::failures()) == 0) { - $this->entityEmailSucceeded(); - } + // if (count(Mail::failures()) == 0) { + // $this->entityEmailSucceeded(); + // } /* Mark entity sent */ $this->entity->service()->markSent()->save(); @@ -149,29 +150,29 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue } } - private function entityEmailSucceeded() - { - switch ($this->reminder_template) { - case 'invoice': - event(new InvoiceWasEmailed($this->invitation, $this->company, Ninja::eventVars())); - break; - case 'reminder1': - event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER1_SENT)); - break; - case 'reminder2': - event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER2_SENT)); - break; - case 'reminder3': - event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER3_SENT)); - break; - case 'reminder_endless': - event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER_ENDLESS_SENT)); - break; - default: - # code... - break; - } - } + // private function entityEmailSucceeded() + // { + // switch ($this->reminder_template) { + // case 'invoice': + // event(new InvoiceWasEmailed($this->invitation, $this->company, Ninja::eventVars())); + // break; + // case 'reminder1': + // event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER1_SENT)); + // break; + // case 'reminder2': + // event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER2_SENT)); + // break; + // case 'reminder3': + // event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER3_SENT)); + // break; + // case 'reminder_endless': + // event(new InvoiceReminderWasEmailed($this->invitation, $this->company, Ninja::eventVars(), Activity::INVOICE_REMINDER_ENDLESS_SENT)); + // break; + // default: + // # code... + // break; + // } + // } private function resolveEmailBuilder() { diff --git a/app/Listeners/Credit/CreditEmailedNotification.php b/app/Listeners/Credit/CreditEmailedNotification.php new file mode 100644 index 000000000000..e218dc6ed3df --- /dev/null +++ b/app/Listeners/Credit/CreditEmailedNotification.php @@ -0,0 +1,63 @@ +company->db); + + $first_notification_sent = true; + + $credit = $event->invitation->credit; + $credit->last_sent_date = now(); + $credit->save(); + + foreach ($event->invitation->company->company_users as $company_user) { + $user = $company_user->user; + + $notification = new EntitySentNotification($event->invitation, 'credit'); + + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'credit', ['all_notifications', 'credit_sent']); + + if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { + unset($methods[$key]); + + EntitySentMailer::dispatch($event->invitation, 'credit', $user, $event->invitation->company); + $first_notification_sent = false; + } + + $notification->method = $methods; + + $user->notify($notification); + } + } +} diff --git a/app/Listeners/Quote/QuoteEmailedNotification.php b/app/Listeners/Quote/QuoteEmailedNotification.php new file mode 100644 index 000000000000..a4766245e9b0 --- /dev/null +++ b/app/Listeners/Quote/QuoteEmailedNotification.php @@ -0,0 +1,63 @@ +company->db); + + $first_notification_sent = true; + + $quote = $event->invitation->quote; + $quote->last_sent_date = now(); + $quote->save(); + + foreach ($event->invitation->company->company_users as $company_user) { + $user = $company_user->user; + + $notification = new EntitySentNotification($event->invitation, 'quote'); + + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'quote', ['all_notifications', 'quote_sent']); + + if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { + unset($methods[$key]); + + EntitySentMailer::dispatch($event->invitation, 'quote', $user, $event->invitation->company); + $first_notification_sent = false; + } + + $notification->method = $methods; + + $user->notify($notification); + } + } +} diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 240049aa40ae..40d6c52223c3 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -11,10 +11,13 @@ namespace App\Models; +use App\Events\Invoice\InvoiceReminderWasEmailed; +use App\Events\Invoice\InvoiceWasEmailed; use App\Events\Invoice\InvoiceWasUpdated; use App\Helpers\Invoice\InvoiceSum; use App\Helpers\Invoice\InvoiceSumInclusive; use App\Jobs\Entity\CreateEntityPdf; +use App\Models\Activity; use App\Models\Presenters\InvoicePresenter; use App\Services\Invoice\InvoiceService; use App\Services\Ledger\LedgerService; @@ -431,4 +434,30 @@ class Invoice extends BaseModel { return $this->calc()->getTotal(); } + + + public function entityEmailEvent($invitation, $reminder_template) + { + + switch ($reminder_template) { + case 'invoice': + event(new InvoiceWasEmailed($invitation, $invitation->company, Ninja::eventVars())); + break; + case 'reminder1': + event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(), Activity::INVOICE_REMINDER1_SENT)); + break; + case 'reminder2': + event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(), Activity::INVOICE_REMINDER2_SENT)); + break; + case 'reminder3': + event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(), Activity::INVOICE_REMINDER3_SENT)); + break; + case 'reminder_endless': + event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(), Activity::INVOICE_REMINDER_ENDLESS_SENT)); + break; + default: + # code... + break; + } + } } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 6c1bd6793c9f..26f9853a63ce 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -24,6 +24,7 @@ use App\Events\Contact\ContactLoggedIn; use App\Events\Credit\CreditWasArchived; use App\Events\Credit\CreditWasCreated; use App\Events\Credit\CreditWasDeleted; +use App\Events\Credit\CreditWasEmailed; use App\Events\Credit\CreditWasEmailedAndFailed; use App\Events\Credit\CreditWasMarkedSent; use App\Events\Credit\CreditWasRestored; @@ -110,6 +111,7 @@ use App\Listeners\Activity\VendorDeletedActivity; use App\Listeners\Activity\VendorRestoredActivity; use App\Listeners\Activity\VendorUpdatedActivity; use App\Listeners\Contact\UpdateContactLastLogin; +use App\Listeners\Credit\CreditEmailedNotification; use App\Listeners\Credit\CreditRestoredActivity; use App\Listeners\Credit\CreditViewedActivity; use App\Listeners\Document\DeleteCompanyDocuments; @@ -120,8 +122,8 @@ use App\Listeners\Invoice\InvoiceArchivedActivity; use App\Listeners\Invoice\InvoiceCancelledActivity; use App\Listeners\Invoice\InvoiceDeletedActivity; use App\Listeners\Invoice\InvoiceEmailActivity; -use App\Listeners\Invoice\InvoiceEmailedNotification; use App\Listeners\Invoice\InvoiceEmailFailedActivity; +use App\Listeners\Invoice\InvoiceEmailedNotification; use App\Listeners\Invoice\InvoicePaidActivity; use App\Listeners\Invoice\InvoiceReminderEmailActivity; use App\Listeners\Invoice\InvoiceRestoredActivity; @@ -129,14 +131,15 @@ use App\Listeners\Invoice\InvoiceReversedActivity; use App\Listeners\Invoice\InvoiceViewedActivity; use App\Listeners\Invoice\UpdateInvoiceActivity; use App\Listeners\Misc\InvitationViewedListener; -use App\Listeners\Payment\PaymentEmailedActivity; use App\Listeners\Payment\PaymentEmailFailureActivity; +use App\Listeners\Payment\PaymentEmailedActivity; use App\Listeners\Payment\PaymentNotification; use App\Listeners\Payment\PaymentRestoredActivity; use App\Listeners\Quote\QuoteApprovedActivity; use App\Listeners\Quote\QuoteArchivedActivity; use App\Listeners\Quote\QuoteDeletedActivity; use App\Listeners\Quote\QuoteEmailActivity; +use App\Listeners\Quote\QuoteEmailedNotification; use App\Listeners\Quote\QuoteRestoredActivity; use App\Listeners\Quote\QuoteViewedActivity; use App\Listeners\Quote\ReachWorkflowSettings; @@ -226,6 +229,7 @@ class EventServiceProvider extends ServiceProvider CreditWasEmailedAndFailed::class => [ ], CreditWasEmailed::class => [ + CreditEmailedNotification::class, ], CreditWasMarkedSent::class => [ ], @@ -332,6 +336,7 @@ class EventServiceProvider extends ServiceProvider ], QuoteWasEmailed::class => [ QuoteEmailActivity::class, + QuoteEmailedNotification::class, ], QuoteWasViewed::class => [ QuoteViewedActivity::class, diff --git a/app/Utils/Traits/MakesReminders.php b/app/Utils/Traits/MakesReminders.php index 6c87794df6e3..f3055b20b5fd 100644 --- a/app/Utils/Traits/MakesReminders.php +++ b/app/Utils/Traits/MakesReminders.php @@ -209,4 +209,7 @@ trait MakesReminders return null; } } + } + + diff --git a/config/ninja.php b/config/ninja.php index 8e2402b989e2..d67bf959d607 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -12,7 +12,7 @@ return [ 'require_https' => env('REQUIRE_HTTPS', true), 'app_url' => rtrim(env('APP_URL', ''), '/'), 'app_domain' => env('APP_DOMAIN', ''), - 'app_version' => '5.0.33', + 'app_version' => '5.0.34', 'minimum_client_version' => '5.0.16', 'terms_version' => '1.0.1', 'api_secret' => env('API_SECRET', false), diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 6538ce145461..8186f608f01d 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -3314,4 +3314,10 @@ return [ 'service' => 'Service', 'pay' => 'Pay', + '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_reminder_endless_sent_subject' => 'Endless reminder for Invoice :invoice was sent to :client', + + ];