- Implement AdminDomainOrderTest to cover admin functionalities for domain orders including listing, confirming registration, KYC updates, activation, rejection, and refunds. - Implement ClientDomainOrderTest to cover client functionalities for placing domain orders, including validation for balance, duplicate orders, and domain normalization. - Ensure proper handling of domain pricing and service statuses throughout the tests.
77 lines
2.7 KiB
PHP
77 lines
2.7 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' => $this->resolvePdfImagePath($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';
|
|
}
|
|
|
|
private function resolvePdfImagePath(?string $path): string
|
|
{
|
|
if (!$path) {
|
|
return '';
|
|
}
|
|
|
|
$urlPath = parse_url($path, PHP_URL_PATH) ?: $path;
|
|
|
|
if (str_starts_with($urlPath, '/storage/')) {
|
|
$localPath = storage_path('app/public/' . ltrim(substr($urlPath, strlen('/storage/')), '/'));
|
|
|
|
return file_exists($localPath) ? $localPath : $path;
|
|
}
|
|
|
|
if (str_starts_with($path, 'storage/')) {
|
|
$localPath = storage_path('app/public/' . substr($path, strlen('storage/')));
|
|
|
|
return file_exists($localPath) ? $localPath : $path;
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
}
|