Files
portal-api/app/Services/InvoicePdfService.php
T
baodev0 6bb4a47bfb Add stubs for various Laravel components and scaffolding
- Created stubs for JavaScript and SASS assets.
- Added stubs for channel, command, component, controller, event, factory, job, listener, mail, middleware, migration, model, notification, observer, policy, request, resource, route provider, seeder, and validation rule.
- Implemented scaffolding for configuration, service provider, and Vite integration.
- Included example views for module structure.
- Introduced a module loader for Vite to handle asset paths dynamically.
2026-05-29 09:28:08 +07:00

54 lines
2.1 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;
$data = [
'invoice' => $invoice,
'invoiceItems' => $invoice->invoiceItems,
'customer' => $invoice->customer,
'order' => $invoice->order,
'companyName' => $settingValue->company ?? 'Thế Giới Số',
'companyAddress' => $settingValue->address ?? '',
'companyPhone' => $settingValue->hotline ?? ($settingValue->tel ?? ''),
'companyEmail' => $settingValue->email ?? '',
'companyLogo' => $settingValue->logo ?? '',
'companyTaxCode' => $settingValue->tax ?? '',
'brandColor' => $settingValue->brand_color ?? '#1a56db',
'cfg' => [
'accent_color' => $settingValue->brand_color ?? '#1a56db',
'show_logo' => true,
'show_company_info' => true,
'show_customer_info' => true,
'show_order_info' => true,
'show_items_table' => true,
'show_summary' => true,
'show_note' => true,
'footer_text' => 'Cảm ơn quý khách đã sử dụng dịch vụ.',
'footer_note' => 'Đây là hóa đơn điện tử được tạo tự động, không cần chữ ký số.',
],
];
return Pdf::loadView('pdf.invoice', $data)
->setPaper('a4', 'portrait')
->output();
}
public function generateFileName(Invoice $invoice): string
{
return 'Hoa-don-' . $invoice->invoice_number . '.pdf';
}
}