1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00

Bug fixes for middleware/ (#3167)

* Fix formatting and variable insertion for email templates

* Refactoring for invoice emails

* Fixes for midddleware
This commit is contained in:
David Bomba 2019-12-25 08:55:29 +11:00 committed by GitHub
parent 5e7512071f
commit 4c2cbb2dee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 25 deletions

View File

@ -257,7 +257,7 @@ class CreateTestData extends Command
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class,rand(1,50))->create([
factory(\App\Models\ClientContact::class,rand(1,5))->create([
'user_id' => $user->id,
'client_id' => $client->id,
'company_id' => $company->id

View File

@ -33,6 +33,7 @@ class SetDb
'errors' => []
];
if( $request->header('X-API-TOKEN') && config('ninja.db.multi_db_enabled'))
{
@ -44,6 +45,10 @@ class SetDb
}
}
else if(!config('ninja.db.multi_db_enabled')){
return $next($request);
}
else {

View File

@ -30,6 +30,8 @@ class EmailInvoice implements ShouldQueue
public $invoice;
public $message_array = [];
/**
* Create a new job instance.
*
@ -53,27 +55,23 @@ class EmailInvoice implements ShouldQueue
/*Jobs are not multi-db aware, need to set! */
MultiDB::setDB($this->invoice->company->db);
$message_array = $this->invoice->getEmailData();
$message_array['title'] = &$message_array['subject'];
$message_array['footer'] = 'The Footer';
//
$variables = array_merge($this->invoice->makeLabels(), $this->invoice->makeValues());
//todo - change runtime config of mail driver if necessary
$template_style = $this->invoice->client->getSetting('email_style');
$this->invoice->invitations->each(function ($invitation) use($message_array, $template_style, $variables){
$this->invoice->invitations->each(function ($invitation) use($template_style){
if($invitation->contact->send_invoice && $invitation->contact->email)
{
//there may be template variables left over for the specific contact? need to reparse here //todo this wont work, as if the variables existed, they'll be overwritten already!
$message_array['body'] = str_replace(array_keys($variables), array_values($variables), $message_array['body']);
$message_array['subject'] = str_replace(array_keys($variables), array_values($variables), $message_array['subject']);
$message_array = $this->invoice->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)
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 ) {
@ -87,7 +85,7 @@ class EmailInvoice implements ShouldQueue
//fire any events
event(new InvoiceWasEmailed($this->invoice));
sleep(5);
//sleep(5);
}

View File

@ -30,6 +30,22 @@ class InvoicePresenter extends EntityPresenter
{
use MakesDates;
public function amount()
{
return Number::formatMoney($this->balance, $this->client);
}
public function invoice_number()
{
if($this->number != '')
return $this->number;
else
return '';
}
public function clientName()
{
return $this->client->present()->name();

View File

@ -11,6 +11,7 @@
namespace App\Utils\Traits;
use App\Models\ClientContact;
use App\Models\Invoice;
use Illuminate\Support\Carbon;
use Parsedown;
@ -28,7 +29,7 @@ trait InvoiceEmailBuilder
* @param string $reminder_template The template name ie reminder1
* @return array
*/
public function getEmailData($reminder_template = null) :array
public function getEmailData($reminder_template = null, $contact = null) :array
{
//client
//$client = $this->client;
@ -37,11 +38,11 @@ trait InvoiceEmailBuilder
$reminder_template = $this->calculateTemplate();
//Need to determine which email template we are producing
return $this->generateTemplateData($reminder_template);
return $this->generateTemplateData($reminder_template, $contact);
}
private function generateTemplateData(string $reminder_template) :array
private function generateTemplateData(string $reminder_template, $contact) :array
{
$data = [];
@ -51,7 +52,7 @@ trait InvoiceEmailBuilder
/* Use default translations if a custom message has not been set*/
if(iconv_strlen($body_template) == 0){
$body_template = trans('texts.invoice_message', [], null, $this->client->locale());
$body_template = trans('texts.invoice_message', ['amount'=>$this->present()->amount(),'account'=>$this->company->present()->name()], null, $this->client->locale());
}
$subject_template = $client->getSetting('email_subject_'.$reminder_template);
@ -59,14 +60,14 @@ trait InvoiceEmailBuilder
if(iconv_strlen($subject_template) == 0){
if($reminder_template == 'invoice')
$subject_template = trans('texts.invoice_subject', [], null, $this->client->locale());
$subject_template = trans('texts.invoice_subject', ['number'=>$this->present()->invoice_number(),'account'=>$this->company->present()->name()], null, $this->client->locale());
else
$subject_template = trans('texts.reminder_subject', [], null, $this->client->locale());
$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);
$data['subject'] = $this->parseTemplate($subject_template, true);
$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();
@ -74,9 +75,9 @@ trait InvoiceEmailBuilder
return $data;
}
private function parseTemplate(string $template_data, bool $is_markdown = true) :string
private function parseTemplate(string $template_data, bool $is_markdown = true, $contact) :string
{
$invoice_variables = $this->makeValues();
$invoice_variables = $this->makeValues($contact);
//process variables
$data = str_replace(array_keys($invoice_variables), array_values($invoice_variables), $template_data);

View File

@ -155,7 +155,7 @@ trait MakesInvoiceValues
* @return array returns an array
* of keyed labels (appended with _label)
*/
public function makeValues() :array
public function makeValues($contact = null) :array
{
if(!$this->client->currency() || !$this->client){
throw new Exception(debug_backtrace()[1]['function'], 1);
@ -246,8 +246,16 @@ trait MakesInvoiceValues
$data['$client.country'] = &$data['$country'];
$data['$email'] = isset($this->client->primary_contact()->first()->email) ?: 'no contact email on record';
$data['$client.email'] = &$data['$email'];
if($contact) {
$data['$contact_name'] = $contact->present()->name();
$data['$contact.name'] = &$data['$contact_name'];
}
else {
$data['$contact_name'] = $this->client->present()->primary_contact_name();
$data['$contact.name'] = &$data['$contact_name'];
}
$data['$company.name'] = $this->company->present()->name();
$data['$company.address1'] = $settings->address1;
$data['$company.address2'] = $settings->address2;