2020-02-19 21:44:12 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-02-19 21:44:12 +01:00
|
|
|
*
|
|
|
|
* @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\Quote;
|
|
|
|
|
|
|
|
use App\Designs\Custom;
|
|
|
|
use App\Designs\Designer;
|
|
|
|
use App\Designs\Modern;
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Design;
|
|
|
|
use App\Models\Invoice;
|
2020-09-18 10:45:26 +02:00
|
|
|
use App\Services\PdfMaker\Design as PdfMakerDesign;
|
2020-08-12 19:21:29 +02:00
|
|
|
use App\Services\PdfMaker\PdfMaker as PdfMakerService;
|
2020-04-16 10:41:25 +02:00
|
|
|
use App\Utils\HtmlEngine;
|
2020-08-04 13:00:19 +02:00
|
|
|
use App\Utils\PhantomJS\Phantom;
|
2020-03-06 14:41:15 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2020-02-19 21:44:12 +01:00
|
|
|
use App\Utils\Traits\MakesInvoiceHtml;
|
|
|
|
use App\Utils\Traits\NumberFormatter;
|
2020-03-06 14:41:15 +01:00
|
|
|
use App\Utils\Traits\Pdf\PdfMaker;
|
2020-02-19 21:44:12 +01:00
|
|
|
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;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
class CreateQuotePdf implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, NumberFormatter, MakesInvoiceHtml, PdfMaker, MakesHash;
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public $quote;
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public $company;
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public $contact;
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
private $disk;
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
public $invitation;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-04-16 10:41:25 +02:00
|
|
|
public function __construct($invitation)
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
2020-04-16 10:41:25 +02:00
|
|
|
$this->invitation = $invitation;
|
|
|
|
|
|
|
|
$this->quote = $invitation->quote;
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
$this->company = $invitation->company;
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
$this->contact = $invitation->contact;
|
2020-02-19 21:44:12 +01:00
|
|
|
|
|
|
|
$this->disk = $disk ?? config('filesystems.default');
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function handle()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (config('ninja.phantomjs_key')) {
|
2020-08-04 13:00:19 +02:00
|
|
|
return (new Phantom)->generate($this->invitation);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-04 13:00:19 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->quote->load('client');
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
App::setLocale($this->contact->preferredLocale());
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$path = $this->quote->client->quote_filepath();
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-05-01 14:43:08 +02:00
|
|
|
$quote_design_id = $this->quote->design_id ? $this->quote->design_id : $this->decodePrimaryKey($this->quote->client->getSetting('quote_design_id'));
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$design = Design::find($quote_design_id);
|
2020-08-12 19:21:29 +02:00
|
|
|
$html = new HtmlEngine(null, $this->invitation, 'quote');
|
|
|
|
|
2020-09-18 10:45:26 +02:00
|
|
|
$template = new PdfMakerDesign(strtolower($design->name));
|
2020-08-12 19:21:29 +02:00
|
|
|
|
|
|
|
$state = [
|
2020-09-18 10:45:26 +02:00
|
|
|
'template' => $template->elements([
|
2020-08-12 19:21:29 +02:00
|
|
|
'client' => $this->quote->client,
|
|
|
|
'entity' => $this->quote,
|
2020-09-06 11:38:10 +02:00
|
|
|
'pdf_variables' => (array) $this->quote->company->settings->pdf_variables,
|
2020-08-12 19:21:29 +02:00
|
|
|
]),
|
|
|
|
'variables' => $html->generateLabelsAndValues(),
|
2020-08-27 08:47:51 +02:00
|
|
|
'options' => [
|
2020-08-27 10:06:08 +02:00
|
|
|
'all_pages_header' => $this->quote->client->getSetting('all_pages_header'),
|
|
|
|
'all_pages_footer' => $this->quote->client->getSetting('all_pages_footer'),
|
2020-08-27 08:47:51 +02:00
|
|
|
],
|
2020-08-12 19:21:29 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMakerService($state);
|
|
|
|
|
|
|
|
$maker
|
2020-09-18 10:45:26 +02:00
|
|
|
->design($template)
|
2020-08-12 19:21:29 +02:00
|
|
|
->build();
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
//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-04-11 13:19:05 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$pdf = $this->makePdf(null, null, $maker->getCompiledHTML());
|
|
|
|
|
|
|
|
$file_path = $path.$this->quote->number.'.pdf';
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$instance = Storage::disk($this->disk)->put($file_path, $pdf);
|
2020-02-19 21:44:12 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $file_path;
|
|
|
|
}
|
2020-02-19 21:44:12 +01:00
|
|
|
}
|