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

123 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2020-08-11 18:50:24 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Invoice;
2020-02-17 21:07:32 +01:00
use App\Designs\Custom;
use App\Designs\Designer;
use App\Designs\Modern;
use App\Libraries\MultiDB;
use App\Models\ClientContact;
use App\Models\Company;
2020-02-17 21:07:32 +01:00
use App\Models\Design;
use App\Models\Invoice;
2020-09-04 13:17:30 +02:00
use App\Services\PdfMaker\Design as PdfMakerDesign;
2020-08-11 18:50:24 +02:00
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
use App\Utils\HtmlEngine;
2020-08-04 13:00:19 +02:00
use App\Utils\PhantomJS\Phantom;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
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;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot;
class CreateInvoicePdf implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash;
public $invoice;
public $company;
public $contact;
private $disk;
public $invitation;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($invitation)
{
$this->invitation = $invitation;
$this->invoice = $invitation->invoice;
$this->company = $invitation->company;
$this->contact = $invitation->contact;
$this->disk = $disk ?? config('filesystems.default');
}
public function handle()
2020-04-17 12:13:19 +02:00
{
2020-08-11 18:50:24 +02:00
if (config('ninja.phantomjs_key')) {
2020-08-04 13:00:19 +02:00
return (new Phantom)->generate($this->invitation);
2020-08-11 18:50:24 +02:00
}
2020-08-04 13:00:19 +02:00
App::setLocale($this->contact->preferredLocale());
2020-02-17 21:07:32 +01:00
$path = $this->invoice->client->invoice_filepath();
$file_path = $path.$this->invoice->number.'.pdf';
$invoice_design_id = $this->invoice->design_id ? $this->invoice->design_id : $this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id'));
$design = Design::find($invoice_design_id);
2020-08-11 18:50:24 +02:00
$html = new HtmlEngine(null, $this->invitation, 'invoice');
2020-09-04 13:17:30 +02:00
$template = new PdfMakerDesign(strtolower($design->name));
2020-08-11 18:50:24 +02:00
$state = [
2020-09-04 13:17:30 +02:00
'template' => $template->elements([
2020-08-11 18:50:24 +02:00
'client' => $this->invoice->client,
'entity' => $this->invoice,
'pdf_variables' => (array) $this->invoice->company->settings->pdf_variables,
2020-08-11 18:50:24 +02:00
]),
'variables' => $html->generateLabelsAndValues(),
'options' => [
'all_pages_header' => $this->invoice->client->getSetting('all_pages_header'),
'all_pages_footer' => $this->invoice->client->getSetting('all_pages_footer'),
],
2020-08-11 18:50:24 +02:00
];
$maker = new PdfMakerService($state);
2020-08-11 18:50:24 +02:00
$maker
2020-09-04 13:17:30 +02:00
->design($template)
2020-08-11 18:50:24 +02:00
->build();
//todo - move this to the client creation stage so we don't keep hitting this unnecessarily
2020-08-12 05:13:39 +02:00
Storage::makeDirectory($path, 0775);
2020-09-07 05:14:43 +02:00
//info($maker->getCompiledHTML(true));
2020-09-04 13:17:30 +02:00
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML(true));
$instance = Storage::disk($this->disk)->put($file_path, $pdf);
return $file_path;
}
}