mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-05 18:52:44 +01:00
parent
162580bcd3
commit
2da1f24868
37
app/Events/Payment/PaymentWasEmailed.php
Normal file
37
app/Events/Payment/PaymentWasEmailed.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com)
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Events\Payment;
|
||||
|
||||
use App\Models\Payment;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
/**
|
||||
* Class PaymentWasEmailed.
|
||||
*/
|
||||
class PaymentWasEmailed
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* @var Payment
|
||||
*/
|
||||
public $payment;
|
||||
|
||||
/**
|
||||
* PaymentWasEmailed constructor.
|
||||
* @param Payment $payment
|
||||
*/
|
||||
public function __construct(Payment $payment)
|
||||
{
|
||||
$this->payment = $payment;
|
||||
}
|
||||
}
|
45
app/Events/Payment/PaymentWasEmailedAndFailed.php
Normal file
45
app/Events/Payment/PaymentWasEmailedAndFailed.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com)
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Events\Payment;
|
||||
|
||||
use App\Models\Payment;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
/**
|
||||
* Class InvoiceWasEmailedAndFailed.
|
||||
*/
|
||||
class PaymentWasEmailedAndFailed
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* @var Payment
|
||||
*/
|
||||
public $payment;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $errors;
|
||||
|
||||
/**
|
||||
* PaymentWasEmailedAndFailed constructor.
|
||||
* @param Payment $payment
|
||||
* @param array $errors
|
||||
*/
|
||||
public function __construct(Payment $payment, array $errors)
|
||||
{
|
||||
$this->payment = $payment;
|
||||
|
||||
$this->errors = $errors;
|
||||
}
|
||||
}
|
45
app/Events/Quote/QuoteWasEmailedAndFailed.php
Normal file
45
app/Events/Quote/QuoteWasEmailedAndFailed.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com)
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Events\Quote;
|
||||
|
||||
use App\Models\Quote;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
/**
|
||||
* Class InvoiceWasEmailedAndFailed.
|
||||
*/
|
||||
class QuoteWasEmailedAndFailed
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* @var Quote
|
||||
*/
|
||||
public $quote;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $errors;
|
||||
|
||||
/**
|
||||
* QuoteWasEmailedAndFailed constructor.
|
||||
* @param Quote $quote
|
||||
* @param array $errors
|
||||
*/
|
||||
public function __construct(Quote $quote, array $errors)
|
||||
{
|
||||
$this->quote = $quote;
|
||||
|
||||
$this->errors = $errors;
|
||||
}
|
||||
}
|
92
app/Jobs/Payment/EmailPayment.php
Normal file
92
app/Jobs/Payment/EmailPayment.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
namespace App\Jobs\Payment;
|
||||
|
||||
use App\Events\Payment\PaymentWasEmailed;
|
||||
use App\Events\Payment\PaymentWasEmailedAndFailed;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Mail\TemplateEmail;
|
||||
use App\Models\Company;
|
||||
use App\Models\Payment;
|
||||
use App\Models\SystemLog;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class EmailPayment implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $payment;
|
||||
|
||||
public $message_array = [];
|
||||
|
||||
private $company;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Payment $payment, Company $company)
|
||||
{
|
||||
$this->payment = $payment;
|
||||
|
||||
$this->company = $company;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
/*Jobs are not multi-db aware, need to set! */
|
||||
MultiDB::setDB($this->company->db);
|
||||
|
||||
//todo - change runtime config of mail driver if necessary
|
||||
|
||||
$template_style = $this->payment->client->getSetting('email_style');
|
||||
|
||||
$this->payment->client->contacts->each(function ($contact) use ($template_style) {
|
||||
if ($contact->email) {
|
||||
$message_array = $this->payment->getEmailData('', $contact);
|
||||
$message_array['title'] = &$message_array['subject'];
|
||||
$message_array['footer'] = "Sent to ".$contact->present()->name();
|
||||
|
||||
//change the runtime config of the mail provider here:
|
||||
|
||||
//send message
|
||||
Mail::to($contact->email, $contact->present()->name())
|
||||
->send(new TemplateEmail($message_array, $template_style, $contact->user, $contact->client));
|
||||
|
||||
if (count(Mail::failures()) > 0) {
|
||||
event(new PaymentWasEmailedAndFailed($this->payment, Mail::failures()));
|
||||
|
||||
return $this->logMailError($errors);
|
||||
}
|
||||
|
||||
//fire any events
|
||||
event(new PaymentWasEmailed($this->payment));
|
||||
|
||||
//sleep(5);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function logMailError($errors)
|
||||
{
|
||||
SystemLogger::dispatch(
|
||||
$errors,
|
||||
SystemLog::CATEGORY_MAIL,
|
||||
SystemLog::EVENT_MAIL_SEND,
|
||||
SystemLog::TYPE_FAILURE,
|
||||
$this->payment->client
|
||||
);
|
||||
}
|
||||
}
|
92
app/Jobs/Quote/EmailQuote.php
Normal file
92
app/Jobs/Quote/EmailQuote.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
namespace App\Jobs\Quote;
|
||||
|
||||
use App\Events\Quote\QuoteWasEmailed;
|
||||
use App\Events\Quote\QuoteWasEmailedAndFailed;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Mail\TemplateEmail;
|
||||
use App\Models\Company;
|
||||
use App\Models\Quote;
|
||||
use App\Models\SystemLog;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class EmailQuote implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $quote;
|
||||
|
||||
public $message_array = [];
|
||||
|
||||
private $company;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Quote $quote, Company $company)
|
||||
{
|
||||
$this->quote = $quote;
|
||||
|
||||
$this->company = $company;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
/*Jobs are not multi-db aware, need to set! */
|
||||
MultiDB::setDB($this->company->db);
|
||||
|
||||
//todo - change runtime config of mail driver if necessary
|
||||
|
||||
$template_style = $this->quote->client->getSetting('email_style');
|
||||
|
||||
$this->quote->invitations->each(function ($invitation) use ($template_style) {
|
||||
if ($invitation->contact->email) {
|
||||
$message_array = $this->quote->getEmailData('', $invitation->contact);
|
||||
$message_array['title'] = &$message_array['subject'];
|
||||
$message_array['footer'] = "Sent to ".$invitation->contact->present()->name();
|
||||
|
||||
//change the runtime config of the mail provider here:
|
||||
|
||||
//send message
|
||||
Mail::to($invitation->contact->email, $invitation->contact->present()->name())
|
||||
->send(new TemplateEmail($message_array, $template_style, $invitation->contact->user, $invitation->contact->client));
|
||||
|
||||
if (count(Mail::failures()) > 0) {
|
||||
event(new QuoteWasEmailedAndFailed($this->quote, Mail::failures()));
|
||||
|
||||
return $this->logMailError($errors);
|
||||
}
|
||||
|
||||
//fire any events
|
||||
event(new QuoteWasEmailed($this->quote));
|
||||
|
||||
//sleep(5);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function logMailError($errors)
|
||||
{
|
||||
SystemLogger::dispatch(
|
||||
$errors,
|
||||
SystemLog::CATEGORY_MAIL,
|
||||
SystemLog::EVENT_MAIL_SEND,
|
||||
SystemLog::TYPE_FAILURE,
|
||||
$this->quote->client
|
||||
);
|
||||
}
|
||||
}
|
@ -85,7 +85,7 @@ class QuoteRepository extends BaseRepository
|
||||
|
||||
/* If no invitations have been created, this is our fail safe to maintain state*/
|
||||
if ($quote->invitations->count() == 0) {
|
||||
CreateQuoteInvitations::dispatchNow($quote, $quote->company);
|
||||
$quote->service()->createInvitations();
|
||||
}
|
||||
|
||||
$quote = $quote->calc()->getInvoice();
|
||||
@ -94,7 +94,7 @@ class QuoteRepository extends BaseRepository
|
||||
|
||||
$finished_amount = $quote->amount;
|
||||
|
||||
$quote = ApplyQuoteNumber::dispatchNow($quote, $quote->client->getMergedSettings(), $quote->company);
|
||||
$quote = $quote->service()->applyNumber()->save();
|
||||
|
||||
return $quote->fresh();
|
||||
}
|
||||
|
84
app/Utils/Traits/PaymentEmailBuilder.php
Normal file
84
app/Utils/Traits/PaymentEmailBuilder.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
namespace App\Utils\Traits;
|
||||
|
||||
use App\Models\ClientContact;
|
||||
use App\Models\Invoice;
|
||||
use Illuminate\Support\Carbon;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
use Parsedown;
|
||||
|
||||
/**
|
||||
* Class PaymentEmailBuilder
|
||||
* @package App\Utils\Traits
|
||||
*/
|
||||
trait PaymentEmailBuilder
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Builds the correct template to send
|
||||
* @param string $reminder_template The template name ie reminder1
|
||||
* @return array
|
||||
*/
|
||||
public function getEmailData($reminder_template = null, $contact = null) :array
|
||||
{
|
||||
//client
|
||||
//$client = $this->client;
|
||||
|
||||
|
||||
|
||||
//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) {
|
||||
$body_template = trans('texts.payment_message', ['amount'=>$this->present()->amount(),'account'=>$this->company->present()->name()], null, $this->client->locale());
|
||||
}
|
||||
|
||||
$subject_template = $client->getSetting('payment_subject');
|
||||
|
||||
if (iconv_strlen($subject_template) == 0) {
|
||||
$subject_template = trans('texts.invoice_subject', ['number'=>$this->present()->invoice_number(),'account'=>$this->company->present()->name()], null, $this->client->locale());
|
||||
}
|
||||
|
||||
$data['body'] = $this->parseTemplate($body_template, false, $contact);
|
||||
$data['subject'] = $this->parseTemplate($subject_template, true, $contact);
|
||||
|
||||
if ($client->getSetting('pdf_email_attachment') !== false) {
|
||||
$data['files'][] = $this->pdf_file_path();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function parseTemplate(string $template_data, bool $is_markdown = true, $contact) :string
|
||||
{
|
||||
$invoice_variables = $this->makeValues($contact);
|
||||
|
||||
//process variables
|
||||
$data = str_replace(array_keys($invoice_variables), array_values($invoice_variables), $template_data);
|
||||
|
||||
//process markdown
|
||||
if ($is_markdown) {
|
||||
//$data = Parsedown::instance()->line($data);
|
||||
|
||||
$converter = new CommonMarkConverter([
|
||||
'html_input' => 'strip',
|
||||
'allow_unsafe_links' => false,
|
||||
]);
|
||||
|
||||
$data = $converter->convertToHtml($data);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
129
app/Utils/Traits/QuoteEmailBuilder.php
Normal file
129
app/Utils/Traits/QuoteEmailBuilder.php
Normal file
@ -0,0 +1,129 @@
|
||||
<?php
|
||||
namespace App\Utils\Traits;
|
||||
|
||||
use App\Models\ClientContact;
|
||||
use App\Models\Quote;
|
||||
use Illuminate\Support\Carbon;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
use Parsedown;
|
||||
|
||||
/**
|
||||
* Class QuoteEmailBuilder
|
||||
* @package App\Utils\Traits
|
||||
*/
|
||||
trait QuoteEmailBuilder
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Builds the correct template to send
|
||||
* @param string $reminder_template The template name ie reminder1
|
||||
* @return array
|
||||
*/
|
||||
public function getEmailData($reminder_template = null, $contact = null) :array
|
||||
{
|
||||
//client
|
||||
//$client = $this->client;
|
||||
|
||||
if (!$reminder_template) {
|
||||
$reminder_template = $this->calculateTemplate();
|
||||
}
|
||||
|
||||
//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) {
|
||||
$body_template = trans('texts.quote_message', ['amount'=>$this->present()->amount(),'account'=>$this->company->present()->name()], null, $this->client->locale());
|
||||
}
|
||||
|
||||
$subject_template = $client->getSetting('email_subject_'.$reminder_template);
|
||||
|
||||
if (iconv_strlen($subject_template) == 0) {
|
||||
if ($reminder_template == 'quote') {
|
||||
$subject_template = trans('texts.quote_subject', ['number'=>$this->present()->invoice_number(),'account'=>$this->company->present()->name()], null, $this->client->locale());
|
||||
} else {
|
||||
$subject_template = trans('texts.reminder_subject', ['number'=>$this->present()->invoice_number(),'account'=>$this->company->present()->name()], null, $this->client->locale());
|
||||
}
|
||||
}
|
||||
|
||||
$data['body'] = $this->parseTemplate($body_template, false, $contact);
|
||||
$data['subject'] = $this->parseTemplate($subject_template, true, $contact);
|
||||
|
||||
if ($client->getSetting('pdf_email_attachment') !== false) {
|
||||
$data['files'][] = $this->pdf_file_path();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function parseTemplate(string $template_data, bool $is_markdown = true, $contact) :string
|
||||
{
|
||||
$quote_variables = $this->makeValues($contact);
|
||||
|
||||
//process variables
|
||||
$data = str_replace(array_keys($quote_variables), array_values($quote_variables), $template_data);
|
||||
|
||||
//process markdown
|
||||
if ($is_markdown) {
|
||||
//$data = Parsedown::instance()->line($data);
|
||||
|
||||
$converter = new CommonMarkConverter([
|
||||
'html_input' => 'strip',
|
||||
'allow_unsafe_links' => false,
|
||||
]);
|
||||
|
||||
$data = $converter->convertToHtml($data);
|
||||
}
|
||||
|
||||
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:
|
||||
# code...
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user