Refactor Custom Invoice/Estimate PDF Templates (#277)

* Add utility class for managing templates

* Register custom pdf template views location

* Update the make:template command to make use of PdfTemplateUtils

* Update PDF invoice/estimate template controllers

* Register pdf_templates filesystem disk

* Remove unused leftovers

* Reformat with pint
This commit is contained in:
Darko Gjorgjijoski
2025-01-13 01:20:13 +01:00
committed by GitHub
parent 12d9d6c801
commit d862ee05e9
11 changed files with 221 additions and 53 deletions

View File

@@ -5,7 +5,7 @@ namespace App\Models;
use App;
use App\Mail\SendInvoiceMail;
use App\Services\SerialNumberFormatter;
use App\Space\ImageUtils;
use App\Space\PdfTemplateUtils;
use App\Traits\GeneratesPdfTrait;
use App\Traits\HasCustomFieldsTrait;
use Barryvdh\DomPDF\Facade\Pdf as PDF;
@@ -15,8 +15,6 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Nwidart\Modules\Facades\Module;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
@@ -582,11 +580,14 @@ class Invoice extends Model implements HasMedia
'taxes' => $taxes,
]);
$template = PdfTemplateUtils::findFormattedTemplate('invoice', $invoiceTemplate, '');
$templatePath = $template['custom'] ? sprintf('pdf_templates::invoice.%s', $invoiceTemplate) : sprintf('app.pdf.invoice.%s', $invoiceTemplate);
if (request()->has('preview')) {
return view('app.pdf.invoice.'.$invoiceTemplate);
return view($templatePath);
}
return PDF::loadView('app.pdf.invoice.'.$invoiceTemplate);
return PDF::loadView($templatePath);
}
public function getEmailAttachmentSetting()
@@ -657,20 +658,6 @@ class Invoice extends Model implements HasMedia
];
}
public static function invoiceTemplates()
{
$templates = Storage::disk('views')->files('/app/pdf/invoice');
$invoiceTemplates = [];
foreach ($templates as $key => $template) {
$templateName = Str::before(basename($template), '.blade.php');
$invoiceTemplates[$key]['name'] = $templateName;
$invoiceTemplates[$key]['path'] = ImageUtils::toBase64Src(resource_path('static/img/PDF/'.$templateName.'.png'));
}
return $invoiceTemplates;
}
public function addInvoicePayment($amount)
{
$this->due_amount += $amount;