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

166 lines
5.2 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) 2020. Entity Ninja LLC (https://entityninja.com)
*
* @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()
{
2020-11-26 22:05:30 +01:00
if (config('ninja.phantomjs_pdf_generation')) {
2020-10-26 01:58:08 +01:00
return (new Phantom)->generate($this->invitation);
}
App::setLocale($this->contact->preferredLocale());
2020-11-10 05:04:53 +01:00
App::forgetInstance('translator');
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
2020-11-10 05:04:53 +01:00
Lang::replace(Ninja::transformTranslations($this->entity->client->getMergedSettings()));
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();
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
Storage::makeDirectory($path, 0775);
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) {
info(print_r($e->getMessage(), 1));
2020-11-22 12:14:49 +01:00
}
2020-11-25 15:19:52 +01:00
if ($pdf) {
2020-11-22 12:14:49 +01:00
$instance = 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;
}
}