feat: replace Excel export with PDF export for transmittals
- Add new TransmittalPDFExportJob to generate PDFs using dompdf - Remove old Excel export implementation (TransmittalsExport) - Update ExportCompleteJob to use new PDF job instead of Excel - Add TestQueueJob for queue testing with new route - Update notification label from "Download File" to "Download PDF File" - Fix auth() helper usage by importing Auth facade consistently
This commit is contained in:
@@ -1,144 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use App\Models\Transmittal;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Maatwebsite\Excel\Concerns\Exportable;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||
use Maatwebsite\Excel\Concerns\WithDefaultStyles;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use PhpOffice\PhpSpreadsheet\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Style;
|
||||
|
||||
class TransmittalsExport implements FromCollection, ShouldAutoSize, ShouldQueue, WithDefaultStyles, WithHeadings, WithMapping
|
||||
{
|
||||
use Exportable, Queueable, SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
private readonly array $id
|
||||
) {}
|
||||
|
||||
public function view(): \Illuminate\Contracts\View\View
|
||||
{
|
||||
$transmittals = Transmittal::query()->with(['client', 'branch', 'files.notes', 'files.remarks'])->whereIn('id', Arr::flatten($this->id))->get();
|
||||
|
||||
return View::make('transmittal.export.transmittal-export-table')->with(['transmittals' => $transmittals]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function defaultStyles(Style $defaultStyle)
|
||||
{
|
||||
return $defaultStyle->applyFromArray([
|
||||
'alignment' => [
|
||||
'horizontal' => Alignment::HORIZONTAL_CENTER,
|
||||
'vertical' => Alignment::VERTICAL_CENTER,
|
||||
],
|
||||
|
||||
]);
|
||||
}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'series',
|
||||
'files',
|
||||
'notes',
|
||||
'remarks',
|
||||
];
|
||||
}
|
||||
|
||||
public function collection()
|
||||
{
|
||||
$transmittals = Transmittal::query()->with(['client', 'branch', 'files.notes', 'files.remarks'])
|
||||
->withCount(['files', 'notes', 'remarks'])->with(['files' => function ($files) {
|
||||
$files->withCount(['notes', 'remarks']);
|
||||
}])
|
||||
->whereIn('id', Arr::flatten($this->id))->get();
|
||||
|
||||
return $transmittals;
|
||||
}
|
||||
|
||||
public function map($transmittal): array
|
||||
{
|
||||
|
||||
$data = [];
|
||||
|
||||
$firstFile = $transmittal->files->first();
|
||||
|
||||
$data[] = [
|
||||
$transmittal->series,
|
||||
$firstFile?->description,
|
||||
$firstFile->notes->first()?->comment,
|
||||
$firstFile->remarks->first()?->remark,
|
||||
];
|
||||
|
||||
//iterate comments and remarks for first file
|
||||
$notes = $firstFile->notes->pluck('comment');
|
||||
$remarks = $firstFile->remarks->pluck('remark');
|
||||
|
||||
$fileNoteCount = count($notes);
|
||||
$fileRemarkCount = count($remarks);
|
||||
|
||||
$fileRowCount = $fileNoteCount;
|
||||
if ($fileRemarkCount > $fileNoteCount) {
|
||||
$fileRowCount = $fileRemarkCount;
|
||||
}
|
||||
|
||||
for ($i = 1; $i < $fileRowCount; $i++) {
|
||||
$data[] = [
|
||||
'',
|
||||
'',
|
||||
$notes[$i] ?? '',
|
||||
$remarks[$i] ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
//file iteration except for first file
|
||||
$fileRowCounter = 0;
|
||||
foreach ($transmittal->files as $file) {
|
||||
$notes = $file->notes->pluck('comment');
|
||||
$remarks = $file->remarks->pluck('remark');
|
||||
|
||||
$fileNoteCount = count($notes);
|
||||
$fileRemarkCount = count($remarks);
|
||||
|
||||
$fileRowCount = $fileNoteCount;
|
||||
if ($fileRemarkCount > $fileNoteCount) {
|
||||
$fileRowCount = $fileRemarkCount;
|
||||
}
|
||||
|
||||
if ($fileRowCounter != 0) {
|
||||
$data[] = [
|
||||
'',
|
||||
$file->description,
|
||||
$file->notes->first()?->comment ?? '',
|
||||
$file->remarks->first()?->remark ?? '',
|
||||
];
|
||||
|
||||
//iterate for remaining notes and remarks
|
||||
for ($i = 1; $i < $fileRowCount; $i++) {
|
||||
$data[] = [
|
||||
'',
|
||||
'',
|
||||
$notes[$i] ?? '',
|
||||
$remarks[$i] ?? '',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$fileRowCounter++;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use App\Commands\Transmittal\GenerateTransmittalSeries;
|
||||
use App\Commands\Transmittal\StoreTransmittalCommand;
|
||||
use App\Filament\Resources\TransmittalResource\Pages;
|
||||
use App\Jobs\ExportCompleteJob;
|
||||
use App\Jobs\TransmittalPDFExportJob;
|
||||
use App\Models\Branch;
|
||||
use App\Models\Client;
|
||||
use App\Models\Transmittal;
|
||||
@@ -112,7 +113,7 @@ class TransmittalResource extends Resource
|
||||
public static function getTableActions(): array
|
||||
{
|
||||
return [
|
||||
Tables\Actions\Action::make('Export')->action(fn ($record) => static::exportTransmittal([$record->id])),
|
||||
Tables\Actions\Action::make('Export')->label('Export as PDF')->action(fn ($record) => static::exportTransmittal([$record->id])),
|
||||
Tables\Actions\ViewAction::make(),
|
||||
Tables\Actions\Action::make('Update Status')
|
||||
->fillForm(function ($record) {
|
||||
@@ -139,7 +140,7 @@ class TransmittalResource extends Resource
|
||||
})
|
||||
->icon('heroicon-o-pencil-square')
|
||||
->slideOver()
|
||||
->hidden(! auth()->user()->can('update_transmittal')),
|
||||
->hidden(! Auth::user()->can('update_transmittal')),
|
||||
Tables\Actions\EditAction::make(),
|
||||
Tables\Actions\DeleteAction::make(),
|
||||
];
|
||||
@@ -151,7 +152,7 @@ class TransmittalResource extends Resource
|
||||
|
||||
static::generateExportNotification();
|
||||
|
||||
ExportCompleteJob::dispatch($recipient, Arr::flatten($id));
|
||||
dispatch(new TransmittalPDFExportJob($recipient, $id));
|
||||
}
|
||||
|
||||
public static function generateExportNotification(): Notification
|
||||
|
||||
@@ -42,7 +42,7 @@ class ExportCompleteJob implements ShouldQueue
|
||||
->title('Export Completed')
|
||||
->actions([
|
||||
NotificationAction::make('download_transmittal-export.pdf')
|
||||
->label('Download File')
|
||||
->label('Download PDF File')
|
||||
->url(Storage::url('transmittal-export.pdf'), true)
|
||||
->markAsRead(),
|
||||
])
|
||||
|
||||
28
app/Jobs/TestQueueJob.php
Normal file
28
app/Jobs/TestQueueJob.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
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\Log;
|
||||
|
||||
class TestQueueJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable;
|
||||
use InteractsWithQueue;
|
||||
use Queueable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
Log::info('TestQueueJob executed');
|
||||
}
|
||||
}
|
||||
|
||||
53
app/Jobs/TransmittalPDFExportJob.php
Normal file
53
app/Jobs/TransmittalPDFExportJob.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\Transmittal;
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use Filament\Forms\Components\Actions;
|
||||
use Filament\Notifications\Livewire\Notifications;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class TransmittalPDFExportJob implements ShouldQueue
|
||||
{
|
||||
use Queueable, Dispatchable, InteractsWithQueue, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(private $user, private array $ids)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$transmittals = Transmittal::query()
|
||||
->with(['client', 'branch', 'files.notes', 'files.remarks'])
|
||||
->whereIn('id', $this->ids)
|
||||
->get();
|
||||
|
||||
$pdf = Pdf::loadView('transmittal.export.transmittal-export-pdf', [
|
||||
'transmittals' => $transmittals,
|
||||
]);
|
||||
|
||||
Storage::disk('public')->put('transmittal-export.pdf', $pdf->output());
|
||||
|
||||
Notifications::make()
|
||||
->success()
|
||||
->title('Export Completed')
|
||||
->actions([
|
||||
Actions\Action::make('download_transmittal-export.pdf')
|
||||
->label('Download PDF File')
|
||||
->url(Storage::url('transmittal-export.pdf'), true)
|
||||
->markAsRead(),
|
||||
])
|
||||
->sendToDatabase($this->user);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
|
||||
use Illuminate\Routing\Middleware\SubstituteBindings;
|
||||
use Illuminate\Session\Middleware\AuthenticateSession;
|
||||
use Illuminate\Session\Middleware\StartSession;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
||||
|
||||
class AdminPanelProvider extends PanelProvider
|
||||
@@ -45,7 +46,7 @@ class AdminPanelProvider extends PanelProvider
|
||||
->url(fn (): string => url(config('horizon.path')))
|
||||
->icon('heroicon-o-queue-list')
|
||||
->group('System')
|
||||
->visible(fn (): bool => auth()->user()?->hasRole('super_admin') ?? false),
|
||||
->visible(fn (): bool => Auth::user()?->hasRole('super_admin') ?? false),
|
||||
])
|
||||
->databaseNotifications()
|
||||
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
|
||||
|
||||
Reference in New Issue
Block a user