2019-12-10 21:25:54 +01:00
|
|
|
<?php
|
2020-08-06 05:04:09 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-08-06 05:04:09 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-08-06 05:04:09 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-08-06 05:04:09 +02:00
|
|
|
*/
|
|
|
|
|
2019-12-10 21:25:54 +01:00
|
|
|
namespace App\Mail;
|
|
|
|
|
2021-08-29 12:54:26 +02:00
|
|
|
use App\Jobs\Invoice\CreateUbl;
|
|
|
|
use App\Models\Account;
|
2021-02-02 09:51:12 +01:00
|
|
|
use App\Models\ClientContact;
|
2021-04-15 15:56:20 +02:00
|
|
|
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
|
2021-03-31 00:58:50 +02:00
|
|
|
use App\Utils\HtmlEngine;
|
2021-11-14 22:52:04 +01:00
|
|
|
use App\Utils\Ninja;
|
2019-12-10 21:25:54 +01:00
|
|
|
use Illuminate\Mail\Mailable;
|
2024-01-18 16:52:25 +01:00
|
|
|
use Illuminate\Support\Facades\URL;
|
2019-12-10 21:25:54 +01:00
|
|
|
|
|
|
|
class TemplateEmail extends Mailable
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
private $build_email;
|
2020-02-15 12:49:31 +01:00
|
|
|
|
|
|
|
private $client;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-02-02 06:11:33 +01:00
|
|
|
private $contact;
|
|
|
|
|
2021-02-18 22:43:47 +01:00
|
|
|
private $company;
|
|
|
|
|
2021-02-22 01:18:52 +01:00
|
|
|
private $invitation;
|
|
|
|
|
|
|
|
public function __construct($build_email, ClientContact $contact, $invitation = null)
|
2019-12-10 21:25:54 +01:00
|
|
|
{
|
2020-02-15 10:01:15 +01:00
|
|
|
$this->build_email = $build_email;
|
2020-02-15 12:49:31 +01:00
|
|
|
|
2021-02-02 06:11:33 +01:00
|
|
|
$this->contact = $contact;
|
|
|
|
|
|
|
|
$this->client = $contact->client;
|
2021-02-18 22:43:47 +01:00
|
|
|
|
|
|
|
$this->company = $contact->company;
|
2021-02-22 01:18:52 +01:00
|
|
|
|
|
|
|
$this->invitation = $invitation;
|
2019-12-10 21:25:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 04:46:11 +01:00
|
|
|
/**
|
2023-02-16 02:36:09 +01:00
|
|
|
* Supports inline attachments for large
|
2023-02-09 04:46:11 +01:00
|
|
|
* attachments in custom designs
|
2023-02-16 02:36:09 +01:00
|
|
|
*
|
2023-02-09 04:46:11 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function buildLinksForCustomDesign(): string
|
|
|
|
{
|
|
|
|
$links = $this->build_email->getAttachmentLinks();
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (count($links) == 0) {
|
2023-02-09 04:46:11 +01:00
|
|
|
return '';
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-02-09 04:46:11 +01:00
|
|
|
|
|
|
|
$link_string = '<ul>';
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach ($this->build_email->getAttachmentLinks() as $link) {
|
2023-02-09 04:46:11 +01:00
|
|
|
$link_string .= "<li>{$link}</li>";
|
|
|
|
}
|
|
|
|
|
|
|
|
$link_string .= '</ul>';
|
|
|
|
|
|
|
|
return $link_string;
|
|
|
|
}
|
|
|
|
|
2019-12-10 21:25:54 +01:00
|
|
|
public function build()
|
|
|
|
{
|
2021-11-30 10:06:05 +01:00
|
|
|
$template_name = 'email.template.'.$this->build_email->getTemplate();
|
2021-06-23 11:00:43 +02:00
|
|
|
|
2024-04-13 23:01:27 +02:00
|
|
|
if (in_array($this->build_email->getTemplate(), ['light', 'dark'])) {
|
2024-03-14 00:55:56 +01:00
|
|
|
$template_name = 'email.template.client';
|
2021-06-23 11:00:43 +02:00
|
|
|
}
|
2019-12-16 12:34:38 +01:00
|
|
|
|
2024-04-13 23:01:27 +02:00
|
|
|
if($this->build_email->getTemplate() == 'premium') {
|
|
|
|
$template_name = 'email.template.client_premium';
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->build_email->getTemplate() == 'custom') {
|
2023-02-09 04:46:11 +01:00
|
|
|
$this->build_email->setBody(str_replace('$body', $this->build_email->getBody().$this->buildLinksForCustomDesign(), $this->client->getSetting('email_style_custom')));
|
2021-04-11 05:46:40 +02:00
|
|
|
}
|
|
|
|
|
2021-04-19 11:41:56 +02:00
|
|
|
$settings = $this->client->getMergedSettings();
|
|
|
|
|
2021-04-22 12:29:00 +02:00
|
|
|
if ($this->build_email->getTemplate() !== 'custom') {
|
|
|
|
$this->build_email->setBody(
|
|
|
|
DesignHelpers::parseMarkdownToHtml($this->build_email->getBody())
|
|
|
|
);
|
|
|
|
}
|
2021-04-15 15:56:20 +02:00
|
|
|
|
2020-02-15 12:49:31 +01:00
|
|
|
$company = $this->client->company;
|
2019-12-16 12:34:38 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->invitation) {
|
2021-03-31 03:55:33 +02:00
|
|
|
$html_variables = (new HtmlEngine($this->invitation))->makeValues();
|
|
|
|
$signature = str_replace(array_keys($html_variables), array_values($html_variables), $settings->email_signature);
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-03-31 03:55:33 +02:00
|
|
|
$signature = $settings->email_signature;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-04-15 15:56:20 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (property_exists($settings, 'email_from_name') && strlen($settings->email_from_name) > 1) {
|
2021-09-03 14:59:48 +02:00
|
|
|
$email_from_name = $settings->email_from_name;
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-09-03 14:59:48 +02:00
|
|
|
$email_from_name = $this->company->present()->name();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-09-03 14:59:48 +02:00
|
|
|
|
|
|
|
$this->from(config('mail.from.address'), $email_from_name);
|
2021-04-15 15:56:20 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (strlen($settings->bcc_email) > 1) {
|
|
|
|
if (Ninja::isHosted()) {
|
2023-10-18 08:28:10 +02:00
|
|
|
|
|
|
|
if($company->account->isPaid()) {
|
|
|
|
$bccs = explode(',', str_replace(' ', '', $settings->bcc_email));
|
|
|
|
$this->bcc(array_slice($bccs, 0, 5));
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
|
|
|
$this->bcc(explode(',', str_replace(' ', '', $settings->bcc_email)));
|
2023-10-18 08:28:10 +02:00
|
|
|
}
|
2022-06-18 00:54:58 +02:00
|
|
|
}
|
2021-06-17 14:47:34 +02:00
|
|
|
|
2023-03-17 09:41:38 +01:00
|
|
|
$this->subject(str_replace("<br>", "", $this->build_email->getSubject()))
|
2022-03-04 00:55:02 +01:00
|
|
|
->text('email.template.text', [
|
|
|
|
'text_body' => $this->build_email->getTextBody(),
|
2020-10-20 01:37:33 +02:00
|
|
|
'whitelabel' => $this->client->user->account->isPaid() ? true : false,
|
|
|
|
'settings' => $settings,
|
2020-02-15 12:49:31 +01:00
|
|
|
])
|
2019-12-10 21:25:54 +01:00
|
|
|
->view($template_name, [
|
2021-02-02 06:11:33 +01:00
|
|
|
'greeting' => ctrans('texts.email_salutation', ['name' => $this->contact->present()->name()]),
|
2020-02-15 10:01:15 +01:00
|
|
|
'body' => $this->build_email->getBody(),
|
|
|
|
'footer' => $this->build_email->getFooter(),
|
2020-04-15 02:30:52 +02:00
|
|
|
'view_link' => $this->build_email->getViewLink(),
|
|
|
|
'view_text' => $this->build_email->getViewText(),
|
2020-11-08 06:21:18 +01:00
|
|
|
'title' => '',
|
2021-03-31 03:55:33 +02:00
|
|
|
'signature' => $signature,
|
2019-12-16 12:34:38 +01:00
|
|
|
'settings' => $settings,
|
2020-09-06 11:38:10 +02:00
|
|
|
'company' => $company,
|
2020-09-28 23:54:12 +02:00
|
|
|
'whitelabel' => $this->client->user->account->isPaid() ? true : false,
|
2022-01-18 22:51:16 +01:00
|
|
|
'logo' => $this->company->present()->logo($settings),
|
2023-02-02 23:56:58 +01:00
|
|
|
'links' => $this->build_email->getAttachmentLinks(),
|
2024-03-21 00:52:02 +01:00
|
|
|
'email_preferences' => (Ninja::isHosted() && $this->invitation && in_array($settings->email_sending_method, ['default', 'mailgun'])) ? $this->company->domain() . URL::signedRoute('client.email_preferences', ['entity' => $this->invitation->getEntityString(), 'invitation_key' => $this->invitation->key], absolute: false) : false,
|
2022-08-07 09:34:23 +02:00
|
|
|
]);
|
2019-12-10 21:25:54 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach ($this->build_email->getAttachments() as $file) {
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('file', $file)) {
|
2022-11-28 05:45:25 +01:00
|
|
|
$this->attachData(base64_decode($file['file']), $file['name']);
|
2023-02-16 02:36:09 +01:00
|
|
|
} else {
|
2022-11-28 05:45:25 +01:00
|
|
|
$this->attach($file['path'], ['as' => $file['name'], 'mime' => null]);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2019-12-22 11:28:41 +01:00
|
|
|
|
2024-04-19 03:12:30 +02:00
|
|
|
if(!$this->invitation)
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
if ($this->invitation->invoice && $settings->ubl_email_attachment && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
|
2022-06-24 13:15:14 +02:00
|
|
|
$ubl_string = (new CreateUbl($this->invitation->invoice))->handle();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
if ($ubl_string) {
|
2021-09-09 09:23:47 +02:00
|
|
|
$this->attachData($ubl_string, $this->invitation->invoice->getFileName('xml'));
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2021-08-29 12:54:26 +02:00
|
|
|
}
|
2024-04-19 03:12:30 +02:00
|
|
|
|
2024-03-10 16:41:34 +01:00
|
|
|
if ($this->invitation->invoice) {
|
2024-04-19 03:12:30 +02:00
|
|
|
if ($this->invitation->invoice->client->getSetting('enable_e_invoice') && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
|
2024-03-10 16:41:34 +01:00
|
|
|
$xml_string = $this->invitation->invoice->service()->getEInvoice($this->invitation->contact);
|
|
|
|
|
|
|
|
if ($xml_string) {
|
|
|
|
$this->attachData($xml_string, $this->invitation->invoice->getEFileName("xml"));
|
|
|
|
}
|
2023-04-28 09:15:19 +02:00
|
|
|
|
2023-08-17 01:24:36 +02:00
|
|
|
}
|
2024-03-10 16:41:34 +01:00
|
|
|
}
|
|
|
|
elseif ($this->invitation->credit){
|
2024-04-19 03:12:30 +02:00
|
|
|
if ($this->invitation->credit->client->getSetting('enable_e_invoice') && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
|
2024-03-10 16:41:34 +01:00
|
|
|
$xml_string = $this->invitation->credit->service()->getECredit($this->invitation->contact);
|
|
|
|
|
|
|
|
if ($xml_string) {
|
|
|
|
$this->attachData($xml_string, $this->invitation->credit->getEFileName("xml"));
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-03-10 16:41:34 +01:00
|
|
|
}
|
2023-04-03 21:18:20 +02:00
|
|
|
}
|
2024-03-10 16:41:34 +01:00
|
|
|
elseif ($this->invitation->quote){
|
2024-04-19 03:12:30 +02:00
|
|
|
if ($this->invitation->quote->client->getSetting('enable_e_invoice') && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
|
2024-03-10 16:41:34 +01:00
|
|
|
$xml_string = $this->invitation->quote->service()->getEQuote($this->invitation->contact);
|
|
|
|
|
|
|
|
if ($xml_string) {
|
|
|
|
$this->attachData($xml_string, $this->invitation->quote->getEFileName("xml"));
|
|
|
|
}
|
2021-08-29 12:54:26 +02:00
|
|
|
|
2024-03-10 16:41:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif ($this->invitation->purchase_order){
|
2024-04-19 03:12:30 +02:00
|
|
|
if ($this->invitation->purchase_order->vendor->getSetting('enable_e_invoice') && $this->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
|
2024-03-10 16:41:34 +01:00
|
|
|
$xml_string = $this->invitation->purchase_order->service()->getEPurchaseOrder($this->invitation->contact);
|
|
|
|
|
|
|
|
if ($xml_string) {
|
|
|
|
$this->attachData($xml_string, $this->invitation->purchase_order->getEFileName("xml"));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2020-11-12 02:43:32 +01:00
|
|
|
return $this;
|
2019-12-10 21:25:54 +01:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|