<?php
namespace Blackpig\FilamentOffice\Resources\InvoiceResource\Pages;
use Filament\Actions\Action;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Mail;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ViewRecord;
use Blackpig\FilamentOffice\Mail\MailInvoice;
use Blackpig\FilamentOffice\Settings\OfficeSettings;
use Blackpig\FilamentOffice\Resources\InvoiceResource;
class ViewInvoice extends ViewRecord
{
protected static string $resource = InvoiceResource::class;
protected static string $view = 'filament-office::resources.invoice.view';
protected function getActions(): array
{
$actions = [
Action::make('close')
->label('Close')
->url($this->previousUrl ?? static::getResource()::getUrl())
->color('secondary'),
Action::make('pdf')
->label('Download')
->action('downloadPdf')
->color('secondary')
->icon(''),
];
if ($this->record->customer->hasEmail()) {
$actions[] = Action::make('send')
->label('Send To Customer')
->action('sendToCustomer')
->color('secondary');
}
return $actions;
}
public function downloadPdf()
{
$locale = $this->record->customer->preferred_language;
App::setLocale($locale);
$data = $this->getPdfData();
$pdf = Pdf::loadView('filament-office::resources.invoice.pdf', $data);
return response()->streamDownload(fn () => print($pdf->output()), "{$this->record->formatted_number}.pdf");
}
public function sendToCustomer()
{
$invoice = $this->record;
$locale = $invoice->customer->preferred_language;
App::setLocale($locale);
$data = $this->getPdfData();
$pdf = PDF::loadView('filament-office::resources.invoice.pdf', $data)
->setWarnings(false);
$data['site_url'] = config('app.url');
Mail::to($invoice->customer->email)
->send(new MailInvoice($data, $pdf));
Notification::make()
->title('Innvoice sent to ' . $invoice->customer->email )
->success()
->send();
}
protected function getPdfData(): array
{
return $data = [
'invoice' => $this->record,
'settings' => app(OfficeSettings::class)
];
}
}