Quote email engine

This commit is contained in:
David Bomba 2020-10-28 10:02:32 +11:00
parent 073caab09b
commit 0a02323792
14 changed files with 195 additions and 27 deletions

View File

@ -12,7 +12,6 @@
namespace App\Events\Quote; namespace App\Events\Quote;
use App\Models\Company; use App\Models\Company;
use App\Models\QuoteWasViewed;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
/** /**

View File

@ -16,6 +16,7 @@ use App\Http\Requests\Credit\ShowCreditRequest;
use App\Http\Requests\Credit\StoreCreditRequest; use App\Http\Requests\Credit\StoreCreditRequest;
use App\Http\Requests\Credit\UpdateCreditRequest; use App\Http\Requests\Credit\UpdateCreditRequest;
use App\Jobs\Credit\StoreCredit; use App\Jobs\Credit\StoreCredit;
use App\Jobs\Entity\EmailEntity;
use App\Jobs\Invoice\EmailCredit; use App\Jobs\Invoice\EmailCredit;
use App\Jobs\Invoice\MarkInvoicePaid; use App\Jobs\Invoice\MarkInvoicePaid;
use App\Models\Client; use App\Models\Client;
@ -545,7 +546,13 @@ class CreditController extends BaseController
} }
break; break;
case 'email': case 'email':
EmailCredit::dispatch($credit, $credit->company); // EmailCredit::dispatch($credit, $credit->company);
$credit->invitations->load('contact.client.country', 'credit.client.country', 'credit.company')->each(function ($invitation) use ($credit) {
EmailEntity::dispatch($invitation, $credit->company);
});
if (! $bulk) { if (! $bulk) {
return response()->json(['message'=>'email sent'], 200); return response()->json(['message'=>'email sent'], 200);
} }

View File

@ -19,10 +19,12 @@ use App\Jobs\Mail\EntitySentMailer;
use App\Models\Credit; use App\Models\Credit;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Quote; use App\Models\Quote;
use App\Models\RecurringInvoice;
use App\Notifications\SendGenericNotification; use App\Notifications\SendGenericNotification;
use App\Transformers\CreditTransformer; use App\Transformers\CreditTransformer;
use App\Transformers\InvoiceTransformer; use App\Transformers\InvoiceTransformer;
use App\Transformers\QuoteTransformer; use App\Transformers\QuoteTransformer;
use App\Transformers\RecurringInvoiceTransformer;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
class EmailController extends BaseController class EmailController extends BaseController
@ -131,21 +133,27 @@ class EmailController extends BaseController
EntitySentMailer::dispatch($invitation, $entity_string, $entity_obj->user, $invitation->company); EntitySentMailer::dispatch($invitation, $entity_string, $entity_obj->user, $invitation->company);
if ($this instanceof Invoice) { if ($entity_obj instanceof Invoice) {
$this->entity_type = Invoice::class; $this->entity_type = Invoice::class;
$this->entity_transformer = InvoiceTransformer::class; $this->entity_transformer = InvoiceTransformer::class;
} }
if ($this instanceof Quote) { if ($entity_obj instanceof Quote) {
$this->entity_type = Quote::class; $this->entity_type = Quote::class;
$this->entity_transformer = QuoteTransformer::class; $this->entity_transformer = QuoteTransformer::class;
} }
if ($this instanceof Credit) { if ($entity_obj instanceof Credit) {
$this->entity_type = Credit::class; $this->entity_type = Credit::class;
$this->entity_transformer = CreditTransformer::class; $this->entity_transformer = CreditTransformer::class;
} }
if ($entity_obj instanceof RecurringInvoice) {
$this->entity_type = RecurringInvoice::class;
$this->entity_transformer = RecurringInvoiceTransformer::class;
}
$entity_obj->service()->markSent()->save(); $entity_obj->service()->markSent()->save();
return $this->itemResponse($entity_obj); return $this->itemResponse($entity_obj);

View File

@ -706,7 +706,7 @@ class InvoiceController extends BaseController
if (request()->has('email_type') && property_exists($invoice->company->settings, request()->input('email_type'))) { if (request()->has('email_type') && property_exists($invoice->company->settings, request()->input('email_type'))) {
$this->reminder_template = $invoice->client->getSetting(request()->input('email_type')); $this->reminder_template = $invoice->client->getSetting(request()->input('email_type'));
} else { } else {
$this->reminder_template = $invoice->calculateTemplate(); $this->reminder_template = $invoice->calculateTemplate('invoice');
} }
//touch reminder1,2,3_sent + last_sent here if the email is a reminder. //touch reminder1,2,3_sent + last_sent here if the email is a reminder.

View File

@ -40,25 +40,14 @@ class UpdateTaskRequest extends Request
/* Ensure we have a client name, and that all emails are unique*/ /* Ensure we have a client name, and that all emails are unique*/
if ($this->input('number')) { if ($this->input('number')) {
$rules['number'] = 'unique:tasks,number,'.$this->id.',id,company_id,'.$this->taskss->company_id; $rules['number'] = 'unique:tasks,number,'.$this->id.',id,company_id,'.$this->task->company_id;
} }
return $this->globalRules($rules); return $this->globalRules($rules);
} }
// public function messages()
// {
// return [
// 'unique' => ctrans('validation.unique', ['attribute' => 'email']),
// 'email' => ctrans('validation.email', ['attribute' => 'email']),
// 'name.required' => ctrans('validation.required', ['attribute' => 'name']),
// 'required' => ctrans('validation.required', ['attribute' => 'email']),
// ];
// }
protected function prepareForValidation() protected function prepareForValidation()
{ {
$input = $this->decodePrimaryKeys($this->all()); $input = $this->decodePrimaryKeys($this->all());
$this->replace($input); $this->replace($input);

View File

@ -50,6 +50,10 @@ class RelatedUserRule implements Rule
*/ */
private function checkUserIsRelated($user_id) : bool private function checkUserIsRelated($user_id) : bool
{ {
if(empty($user_id))
return true;
return User::query() return User::query()
->where('id', $user_id) ->where('id', $user_id)
->where('account_id', auth()->user()->company()->account_id) ->where('account_id', auth()->user()->company()->account_id)

View File

@ -66,7 +66,7 @@ class ReminderJob implements ShouldQueue
if ($invoice->isPayable()) { if ($invoice->isPayable()) {
$reminder_template = $invoice->calculateTemplate(); $reminder_template = $invoice->calculateTemplate('invoice');
$invoice->service()->touchReminder($this->reminder_template)->save(); $invoice->service()->touchReminder($this->reminder_template)->save();
$invoice->invitations->each(function ($invitation) use ($invoice) { $invoice->invitations->each(function ($invitation) use ($invoice) {

View File

@ -0,0 +1,90 @@
<?php
/**
* Quote Ninja (https://quoteninja.com).
*
* @link https://github.com/quoteninja/quoteninja source repository
*
* @copyright Copyright (c) 2020. Quote Ninja LLC (https://quoteninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Mail\Engine;
use App\Utils\Number;
class QuoteEmailEngine extends BaseEmailEngine
{
public $invitation;
public $client;
public $quote;
public $contact;
public $reminder_template;
public function __construct($invitation, $reminder_template)
{
$this->invitation = $invitation;
$this->reminder_template = $reminder_template;
$this->client = $invitation->contact->client;
$this->quote = $invitation->quote;
$this->contact = $invitation->contact;
}
public function build()
{
$body_template = $this->client->getSetting('email_template_'.$this->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',
[
'quote' => $this->quote->number,
'company' => $this->quote->company->present()->name(),
'amount' => Number::formatMoney($this->quote->balance, $this->client),
],
null,
$this->client->locale()
);
}
$subject_template = $this->client->getSetting('email_subject_'.$this->reminder_template);
if (iconv_strlen($subject_template) == 0) {
$subject_template = trans(
'texts.quote_subject',
[
'number' => $this->quote->number,
'account' => $this->quote->company->present()->name(),
],
null,
$this->client->locale()
);
}
$this->setTemplate($this->client->getSetting('email_style'))
->setContact($this->contact)
->setVariables($this->quote->makeValues($this->contact))//move make values into the htmlengine
->setSubject($subject_template)
->setBody($body_template)
->setFooter("<a href='{$this->invitation->getLink()}'>".ctrans('texts.view_quote').'</a>')
->setViewLink($this->invitation->getLink())
->setViewText(ctrans('texts.view_quote'));
if ($this->client->getSetting('pdf_email_attachment') !== false) {
$this->setAttachments($invitation->pdf_file_path());
}
return $this;
}
}

View File

@ -26,6 +26,7 @@ class Task extends BaseModel
'client_id', 'client_id',
'invoice_id', 'invoice_id',
'project_id', 'project_id',
'assigned_user_id',
'custom_value1', 'custom_value1',
'custom_value2', 'custom_value2',
'custom_value3', 'custom_value3',

View File

@ -15,6 +15,7 @@ use App\Models\Credit;
use App\Services\Credit\ApplyPayment; use App\Services\Credit\ApplyPayment;
use App\Services\Credit\CreateInvitations; use App\Services\Credit\CreateInvitations;
use App\Services\Credit\MarkSent; use App\Services\Credit\MarkSent;
use App\Services\Credit\SendEmail;
class CreditService class CreditService
{ {
@ -55,6 +56,14 @@ class CreditService
return $this; return $this;
} }
public function sendEmail($contact = null)
{
$send_email = new SendEmail($this->credit, null, $contact);
return $send_email->run();
}
public function setCalculatedStatus() public function setCalculatedStatus()
{ {

View File

@ -0,0 +1,60 @@
<?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\Services\Credit;
use App\Helpers\Email\CreditEmail;
use App\Jobs\Entity\EmailEntity;
use App\Jobs\Credit\EmailCredit;
use App\Models\ClientContact;
use App\Models\Credit;
class SendEmail
{
public $credit;
protected $reminder_template;
protected $contact;
public function __construct($credit, $reminder_template = null, ClientContact $contact = null)
{
$this->credit = $credit;
$this->reminder_template = $reminder_template;
$this->contact = $contact;
}
/**
* Builds the correct template to send.
* @param string $this->reminder_template The template name ie reminder1
* @return array
*/
public function run()
{
if (! $this->reminder_template) {
$this->reminder_template = $this->credit->calculateTemplate('credit');
}
$this->credit->invitations->each(function ($invitation) {
if ($invitation->contact->send_email && $invitation->contact->email) {
$email_builder = (new CreditEmail())->build($invitation, $this->reminder_template);
// EmailCredit::dispatchNow($email_builder, $invitation, $invitation->company);
EmailEntity::dispatchNow($invitation, $invitation->company);
}
});
$this->credit->service()->markSent();
}
}

View File

@ -12,6 +12,7 @@
namespace App\Services\Invoice; namespace App\Services\Invoice;
use App\Helpers\Email\InvoiceEmail; use App\Helpers\Email\InvoiceEmail;
use App\Jobs\Entity\EmailEntity;
use App\Jobs\Invoice\EmailInvoice; use App\Jobs\Invoice\EmailInvoice;
use App\Models\ClientContact; use App\Models\ClientContact;
use App\Models\Invoice; use App\Models\Invoice;
@ -50,7 +51,9 @@ class SendEmail extends AbstractService
$email_builder = (new InvoiceEmail())->build($invitation, $this->reminder_template); $email_builder = (new InvoiceEmail())->build($invitation, $this->reminder_template);
if ($invitation->contact->send_email && $invitation->contact->email) { if ($invitation->contact->send_email && $invitation->contact->email) {
EmailInvoice::dispatch($email_builder, $invitation, $invitation->company); // EmailInvoice::dispatch($email_builder, $invitation, $invitation->company);
EmailEntity::dispatchNow($invitation, $invitation->company);
} }
}); });
} }

View File

@ -12,6 +12,7 @@
namespace App\Services\Quote; namespace App\Services\Quote;
use App\Helpers\Email\QuoteEmail; use App\Helpers\Email\QuoteEmail;
use App\Jobs\Entity\EmailEntity;
use App\Jobs\Quote\EmailQuote; use App\Jobs\Quote\EmailQuote;
use App\Models\ClientContact; use App\Models\ClientContact;
use App\Models\Quote; use App\Models\Quote;
@ -48,7 +49,9 @@ class SendEmail
if ($invitation->contact->send_email && $invitation->contact->email) { if ($invitation->contact->send_email && $invitation->contact->email) {
$email_builder = (new QuoteEmail())->build($invitation, $this->reminder_template); $email_builder = (new QuoteEmail())->build($invitation, $this->reminder_template);
EmailQuote::dispatchNow($email_builder, $invitation, $invitation->company); // EmailQuote::dispatchNow($email_builder, $invitation, $invitation->company);
EmailEntity::dispatchNow($invitation, $invitation->company);
} }
}); });

View File

@ -40,7 +40,7 @@ class SendFailedEmailsTest extends TestCase
public function testReminderFires() public function testReminderFires()
{ {
$invitation = $this->invoice->invitations->first(); $invitation = $this->invoice->invitations->first();
$reminder_template = $this->invoice->calculateTemplate(); $reminder_template = $this->invoice->calculateTemplate('invoice');
$sl = [ $sl = [
'entity_name' => \App\Models\InvoiceInvitation::class, 'entity_name' => \App\Models\InvoiceInvitation::class,
@ -63,11 +63,6 @@ class SendFailedEmailsTest extends TestCase
$this->assertNotNull($sys_log); $this->assertNotNull($sys_log);
// Queue::fake();
SendFailedEmails::dispatch(); SendFailedEmails::dispatch();
//Queue::assertPushed(SendFailedEmails::class);
//Queue::assertPushed(EmailInvoice::class);
//$this->expectsJobs(EmailInvoice::class);
} }
} }