1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00

Merge pull request #4218 from turbo124/v5-develop

Email / Credit Email engine
This commit is contained in:
David Bomba 2020-10-28 10:30:21 +11:00 committed by GitHub
commit d211fb3afa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 347 additions and 41 deletions

View File

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

View File

@ -12,7 +12,6 @@
namespace App\Events\Quote;
use App\Models\Company;
use App\Models\QuoteWasViewed;
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\UpdateCreditRequest;
use App\Jobs\Credit\StoreCredit;
use App\Jobs\Entity\EmailEntity;
use App\Jobs\Invoice\EmailCredit;
use App\Jobs\Invoice\MarkInvoicePaid;
use App\Models\Client;
@ -545,7 +546,13 @@ class CreditController extends BaseController
}
break;
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) {
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\Invoice;
use App\Models\Quote;
use App\Models\RecurringInvoice;
use App\Notifications\SendGenericNotification;
use App\Transformers\CreditTransformer;
use App\Transformers\InvoiceTransformer;
use App\Transformers\QuoteTransformer;
use App\Transformers\RecurringInvoiceTransformer;
use App\Utils\Traits\MakesHash;
class EmailController extends BaseController
@ -131,21 +133,27 @@ class EmailController extends BaseController
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_transformer = InvoiceTransformer::class;
}
if ($this instanceof Quote) {
if ($entity_obj instanceof Quote) {
$this->entity_type = Quote::class;
$this->entity_transformer = QuoteTransformer::class;
}
if ($this instanceof Credit) {
if ($entity_obj instanceof Credit) {
$this->entity_type = Credit::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();
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'))) {
$this->reminder_template = $invoice->client->getSetting(request()->input('email_type'));
} 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.

View File

@ -40,25 +40,14 @@ class UpdateTaskRequest extends Request
/* Ensure we have a client name, and that all emails are unique*/
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);
}
// 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()
{
$input = $this->decodePrimaryKeys($this->all());
$this->replace($input);

View File

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

View File

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

View File

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

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

@ -13,7 +13,6 @@ namespace App\Models;
use App\DataMapper\ClientSettings;
use App\DataMapper\CompanySettings;
use App\Designs\Designer;
use App\Filters\QueryFilters;
use App\Models\Design;
use App\Utils\Traits\MakesHash;
@ -176,15 +175,6 @@ class BaseModel extends Model
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
public function getEntityDesigner()
{
$design = Design::find($this->decodePrimaryKey($this->client->getSetting('invoice_design_id')));
$entity = strtolower(class_basename($this));
return new Designer($this, $design, $this->client->getSetting('pdf_variables'), $entity);
}
/**
* @return string
*/

View File

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

View File

@ -17,11 +17,18 @@ use App\Models\Backup;
use App\Models\Client;
use App\Models\CompanyToken;
use App\Models\Credit;
use App\Models\Design;
use App\Models\Invoice;
use App\Models\Quote;
use App\Models\RecurringInvoice;
use App\Models\User;
use App\Utils\HtmlEngine;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use Illuminate\Support\Facades\Log;
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
/**
* Class for activity repository.
@ -29,7 +36,7 @@ use Illuminate\Support\Facades\Log;
class ActivityRepository extends BaseRepository
{
use MakesInvoiceHtml;
use MakesHash;
/**
* Save the Activity.
*
@ -68,7 +75,7 @@ class ActivityRepository extends BaseRepository
if (get_class($entity) == Invoice::class || get_class($entity) == Quote::class || get_class($entity) == Credit::class) {
$contact = $entity->client->primary_contact()->first();
$backup->html_backup = $this->generateEntityHtml($entity->getEntityDesigner(), $entity, $contact);
$backup->html_backup = $this->generateHtml($entity);
$backup->amount = $entity->amount;
}
@ -90,4 +97,55 @@ class ActivityRepository extends BaseRepository
return false;
}
private function generateHtml($entity)
{
$entity_design_id = '';
if($entity instanceof Invoice || $entity instanceof RecurringInvoice){
$entity_design_id = 'invoice_design_id';
}
elseif($entity instanceof Quote){
$entity_design_id = 'quote_design_id';
}
elseif($entity instanceof Credit){
$entity_design_id = 'credit_design_id';
}
$entity_design_id = $entity->design_id ? $entity->design_id : $this->decodePrimaryKey($entity->client->getSetting($entity_design_id));
$design = Design::find($entity_design_id);
$html = new HtmlEngine($entity->invitations->first());
if ($design->is_custom) {
$options = [
'custom_partials' => json_decode(json_encode($design->design), true)
];
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
} else {
$template = new PdfMakerDesign(strtolower($design->name));
}
$state = [
'template' => $template->elements([
'client' => $entity->client,
'entity' => $entity,
'pdf_variables' => (array) $entity->company->settings->pdf_variables,
'products' => $design->design->product,
]),
'variables' => $html->generateLabelsAndValues(),
'options' => [
'all_pages_header' => $entity->client->getSetting('all_pages_header'),
'all_pages_footer' => $entity->client->getSetting('all_pages_footer'),
],
];
$maker = new PdfMakerService($state);
return $maker->design($template)
->build()
->getCompiledHTML(true);
}
}

View File

@ -15,6 +15,7 @@ use App\Models\Credit;
use App\Services\Credit\ApplyPayment;
use App\Services\Credit\CreateInvitations;
use App\Services\Credit\MarkSent;
use App\Services\Credit\SendEmail;
class CreditService
{
@ -55,6 +56,14 @@ class CreditService
return $this;
}
public function sendEmail($contact = null)
{
$send_email = new SendEmail($this->credit, null, $contact);
return $send_email->run();
}
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;
use App\Helpers\Email\InvoiceEmail;
use App\Jobs\Entity\EmailEntity;
use App\Jobs\Invoice\EmailInvoice;
use App\Models\ClientContact;
use App\Models\Invoice;
@ -50,7 +51,9 @@ class SendEmail extends AbstractService
$email_builder = (new InvoiceEmail())->build($invitation, $this->reminder_template);
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;
use App\Helpers\Email\QuoteEmail;
use App\Jobs\Entity\EmailEntity;
use App\Jobs\Quote\EmailQuote;
use App\Models\ClientContact;
use App\Models\Quote;
@ -48,7 +49,9 @@ class SendEmail
if ($invitation->contact->send_email && $invitation->contact->email) {
$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

@ -57,7 +57,8 @@ Route::group(['middleware' => ['auth:contact', 'locale'], 'prefix' => 'client',
Route::get('quotes/{quote}', 'ClientPortal\QuoteController@show')->name('quote.show');
Route::get('quotes/{quote_invitation}', 'ClientPortal\QuoteController@show')->name('quote.show_invitation');
Route::resource('credits', 'ClientPortal\CreditController')->only('index', 'show');
Route::get('credits', 'ClientPortal\CreditController@index')->name('credits.index');
Route::get('credits/{credit}', 'ClientPortal\CreditController@show')->name('credits.show');
Route::get('client/switch_company/{contact}', 'ClientPortal\SwitchCompanyController')->name('switch_company');

View File

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