2017-01-30 21:40:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Ninja\Mailers;
|
2015-03-17 07:45:25 +10:00
|
|
|
|
2015-10-11 17:41:09 +03:00
|
|
|
use App\Models\Invitation;
|
2015-03-31 21:50:58 +03:00
|
|
|
use App\Models\Invoice;
|
|
|
|
|
use App\Models\Payment;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
2015-03-17 07:45:25 +10:00
|
|
|
class UserMailer extends Mailer
|
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 21:40:43 +02:00
|
|
|
* @param User $user
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param User|null $invitor
|
|
|
|
|
*/
|
2015-03-17 07:45:25 +10:00
|
|
|
public function sendConfirmation(User $user, User $invitor = null)
|
|
|
|
|
{
|
2017-01-30 21:40:43 +02:00
|
|
|
if (! $user->email) {
|
2015-03-17 07:45:25 +10:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$view = 'confirm';
|
|
|
|
|
$subject = trans('texts.confirmation_subject');
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'user' => $user,
|
|
|
|
|
'invitationMessage' => $invitor ? trans('texts.invitation_message', ['invitor' => $invitor->getDisplayName()]) : '',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($invitor) {
|
|
|
|
|
$fromEmail = $invitor->email;
|
|
|
|
|
$fromName = $invitor->getDisplayName();
|
|
|
|
|
} else {
|
|
|
|
|
$fromEmail = CONTACT_EMAIL;
|
|
|
|
|
$fromName = CONTACT_NAME;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->sendTo($user->email, $fromEmail, $fromName, $subject, $view, $data);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 10:52:53 +02:00
|
|
|
/**
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param User|null $invitor
|
|
|
|
|
*/
|
|
|
|
|
public function sendEmailChanged(User $user)
|
|
|
|
|
{
|
|
|
|
|
$oldEmail = $user->getOriginal('email');
|
|
|
|
|
$newEmail = $user->email;
|
|
|
|
|
|
|
|
|
|
if (! $oldEmail || ! $newEmail) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$view = 'user_message';
|
|
|
|
|
$subject = trans('texts.email_address_changed');
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'user' => $user,
|
|
|
|
|
'userName' => $user->getDisplayName(),
|
|
|
|
|
'primaryMessage' => trans('texts.email_address_changed_message', ['old_email' => $oldEmail, 'new_email' => $newEmail]),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->sendTo($oldEmail, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 21:40:43 +02:00
|
|
|
* @param User $user
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param Invoice $invoice
|
|
|
|
|
* @param $notificationType
|
|
|
|
|
* @param Payment|null $payment
|
|
|
|
|
*/
|
|
|
|
|
public function sendNotification(
|
|
|
|
|
User $user,
|
|
|
|
|
Invoice $invoice,
|
|
|
|
|
$notificationType,
|
2022-01-02 23:01:07 +11:00
|
|
|
?Payment $payment,
|
2017-04-16 10:06:42 +03:00
|
|
|
$notes = false
|
2017-01-30 18:05:31 +02:00
|
|
|
) {
|
2018-05-14 20:08:32 +03:00
|
|
|
if (! $user->shouldNotify($invoice)) {
|
2015-03-17 07:45:25 +10:00
|
|
|
return;
|
|
|
|
|
}
|
2016-06-07 14:39:53 +03:00
|
|
|
|
2016-02-03 22:53:18 +02:00
|
|
|
$entityType = $invoice->getEntityType();
|
|
|
|
|
$view = ($notificationType == 'approved' ? ENTITY_QUOTE : ENTITY_INVOICE) . "_{$notificationType}";
|
2015-12-07 15:34:55 +02:00
|
|
|
$account = $user->account;
|
|
|
|
|
$client = $invoice->client;
|
2017-03-23 15:55:46 +02:00
|
|
|
$link = $invoice->present()->multiAccountLink;
|
2017-01-29 22:07:44 +02:00
|
|
|
|
2015-03-17 07:45:25 +10:00
|
|
|
$data = [
|
|
|
|
|
'entityType' => $entityType,
|
2015-12-07 15:34:55 +02:00
|
|
|
'clientName' => $client->getDisplayName(),
|
|
|
|
|
'accountName' => $account->getDisplayName(),
|
2015-03-17 07:45:25 +10:00
|
|
|
'userName' => $user->getDisplayName(),
|
2015-12-07 15:34:55 +02:00
|
|
|
'invoiceAmount' => $account->formatMoney($invoice->getRequestedAmount(), $client),
|
2015-03-17 07:45:25 +10:00
|
|
|
'invoiceNumber' => $invoice->invoice_number,
|
2017-01-29 22:07:44 +02:00
|
|
|
'invoiceLink' => $link,
|
2015-12-16 13:49:26 +02:00
|
|
|
'account' => $account,
|
2015-03-17 07:45:25 +10:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($payment) {
|
2016-04-30 17:54:56 -04:00
|
|
|
$data['payment'] = $payment;
|
2015-12-07 15:34:55 +02:00
|
|
|
$data['paymentAmount'] = $account->formatMoney($payment->amount, $client);
|
2015-03-17 07:45:25 +10:00
|
|
|
}
|
|
|
|
|
|
2015-12-07 15:34:55 +02:00
|
|
|
$subject = trans("texts.notification_{$entityType}_{$notificationType}_subject", [
|
|
|
|
|
'invoice' => $invoice->invoice_number,
|
2017-01-30 21:40:43 +02:00
|
|
|
'client' => $client->getDisplayName(),
|
2015-12-07 15:34:55 +02:00
|
|
|
]);
|
2016-06-07 14:39:53 +03:00
|
|
|
|
2017-04-16 10:06:42 +03:00
|
|
|
if ($notes) {
|
|
|
|
|
$subject .= ' [' . trans('texts.notes_' . $notes) . ']';
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-17 07:45:25 +10:00
|
|
|
$this->sendTo($user->email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
|
|
|
|
|
}
|
2015-10-11 17:41:09 +03:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
|
* @param Invitation $invitation
|
|
|
|
|
*/
|
2015-10-11 17:41:09 +03:00
|
|
|
public function sendEmailBounced(Invitation $invitation)
|
|
|
|
|
{
|
|
|
|
|
$user = $invitation->user;
|
2015-12-16 13:49:26 +02:00
|
|
|
$account = $user->account;
|
2015-10-11 17:41:09 +03:00
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
|
$entityType = $invoice->getEntityType();
|
|
|
|
|
|
2017-01-30 21:40:43 +02:00
|
|
|
if (! $user->email) {
|
2015-10-11 17:41:09 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subject = trans("texts.notification_{$entityType}_bounced_subject", ['invoice' => $invoice->invoice_number]);
|
|
|
|
|
$view = 'email_bounced';
|
|
|
|
|
$data = [
|
|
|
|
|
'userName' => $user->getDisplayName(),
|
|
|
|
|
'emailError' => $invitation->email_error,
|
|
|
|
|
'entityType' => $entityType,
|
|
|
|
|
'contactName' => $invitation->contact->getDisplayName(),
|
|
|
|
|
'invoiceNumber' => $invoice->invoice_number,
|
|
|
|
|
];
|
2016-06-07 14:39:53 +03:00
|
|
|
|
2015-10-11 17:41:09 +03:00
|
|
|
$this->sendTo($user->email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
|
|
|
|
|
}
|
2016-08-13 22:19:37 +03:00
|
|
|
|
2017-03-23 15:55:46 +02:00
|
|
|
/**
|
|
|
|
|
* @param Invitation $invitation
|
|
|
|
|
*/
|
2017-07-19 12:59:56 +03:00
|
|
|
public function sendMessage($user, $subject, $message, $data = false)
|
2017-03-23 15:55:46 +02:00
|
|
|
{
|
|
|
|
|
if (! $user->email) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-19 12:59:56 +03:00
|
|
|
$invoice = $data && isset($data['invoice']) ? $data['invoice'] : false;
|
2017-03-23 15:55:46 +02:00
|
|
|
$view = 'user_message';
|
2017-07-19 12:59:56 +03:00
|
|
|
|
|
|
|
|
$data = $data ?: [];
|
|
|
|
|
$data += [
|
2017-03-23 15:55:46 +02:00
|
|
|
'userName' => $user->getDisplayName(),
|
2017-09-03 18:36:40 +03:00
|
|
|
'primaryMessage' => $message,
|
|
|
|
|
//'secondaryMessage' => $message,
|
2017-04-02 16:54:07 +03:00
|
|
|
'invoiceLink' => $invoice ? $invoice->present()->multiAccountLink : false,
|
2017-03-23 15:55:46 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->sendTo($user->email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-13 22:19:37 +03:00
|
|
|
public function sendSecurityCode($user, $code)
|
|
|
|
|
{
|
2017-01-30 21:40:43 +02:00
|
|
|
if (! $user->email) {
|
2016-08-13 22:19:37 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subject = trans('texts.security_code_email_subject');
|
|
|
|
|
$view = 'security_code';
|
|
|
|
|
$data = [
|
|
|
|
|
'userName' => $user->getDisplayName(),
|
|
|
|
|
'code' => $code,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->sendTo($user->email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
|
|
|
|
|
}
|
2017-11-14 22:34:56 +02:00
|
|
|
|
|
|
|
|
public function sendPasswordReset($user, $token)
|
|
|
|
|
{
|
|
|
|
|
if (! $user->email) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subject = trans('texts.your_password_reset_link');
|
|
|
|
|
$view = 'password';
|
|
|
|
|
$data = [
|
|
|
|
|
'token' => $token,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->sendTo($user->email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
|
|
|
|
|
}
|
2017-11-23 10:10:14 +02:00
|
|
|
|
|
|
|
|
public function sendScheduledReport($scheduledReport, $file)
|
|
|
|
|
{
|
|
|
|
|
$user = $scheduledReport->user;
|
|
|
|
|
$config = json_decode($scheduledReport->config);
|
|
|
|
|
|
|
|
|
|
if (! $user->email) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$subject = sprintf('%s - %s %s', APP_NAME, trans('texts.' . $config->report_type), trans('texts.report'));
|
|
|
|
|
$view = 'user_message';
|
|
|
|
|
$data = [
|
|
|
|
|
'userName' => $user->getDisplayName(),
|
|
|
|
|
'primaryMessage' => trans('texts.scheduled_report_attached', ['type' => trans('texts.' . $config->report_type)]),
|
|
|
|
|
'documents' => [[
|
|
|
|
|
'name' => $file->filename . '.' . $config->export_format,
|
|
|
|
|
'data' => $file->string($config->export_format),
|
|
|
|
|
]]
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->sendTo($user->email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
|
|
|
|
|
}
|
2015-03-17 07:45:25 +10:00
|
|
|
}
|