2020-02-12 10:21:06 +01:00
|
|
|
<?php
|
2020-03-18 10:40:15 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-18 10:40:15 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-03-18 10:40:15 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-03-18 10:40:15 +01:00
|
|
|
*/
|
|
|
|
|
2020-02-12 10:21:06 +01:00
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
|
|
|
use App\Models\Quote;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
use League\CommonMark\CommonMarkConverter;
|
|
|
|
use Parsedown;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class QuoteEmailBuilder.
|
2020-02-12 10:21:06 +01:00
|
|
|
*/
|
|
|
|
trait QuoteEmailBuilder
|
|
|
|
{
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Builds the correct template to send.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param null $reminder_template The template name ie reminder1
|
|
|
|
* @param null $contact
|
2020-02-12 10:21:06 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getEmailData($reminder_template = null, $contact = null) :array
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $reminder_template) {
|
2020-11-05 02:33:31 +01:00
|
|
|
$reminder_template = 'quote';
|
|
|
|
|
|
|
|
//$reminder_template = $this->calculateTemplate('quote');
|
2020-02-12 10:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Need to determine which email template we are producing
|
|
|
|
return $this->generateTemplateData($reminder_template, $contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generateTemplateData(string $reminder_template, $contact) :array
|
|
|
|
{
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
$client = $this->client;
|
|
|
|
|
|
|
|
$body_template = $client->getSetting('email_template_'.$reminder_template);
|
|
|
|
|
|
|
|
/* Use default translations if a custom message has not been set*/
|
|
|
|
if (iconv_strlen($body_template) == 0) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$body_template = trans('texts.quote_message', ['amount'=>$this->present()->amount(), 'account'=>$this->company->present()->name()], null, $this->client->locale());
|
2020-02-12 10:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$subject_template = $client->getSetting('email_subject_'.$reminder_template);
|
|
|
|
|
|
|
|
if (iconv_strlen($subject_template) == 0) {
|
|
|
|
if ($reminder_template == 'quote') {
|
2020-09-06 11:38:10 +02:00
|
|
|
$subject_template = trans('texts.quote_subject', ['number'=>$this->present()->invoice_number(), 'account'=>$this->company->present()->name()], null, $this->client->locale());
|
2020-02-12 10:21:06 +01:00
|
|
|
} else {
|
2020-09-06 11:38:10 +02:00
|
|
|
$subject_template = trans('texts.reminder_subject', ['number'=>$this->present()->invoice_number(), 'account'=>$this->company->present()->name()], null, $this->client->locale());
|
2020-02-12 10:21:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 11:48:45 +01:00
|
|
|
$data['body'] = $this->parseTemplate($body_template, true, $contact);
|
|
|
|
$data['subject'] = $this->parseTemplate($subject_template, false, $contact);
|
2020-02-12 10:21:06 +01:00
|
|
|
|
|
|
|
if ($client->getSetting('pdf_email_attachment') !== false) {
|
|
|
|
$data['files'][] = $this->pdf_file_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
private function parseTemplate(string $template_data, bool $is_markdown, $contact) :string
|
2020-02-12 10:21:06 +01:00
|
|
|
{
|
2020-10-28 07:27:10 +01:00
|
|
|
// $quote_variables = $this->makeValues($contact);
|
2020-02-12 10:21:06 +01:00
|
|
|
|
|
|
|
//process variables
|
2020-04-15 02:30:52 +02:00
|
|
|
// $data = str_replace(array_keys($quote_variables), array_values($quote_variables), $template_data);
|
|
|
|
$data = strtr($template_data, $quote_variables);
|
2020-02-12 10:21:06 +01:00
|
|
|
|
|
|
|
//process markdown
|
|
|
|
if ($is_markdown) {
|
|
|
|
//$data = Parsedown::instance()->line($data);
|
|
|
|
|
|
|
|
$converter = new CommonMarkConverter([
|
2022-06-21 11:57:17 +02:00
|
|
|
'html_input' => 'allow',
|
2020-02-13 11:48:45 +01:00
|
|
|
'allow_unsafe_links' => true,
|
2020-02-12 10:21:06 +01:00
|
|
|
]);
|
|
|
|
|
2022-03-16 08:50:34 +01:00
|
|
|
$data = $converter->convert($data);
|
2020-02-12 10:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function calculateTemplate() :string
|
|
|
|
{
|
|
|
|
//if invoice is currently a draft, or being marked as sent, this will be the initial email
|
|
|
|
$client = $this->client;
|
|
|
|
|
|
|
|
//if the invoice
|
|
|
|
if ($this->status_id == Quote::STATUS_DRAFT || Carbon::parse($this->due_date) > now()) {
|
|
|
|
return 'quote';
|
|
|
|
} elseif ($client->getSetting('enable_reminder1') !== false && $this->inReminderWindow($client->getSetting('schedule_reminder1'), $client->getSetting('num_days_reminder1'))) {
|
|
|
|
return 'template1';
|
|
|
|
} elseif ($client->getSetting('enable_reminder2') !== false && $this->inReminderWindow($client->getSetting('schedule_reminder2'), $client->getSetting('num_days_reminder2'))) {
|
|
|
|
return 'template2';
|
|
|
|
} elseif ($client->getSetting('enable_reminder3') !== false && $this->inReminderWindow($client->getSetting('schedule_reminder3'), $client->getSetting('num_days_reminder3'))) {
|
|
|
|
return 'template3';
|
|
|
|
} else {
|
|
|
|
return 'quote';
|
|
|
|
}
|
|
|
|
|
|
|
|
//also implement endless reminders here
|
|
|
|
}
|
|
|
|
|
|
|
|
private function inReminderWindow($schedule_reminder, $num_days_reminder)
|
|
|
|
{
|
|
|
|
switch ($schedule_reminder) {
|
|
|
|
case 'after_invoice_date':
|
|
|
|
return Carbon::parse($this->date)->addDays($num_days_reminder)->startOfDay()->eq(Carbon::now()->startOfDay());
|
|
|
|
break;
|
|
|
|
case 'before_due_date':
|
|
|
|
return Carbon::parse($this->due_date)->subDays($num_days_reminder)->startOfDay()->eq(Carbon::now()->startOfDay());
|
|
|
|
break;
|
|
|
|
case 'after_due_date':
|
|
|
|
return Carbon::parse($this->due_date)->addDays($num_days_reminder)->startOfDay()->eq(Carbon::now()->startOfDay());
|
|
|
|
break;
|
|
|
|
default:
|
2020-09-06 11:38:10 +02:00
|
|
|
// code...
|
2020-02-12 10:21:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|