1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Jobs/Entity/CreateRawPdf.php

118 lines
3.2 KiB
PHP
Raw Normal View History

2021-09-28 07:29:31 +02:00
<?php
/**
* Entity Ninja (https://entityninja.com).
*
* @link https://github.com/entityninja/entityninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Entity Ninja LLC (https://entityninja.com)
2021-09-28 07:29:31 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Entity;
2023-11-26 08:41:42 +01:00
use App\Exceptions\FilePermissionsFailure;
2021-09-28 07:29:31 +02:00
use App\Models\Credit;
2023-11-26 08:41:42 +01:00
use App\Models\CreditInvitation;
2021-09-28 07:29:31 +02:00
use App\Models\Invoice;
2023-11-26 08:41:42 +01:00
use App\Models\InvoiceInvitation;
use App\Models\PurchaseOrder;
2023-11-26 08:41:42 +01:00
use App\Models\PurchaseOrderInvitation;
use App\Models\Quote;
2021-09-28 07:29:31 +02:00
use App\Models\QuoteInvitation;
use App\Models\RecurringInvoice;
2023-11-26 08:41:42 +01:00
use App\Models\RecurringInvoiceInvitation;
use App\Services\Pdf\PdfService;
2023-11-26 08:41:42 +01:00
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesInvoiceHtml;
2023-11-26 08:41:42 +01:00
use App\Utils\Traits\NumberFormatter;
2022-05-25 14:00:17 +02:00
use App\Utils\Traits\Pdf\PageNumbering;
2023-11-26 08:41:42 +01:00
use App\Utils\Traits\Pdf\PdfMaker;
2021-09-28 07:29:31 +02:00
2023-10-26 04:57:44 +02:00
class CreateRawPdf
2021-09-28 07:29:31 +02:00
{
2024-01-14 05:05:00 +01:00
use NumberFormatter;
use MakesInvoiceHtml;
use PdfMaker;
use MakesHash;
use PageNumbering;
2021-09-28 07:29:31 +02:00
public Invoice | Credit | Quote | RecurringInvoice | PurchaseOrder $entity;
2021-09-28 07:29:31 +02:00
public $company;
public $contact;
public $invitation;
public $entity_string = '';
/**
* @param $invitation
*/
public function __construct($invitation, private ?string $type = null)
2021-09-28 07:29:31 +02:00
{
2021-09-28 07:29:31 +02:00
$this->invitation = $invitation;
if ($invitation instanceof InvoiceInvitation) {
$this->entity = $invitation->invoice;
$this->entity_string = 'invoice';
} elseif ($invitation instanceof QuoteInvitation) {
$this->entity = $invitation->quote;
$this->entity_string = 'quote';
} elseif ($invitation instanceof CreditInvitation) {
$this->entity = $invitation->credit;
$this->entity_string = 'credit';
} elseif ($invitation instanceof RecurringInvoiceInvitation) {
$this->entity = $invitation->recurring_invoice;
$this->entity_string = 'recurring_invoice';
2023-11-26 08:41:42 +01:00
} elseif ($invitation instanceof PurchaseOrderInvitation) {
$this->entity = $invitation->purchase_order;
$this->entity_string = 'purchase_order';
2021-09-28 07:29:31 +02:00
}
}
private function resolveType(): string
{
2023-11-26 08:41:42 +01:00
if($this->type) {
return $this->type;
2023-11-26 08:41:42 +01:00
}
$type = 'product';
match($this->entity_string) {
'purchase_order' => $type = 'purchase_order',
'invoice' => $type = 'product',
'quote' => $type = 'product',
'credit' => $type = 'product',
'recurring_invoice' => $type = 'product',
2024-04-03 07:03:52 +02:00
default => $type = 'product',
};
return $type;
}
2021-09-28 07:29:31 +02:00
public function handle()
{
2023-10-24 07:26:16 +02:00
/** Testing this override to improve PDF generation performance */
$ps = new PdfService($this->invitation, $this->resolveType(), [
2023-10-26 08:13:06 +02:00
'client' => $this->entity->client ?? false,
'vendor' => $this->entity->vendor ?? false,
2023-10-24 07:26:16 +02:00
"{$this->entity_string}s" => [$this->entity],
]);
$pdf = $ps->boot()->getPdf();
nlog("pdf timer = ". $ps->execution_time);
2023-11-15 04:57:07 +01:00
return $pdf;
2021-09-28 07:29:31 +02:00
throw new FilePermissionsFailure('Unable to generate the raw PDF');
2021-09-28 07:29:31 +02:00
}
2024-01-14 05:05:00 +01:00
2021-09-28 07:29:31 +02:00
public function failed($e)
{
}
}