invoiceninja/app/Ninja/Mailers/ContactMailer.php

331 lines
10 KiB
PHP
Raw Normal View History

2017-01-30 21:40:43 +02:00
<?php
2015-03-17 07:45:25 +10:00
2017-01-30 21:40:43 +02:00
namespace App\Ninja\Mailers;
use App\Events\InvoiceWasEmailed;
use App\Events\QuoteWasEmailed;
use App\Models\Invitation;
2015-03-31 21:50:58 +03:00
use App\Models\Invoice;
use App\Models\Payment;
2017-01-30 21:40:43 +02:00
use App\Services\TemplateService;
use Auth;
use Event;
use Utils;
2015-03-17 07:45:25 +10:00
class ContactMailer extends Mailer
{
/**
* @var TemplateService
*/
protected $templateService;
/**
* ContactMailer constructor.
2017-01-30 21:40:43 +02:00
*
* @param TemplateService $templateService
*/
2016-05-05 17:46:22 +03:00
public function __construct(TemplateService $templateService)
{
$this->templateService = $templateService;
}
/**
* @param Invoice $invoice
2017-01-30 21:40:43 +02:00
* @param bool $reminder
* @param bool $pdfString
*
* @return bool|null|string
*/
2017-03-09 16:09:52 +02:00
public function sendInvoice(Invoice $invoice, $reminder = false, $template = false)
2015-03-17 07:45:25 +10:00
{
2017-01-29 13:30:57 +02:00
if ($invoice->is_recurring) {
return false;
}
$invoice->load('invitations', 'client.language', 'account');
2015-03-17 07:45:25 +10:00
$entityType = $invoice->getEntityType();
$client = $invoice->client;
$account = $invoice->account;
$response = null;
2015-11-16 21:29:22 +02:00
if ($client->trashed()) {
2016-05-15 23:16:08 +03:00
return trans('texts.email_error_inactive_client');
2015-11-16 21:29:22 +02:00
} elseif ($invoice->trashed()) {
2016-05-15 23:16:08 +03:00
return trans('texts.email_error_inactive_invoice');
2015-09-29 13:21:57 +03:00
}
$account->loadLocalizationSettings($client);
2017-02-05 23:29:11 +02:00
$emailTemplate = !empty($template['body']) ? $template['body'] : $account->getEmailTemplate($reminder ?: $entityType);
$emailSubject = !empty($template['subject']) ? $template['subject'] : $account->getEmailSubject($reminder ?: $entityType);
2015-03-17 07:45:25 +10:00
2015-09-17 22:01:06 +03:00
$sent = false;
2017-03-09 16:09:52 +02:00
$pdfString = false;
2017-03-09 16:09:52 +02:00
if ($account->attachPDF()) {
2016-02-17 21:13:55 +02:00
$pdfString = $invoice->getPDFString();
2015-10-13 10:11:44 +03:00
}
2016-05-15 23:16:08 +03:00
$documentStrings = [];
if ($account->document_email_attachment && $invoice->hasDocuments()) {
$documents = $invoice->documents;
2016-05-15 23:16:08 +03:00
2017-01-30 18:05:31 +02:00
foreach ($invoice->expenses as $expense) {
$documents = $documents->merge($expense->documents);
}
$documents = $documents->sortBy('size');
2016-05-15 23:16:08 +03:00
$size = 0;
$maxSize = MAX_EMAIL_DOCUMENTS_SIZE * 1000;
2017-01-30 18:05:31 +02:00
foreach ($documents as $document) {
$size += $document->size;
2017-01-30 18:05:31 +02:00
if ($size > $maxSize) {
break;
}
2016-05-15 23:16:08 +03:00
$documentStrings[] = [
'name' => $document->name,
'data' => $document->getRaw(),
];
}
}
2015-10-13 10:11:44 +03:00
$isFirst = true;
2015-03-17 07:45:25 +10:00
foreach ($invoice->invitations as $invitation) {
$response = $this->sendInvitation($invitation, $invoice, $emailTemplate, $emailSubject, $pdfString, $documentStrings, $reminder, $isFirst);
$isFirst = false;
2015-11-16 21:51:34 +02:00
if ($response === true) {
2015-10-11 17:41:09 +03:00
$sent = true;
2015-03-17 07:45:25 +10:00
}
2015-10-11 17:41:09 +03:00
}
2016-05-15 23:16:08 +03:00
2015-10-11 17:41:09 +03:00
$account->loadLocalizationSettings();
2015-09-17 22:01:06 +03:00
2015-10-11 17:41:09 +03:00
if ($sent === true) {
2016-05-26 17:56:54 +03:00
if ($invoice->isType(INVOICE_TYPE_QUOTE)) {
2015-10-28 21:22:07 +02:00
event(new QuoteWasEmailed($invoice));
} else {
event(new InvoiceWasEmailed($invoice));
}
2015-10-11 17:41:09 +03:00
}
2015-11-16 21:51:34 +02:00
return $response;
2015-10-11 17:41:09 +03:00
}
/**
* @param Invitation $invitation
2017-01-30 21:40:43 +02:00
* @param Invoice $invoice
* @param $body
* @param $subject
* @param $pdfString
* @param $documentStrings
2017-01-30 21:49:42 +02:00
* @param mixed $reminder
2017-01-30 21:40:43 +02:00
*
* @throws \Laracasts\Presenter\Exceptions\PresenterException
2017-01-30 21:40:43 +02:00
*
* @return bool|string
*/
private function sendInvitation(
2017-01-11 14:06:15 +02:00
Invitation $invitation,
Invoice $invoice,
$body,
$subject,
$pdfString,
2017-01-05 12:46:03 +02:00
$documentStrings,
$reminder,
$isFirst
2017-01-30 18:05:31 +02:00
) {
2015-10-11 17:41:09 +03:00
$client = $invoice->client;
$account = $invoice->account;
2016-05-15 23:16:08 +03:00
2015-10-11 17:41:09 +03:00
if (Auth::check()) {
$user = Auth::user();
} else {
$user = $invitation->user;
if ($invitation->user->trashed()) {
$user = $account->users()->orderBy('id')->first();
2015-03-17 07:45:25 +10:00
}
2015-10-11 17:41:09 +03:00
}
2015-03-17 07:45:25 +10:00
2017-01-30 21:40:43 +02:00
if (! $user->email || ! $user->registered) {
2016-05-15 23:16:08 +03:00
return trans('texts.email_error_user_unregistered');
2017-01-30 21:40:43 +02:00
} elseif (! $user->confirmed) {
2016-05-15 23:16:08 +03:00
return trans('texts.email_error_user_unconfirmed');
2017-01-30 21:40:43 +02:00
} elseif (! $invitation->contact->email) {
2016-05-15 23:16:08 +03:00
return trans('texts.email_error_invalid_contact_email');
2015-11-16 21:51:34 +02:00
} elseif ($invitation->contact->trashed()) {
2016-05-15 23:16:08 +03:00
return trans('texts.email_error_inactive_contact');
2015-10-11 17:41:09 +03:00
}
2015-03-17 07:45:25 +10:00
2015-10-11 17:41:09 +03:00
$variables = [
'account' => $account,
'client' => $client,
'invitation' => $invitation,
2017-01-30 21:40:43 +02:00
'amount' => $invoice->getRequestedAmount(),
2015-10-11 17:41:09 +03:00
];
2016-05-15 23:16:08 +03:00
2016-06-26 12:59:42 +03:00
// Let the client know they'll be billed later
2016-06-20 17:14:43 +03:00
if ($client->autoBillLater()) {
2016-06-26 12:59:42 +03:00
$variables['autobill'] = $invoice->present()->autoBillEmailMessage();
}
2016-06-20 17:14:43 +03:00
if (empty($invitation->contact->password) && $account->hasFeature(FEATURE_CLIENT_PORTAL_PASSWORD) && $account->enable_portal_password && $account->send_portal_password) {
// The contact needs a password
$variables['password'] = $password = $this->generatePassword();
$invitation->contact->password = bcrypt($password);
$invitation->contact->save();
}
2015-03-17 07:45:25 +10:00
2015-10-13 10:11:44 +03:00
$data = [
2016-05-05 17:46:22 +03:00
'body' => $this->templateService->processVariables($body, $variables),
2015-10-13 10:11:44 +03:00
'link' => $invitation->getLink(),
'entityType' => $invoice->getEntityType(),
'invoiceId' => $invoice->id,
'invitation' => $invitation,
2015-11-06 12:59:53 +02:00
'account' => $account,
2015-12-02 15:26:06 +02:00
'client' => $client,
2015-11-08 10:43:49 +02:00
'invoice' => $invoice,
'documents' => $documentStrings,
2017-01-05 12:46:03 +02:00
'notes' => $reminder,
'bccEmail' => $isFirst ? $account->getBccEmail() : false,
'fromEmail' => $account->getFromEmail(),
2015-10-13 10:11:44 +03:00
];
if ($account->attachPDF()) {
2015-10-13 10:11:44 +03:00
$data['pdfString'] = $pdfString;
$data['pdfFileName'] = $invoice->getFileName();
}
2015-03-17 07:45:25 +10:00
2016-05-05 17:46:22 +03:00
$subject = $this->templateService->processVariables($subject, $variables);
2015-10-11 17:41:09 +03:00
$fromEmail = $user->email;
2016-05-05 17:46:22 +03:00
$view = $account->getTemplateView(ENTITY_INVOICE);
2016-05-15 23:16:08 +03:00
2015-12-02 15:26:06 +02:00
$response = $this->sendTo($invitation->contact->email, $fromEmail, $account->getDisplayName(), $subject, $view, $data);
2015-07-02 23:21:29 +03:00
2015-10-11 17:41:09 +03:00
if ($response === true) {
return true;
} else {
2015-11-16 21:51:34 +02:00
return $response;
2015-03-17 07:45:25 +10:00
}
}
2016-05-15 23:16:08 +03:00
/**
* @param int $length
2017-01-30 21:40:43 +02:00
*
* @return string
*/
protected function generatePassword($length = 9)
{
$sets = [
'abcdefghjkmnpqrstuvwxyz',
'ABCDEFGHJKMNPQRSTUVWXYZ',
'23456789',
];
$all = '';
$password = '';
2017-01-30 18:05:31 +02:00
foreach ($sets as $set) {
$password .= $set[array_rand(str_split($set))];
$all .= $set;
}
$all = str_split($all);
2017-01-30 18:05:31 +02:00
for ($i = 0; $i < $length - count($sets); $i++) {
$password .= $all[array_rand($all)];
2017-01-30 18:05:31 +02:00
}
$password = str_shuffle($password);
2016-05-15 23:16:08 +03:00
return $password;
}
2015-03-17 07:45:25 +10:00
/**
* @param Payment $payment
*/
2015-03-17 07:45:25 +10:00
public function sendPaymentConfirmation(Payment $payment)
{
$account = $payment->account;
$client = $payment->client;
$account->loadLocalizationSettings($client);
2015-03-17 07:45:25 +10:00
$invoice = $payment->invoice;
$accountName = $account->getDisplayName();
$emailTemplate = $account->getEmailTemplate(ENTITY_PAYMENT);
2015-09-17 22:01:06 +03:00
$emailSubject = $invoice->account->getEmailSubject(ENTITY_PAYMENT);
2015-03-17 07:45:25 +10:00
if ($payment->invitation) {
$user = $payment->invitation->user;
2015-06-15 20:37:46 +03:00
$contact = $payment->contact;
2015-09-17 22:01:06 +03:00
$invitation = $payment->invitation;
} else {
$user = $payment->user;
$contact = $client->contacts[0];
2015-09-17 22:01:06 +03:00
$invitation = $payment->invoice->invitations[0];
}
2015-09-17 22:01:06 +03:00
$variables = [
'account' => $account,
'client' => $client,
'invitation' => $invitation,
2015-11-06 12:59:53 +02:00
'amount' => $payment->amount,
2015-09-17 22:01:06 +03:00
];
$data = [
2016-05-05 17:46:22 +03:00
'body' => $this->templateService->processVariables($emailTemplate, $variables),
2015-12-16 13:49:26 +02:00
'link' => $invitation->getLink(),
'invoice' => $invoice,
'client' => $client,
2015-11-06 12:59:53 +02:00
'account' => $account,
2015-12-16 13:49:26 +02:00
'payment' => $payment,
'entityType' => ENTITY_INVOICE,
'bccEmail' => $account->getBccEmail(),
'fromEmail' => $account->getFromEmail(),
2015-09-17 22:01:06 +03:00
];
if ($account->attachPDF()) {
2016-02-17 21:13:55 +02:00
$data['pdfString'] = $invoice->getPDFString();
2015-10-13 10:11:44 +03:00
$data['pdfFileName'] = $invoice->getFileName();
2015-09-17 22:01:06 +03:00
}
2015-09-07 12:07:55 +03:00
2016-05-05 17:46:22 +03:00
$subject = $this->templateService->processVariables($emailSubject, $variables);
2015-10-13 10:11:44 +03:00
$data['invoice_id'] = $payment->invoice->id;
2016-05-05 17:46:22 +03:00
$view = $account->getTemplateView('payment_confirmation');
2015-12-31 12:23:39 +02:00
2015-06-15 20:37:46 +03:00
if ($user->email && $contact->email) {
$this->sendTo($contact->email, $user->email, $accountName, $subject, $view, $data);
}
$account->loadLocalizationSettings();
2015-03-17 07:45:25 +10:00
}
/**
* @param $name
* @param $email
* @param $amount
* @param $license
* @param $productId
*/
2015-03-17 07:45:25 +10:00
public function sendLicensePaymentConfirmation($name, $email, $amount, $license, $productId)
{
$view = 'license_confirmation';
$subject = trans('texts.payment_subject');
2016-05-15 23:16:08 +03:00
2015-03-17 07:45:25 +10:00
if ($productId == PRODUCT_ONE_CLICK_INSTALL) {
$license = "Softaculous install license: $license";
} elseif ($productId == PRODUCT_INVOICE_DESIGNS) {
$license = "Invoice designs license: $license";
} elseif ($productId == PRODUCT_WHITE_LABEL) {
$license = "White label license: $license";
}
2016-05-15 23:16:08 +03:00
2015-03-17 07:45:25 +10:00
$data = [
'client' => $name,
'amount' => Utils::formatMoney($amount, DEFAULT_CURRENCY, DEFAULT_COUNTRY),
2017-01-30 21:40:43 +02:00
'license' => $license,
2015-03-17 07:45:25 +10:00
];
2016-05-15 23:16:08 +03:00
2015-03-17 07:45:25 +10:00
$this->sendTo($email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
}
}