2015-03-24 09:21:12 +01:00
|
|
|
<?php namespace App\Ninja\Mailers;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
use Utils;
|
2015-04-06 08:58:47 +02:00
|
|
|
use Event;
|
2015-05-29 21:33:55 +02:00
|
|
|
use URL;
|
2015-09-17 21:01:06 +02:00
|
|
|
use Auth;
|
2015-03-31 20:50:58 +02:00
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\Activity;
|
2015-06-10 22:55:23 +02:00
|
|
|
use App\Models\Gateway;
|
2015-10-28 20:22:07 +01:00
|
|
|
|
|
|
|
use App\Events\InvoiceWasEmailed;
|
|
|
|
use App\Events\QuoteWasEmailed;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
class ContactMailer extends Mailer
|
|
|
|
{
|
2015-10-13 09:11:44 +02:00
|
|
|
public function sendInvoice(Invoice $invoice, $reminder = false, $pdfString = false)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-09-10 19:50:09 +02:00
|
|
|
$invoice->load('invitations', 'client.language', 'account');
|
2015-03-16 22:45:25 +01:00
|
|
|
$entityType = $invoice->getEntityType();
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
$client = $invoice->client;
|
|
|
|
$account = $invoice->account;
|
|
|
|
|
2015-09-29 12:21:57 +02:00
|
|
|
if ($invoice->trashed() || $client->trashed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
$account->loadLocalizationSettings($client);
|
2015-10-11 16:41:09 +02:00
|
|
|
$emailTemplate = $account->getEmailTemplate($reminder ?: $entityType);
|
|
|
|
$emailSubject = $account->getEmailSubject($reminder ?: $entityType);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-09-17 21:01:06 +02:00
|
|
|
$sent = false;
|
2015-07-15 14:10:54 +02:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
if ($account->attatchPDF() && !$pdfString) {
|
|
|
|
$pdfString = $invoice->getPDFString();
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
foreach ($invoice->invitations as $invitation) {
|
2015-10-13 09:11:44 +02:00
|
|
|
if ($this->sendInvitation($invitation, $invoice, $emailTemplate, $emailSubject, $pdfString)) {
|
2015-10-11 16:41:09 +02:00
|
|
|
$sent = true;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-10-11 16:41:09 +02:00
|
|
|
}
|
2015-09-17 21:01:06 +02:00
|
|
|
|
2015-10-11 16:41:09 +02:00
|
|
|
$account->loadLocalizationSettings();
|
2015-09-17 21:01:06 +02:00
|
|
|
|
2015-10-11 16:41:09 +02:00
|
|
|
if ($sent === true) {
|
2015-10-28 20:22:07 +01:00
|
|
|
if ($invoice->is_quote) {
|
|
|
|
event(new QuoteWasEmailed($invoice));
|
|
|
|
} else {
|
|
|
|
event(new InvoiceWasEmailed($invoice));
|
|
|
|
}
|
2015-10-11 16:41:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sent ?: trans('texts.email_error');
|
|
|
|
}
|
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
private function sendInvitation($invitation, $invoice, $body, $subject, $pdfString)
|
2015-10-11 16:41:09 +02:00
|
|
|
{
|
|
|
|
$client = $invoice->client;
|
|
|
|
$account = $invoice->account;
|
|
|
|
|
|
|
|
if (Auth::check()) {
|
|
|
|
$user = Auth::user();
|
|
|
|
} else {
|
|
|
|
$user = $invitation->user;
|
|
|
|
if ($invitation->user->trashed()) {
|
|
|
|
$user = $account->users()->orderBy('id')->first();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-10-11 16:41:09 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-11 16:41:09 +02:00
|
|
|
if (!$user->email || !$user->confirmed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$invitation->contact->email || $invitation->contact->trashed()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-11 16:41:09 +02:00
|
|
|
$variables = [
|
|
|
|
'account' => $account,
|
|
|
|
'client' => $client,
|
|
|
|
'invitation' => $invitation,
|
|
|
|
'amount' => $invoice->getRequestedAmount()
|
|
|
|
];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
$data = [
|
|
|
|
'body' => $this->processVariables($body, $variables),
|
|
|
|
'link' => $invitation->getLink(),
|
|
|
|
'entityType' => $invoice->getEntityType(),
|
|
|
|
'invoiceId' => $invoice->id,
|
|
|
|
'invitation' => $invitation,
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($account->attatchPDF()) {
|
|
|
|
$data['pdfString'] = $pdfString;
|
|
|
|
$data['pdfFileName'] = $invoice->getFileName();
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-11 16:41:09 +02:00
|
|
|
$subject = $this->processVariables($subject, $variables);
|
|
|
|
$fromEmail = $user->email;
|
2015-10-29 15:42:05 +01:00
|
|
|
|
2015-10-11 16:41:09 +02:00
|
|
|
$response = $this->sendTo($invitation->contact->email, $fromEmail, $account->getDisplayName(), $subject, ENTITY_INVOICE, $data);
|
2015-07-02 22:21:29 +02:00
|
|
|
|
2015-10-11 16:41:09 +02:00
|
|
|
if ($response === true) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sendPaymentConfirmation(Payment $payment)
|
|
|
|
{
|
2015-09-10 19:50:09 +02:00
|
|
|
$account = $payment->account;
|
|
|
|
$client = $payment->client;
|
|
|
|
|
|
|
|
$account->loadLocalizationSettings($client);
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$invoice = $payment->invoice;
|
|
|
|
$view = 'payment_confirmation';
|
2015-09-10 19:50:09 +02:00
|
|
|
$accountName = $account->getDisplayName();
|
|
|
|
$emailTemplate = $account->getEmailTemplate(ENTITY_PAYMENT);
|
2015-09-17 21:01:06 +02:00
|
|
|
$emailSubject = $invoice->account->getEmailSubject(ENTITY_PAYMENT);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-06-14 19:30:01 +02:00
|
|
|
if ($payment->invitation) {
|
|
|
|
$user = $payment->invitation->user;
|
2015-06-15 19:37:46 +02:00
|
|
|
$contact = $payment->contact;
|
2015-09-17 21:01:06 +02:00
|
|
|
$invitation = $payment->invitation;
|
2015-06-14 19:30:01 +02:00
|
|
|
} else {
|
|
|
|
$user = $payment->user;
|
2015-09-10 19:50:09 +02:00
|
|
|
$contact = $client->contacts[0];
|
2015-09-17 21:01:06 +02:00
|
|
|
$invitation = $payment->invoice->invitations[0];
|
2015-06-14 19:30:01 +02:00
|
|
|
}
|
|
|
|
|
2015-09-17 21:01:06 +02:00
|
|
|
$variables = [
|
|
|
|
'account' => $account,
|
|
|
|
'client' => $client,
|
|
|
|
'invitation' => $invitation,
|
|
|
|
'amount' => $payment->amount
|
|
|
|
];
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'body' => $this->processVariables($emailTemplate, $variables)
|
|
|
|
];
|
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
if ($account->attatchPDF()) {
|
|
|
|
$data['pdfString'] = $invoice->getPDFString();
|
|
|
|
$data['pdfFileName'] = $invoice->getFileName();
|
2015-09-17 21:01:06 +02:00
|
|
|
}
|
2015-09-07 11:07:55 +02:00
|
|
|
|
2015-10-13 09:11:44 +02:00
|
|
|
$subject = $this->processVariables($emailSubject, $variables);
|
|
|
|
$data['invoice_id'] = $payment->invoice->id;
|
|
|
|
|
2015-06-15 19:37:46 +02:00
|
|
|
if ($user->email && $contact->email) {
|
|
|
|
$this->sendTo($contact->email, $user->email, $accountName, $subject, $view, $data);
|
|
|
|
}
|
2015-09-10 19:50:09 +02:00
|
|
|
|
|
|
|
$account->loadLocalizationSettings();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function sendLicensePaymentConfirmation($name, $email, $amount, $license, $productId)
|
|
|
|
{
|
|
|
|
$view = 'license_confirmation';
|
|
|
|
$subject = trans('texts.payment_subject');
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'account' => trans('texts.email_from'),
|
|
|
|
'client' => $name,
|
|
|
|
'amount' => Utils::formatMoney($amount, 1),
|
|
|
|
'license' => $license
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->sendTo($email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data);
|
|
|
|
}
|
2015-07-15 14:10:54 +02:00
|
|
|
|
2015-09-17 21:01:06 +02:00
|
|
|
private function processVariables($template, $data)
|
|
|
|
{
|
|
|
|
$variables = [
|
|
|
|
'$footer' => $data['account']->getEmailFooter(),
|
|
|
|
'$link' => $data['invitation']->getLink(),
|
|
|
|
'$client' => $data['client']->getDisplayName(),
|
|
|
|
'$account' => $data['account']->getDisplayName(),
|
|
|
|
'$contact' => $data['invitation']->contact->getDisplayName(),
|
2015-09-28 22:37:32 +02:00
|
|
|
'$firstName' => $data['invitation']->contact->first_name,
|
2015-09-17 21:01:06 +02:00
|
|
|
'$amount' => Utils::formatMoney($data['amount'], $data['client']->getCurrencyId()),
|
|
|
|
'$invoice' => $data['invitation']->invoice->invoice_number,
|
|
|
|
'$quote' => $data['invitation']->invoice->invoice_number,
|
|
|
|
'$advancedRawInvoice->' => '$'
|
|
|
|
];
|
|
|
|
|
|
|
|
// Add variables for available payment types
|
|
|
|
foreach (Gateway::getPaymentTypeLinks() as $type) {
|
|
|
|
$variables["\${$type}_link"] = URL::to("/payment/{$data['invitation']->invitation_key}/{$type}");
|
|
|
|
}
|
|
|
|
|
|
|
|
$str = str_replace(array_keys($variables), array_values($variables), $template);
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|