Merge pull request #4216 from turbo124/v5-develop

Refactor HTMLEngine
This commit is contained in:
David Bomba 2020-10-28 06:17:49 +11:00 committed by GitHub
commit 073caab09b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 1581 additions and 771 deletions

View File

@ -112,7 +112,6 @@ class CreateSingleAccount extends Command
$company = Company::factory()->create([
'account_id' => $account->id,
'slack_webhook_url' => config('ninja.notification.slack'),
'use_credits_payment' => 'always',
]);
$account->default_company_id = $company->id;

View File

@ -189,6 +189,7 @@ class CompanySettings extends BaseSettings
public $enable_reminder1 = false;
public $enable_reminder2 = false;
public $enable_reminder3 = false;
public $enable_reminder_endless = false;
public $num_days_reminder1 = 0;
public $num_days_reminder2 = 0;
@ -256,6 +257,7 @@ class CompanySettings extends BaseSettings
public $use_credits_payment = 'off'; //always, option, off
public static $casts = [
'enable_reminder_endless' => 'bool',
'use_credits_payment' => 'string',
'recurring_invoice_number_pattern' => 'string',
'recurring_invoice_number_counter' => 'int',

View File

@ -124,11 +124,6 @@ class EmailTemplateDefaults
public static function emailInvoiceTemplate()
{
$converter = new CommonMarkConverter([
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
$invoice_message = '<p>'.self::transformText('invoice_message').'</p><br><br><p>$view_link</p>';
return $invoice_message;
@ -141,12 +136,9 @@ class EmailTemplateDefaults
public static function emailQuoteTemplate()
{
$converter = new CommonMarkConverter([
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
$quote_message = '<p>'.self::transformText('quote_message').'</p><br><br><p>$view_link</p>';
return $converter->convertToHtml(self::transformText('quote_message'));
return $quote_message;
}
public static function emailPaymentSubject()
@ -167,13 +159,9 @@ class EmailTemplateDefaults
public static function emailCreditTemplate()
{
$converter = new CommonMarkConverter([
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
return $converter->convertToHtml(self::transformText('credit_message'));
$credit_message = '<p>'.self::transformText('credit_message').'</p><br><br><p>$view_link</p>';
return $credit_message;
}
public static function emailPaymentPartialTemplate()

View File

@ -20,26 +20,6 @@ class EmailBuilder
public $view_link;
public $view_text;
private function parseTemplate(string $data, bool $is_markdown = true, $contact = null): string
{
//process variables
if (! empty($this->variables)) {
$data = str_replace(array_keys($this->variables), array_values($this->variables), $data);
}
//process markdown
if ($is_markdown) {
$converter = new CommonMarkConverter([
'html_input' => 'allow',
'allow_unsafe_links' => true,
]);
$data = $converter->convertToHtml($data);
}
return $data;
}
/**
* @param $footer
* @return $this
@ -75,7 +55,6 @@ class EmailBuilder
*/
public function setSubject($subject)
{
//$this->subject = $this->parseTemplate($subject, false, $this->contact);
if (! empty($this->variables)) {
$subject = str_replace(array_keys($this->variables), array_values($this->variables), $subject);
@ -92,8 +71,7 @@ class EmailBuilder
*/
public function setBody($body)
{
//$this->body = $this->parseTemplate($body, true);
//todo move this to use HTMLEngine
if (! empty($this->variables)) {
$body = str_replace(array_keys($this->variables), array_values($this->variables), $body);
}

View File

@ -8,6 +8,7 @@
namespace App\Helpers\Email;
use App\Helpers\Email\EntityEmailInterface;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Utils\Number;
@ -21,7 +22,7 @@ class InvoiceEmail extends EmailBuilder
$contact = $invitation->contact;
if (! $reminder_template) {
$reminder_template = $invoice->calculateTemplate();
$reminder_template = $invoice->calculateTemplate('invoice');
}
$body_template = $client->getSetting('email_template_'.$reminder_template);

View File

@ -13,6 +13,7 @@ namespace App\Http\Controllers;
use App\Helpers\Email\InvoiceEmail;
use App\Http\Requests\Email\SendEmailRequest;
use App\Jobs\Entity\EmailEntity;
use App\Jobs\Invoice\EmailInvoice;
use App\Jobs\Mail\EntitySentMailer;
use App\Models\Credit;
@ -116,9 +117,9 @@ class EmailController extends BaseController
$entity_obj->invitations->each(function ($invitation) use ($subject, $body, $entity_string, $entity_obj) {
if ($invitation->contact->send_email && $invitation->contact->email) {
$when = now()->addSeconds(1);
$invitation->contact->notify((new SendGenericNotification($invitation, $entity_string, $subject, $body))->delay($when));
EmailEntity::dispatchNow($invitation, $invitation->company);
//$invitation->contact->notify((new SendGenericNotification($invitation, $entity_string, $subject, $body))->delay($when));
}
});

View File

@ -98,7 +98,7 @@ class PreviewController extends BaseController
$entity_obj->load('client');
$html = new HtmlEngine(null, $entity_obj->invitations()->first(), request()->entity_type);
$html = new HtmlEngine($entity_obj->invitations()->first());
$design_namespace = 'App\Services\PdfMaker\Designs\\'.request()->design['name'];
@ -175,7 +175,7 @@ class PreviewController extends BaseController
return response()->json(['message' => 'Invalid custom design object'], 400);
}
$html = new HtmlEngine(null, $invoice->invitations()->first(), 'invoice');
$html = new HtmlEngine($invoice->invitations()->first());
$design = new Design(Design::CUSTOM, ['custom_partials' => request()->design['design']]);

View File

@ -1,99 +0,0 @@
<?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\Jobs\Credit;
use App\Designs\Custom;
use App\Designs\Designer;
use App\Designs\Modern;
use App\Libraries\MultiDB;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\Design;
use App\Models\Invoice;
use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
use App\Utils\Traits\Pdf\PdfMaker;
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\App;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot;
class CreateCreditPdf implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash;
public $credit;
public $company;
public $contact;
private $disk;
public $invitation;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($invitation)
{
$this->invitation = $invitation;
$this->credit = $invitation->credit;
$this->company = $invitation->company;
$this->contact = $invitation->contact;
$this->disk = $disk ?? config('filesystems.default');
}
public function handle()
{
if (config('ninja.phantomjs_key')) {
return (new Phantom)->generate($this->invitation);
}
$this->credit->load('client');
App::setLocale($this->contact->preferredLocale());
$path = $this->credit->client->credit_filepath();
$file_path = $path.$this->credit->number.'.pdf';
$credit_design_id = $this->credit->design_id ? $this->credit->design_id : $this->decodePrimaryKey($this->credit->client->getSetting('credit_design_id'));
$design = Design::find($credit_design_id);
$designer = new Designer($this->credit, $design, $this->credit->client->getSetting('pdf_variables'), 'credit');
$html = (new HtmlEngine($designer, $this->invitation, 'credit'))->build();
Storage::makeDirectory($path, 0775);
$pdf = $this->makePdf(null, null, $html);
$instance = Storage::disk($this->disk)->put($file_path, $pdf);
return $file_path;
}
}

View File

@ -123,7 +123,7 @@ class CreateEntityPdf implements ShouldQueue
$entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting($entity_design_id));
$design = Design::find($entity_design_id);
$html = new HtmlEngine(null, $this->invitation, $this->entity_string);
$html = new HtmlEngine($this->invitation);
if ($design->is_custom) {
$options = [

View File

@ -0,0 +1,187 @@
<?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\Jobs\Entity;
use App\DataMapper\Analytics\EmailInvoiceFailure;
use App\Events\Invoice\InvoiceWasEmailed;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Helpers\Email\InvoiceEmail;
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Utils\SystemLogger;
use App\Libraries\MultiDB;
use App\Mail\TemplateEmail;
use App\Models\Company;
use App\Models\CreditInvitation;
use App\Models\Invoice;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoiceInvitation;
use App\Models\SystemLog;
use App\Utils\HtmlEngine;
use App\Utils\Ninja;
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;
use Illuminate\Support\Str;
use Symfony\Component\Mime\Test\Constraint\EmailTextBodyContains;
use Turbo124\Beacon\Facades\LightLogs;
/*Multi Mailer implemented*/
class EmailEntity extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $invitation;
public $company;
public $settings;
public $entity_string;
public $reminder_template;
public $entity;
public $html_engine;
public $email_entity_builder;
/**
* EmailEntity constructor.
* @param Invitation $invitation
* @param Company $company
* @param ?string $reminder_template
*/
public function __construct($invitation, Company $company, ?string $reminder_template = null)
{
$this->company = $company;
$this->invitation = $invitation;
$this->settings = $invitation->contact->client->getMergedSettings();
$this->entity_string = $this->resolveEntityString();
$this->entity = $invitation->{$this->entity_string};
$this->reminder_template = $reminder_template ?: $this->findReminderTemplate();
$this->html_engine = new HtmlEngine($invitation);
$this->email_entity_builder = $this->resolveEmailBuilder();
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$this->setMailDriver();
try {
Mail::to($this->invitation->contact->email, $this->invitation->contact->present()->name())
->send(
new TemplateEmail(
$this->email_entity_builder,
$this->invitation->contact->user,
$this->invitation->contact->client
)
);
} catch (\Swift_TransportException $e) {
$this->entityEmailFailed($e->getMessage());
}
if (count(Mail::failures()) > 0) {
$this->logMailError(Mail::failures(), $this->entity->client);
} else {
$this->entityEmailSucceeded();
}
/* Mark entity sent */
$this->entity->service()->markSent()->save();
}
public function failed($exception = null)
{
info('the job failed');
$job_failure = new EmailInvoiceFailure();
$job_failure->string_metric5 = $this->entity_string;
$job_failure->string_metric6 = $exception->getMessage();
LightLogs::create($job_failure)
->batch();
}
private function resolveEntityString() :string
{
if($this->invitation instanceof InvoiceInvitation)
return 'invoice';
elseif($this->invitation instanceof QuoteInvitation)
return 'quote';
elseif($this->invitation instanceof CreditInvitation)
return 'credit';
elseif($this->invitation instanceof RecurringInvoiceInvitation)
return 'recurring_invoice';
}
private function entityEmailFailed($message)
{
switch ($this->entity_string) {
case 'invoice':
event(new InvoiceWasEmailedAndFailed($this->invitation->invoice, $this->company, $message, Ninja::eventVars()));
break;
default:
# code...
break;
}
}
private function entityEmailSucceeded()
{
switch ($this->entity_string) {
case 'invoice':
event(new InvoiceWasEmailed($this->invitation, $this->company, Ninja::eventVars()));
break;
default:
# code...
break;
}
}
private function findReminderTemplate()
{
}
private function resolveEmailBuilder()
{
$class = 'App\Mail\Engine\\' . ucfirst(Str::camel($this->entity_string)) . "EmailEngine";
return (new $class($this->invitation, $this->reminder_template))->build();
}
}

View File

@ -1,129 +0,0 @@
<?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\Jobs\Invoice;
use App\Designs\Custom;
use App\Designs\Designer;
use App\Designs\Modern;
use App\Libraries\MultiDB;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\Design;
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Models\Invoice;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
use App\Utils\Traits\Pdf\PdfMaker;
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\App;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot;
class CreateInvoicePdf implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash;
public $invoice;
public $company;
public $contact;
private $disk;
public $invitation;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($invitation)
{
$this->invitation = $invitation;
$this->invoice = $invitation->invoice;
$this->company = $invitation->company;
$this->contact = $invitation->contact;
$this->disk = $disk ?? config('filesystems.default');
}
public function handle()
{
if (config('ninja.phantomjs_key')) {
return (new Phantom)->generate($this->invitation);
}
App::setLocale($this->contact->preferredLocale());
$path = $this->invoice->client->invoice_filepath();
$file_path = $path.$this->invoice->number.'.pdf';
$invoice_design_id = $this->invoice->design_id ? $this->invoice->design_id : $this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id'));
$design = Design::find($invoice_design_id);
$html = new HtmlEngine(null, $this->invitation, 'invoice');
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' => $this->invoice->client,
'entity' => $this->invoice,
'pdf_variables' => (array) $this->invoice->company->settings->pdf_variables,
'products' => $design->design->product,
]),
'variables' => $html->generateLabelsAndValues(),
'options' => [
'all_pages_header' => $this->invoice->client->getSetting('all_pages_header'),
'all_pages_footer' => $this->invoice->client->getSetting('all_pages_footer'),
],
];
$maker = new PdfMakerService($state);
$maker
->design($template)
->build();
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
Storage::makeDirectory($path, 0775);
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
$instance = Storage::disk($this->disk)->put($file_path, $pdf);
return $file_path;
}
}

View File

@ -71,6 +71,7 @@ class EmailInvoice extends BaseMailerJob implements ShouldQueue
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$this->setMailDriver();

View File

@ -1,121 +0,0 @@
<?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\Jobs\Quote;
use App\Designs\Custom;
use App\Designs\Designer;
use App\Designs\Modern;
use App\Libraries\MultiDB;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\Design;
use App\Models\Invoice;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\NumberFormatter;
use App\Utils\Traits\Pdf\PdfMaker;
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\App;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot;
class CreateQuotePdf implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash;
public $quote;
public $company;
public $contact;
private $disk;
public $invitation;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($invitation)
{
$this->invitation = $invitation;
$this->quote = $invitation->quote;
$this->company = $invitation->company;
$this->contact = $invitation->contact;
$this->disk = $disk ?? config('filesystems.default');
}
public function handle()
{
if (config('ninja.phantomjs_key')) {
return (new Phantom)->generate($this->invitation);
}
$this->quote->load('client');
App::setLocale($this->contact->preferredLocale());
$path = $this->quote->client->quote_filepath();
$quote_design_id = $this->quote->design_id ? $this->quote->design_id : $this->decodePrimaryKey($this->quote->client->getSetting('quote_design_id'));
$design = Design::find($quote_design_id);
$html = new HtmlEngine(null, $this->invitation, 'quote');
$template = new PdfMakerDesign(strtolower($design->name));
$state = [
'template' => $template->elements([
'client' => $this->quote->client,
'entity' => $this->quote,
'pdf_variables' => (array) $this->quote->company->settings->pdf_variables,
'products' => $design->design->product,
]),
'variables' => $html->generateLabelsAndValues(),
'options' => [
'all_pages_header' => $this->quote->client->getSetting('all_pages_header'),
'all_pages_footer' => $this->quote->client->getSetting('all_pages_footer'),
],
];
$maker = new PdfMakerService($state);
$maker
->design($template)
->build();
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
Storage::makeDirectory($path, 0775);
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML());
$file_path = $path.$this->quote->number.'.pdf';
$instance = Storage::disk($this->disk)->put($file_path, $pdf);
return $file_path;
}
}

View File

@ -0,0 +1,135 @@
<?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\Mail\Engine;
class BaseEmailEngine implements EngineInterface
{
public $footer;
public $variables;
public $contact;
public $subject;
public $body;
public $template_style;
public $attachments;
public $link;
public $text;
public function setFooter($footer)
{
$this->footer = $footer;
return $this;
}
public function setVariables($variables)
{
$this->variables = $variables;
return $this;
}
public function setContact($contact)
{
$this->contact = $contact;
return $this;
}
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
public function setBody($body)
{
$this->body = $body;
return $this;
}
public function setTemplate($template_style)
{
$this->template_style = $template_style;
return $this;
}
public function setAttachments($attachments)
{
$this->attachments = $attachments;
return $this;
}
public function setViewLink($link)
{
$this->link = $link;
return $this;
}
public function setViewText($text)
{
$this->text = $text;
return $this;
}
public function getSubject()
{
return $this->subject;
}
public function getBody()
{
return $this->body;
}
public function getAttachments()
{
return $this->attachments;
}
public function getFooter()
{
return $this->footer;
}
public function getTemplate()
{
return $this->template_style;
}
public function getViewLink()
{
return $this->link;
}
public function getViewText()
{
return $this->text;
}
public function build(){}
}

View File

@ -0,0 +1,51 @@
<?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\Mail\Engine;
interface EngineInterface
{
public function setFooter($footer);
public function setVariables($variables);
public function setContact($contact);
public function setSubject($subject);
public function setBody($body);
public function setTemplate($template_style);
public function setAttachments($attachments);
public function setViewLink($link);
public function setViewText($text);
public function getSubject();
public function getBody();
public function getAttachments();
public function getFooter();
public function getTemplate();
public function getViewLink();
public function getViewText();
public function build();
}

View File

@ -0,0 +1,90 @@
<?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\Mail\Engine;
use App\Utils\Number;
class InvoiceEmailEngine extends BaseEmailEngine
{
public $invitation;
public $client;
public $invoice;
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->invoice = $invitation->invoice;
$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.invoice_message',
[
'invoice' => $this->invoice->number,
'company' => $this->invoice->company->present()->name(),
'amount' => Number::formatMoney($this->invoice->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.invoice_subject',
[
'number' => $this->invoice->number,
'account' => $this->invoice->company->present()->name(),
],
null,
$this->client->locale()
);
}
$this->setTemplate($this->client->getSetting('email_style'))
->setContact($this->contact)
->setVariables($this->invoice->makeValues($this->contact))//move make values into the htmlengine
->setSubject($subject_template)
->setBody($body_template)
->setFooter("<a href='{$this->invitation->getLink()}'>".ctrans('texts.view_invoice').'</a>')
->setViewLink($this->invitation->getLink())
->setViewText(ctrans('texts.view_invoice'));
if ($this->client->getSetting('pdf_email_attachment') !== false) {
$this->setAttachments($invitation->pdf_file_path());
}
return $this;
}
}

View File

@ -46,9 +46,7 @@ class TemplateEmail extends Mailable
*/
public function build()
{
/*Alter Run Time Mailer configuration (driver etc etc) to regenerate the Mailer Singleton*/
//if using a system level template
$template_name = 'email.template.'.$this->build_email->getTemplate();
$settings = $this->client->getMergedSettings();

View File

@ -35,6 +35,7 @@ class Task extends BaseModel
'time_log',
'status_id',
'status_sort_order',
'invoice_documents',
];
protected $touches = [];

View File

@ -222,7 +222,6 @@ class InvoiceService
public function updateStatus()
{
info("invoice balance = {$this->invoice->balance}");
if((int)$this->invoice->balance == 0)
$this->setStatus(Invoice::STATUS_PAID);

View File

@ -43,7 +43,7 @@ class SendEmail extends AbstractService
public function run()
{
if (! $this->reminder_template) {
$this->reminder_template = $this->invoice->calculateTemplate();
$this->reminder_template = $this->invoice->calculateTemplate('invoice');
}
$this->invoice->invitations->each(function ($invitation) {

View File

@ -65,7 +65,7 @@ class TriggeredActions extends AbstractService
private function sendEmail()
{
//$reminder_template = $this->invoice->calculateTemplate();
//$reminder_template = $this->invoice->calculateTemplate('invoice');
$reminder_template = 'payment';
$this->invoice->invitations->load('contact.client.country', 'invoice.client.country', 'invoice.company')->each(function ($invitation) use ($reminder_template) {

View File

@ -41,7 +41,7 @@ class SendEmail
public function run()
{
if (! $this->reminder_template) {
$this->reminder_template = $this->quote->calculateTemplate();
$this->reminder_template = $this->quote->calculateTemplate('quote');
}
$this->quote->invitations->each(function ($invitation) {

View File

@ -154,6 +154,7 @@ class CompanyTransformer extends EntityTransformer
'invoice_task_timelog' => (bool) $company->invoice_task_timelog,
'auto_start_tasks' => (bool) $company->auto_start_tasks,
'invoice_task_documents' => (bool) $company->invoice_task_documents,
'use_credits_payment' => 'always', //todo remove
];
}

View File

@ -48,15 +48,15 @@ class TaskTransformer extends EntityTransformer
'assigned_user_id' => (string) $this->encodePrimaryKey($task->assigned_user_id),
'number' => (string) $task->number ?: '',
'start_time' => (int) $task->start_time,
'description' => $task->description ?: '',
'duration' => 0,
'description' => (string) $task->description ?: '',
'duration' => (int) $task->duration ?: 0,
'rate' => (float) $task->rate ?: 0,
'created_at' => (int) $task->created_at,
'updated_at' => (int) $task->updated_at,
'archived_at' => (int) $task->deleted_at,
'invoice_id' => $this->encodePrimaryKey($task->invoice_id),
'client_id' => $this->encodePrimaryKey($task->client_id),
'project_id' => $this->encodePrimaryKey($task->project_id),
'invoice_id' => $this->encodePrimaryKey($task->invoice_id) ?: '',
'client_id' => $this->encodePrimaryKey($task->client_id) ?: '',
'project_id' => $this->encodePrimaryKey($task->project_id) ?: '',
'is_deleted' => (bool) $task->is_deleted,
'time_log' => $task->time_log ?: '',
'is_running' => (bool) $task->is_running,
@ -64,7 +64,7 @@ class TaskTransformer extends EntityTransformer
'custom_value2' => $task->custom_value2 ?: '',
'custom_value3' => $task->custom_value3 ?: '',
'custom_value4' => $task->custom_value4 ?: '',
'status_id' => $this->encodePrimaryKey($task->status_id),
'status_id' => $this->encodePrimaryKey($task->status_id) ?: '',
'status_sort_order' => (int) $task->status_sort_order,
];
}

View File

@ -12,8 +12,11 @@
namespace App\Utils;
use App\Designs\Designer;
use App\Models\Country;
use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoiceInvitation;
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
use Illuminate\Support\Facades\App;
@ -38,15 +41,14 @@ class HtmlEngine
public $entity_string;
public $designer;
public function __construct($designer, $invitation, $entity_string)
public function __construct($invitation)
{
$this->designer = $designer;
$this->invitation = $invitation;
$this->entity = $invitation->{$entity_string};
$this->entity_string = $this->resolveEntityString();
$this->entity = $invitation->{$this->entity_string};
$this->company = $invitation->company;
@ -58,34 +60,32 @@ class HtmlEngine
$this->entity_calc = $this->entity->calc();
$this->entity_string = $entity_string;
}
public function build() :string
{
App::setLocale($this->client->preferredLocale());
$values_and_labels = $this->generateLabelsAndValues();
$this->designer->build();
$data = [];
$data['entity'] = $this->entity;
$data['lang'] = $this->client->preferredLocale();
$data['includes'] = $this->designer->getIncludes();
$data['header'] = $this->designer->getHeader();
$data['body'] = $this->designer->getBody();
$data['footer'] = $this->designer->getFooter();
$html = view('pdf.stub', $data)->render();
$html = $this->parseLabelsAndValues($values_and_labels['labels'], $values_and_labels['values'], $html);
return $html;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private function resolveEntityString()
{
switch ($this->invitation) {
case ($this->invitation instanceof InvoiceInvitation):
return 'invoice';
break;
case ($this->invitation instanceof CreditInvitation):
return 'credit';
break;
case ($this->invitation instanceof QuoteInvitation):
return 'quote';
break;
case ($this->invitation instanceof RecurringInvoiceInvitation):
return 'recurring_invoice';
break;
default:
# code...
break;
}
}
public function buildEntityDataArray() :array
{
if (! $this->client->currency()) {

View File

@ -16,10 +16,14 @@ use App\Models\CreditInvitation;
use App\Models\Design;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
use App\Services\PdfMaker\Design as PdfDesignModel;
use App\Services\PdfMaker\Design as PdfMakerDesign;
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
use App\Utils\HtmlEngine;
use App\Utils\Traits\MakesHash;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class Phantom
{
@ -37,10 +41,13 @@ class Phantom
if ($invitation instanceof InvoiceInvitation) {
$entity = 'invoice';
$entity_design_id = 'invoice_design_id';
} elseif ($invitation instanceof CreditInvitation) {
$entity = 'credit';
$entity_design_id = 'credit_design_id';
} elseif ($invitation instanceof QuoteInvitation) {
$entity = 'quote';
$entity_design_id = 'quote_design_id';
}
$entity_obj = $invitation->{$entity};
@ -76,25 +83,54 @@ class Phantom
public function displayInvitation(string $entity, string $invitation_key)
{
$key = $entity.'_id';
$invitation_instance = 'App\Models\\'.ucfirst($entity).'Invitation';
$invitation_instance = 'App\Models\\'.Str::camel(ucfirst($entity)).'Invitation';
$invitation = $invitation_instance::whereRaw('BINARY `key`= ?', [$invitation_key])->first();
$entity_obj = $invitation->{$entity};
$entity_obj->load('client');
App::setLocale($invitation->contact->preferredLocale());
$design_id = $entity_obj->design_id ? $entity_obj->design_id : $this->decodePrimaryKey($entity_obj->client->getSetting($entity.'_design_id'));
$entity_design_id = $entity . '_design_id';
$design_id = $entity_obj->design_id ? $entity_obj->design_id : $this->decodePrimaryKey($entity_obj->client->getSetting($entity_design_id));
$design = Design::find($design_id);
$html = new HtmlEngine($invitation);
$designer = new Designer($entity_obj, $design, $entity_obj->client->getSetting('pdf_variables'), $entity);
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' => $this->entity->client,
'entity' => $this->entity,
'pdf_variables' => (array) $this->entity->company->settings->pdf_variables,
'products' => $design->design->product,
]),
'variables' => $html->generateLabelsAndValues(),
'options' => [
'all_pages_header' => $entity_obj->client->getSetting('all_pages_header'),
'all_pages_footer' => $entity_obj->client->getSetting('all_pages_footer'),
],
];
$maker = new PdfMakerService($state);
$data['html'] = $maker->design($template)
->build()
->getCompiledHTML(true);
$data['html'] = (new HtmlEngine($designer, $invitation, $entity))->build();
return view('pdf.html', $data);
}

View File

@ -33,7 +33,7 @@ trait InvoiceEmailBuilder
//$client = $this->client;
if (! $reminder_template) {
$reminder_template = $this->calculateTemplate();
$reminder_template = $this->calculateTemplate('invoice');
}
//Need to determine which email template we are producing
@ -78,8 +78,6 @@ trait InvoiceEmailBuilder
{
$invoice_variables = $this->makeValues($contact);
//process variables
// $data = str_replace(array_keys($invoice_variables), array_values($invoice_variables), $template_data);
$data = strtr($template_data, $invoice_variables);
//process markdown

View File

@ -11,6 +11,7 @@
namespace App\Utils\Traits;
use App\Models\RecurringInvoice;
use Illuminate\Support\Carbon;
/**
@ -181,7 +182,7 @@ trait MakesReminders
}
}
public function calculateTemplate(): string
public function calculateTemplate(string $entity_string): string
{
//if invoice is currently a draft, or being marked as sent, this will be the initial email
$client = $this->client;
@ -202,10 +203,58 @@ trait MakesReminders
$client->getSetting('num_days_reminder3')
)) {
return 'reminder3';
} else {
return 'invoice';
} elseif($client->getSetting('enable_reminder_endless') !== false && $this->checkEndlessReminder(
$this->last_sent_date,
$client->getSetting('endless_reminder_frequency_id')
)){
return 'endless_reminder';
}
else {
return $entity_string;
}
//also implement endless reminders here
}
private function checkEndlessReminder($last_sent_date, $endless_reminder_frequency_id) :bool
{
if(Carbon::now()->startOfDay()->eq($this->addTimeInterval($endless_reminder_frequency_id)))
return true;
return false;
}
private function addTimeInterval($endless_reminder_frequency_id) :?Carbon
{
if(!$this->next_send_date){
return null;
}
switch ($endless_reminder_frequency_id) {
case RecurringInvoice::FREQUENCY_WEEKLY:
return Carbon::parse($this->next_send_date)->addWeek()->startOfDay();
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
return Carbon::parse($this->next_send_date)->addWeeks(2)->startOfDay();
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
return Carbon::parse($this->next_send_date)->addWeeks(4)->startOfDay();
case RecurringInvoice::FREQUENCY_MONTHLY:
return Carbon::parse($this->next_send_date)->addMonthNoOverflow()->startOfDay();
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
return Carbon::parse($this->next_send_date)->addMonthsNoOverflow(2)->startOfDay();
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
return Carbon::parse($this->next_send_date)->addMonthsNoOverflow(3)->startOfDay();
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
return Carbon::parse($this->next_send_date)->addMonthsNoOverflow(4)->startOfDay();
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
return Carbon::parse($this->next_send_date)->addMonthsNoOverflow(6)->startOfDay();
case RecurringInvoice::FREQUENCY_ANNUALLY:
return Carbon::parse($this->next_send_date)->addYear()->startOfDay();
case RecurringInvoice::FREQUENCY_TWO_YEARS:
return Carbon::parse($this->next_send_date)->addYears(2)->startOfDay();
case RecurringInvoice::FREQUENCY_THREE_YEARS:
return Carbon::parse($this->next_send_date)->addYears(3)->startOfDay();
default:
return null;
}
}
}

View File

@ -33,7 +33,7 @@ trait QuoteEmailBuilder
//$client = $this->client;
if (! $reminder_template) {
$reminder_template = $this->calculateTemplate();
$reminder_template = $this->calculateTemplate('quote');
}
//Need to determine which email template we are producing

1254
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class TasksInvoiceDocuments extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('tasks', function(Blueprint $table){
$table->boolean('invoice_documents')->default(0);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

View File

@ -37,7 +37,7 @@ class ExampleIntegrationTest extends TestCase
$invoice = $this->invoice;
$invitation = $invoice->invitations()->first();
$engine = new HtmlEngine(null, $invitation, 'invoice');
$engine = new HtmlEngine($invitation);
$design = new Design(
Design::CLEAN

View File

@ -10,7 +10,7 @@
*/
namespace Tests\Integration;
use App\Jobs\Invoice\CreateInvoicePdf;
use App\Jobs\Entity\CreateEntityPdf;
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Cache;
@ -36,7 +36,7 @@ class InvoiceUploadTest extends TestCase
public function testInvoiceUploadWorks()
{
CreateInvoicePdf::dispatchNow($this->invoice->invitations->first());
CreateEntityPdf::dispatchNow($this->invoice->invitations->first());
$this->assertNotNull($this->invoice->service()->getInvoicePdf($this->invoice->client->primary_contact()->first()));
}