1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Jobs/Entity/CreateEntityPdf.php

225 lines
7.1 KiB
PHP
Raw Normal View History

2020-10-26 01:58:08 +01:00
<?php
/**
* Entity Ninja (https://entityninja.com).
*
* @link https://github.com/entityninja/entityninja source repository
*
* @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;
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;
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-07-07 13:39:49 +02:00
$this->disk = Ninja::isHosted() ? config('filesystems.default') : $disk;
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()));
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') {
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';
} elseif ($this->entity instanceof RecurringInvoice) {
2021-06-12 13:50:01 +02:00
$path = $this->entity->client->recurring_invoice_filepath($this->invitation);
$entity_design_id = 'invoice_design_id';
2020-10-26 10:13:00 +01:00
}
2020-10-26 01:58:08 +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-07-14 08:53:45 +02: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,
'$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'),
],
'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 {
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
}
if (config('ninja.log_pdf_html')) {
2021-01-05 17:45:34 +01:00
info($maker->getCompiledHTML());
}
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-05-16 01:41:12 +02:00
2021-07-07 10:43:34 +02:00
Storage::disk($this->disk)->put($file_path, $pdf);
2021-07-07 10:32:10 +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
public function failed($e)
2020-12-13 11:33:30 +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
}