Merge PDFs if embed documents is enabled

This commit is contained in:
David Bomba 2024-06-11 08:14:21 +10:00
parent 32744d3fa8
commit 2b8f96d208
6 changed files with 67 additions and 4 deletions

View File

@ -119,6 +119,10 @@ class CreditEmailEngine extends BaseEmailEngine
$pdf = ((new CreateRawPdf($this->invitation))->handle());
if($this->client->getSetting('embed_documents') && ($this->credit->documents()->where('is_public', true)->count() > 0 || $this->credit->company->documents()->where('is_public', true)->count() > 0)) {
$pdf = $this->credit->documentMerge($pdf);
}
$this->setAttachments([['file' => base64_encode($pdf), 'name' => $this->credit->numberFormatter().'.pdf']]);
}

View File

@ -131,6 +131,10 @@ class InvoiceEmailEngine extends BaseEmailEngine
if ($this->client->getSetting('pdf_email_attachment') !== false && $this->invoice->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
$pdf = ((new CreateRawPdf($this->invitation))->handle());
if($this->client->getSetting('embed_documents') && ($this->invoice->documents()->where('is_public', true)->count() > 0 || $this->invoice->company->documents()->where('is_public', true)->count() > 0)) {
$pdf = $this->invoice->documentMerge($pdf);
}
$this->setAttachments([['file' => base64_encode($pdf), 'name' => $this->invoice->numberFormatter().'.pdf']]);
}

View File

@ -123,6 +123,10 @@ class PurchaseOrderEmailEngine extends BaseEmailEngine
$pdf = (new CreateRawPdf($this->invitation))->handle();
if($this->vendor->getSetting('embed_documents') && ($this->purchase_order->documents()->where('is_public', true)->count() > 0 || $this->purchase_order->company->documents()->where('is_public', true)->count() > 0)) {
$pdf = $this->purchase_order->documentMerge($pdf);
}
$this->setAttachments([['file' => base64_encode($pdf), 'name' => $this->purchase_order->numberFormatter().'.pdf']]);
}

View File

@ -17,6 +17,7 @@ use App\Models\Account;
use App\Utils\HtmlEngine;
use Illuminate\Support\Str;
use App\Jobs\Entity\CreateRawPdf;
use App\Services\PdfMaker\PdfMerge;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Cache;
@ -117,6 +118,10 @@ class QuoteEmailEngine extends BaseEmailEngine
if ($this->client->getSetting('pdf_email_attachment') !== false && $this->quote->company->account->hasFeature(Account::FEATURE_PDF_ATTACHMENT)) {
$pdf = ((new CreateRawPdf($this->invitation))->handle());
if($this->client->getSetting('embed_documents') && ($this->quote->documents()->where('is_public', true)->count() > 0 || $this->quote->company->documents()->where('is_public', true)->count() > 0)){
$pdf = $this->quote->documentMerge($pdf);
}
$this->setAttachments([['file' => base64_encode($pdf), 'name' => $this->quote->numberFormatter().'.pdf']]);
}

View File

@ -11,16 +11,17 @@
namespace App\Models;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use App\Utils\Traits\MakesHash;
use App\Jobs\Entity\CreateRawPdf;
use App\Jobs\Util\WebhookHandler;
use App\Models\Traits\Excludable;
use App\Utils\Traits\MakesHash;
use App\Services\PdfMaker\PdfMerge;
use Illuminate\Database\Eloquent\Model;
use App\Utils\Traits\UserSessionAttributes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
/**
* Class BaseModel
@ -77,6 +78,8 @@ class BaseModel extends Model
use UserSessionAttributes;
use HasFactory;
use Excludable;
public int $max_attachment_size = 3000000;
protected $appends = [
'hashed_id',
@ -337,4 +340,42 @@ class BaseModel extends Model
return strtr($section, $variables['values']);
}
/**
* Merged PDFs associated with the entity / company
* into a single document
*
* @param string $pdf
* @return mixed
*/
public function documentMerge(string $pdf): mixed
{
$files = collect([$pdf]);
$entity_docs = $this->documents()
->where('is_public', true)
->get()
->filter(function ($document) {
return $document->size < $this->max_attachment_size && stripos($document->name, ".pdf") !== false;
})->map(function ($d) {
return $d->getFile();
});
$files->push($entity_docs);
$company_docs = $this->company->documents()
->where('is_public', true)
->get()
->filter(function ($document) {
return $document->size < $this->max_attachment_size && stripos($document->name, ".pdf") !== false;
})->map(function ($d) {
return $d->getFile();
});
$files->push($company_docs);
$pdf = (new PdfMerge($files->flatten()->toArray()))->run();
return $pdf;
}
}

View File

@ -309,6 +309,11 @@ class EmailDefaults
/** Purchase Order / Invoice / Credit / Quote PDF */
if ($this->email->email_object->settings->pdf_email_attachment) {
$pdf = ((new CreateRawPdf($this->email->email_object->invitation))->handle());
if($this->email->email_object->settings->embed_documents && ($this->email->email_object->entity->documents()->where('is_public', true)->count() > 0 || $this->email->email_object->entity->company->documents()->where('is_public', true)->count() > 0)) {
$pdf = $this->email->email_object->entity->documentMerge($pdf);
}
$this->email->email_object->attachments = array_merge($this->email->email_object->attachments, [['file' => base64_encode($pdf), 'name' => $this->email->email_object->entity->numberFormatter().'.pdf']]);
}