2020-10-26 01:58:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2022-06-06 00:49:41 +02:00
|
|
|
* Invoice Ninja (https://entityninja.com).
|
2020-10-26 01:58:08 +01:00
|
|
|
*
|
2022-06-06 00:49:41 +02:00
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
2020-10-26 01:58:08 +01:00
|
|
|
*
|
2022-06-06 00:49:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.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;
|
2022-01-04 02:52:17 +01:00
|
|
|
use App\Libraries\MultiDB;
|
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;
|
2022-05-25 08:34:43 +02:00
|
|
|
use App\Utils\Traits\Pdf\PageNumbering;
|
|
|
|
use App\Utils\Traits\Pdf\PDF;
|
2020-10-26 01:58:08 +01:00
|
|
|
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;
|
2022-05-25 08:34:43 +02:00
|
|
|
use setasign\Fpdi\PdfParser\StreamReader;
|
2020-10-26 01:58:08 +01:00
|
|
|
|
|
|
|
class CreateEntityPdf implements ShouldQueue
|
|
|
|
{
|
2022-05-25 08:34:43 +02:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash, PageNumbering;
|
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 = '';
|
|
|
|
|
2021-10-08 06:00:17 +02:00
|
|
|
public $client;
|
|
|
|
|
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
|
|
|
*/
|
2022-06-13 01:33:40 +02:00
|
|
|
public function __construct($invitation, $disk = null)
|
2020-10-26 01:58:08 +01:00
|
|
|
{
|
|
|
|
$this->invitation = $invitation;
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($invitation instanceof InvoiceInvitation) {
|
2021-10-08 07:29:06 +02:00
|
|
|
// $invitation->load('contact.client.company','invoice.client','invoice.user.account');
|
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) {
|
2021-10-08 07:29:06 +02:00
|
|
|
// $invitation->load('contact.client.company','quote.client','quote.user.account');
|
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) {
|
2021-10-08 07:29:06 +02:00
|
|
|
// $invitation->load('contact.client.company','credit.client','credit.user.account');
|
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) {
|
2021-10-08 07:29:06 +02:00
|
|
|
// $invitation->load('contact.client.company','recurring_invoice');
|
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-10-08 06:00:17 +02:00
|
|
|
$this->client = $invitation->contact->client;
|
2021-10-13 05:44:10 +02:00
|
|
|
$this->client->load('company');
|
2021-07-07 13:39:49 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->disk = $disk ?? config('filesystems.default');
|
2020-10-26 01:58:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
2022-01-04 02:52:17 +01:00
|
|
|
MultiDB::setDb($this->company->db);
|
2021-10-14 07:25:09 +02:00
|
|
|
|
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');
|
2022-12-23 03:22:01 +01:00
|
|
|
|
2021-05-31 12:40:34 +02:00
|
|
|
/* Set the locale*/
|
2021-10-13 05:44:10 +02:00
|
|
|
App::setLocale($this->client->locale());
|
2021-05-31 12:40:34 +02:00
|
|
|
|
2021-05-07 06:28:34 +02:00
|
|
|
/* Set customized translations _NOW_ */
|
2021-10-08 06:00:17 +02:00
|
|
|
$t->replace(Ninja::transformTranslations($this->client->getMergedSettings()));
|
2020-12-11 09:54:40 +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-10-08 06:00:17 +02:00
|
|
|
$path = $this->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-10-08 06:00:17 +02:00
|
|
|
$path = $this->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-10-08 06:00:17 +02:00
|
|
|
$path = $this->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-10-08 06:00:17 +02:00
|
|
|
$path = $this->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
|
|
|
|
2021-10-08 06:00:17 +02:00
|
|
|
$entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->client->getSetting($entity_design_id));
|
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 */
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $design) {
|
2021-06-23 22:55:33 +02:00
|
|
|
$design = Design::find(2);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-06-23 22:55:33 +02:00
|
|
|
|
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 = [
|
2022-06-21 11:57:17 +02: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([
|
2021-10-08 06:00:17 +02:00
|
|
|
'client' => $this->client,
|
2020-10-26 01:58:08 +01:00
|
|
|
'entity' => $this->entity,
|
2021-10-08 06:00:17 +02:00
|
|
|
'pdf_variables' => (array) $this->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' => [
|
2022-02-22 12:24:05 +01:00
|
|
|
'all_pages_header' => $this->entity->client->getSetting('all_pages_header'),
|
|
|
|
'all_pages_footer' => $this->entity->client->getSetting('all_pages_footer'),
|
2020-10-26 01:58:08 +01:00
|
|
|
],
|
2022-02-22 12:24:05 +01:00
|
|
|
'process_markdown' => $this->entity->client->company->markdown_enabled,
|
2020-10-26 01:58:08 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$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 {
|
2022-06-21 11:57:17 +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));
|
2022-05-25 14:00:17 +02:00
|
|
|
|
|
|
|
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($numbered_pdf) {
|
2022-05-25 14:00:17 +02:00
|
|
|
$pdf = $numbered_pdf;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-18 01:53:08 +01:00
|
|
|
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-05-25 14:00:17 +02:00
|
|
|
$numbered_pdf = $this->pageNumbering($pdf, $this->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($numbered_pdf) {
|
2022-05-25 14:00:17 +02:00
|
|
|
$pdf = $numbered_pdf;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-03-18 01:53:08 +01: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-05-15 04:19:36 +02:00
|
|
|
if ($pdf) {
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2022-10-23 23:42:38 +02:00
|
|
|
Storage::disk($this->disk)->put($file_path, $pdf);
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\Exception $e) {
|
2021-05-16 01:41:12 +02:00
|
|
|
throw new FilePermissionsFailure($e->getMessage());
|
2021-05-15 04:19:36 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-01 07:10:05 +01:00
|
|
|
|
|
|
|
$this->invitation = null;
|
|
|
|
$this->entity = null;
|
|
|
|
$this->company = null;
|
|
|
|
$this->client = null;
|
|
|
|
$this->contact = null;
|
2022-11-01 11:20:28 +01:00
|
|
|
$maker = null;
|
|
|
|
$state = null;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
2020-10-26 01:58:08 +01:00
|
|
|
}
|