1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Ninja/Mailers/ContactMailer.php

169 lines
6.0 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-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
{
public function sendInvoice(Invoice $invoice)
{
$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';
$subject = trans("texts.{$entityType}_subject", ['invoice' => $invoice->invoice_number, 'account' => $invoice->account->getDisplayName()]);
$accountName = $invoice->account->getDisplayName();
$emailTemplate = $invoice->account->getEmailTemplate($entityType);
$invoiceAmount = Utils::formatMoney($invoice->getRequestedAmount(), $client->getCurrencyId());
2015-03-16 22:45:25 +01:00
$this->initClosure($invoice);
2015-03-16 22:45:25 +01:00
foreach ($invoice->invitations as $invitation) {
2015-06-10 10:34:20 +02:00
if (!$invitation->user || !$invitation->user->email || $invitation->user->trashed()) {
2015-03-16 22:45:25 +01:00
return false;
}
2015-06-10 10:34:20 +02:00
if (!$invitation->contact || !$invitation->contact->email || $invitation->contact->trashed()) {
2015-03-16 22:45:25 +01:00
return false;
}
$invitation->sent_date = \Carbon::now()->toDateTimeString();
$invitation->save();
$variables = [
'$footer' => $invoice->account->getEmailFooter(),
'$link' => $invitation->getLink(),
'$client' => $client->getDisplayName(),
2015-03-16 22:45:25 +01:00
'$account' => $accountName,
'$contact' => $invitation->contact->getDisplayName(),
'$amount' => $invoiceAmount,
'$advancedRawInvoice->' => '$'
2015-03-16 22:45:25 +01:00
];
// Add variables for available payment types
2015-06-10 22:55:23 +02:00
foreach (Gateway::getPaymentTypeLinks() as $type) {
$variables["\${$type}_link"] = URL::to("/payment/{$invitation->invitation_key}/{$type}");
}
2015-03-16 22:45:25 +01:00
$data['body'] = str_replace(array_keys($variables), array_values($variables), $emailTemplate);
$data['body'] = preg_replace_callback('/\{\{\$?(.*)\}\}/', $this->advancedTemplateHandler, $data['body']);
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
$fromEmail = $invitation->user->email;
2015-07-02 22:21:29 +02:00
$response = $this->sendTo($invitation->contact->email, $fromEmail, $accountName, $subject, $view, $data);
if ($response !== true) {
return $response;
}
2015-03-16 22:45:25 +01:00
Activity::emailInvoice($invitation);
}
2015-03-16 22:45:25 +01:00
if (!$invoice->isSent()) {
$invoice->invoice_status_id = INVOICE_STATUS_SENT;
$invoice->save();
}
$account->loadLocalizationSettings();
2015-03-16 22:45:25 +01:00
2015-03-31 20:50:58 +02:00
Event::fire(new InvoiceSent($invoice));
2015-07-28 09:00:00 +02:00
return $response;
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';
$subject = trans('texts.payment_subject', ['invoice' => $invoice->invoice_number]);
$accountName = $account->getDisplayName();
$emailTemplate = $account->getEmailTemplate(ENTITY_PAYMENT);
2015-03-16 22:45:25 +01:00
$variables = [
'$footer' => $account->getEmailFooter(),
'$client' => $client->getDisplayName(),
2015-03-16 22:45:25 +01:00
'$account' => $accountName,
'$amount' => Utils::formatMoney($payment->amount, $client->getCurrencyId())
2015-03-16 22:45:25 +01:00
];
if ($payment->invitation) {
$user = $payment->invitation->user;
2015-06-15 19:37:46 +02:00
$contact = $payment->contact;
2015-09-07 11:07:55 +02:00
$variables['$link'] = $payment->invitation->getLink();
} else {
$user = $payment->user;
$contact = $client->contacts[0];
2015-09-07 11:07:55 +02:00
$variables['$link'] = $payment->invoice->invitations[0]->getLink();
}
2015-09-07 11:07:55 +02:00
$data = ['body' => str_replace(array_keys($variables), array_values($variables), $emailTemplate)];
//$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);
}
$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);
}
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
}