1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Ninja/Mailers/ContactMailer.php

207 lines
6.9 KiB
PHP
Raw Normal View History

<?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;
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-03-31 20:50:58 +02:00
use App\Events\InvoiceSent;
2015-03-16 22:45:25 +01:00
class ContactMailer extends Mailer
{
2015-09-17 21:01:06 +02:00
public function sendInvoice(Invoice $invoice, $reminder = false)
2015-03-16 22:45:25 +01:00
{
$invoice->load('invitations', 'client.language', 'account');
2015-03-16 22:45:25 +01:00
$entityType = $invoice->getEntityType();
$client = $invoice->client;
$account = $invoice->account;
$account->loadLocalizationSettings($client);
2015-03-16 22:45:25 +01:00
$view = 'invoice';
$accountName = $invoice->account->getDisplayName();
2015-09-17 21:01:06 +02:00
$emailTemplate = $invoice->account->getEmailTemplate($reminder ?: $entityType);
$emailSubject = $invoice->account->getEmailSubject($reminder ?: $entityType);
2015-03-16 22:45:25 +01:00
$this->initClosure($invoice);
2015-09-17 21:01:06 +02:00
$response = false;
$sent = false;
2015-03-16 22:45:25 +01:00
foreach ($invoice->invitations as $invitation) {
2015-09-17 21:01:06 +02:00
if (Auth::check()) {
$user = Auth::user();
} else {
$user = $invitation->user;
if ($invitation->user->trashed()) {
$user = $account->getPrimaryUser();
}
2015-03-16 22:45:25 +01:00
}
2015-09-17 21:01:06 +02:00
if (!$user->email || !$user->confirmed) {
continue;
}
if (!$invitation->contact->email
|| $invitation->contact->trashed()) {
continue;
2015-03-16 22:45:25 +01:00
}
$invitation->sent_date = \Carbon::now()->toDateTimeString();
$invitation->save();
$variables = [
2015-09-17 21:01:06 +02:00
'account' => $account,
'client' => $client,
'invitation' => $invitation,
'amount' => $invoice->getRequestedAmount()
2015-03-16 22:45:25 +01:00
];
2015-09-17 21:01:06 +02:00
$data['body'] = $this->processVariables($emailTemplate, $variables);
2015-03-16 22:45:25 +01:00
$data['link'] = $invitation->getLink();
$data['entityType'] = $entityType;
2015-04-22 23:40:21 +02:00
$data['invoice_id'] = $invoice->id;
2015-03-16 22:45:25 +01:00
2015-09-17 21:01:06 +02:00
$subject = $this->processVariables($emailSubject, $variables);
$fromEmail = $user->email;
2015-07-02 22:21:29 +02:00
$response = $this->sendTo($invitation->contact->email, $fromEmail, $accountName, $subject, $view, $data);
2015-09-17 21:01:06 +02:00
if ($response === true) {
$sent = true;
Activity::emailInvoice($invitation);
2015-07-02 22:21:29 +02:00
}
2015-03-16 22:45:25 +01:00
}
2015-09-17 21:01:06 +02:00
if ($sent === true) {
if (!$invoice->isSent()) {
$invoice->invoice_status_id = INVOICE_STATUS_SENT;
$invoice->save();
}
$account->loadLocalizationSettings();
Event::fire(new InvoiceSent($invoice));
2015-03-16 22:45:25 +01:00
}
2015-07-28 09:00:00 +02:00
2015-09-17 21:01:06 +02:00
return $response ?: trans('texts.email_error');
2015-03-16 22:45:25 +01:00
}
public function sendPaymentConfirmation(Payment $payment)
{
$account = $payment->account;
$client = $payment->client;
$account->loadLocalizationSettings($client);
2015-03-16 22:45:25 +01:00
$invoice = $payment->invoice;
$view = 'payment_confirmation';
$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-09-17 21:01:06 +02:00
$this->initClosure($invoice);
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;
} else {
$user = $payment->user;
$contact = $client->contacts[0];
2015-09-17 21:01:06 +02:00
$invitation = $payment->invoice->invitations[0];
}
2015-09-17 21:01:06 +02:00
$variables = [
'account' => $account,
'client' => $client,
'invitation' => $invitation,
'amount' => $payment->amount
];
$data = [
'body' => $this->processVariables($emailTemplate, $variables)
];
$subject = $this->processVariables($emailSubject, $variables);
$data['invoice_id'] = $payment->invoice->id;
if ($invoice->account->pdf_email_attachment) {
$invoice->updateCachedPDF();
}
2015-09-07 11:07:55 +02:00
2015-06-15 19:37:46 +02:00
if ($user->email && $contact->email) {
$this->sendTo($contact->email, $user->email, $accountName, $subject, $view, $data);
}
$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-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(),
'$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);
$str = preg_replace_callback('/\{\{\$?(.*)\}\}/', $this->advancedTemplateHandler, $str);
return $str;
}
private function initClosure($object)
{
$this->advancedTemplateHandler = function($match) use ($object) {
for ($i = 1; $i < count($match); $i++) {
$blobConversion = $match[$i];
if (isset($$blobConversion)) {
return $$blobConversion;
} else if (preg_match('/trans\(([\w\.]+)\)/', $blobConversion, $regexTranslation)) {
return trans($regexTranslation[1]);
} else if (strpos($blobConversion, '->') !== false) {
return Utils::stringToObjectResolution($object, $blobConversion);
}
}
};
}
2015-03-16 22:45:25 +01:00
}