39 lines
1.2 KiB
PHP
39 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Invoice;
|
|
use App\Models\Setting;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
|
|
class InvoicePdfService
|
|
{
|
|
public function generate(Invoice $invoice): string
|
|
{
|
|
$invoice->load(['invoiceItems.product', 'customer', 'order.orderItems']);
|
|
|
|
$setting = Setting::where('key', 'setting')->first();
|
|
$settingValue = $setting ? json_decode((string) $setting->value) : null;
|
|
|
|
$customer = $invoice->customer;
|
|
$order = $invoice->order;
|
|
|
|
$data = [
|
|
'invoice' => $invoice,
|
|
'invoiceItems' => $invoice->invoiceItems,
|
|
'customer' => $customer,
|
|
'order' => $order,
|
|
'companyName' => $settingValue->company ?? 'Thế Giới Số',
|
|
'companyAddress' => $settingValue->address ?? '',
|
|
'companyPhone' => $settingValue->phone ?? '',
|
|
'companyEmail' => $settingValue->email ?? '',
|
|
];
|
|
|
|
return Pdf::loadView('pdf.invoice', $data)->output();
|
|
}
|
|
|
|
public function generateFileName(Invoice $invoice): string
|
|
{
|
|
return 'Hoa-don-' . $invoice->invoice_number . '.pdf';
|
|
}
|
|
} |