2020-03-03 10:44:26 +01:00
|
|
|
<?php
|
|
|
|
|
2020-03-04 12:09:43 +01:00
|
|
|
namespace App\Notifications\Admin;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
|
|
|
use App\Utils\Number;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2020-03-11 01:38:11 +01:00
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2020-03-03 10:44:26 +01:00
|
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
|
|
|
use Illuminate\Notifications\Messages\SlackMessage;
|
|
|
|
use Illuminate\Notifications\Notification;
|
|
|
|
|
2020-03-04 12:09:43 +01:00
|
|
|
class EntityViewedNotification extends Notification implements ShouldQueue
|
2020-03-03 10:44:26 +01:00
|
|
|
{
|
2020-03-11 01:38:11 +01:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new notification instance.
|
|
|
|
*
|
|
|
|
* @return void
|
2020-03-04 12:09:43 +01:00
|
|
|
* @
|
2020-03-03 10:44:26 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
protected $invitation;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
protected $entity_name;
|
2020-03-04 12:09:43 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
protected $entity;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
protected $company;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
protected $settings;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public $method;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
protected $contact;
|
2020-03-03 10:44:26 +01:00
|
|
|
|
2020-03-04 12:09:43 +01:00
|
|
|
public function __construct($invitation, $entity_name, $is_system = false, $settings = null)
|
2020-03-03 10:44:26 +01:00
|
|
|
{
|
2020-03-05 08:14:57 +01:00
|
|
|
$this->entity_name = $entity_name;
|
2020-03-04 12:09:43 +01:00
|
|
|
$this->entity = $invitation->{$entity_name};
|
2020-03-03 10:44:26 +01:00
|
|
|
$this->contact = $invitation->contact;
|
2020-03-04 12:09:43 +01:00
|
|
|
$this->company = $invitation->company;
|
|
|
|
$this->settings = $this->entity->client->getMergedSettings();
|
2020-03-03 10:44:26 +01:00
|
|
|
$this->is_system = $is_system;
|
2020-03-05 09:36:52 +01:00
|
|
|
$this->invitation = $invitation;
|
2020-03-10 23:20:09 +01:00
|
|
|
$this->method = null;
|
2020-03-03 10:44:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the notification's delivery channels.
|
|
|
|
*
|
|
|
|
* @param mixed $notifiable
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function via($notifiable)
|
|
|
|
{
|
2020-03-10 23:20:09 +01:00
|
|
|
return $this->method ?: [];
|
2020-03-03 10:44:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mail representation of the notification.
|
|
|
|
*
|
|
|
|
* @param mixed $notifiable
|
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage
|
|
|
|
*/
|
|
|
|
public function toMail($notifiable)
|
|
|
|
{
|
2020-03-04 12:09:43 +01:00
|
|
|
$data = $this->buildDataArray();
|
|
|
|
$subject = $this->buildSubject();
|
2020-03-03 10:44:26 +01:00
|
|
|
|
|
|
|
return (new MailMessage)
|
|
|
|
->subject($subject)
|
|
|
|
->markdown('email.admin.generic', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the array representation of the notification.
|
|
|
|
*
|
|
|
|
* @param mixed $notifiable
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toArray($notifiable)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
//
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toSlack($notifiable)
|
|
|
|
{
|
|
|
|
$logo = $this->company->present()->logo();
|
2020-03-04 12:09:43 +01:00
|
|
|
$amount = Number::formatMoney($this->entity->amount, $this->entity->client);
|
2020-03-03 10:44:26 +01:00
|
|
|
|
|
|
|
return (new SlackMessage)
|
2020-03-05 08:14:57 +01:00
|
|
|
->from(ctrans('texts.notification_bot'))
|
|
|
|
->success()
|
|
|
|
->image('https://app.invoiceninja.com/favicon-v2.png')
|
2020-03-21 06:37:30 +01:00
|
|
|
->content(ctrans(
|
|
|
|
"texts.notification_{$this->entity_name}_viewed",
|
2020-03-03 10:44:26 +01:00
|
|
|
[
|
2020-03-21 06:37:30 +01:00
|
|
|
'amount' => $amount,
|
|
|
|
'client' => $this->contact->present()->name(),
|
2020-03-04 12:09:43 +01:00
|
|
|
$this->entity_name => $this->entity->number
|
2020-03-21 06:37:30 +01:00
|
|
|
]
|
|
|
|
))
|
|
|
|
->attachment(function ($attachment) use ($amount) {
|
2020-03-05 08:14:57 +01:00
|
|
|
$attachment->title(ctrans('texts.entity_number_placeholder', ['entity' => ucfirst($this->entity_name), 'entity_number' => $this->entity->number]), $this->invitation->getAdminLink())
|
|
|
|
->fields([
|
|
|
|
ctrans('texts.client') => $this->contact->present()->name(),
|
|
|
|
ctrans('texts.status_viewed') => $this->invitation->viewed_date,
|
|
|
|
]);
|
|
|
|
});
|
2020-03-03 10:44:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 12:09:43 +01:00
|
|
|
|
|
|
|
private function buildDataArray()
|
|
|
|
{
|
|
|
|
$amount = Number::formatMoney($this->entity->amount, $this->entity->client);
|
|
|
|
|
|
|
|
$data = [
|
2020-03-05 09:36:52 +01:00
|
|
|
'title' => $this->buildSubject(),
|
2020-03-21 06:37:30 +01:00
|
|
|
'message' => ctrans(
|
|
|
|
"texts.notification_{$this->entity_name}_viewed",
|
2020-03-04 12:09:43 +01:00
|
|
|
[
|
2020-03-21 06:37:30 +01:00
|
|
|
'amount' => $amount,
|
|
|
|
'client' => $this->contact->present()->name(),
|
2020-03-04 12:09:43 +01:00
|
|
|
$this->entity_name => $this->entity->number,
|
2020-03-21 06:37:30 +01:00
|
|
|
]
|
|
|
|
),
|
2020-03-11 12:05:05 +01:00
|
|
|
'url' => $this->invitation->getAdminLink(),
|
2020-03-04 12:09:43 +01:00
|
|
|
'button' => ctrans("texts.view_{$this->entity_name}"),
|
|
|
|
'signature' => $this->settings->email_signature,
|
|
|
|
'logo' => $this->company->present()->logo(),
|
|
|
|
];
|
|
|
|
|
2020-03-05 08:14:57 +01:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildSubject()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
$subject = ctrans(
|
|
|
|
"texts.notification_{$this->entity_name}_viewed_subject",
|
|
|
|
[
|
|
|
|
'client' => $this->contact->present()->name(),
|
2020-03-05 08:14:57 +01:00
|
|
|
$this->entity_name => $this->entity->number,
|
2020-03-21 06:37:30 +01:00
|
|
|
]
|
|
|
|
);
|
2020-03-05 08:14:57 +01:00
|
|
|
|
|
|
|
return $subject;
|
2020-03-04 12:09:43 +01:00
|
|
|
}
|
2020-03-03 10:44:26 +01:00
|
|
|
}
|