diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 8124381d36..e8f0877726 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -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 @@ -318,7 +318,7 @@ class CreateTestData extends Command $this->invoice_repo->markSent($invoice); CreateInvoiceInvitations::dispatch($invoice); - + if(rand(0, 1)) { $payment = PaymentFactory::create($client->company->id, $client->user->id); diff --git a/app/Http/Middleware/SetDb.php b/app/Http/Middleware/SetDb.php index 5a01770729..036da2e472 100644 --- a/app/Http/Middleware/SetDb.php +++ b/app/Http/Middleware/SetDb.php @@ -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 { diff --git a/app/Jobs/Invoice/EmailInvoice.php b/app/Jobs/Invoice/EmailInvoice.php index b7d5193131..23c97b2f47 100644 --- a/app/Jobs/Invoice/EmailInvoice.php +++ b/app/Jobs/Invoice/EmailInvoice.php @@ -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); } diff --git a/app/Models/Presenters/InvoicePresenter.php b/app/Models/Presenters/InvoicePresenter.php index 496c78c3a1..6c91bab2c7 100644 --- a/app/Models/Presenters/InvoicePresenter.php +++ b/app/Models/Presenters/InvoicePresenter.php @@ -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(); diff --git a/app/Utils/Traits/InvoiceEmailBuilder.php b/app/Utils/Traits/InvoiceEmailBuilder.php index 534fd2f4d0..e3f0612d78 100644 --- a/app/Utils/Traits/InvoiceEmailBuilder.php +++ b/app/Utils/Traits/InvoiceEmailBuilder.php @@ -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); diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index e11f6b5b3b..a439cc024c 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -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;