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;
|
2024-05-15 22:00:44 +02:00
|
|
|
use App\Jobs\EDocument\MergeEDocument;
|
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;
|
2023-10-26 08:19:28 +02:00
|
|
|
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;
|
2023-10-24 07:00:13 +02:00
|
|
|
use App\Services\Pdf\PdfService;
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2023-10-26 08:19:28 +02:00
|
|
|
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
|
|
|
|
2023-10-26 08:19:28 +02:00
|
|
|
public Invoice | Credit | Quote | RecurringInvoice | PurchaseOrder $entity;
|
2021-09-28 07:29:31 +02:00
|
|
|
|
2024-06-17 05:45:24 +02:00
|
|
|
public \App\Models\Company $company;
|
2021-09-28 07:29:31 +02:00
|
|
|
|
|
|
|
public $contact;
|
|
|
|
|
|
|
|
public $invitation;
|
|
|
|
|
|
|
|
public $entity_string = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $invitation
|
|
|
|
*/
|
2023-10-26 08:39:42 +02:00
|
|
|
public function __construct($invitation, private ?string $type = null)
|
2021-09-28 07:29:31 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-09-28 07:29:31 +02:00
|
|
|
$this->invitation = $invitation;
|
2024-06-17 05:45:24 +02:00
|
|
|
$this->company = $invitation->company;
|
2021-09-28 07:29:31 +02:00
|
|
|
|
|
|
|
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) {
|
2023-10-26 08:19:28 +02:00
|
|
|
$this->entity = $invitation->purchase_order;
|
|
|
|
$this->entity_string = 'purchase_order';
|
2021-09-28 07:29:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-26 08:39:42 +02:00
|
|
|
private function resolveType(): string
|
|
|
|
{
|
2023-11-26 08:41:42 +01:00
|
|
|
if($this->type) {
|
2023-10-26 08:39:42 +02:00
|
|
|
return $this->type;
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|
2023-10-26 08:39:42 +02: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',
|
2023-10-26 08:39:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return $type;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-20 21:25:30 +02:00
|
|
|
/**
|
|
|
|
* @throws FilePermissionsFailure
|
|
|
|
*/
|
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 */
|
2023-10-26 08:39:42 +02:00
|
|
|
$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],
|
|
|
|
]);
|
|
|
|
|
2024-05-20 21:25:30 +02:00
|
|
|
try {
|
|
|
|
$pdf = $ps->boot()->getPdf();
|
2024-06-18 05:37:54 +02:00
|
|
|
} catch (\Exception $e) {
|
2024-06-22 12:33:53 +02:00
|
|
|
nlog($e->getMessage());
|
2024-05-20 21:25:30 +02:00
|
|
|
throw new FilePermissionsFailure('Unable to generate the raw PDF');
|
|
|
|
}
|
2024-06-18 03:59:05 +02:00
|
|
|
|
2024-06-14 09:09:44 +02:00
|
|
|
if ($this->entity_string == "invoice" && $this->entity->client->getSetting("merge_e_invoice_to_pdf")) {
|
2024-05-16 07:54:03 +02:00
|
|
|
$pdf = (new MergeEDocument($this->entity, $pdf))->handle();
|
2024-08-22 08:45:06 +02:00
|
|
|
}
|
2024-06-17 05:45:24 +02:00
|
|
|
|
2024-06-18 03:59:05 +02:00
|
|
|
$merge_docs = isset($this->entity->client) ? $this->entity->client->getSetting('embed_documents') : $this->company->getSetting('embed_documents');
|
2024-06-17 05:45:24 +02:00
|
|
|
|
|
|
|
if($merge_docs && ($this->entity->documents()->where('is_public', true)->count() > 0 || $this->company->documents()->where('is_public', true)->count() > 0)) {
|
|
|
|
$pdf = $this->entity->documentMerge($pdf);
|
2024-05-15 22:00:44 +02:00
|
|
|
}
|
2024-06-17 05:45:24 +02:00
|
|
|
|
2023-11-15 04:57:07 +01:00
|
|
|
return $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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|