1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 03:02:34 +01:00
invoiceninja/app/Jobs/Entity/CreateEntityPdf.php

175 lines
5.3 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
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Entity;
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\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;
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
*/
public function __construct($invitation)
{
$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;
$this->disk = $disk ?? config('filesystems.default');
}
public function handle()
{
App::setLocale($this->contact->preferredLocale());
2020-11-10 05:04:53 +01:00
App::forgetInstance('translator');
Lang::replace(Ninja::transformTranslations($this->entity->client->getMergedSettings()));
if (config('ninja.phantomjs_pdf_generation')) {
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) {
2020-10-26 01:58:08 +01:00
$path = $this->entity->client->invoice_filepath();
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) {
2020-10-26 01:58:08 +01:00
$path = $this->entity->client->quote_filepath();
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) {
2020-10-26 01:58:08 +01:00
$path = $this->entity->client->credit_filepath();
2020-10-26 10:13:00 +01:00
$entity_design_id = 'credit_design_id';
}
2020-10-26 01:58:08 +01:00
$file_path = $path.$this->entity->number.'.pdf';
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
$design = Design::find($entity_design_id);
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
}
$state = [
'template' => $template->elements([
'client' => $this->entity->client,
'entity' => $this->entity,
'pdf_variables' => (array) $this->entity->company->settings->pdf_variables,
'$product' => $design->design->product,
2020-10-26 01:58:08 +01:00
]),
'variables' => $html->generateLabelsAndValues(),
'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 {
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
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());
}
2020-11-25 15:19:52 +01:00
if ($pdf) {
2021-01-05 17:45:34 +01:00
Storage::disk($this->disk)->put($file_path, $pdf);
2020-11-25 15:19:52 +01:00
}
2020-10-26 01:58:08 +01:00
return $file_path;
}
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
}
// public function failed(\Exception $exception)
// {
// nlog("help!");
// }
2020-10-26 01:58:08 +01:00
}