invoiceninja/app/Jobs/Entity/CreateEntityPdf.php

98 lines
2.8 KiB
PHP
Raw Normal View History

2020-10-26 11:58:08 +11:00
<?php
/**
2022-06-06 08:49:41 +10:00
* Invoice Ninja (https://entityninja.com).
2020-10-26 11:58:08 +11:00
*
2022-06-06 08:49:41 +10:00
* @link https://github.com/invoiceninja/invoiceninja source repository
2020-10-26 11:58:08 +11:00
*
2022-06-06 08:49:41 +10:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2020-10-26 11:58:08 +11:00
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2020-10-26 11:58:08 +11:00
*/
namespace App\Jobs\Entity;
2021-04-12 14:36:51 +10:00
use App\Exceptions\FilePermissionsFailure;
2022-01-04 12:52:17 +11:00
use App\Libraries\MultiDB;
2020-10-26 15:06:58 +11:00
use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoiceInvitation;
2022-12-29 01:50:11 +11:00
use App\Services\Pdf\PdfService;
2020-10-26 11:58:08 +11: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\Storage;
class CreateEntityPdf implements ShouldQueue
{
2022-12-29 01:50:11 +11:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2020-10-26 11:58:08 +11:00
public $entity;
private $disk;
public $invitation;
/**
* Create a new job instance.
*
2020-10-28 21:10:49 +11:00
* @param $invitation
2020-10-26 11:58:08 +11:00
*/
public function __construct($invitation, $disk = null)
2020-10-26 11:58:08 +11:00
{
2022-12-29 01:50:11 +11:00
$this->invitation = $invitation;
2021-07-07 21:39:49 +10:00
$this->disk = $disk ?? config('filesystems.default');
2022-12-29 01:50:11 +11:00
2020-10-26 11:58:08 +11:00
}
public function handle()
{
2021-10-14 16:25:09 +11:00
2022-12-29 01:50:11 +11:00
$starttime = microtime(true);
2021-05-07 14:28:34 +10:00
2022-12-29 01:50:11 +11:00
MultiDB::setDb($this->invitation->company->db);
2022-12-29 01:50:11 +11:00
if ($this->invitation instanceof InvoiceInvitation) {
$this->entity = $this->invitation->invoice;
$path = $this->invitation->contact->client->invoice_filepath($this->invitation);
} elseif ($this->invitation instanceof QuoteInvitation) {
$this->entity = $this->invitation->quote;
$path = $this->invitation->contact->client->quote_filepath($this->invitation);
} elseif ($this->invitation instanceof CreditInvitation) {
$this->entity = $this->invitation->credit;
$path = $this->invitation->contact->client->credit_filepath($this->invitation);
} elseif ($this->invitation instanceof RecurringInvoiceInvitation) {
$this->entity = $this->invitation->recurring_invoice;
$path = $this->invitation->contact->client->recurring_invoice_filepath($this->invitation);
2020-10-26 11:58:08 +11:00
2020-10-26 20:13:00 +11:00
}
2020-10-26 11:58:08 +11:00
$file_path = $path.$this->entity->numberFormatter().'.pdf';
2022-12-29 01:50:11 +11:00
$pdf = (new PdfService($this->invitation))->getPdf();
$endtime = microtime(true);
nlog($endtime - $starttime);
2021-05-15 12:19:36 +10:00
if ($pdf) {
try {
2022-10-24 08:42:38 +11:00
Storage::disk($this->disk)->put($file_path, $pdf);
} catch (\Exception $e) {
2021-05-16 09:41:12 +10:00
throw new FilePermissionsFailure($e->getMessage());
2021-05-15 12:19:36 +10:00
}
}
2022-11-01 17:10:05 +11:00
2022-11-01 21:20:28 +11:00
2021-05-15 12:19:36 +10:00
return $file_path;
2020-10-26 11:58:08 +11:00
}
2020-12-13 21:33:30 +11:00
public function failed($e)
2020-12-13 21:33:30 +11:00
{
}
2020-10-26 11:58:08 +11:00
}