2020-10-26 01:58:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entity Ninja (https://entityninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/entityninja/entityninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Entity Ninja LLC (https://entityninja.com)
|
2020-10-26 01:58:08 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-10-26 01:58:08 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Entity;
|
|
|
|
|
2021-04-12 06:36:51 +02:00
|
|
|
use App\Exceptions\FilePermissionsFailure;
|
2021-03-17 02:04:54 +01:00
|
|
|
use App\Models\Account;
|
2020-10-26 05:06:58 +01:00
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\CreditInvitation;
|
2020-10-26 01:58:08 +01:00
|
|
|
use App\Models\Design;
|
2020-10-26 05:06:58 +01:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\InvoiceInvitation;
|
2020-10-26 01:58:08 +01:00
|
|
|
use App\Models\Quote;
|
2020-10-26 05:06:58 +01:00
|
|
|
use App\Models\QuoteInvitation;
|
2021-01-15 03:02:55 +01:00
|
|
|
use App\Models\RecurringInvoice;
|
2020-10-26 05:06:58 +01:00
|
|
|
use App\Models\RecurringInvoiceInvitation;
|
2020-10-26 01:58:08 +01:00
|
|
|
use App\Services\PdfMaker\Design as PdfDesignModel;
|
|
|
|
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
|
|
|
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
|
2021-03-18 01:53:08 +01:00
|
|
|
use App\Utils\HostedPDF\NinjaPdf;
|
2020-10-26 01:58:08 +01:00
|
|
|
use App\Utils\HtmlEngine;
|
2020-11-10 05:04:53 +01:00
|
|
|
use App\Utils\Ninja;
|
2020-10-26 01:58:08 +01:00
|
|
|
use App\Utils\PhantomJS\Phantom;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-10-26 05:06:58 +01:00
|
|
|
use App\Utils\Traits\MakesInvoiceHtml;
|
2020-10-26 01:58:08 +01:00
|
|
|
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;
|
2020-11-10 05:04:53 +01:00
|
|
|
use Illuminate\Support\Facades\Lang;
|
2020-10-26 01:58:08 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
class CreateEntityPdf implements ShouldQueue
|
|
|
|
{
|
2020-10-26 05:06:58 +01:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash;
|
2020-10-26 01:58:08 +01:00
|
|
|
|
|
|
|
public $entity;
|
|
|
|
|
|
|
|
public $company;
|
|
|
|
|
|
|
|
public $contact;
|
|
|
|
|
|
|
|
private $disk;
|
|
|
|
|
|
|
|
public $invitation;
|
|
|
|
|
2020-10-26 05:06:58 +01:00
|
|
|
public $entity_string = '';
|
|
|
|
|
2020-10-26 01:58:08 +01:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param $invitation
|
2020-10-26 01:58:08 +01:00
|
|
|
*/
|
2021-05-16 01:41:12 +02:00
|
|
|
public function __construct($invitation, $disk = 'public')
|
2020-10-26 01:58:08 +01:00
|
|
|
{
|
|
|
|
$this->invitation = $invitation;
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($invitation instanceof InvoiceInvitation) {
|
2020-10-26 01:58:08 +01:00
|
|
|
$this->entity = $invitation->invoice;
|
2020-10-26 05:06:58 +01:00
|
|
|
$this->entity_string = 'invoice';
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($invitation instanceof QuoteInvitation) {
|
2020-10-26 01:58:08 +01:00
|
|
|
$this->entity = $invitation->quote;
|
2020-10-26 05:06:58 +01:00
|
|
|
$this->entity_string = 'quote';
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($invitation instanceof CreditInvitation) {
|
2020-10-26 01:58:08 +01:00
|
|
|
$this->entity = $invitation->credit;
|
2020-10-26 05:06:58 +01:00
|
|
|
$this->entity_string = 'credit';
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($invitation instanceof RecurringInvoiceInvitation) {
|
2020-10-26 05:06:58 +01:00
|
|
|
$this->entity = $invitation->recurring_invoice;
|
|
|
|
$this->entity_string = 'recurring_invoice';
|
|
|
|
}
|
2020-10-26 01:58:08 +01:00
|
|
|
|
|
|
|
$this->company = $invitation->company;
|
|
|
|
|
|
|
|
$this->contact = $invitation->contact;
|
|
|
|
|
2021-05-16 01:41:12 +02:00
|
|
|
$this->disk = $disk;
|
2021-05-15 04:19:36 +02:00
|
|
|
|
|
|
|
// $this->disk = $disk ?? config('filesystems.default');
|
2020-10-26 01:58:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
2021-05-07 06:28:34 +02:00
|
|
|
|
|
|
|
/* Forget the singleton*/
|
2020-11-10 05:04:53 +01:00
|
|
|
App::forgetInstance('translator');
|
2021-05-07 06:28:34 +02:00
|
|
|
|
|
|
|
/* Init a new copy of the translator*/
|
|
|
|
$t = app('translator');
|
2021-05-31 12:40:34 +02:00
|
|
|
/* Set the locale*/
|
|
|
|
App::setLocale($this->contact->preferredLocale());
|
|
|
|
|
2021-05-07 06:28:34 +02:00
|
|
|
/* Set customized translations _NOW_ */
|
2021-05-31 12:40:34 +02:00
|
|
|
$t->replace(Ninja::transformTranslations($this->entity->client->getMergedSettings()));
|
2020-12-11 09:54:40 +01:00
|
|
|
|
2021-06-13 11:44:33 +02:00
|
|
|
/*This line of code hurts... it deletes ALL $entity PDFs... this causes a race condition when trying to send an email*/
|
2021-06-13 06:19:40 +02:00
|
|
|
// $this->entity->service()->deletePdf();
|
2021-03-01 00:40:18 +01:00
|
|
|
|
2021-05-09 13:30:31 +02:00
|
|
|
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
2020-12-11 09:54:40 +01:00
|
|
|
return (new Phantom)->generate($this->invitation);
|
|
|
|
}
|
2020-10-26 01:58:08 +01:00
|
|
|
|
2020-10-26 10:13:00 +01:00
|
|
|
$entity_design_id = '';
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($this->entity instanceof Invoice) {
|
2021-06-12 13:50:01 +02:00
|
|
|
$path = $this->entity->client->invoice_filepath($this->invitation);
|
2020-10-26 10:13:00 +01:00
|
|
|
$entity_design_id = 'invoice_design_id';
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($this->entity instanceof Quote) {
|
2021-06-12 13:50:01 +02:00
|
|
|
$path = $this->entity->client->quote_filepath($this->invitation);
|
2020-10-26 10:13:00 +01:00
|
|
|
$entity_design_id = 'quote_design_id';
|
2020-11-25 15:19:52 +01:00
|
|
|
} elseif ($this->entity instanceof Credit) {
|
2021-06-12 13:50:01 +02:00
|
|
|
$path = $this->entity->client->credit_filepath($this->invitation);
|
2020-10-26 10:13:00 +01:00
|
|
|
$entity_design_id = 'credit_design_id';
|
2021-01-15 03:02:55 +01:00
|
|
|
} elseif ($this->entity instanceof RecurringInvoice) {
|
2021-06-12 13:50:01 +02:00
|
|
|
$path = $this->entity->client->recurring_invoice_filepath($this->invitation);
|
2021-01-15 03:02:55 +01:00
|
|
|
$entity_design_id = 'invoice_design_id';
|
2020-10-26 10:13:00 +01:00
|
|
|
}
|
2020-10-26 01:58:08 +01:00
|
|
|
|
2021-03-17 12:29:20 +01:00
|
|
|
$file_path = $path.$this->entity->numberFormatter().'.pdf';
|
2020-10-26 01:58:08 +01:00
|
|
|
|
2020-10-26 10:13:00 +01:00
|
|
|
$entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting($entity_design_id));
|
2020-10-26 01:58:08 +01:00
|
|
|
|
2021-03-17 02:04:54 +01:00
|
|
|
if(!$this->company->account->hasFeature(Account::FEATURE_DIFFERENT_DESIGNS))
|
|
|
|
$entity_design_id = 2;
|
|
|
|
|
2020-10-26 01:58:08 +01:00
|
|
|
$design = Design::find($entity_design_id);
|
2021-06-23 22:55:33 +02:00
|
|
|
|
|
|
|
/* Catch all in case migration doesn't pass back a valid design */
|
|
|
|
if(!$design)
|
|
|
|
$design = Design::find(2);
|
|
|
|
|
2020-10-27 12:57:12 +01:00
|
|
|
$html = new HtmlEngine($this->invitation);
|
2020-10-26 01:58:08 +01:00
|
|
|
|
|
|
|
if ($design->is_custom) {
|
2020-11-25 15:19:52 +01:00
|
|
|
$options = [
|
2020-10-26 01:58:08 +01:00
|
|
|
'custom_partials' => json_decode(json_encode($design->design), true)
|
|
|
|
];
|
2020-11-25 15:19:52 +01:00
|
|
|
$template = new PdfMakerDesign(PdfDesignModel::CUSTOM, $options);
|
2020-10-26 01:58:08 +01:00
|
|
|
} else {
|
2020-11-25 15:19:52 +01:00
|
|
|
$template = new PdfMakerDesign(strtolower($design->name));
|
2020-10-26 01:58:08 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:42:35 +01:00
|
|
|
$variables = $html->generateLabelsAndValues();
|
|
|
|
|
2020-10-26 01:58:08 +01:00
|
|
|
$state = [
|
|
|
|
'template' => $template->elements([
|
|
|
|
'client' => $this->entity->client,
|
|
|
|
'entity' => $this->entity,
|
|
|
|
'pdf_variables' => (array) $this->entity->company->settings->pdf_variables,
|
2020-11-04 14:56:08 +01:00
|
|
|
'$product' => $design->design->product,
|
2021-02-16 14:42:35 +01:00
|
|
|
'variables' => $variables,
|
2020-10-26 01:58:08 +01:00
|
|
|
]),
|
2021-02-16 14:42:35 +01:00
|
|
|
'variables' => $variables,
|
2020-10-26 01:58:08 +01:00
|
|
|
'options' => [
|
|
|
|
'all_pages_header' => $this->entity->client->getSetting('all_pages_header'),
|
|
|
|
'all_pages_footer' => $this->entity->client->getSetting('all_pages_footer'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMakerService($state);
|
|
|
|
|
|
|
|
$maker
|
|
|
|
->design($template)
|
|
|
|
->build();
|
|
|
|
|
2020-11-22 12:14:49 +01:00
|
|
|
$pdf = null;
|
2020-10-26 01:58:08 +01:00
|
|
|
|
2020-11-22 12:14:49 +01:00
|
|
|
try {
|
2021-03-18 01:53:08 +01:00
|
|
|
|
2021-05-09 13:30:31 +02:00
|
|
|
if(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){
|
2021-03-18 01:53:08 +01:00
|
|
|
$pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
|
|
|
|
}
|
2021-04-12 06:36:51 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
} catch (\Exception $e) {
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog(print_r($e->getMessage(), 1));
|
2020-11-22 12:14:49 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 15:44:01 +01:00
|
|
|
if (config('ninja.log_pdf_html')) {
|
2021-01-05 17:45:34 +01:00
|
|
|
info($maker->getCompiledHTML());
|
2020-12-17 15:44:01 +01:00
|
|
|
}
|
|
|
|
|
2021-04-12 06:36:51 +02:00
|
|
|
|
2021-05-15 04:19:36 +02:00
|
|
|
if ($pdf) {
|
2021-04-12 06:36:51 +02:00
|
|
|
|
2021-05-15 04:19:36 +02:00
|
|
|
try{
|
2021-06-13 01:58:23 +02:00
|
|
|
|
2021-06-13 06:09:33 +02:00
|
|
|
if(!Storage::disk($this->disk)->exists($path))
|
|
|
|
Storage::disk($this->disk)->makeDirectory($path, 0775);
|
2021-06-13 01:58:23 +02:00
|
|
|
|
2021-06-13 05:57:08 +02:00
|
|
|
nlog($file_path);
|
|
|
|
|
2021-05-15 04:19:36 +02:00
|
|
|
Storage::disk($this->disk)->put($file_path, $pdf);
|
2021-05-16 01:41:12 +02:00
|
|
|
|
2021-05-15 04:19:36 +02:00
|
|
|
}
|
|
|
|
catch(\Exception $e)
|
|
|
|
{
|
2021-04-12 06:36:51 +02:00
|
|
|
|
2021-05-16 01:41:12 +02:00
|
|
|
throw new FilePermissionsFailure($e->getMessage());
|
2021-04-12 06:36:51 +02:00
|
|
|
|
2021-05-15 04:19:36 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-26 01:58:08 +01:00
|
|
|
|
2021-05-15 04:19:36 +02:00
|
|
|
return $file_path;
|
2020-10-26 01:58:08 +01:00
|
|
|
}
|
2020-12-13 11:33:30 +01:00
|
|
|
|
2021-01-08 02:44:31 +01:00
|
|
|
public function failed($e)
|
2020-12-13 11:33:30 +01:00
|
|
|
{
|
2021-01-08 02:44:31 +01:00
|
|
|
|
2020-12-13 11:33:30 +01:00
|
|
|
}
|
2021-05-16 01:41:12 +02:00
|
|
|
|
2020-10-26 01:58:08 +01:00
|
|
|
}
|