129 lines
5.0 KiB
PHP
129 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Invoice;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Jobs\Mail\SendInformationVirtualMachine;
|
|
use App\Jobs\VirtualMachine\Provision\SendVmCredentialsEmailJob;
|
|
use App\Models\Invoice;
|
|
use App\Models\ModuleVmware;
|
|
use App\Models\Setting;
|
|
use App\Services\InvoicePdfService;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class InvoiceTestController extends Controller
|
|
{
|
|
public function sendInvoiceEmail(Request $request, string $invoiceNumber): JsonResponse
|
|
{
|
|
$invoice = Invoice::with(['invoiceItems.product', 'customer', 'order.orderItems'])
|
|
->where('invoice_number', $invoiceNumber)
|
|
->first();
|
|
|
|
if (!$invoice) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Không tìm thấy hóa đơn.',
|
|
], 404);
|
|
}
|
|
|
|
$customer = $invoice->customer;
|
|
if (!$customer?->email) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Khách hàng không có email.',
|
|
], 400);
|
|
}
|
|
|
|
try {
|
|
$pdfService = app(InvoicePdfService::class);
|
|
$invoicePdf = $pdfService->generate($invoice);
|
|
$invoicePdfFileName = $pdfService->generateFileName($invoice);
|
|
} catch (\Throwable $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Lỗi tạo PDF: ' . $e->getMessage(),
|
|
], 500);
|
|
}
|
|
|
|
$vm = ModuleVmware::with(['customer', 'os.osGroup', 'order.orderItems'])
|
|
->where('order_id', $invoice->order_id)
|
|
->first();
|
|
|
|
if ($vm) {
|
|
SendVmCredentialsEmailJob::dispatch($vm->id);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Đã dispatch SendVmCredentialsEmailJob (có PDF hóa đơn đính kèm).',
|
|
'data' => [
|
|
'invoice_id' => $invoice->id,
|
|
'invoice_number' => $invoice->invoice_number,
|
|
'customer_email' => $customer->email,
|
|
'vm_id' => $vm->id,
|
|
'pdf_file_name' => $invoicePdfFileName,
|
|
],
|
|
]);
|
|
}
|
|
|
|
$setting = Setting::where('key', 'setting')->first();
|
|
$settingValue = $setting ? json_decode((string) $setting->value) : null;
|
|
|
|
$emailData = [
|
|
'company_name' => $settingValue->company ?? 'Thế Giới Số',
|
|
'company_logo' => $settingValue->logo ?? '',
|
|
'client_name' => trim($customer->firstname . ' ' . $customer->lastname),
|
|
'product_name' => $invoice->invoiceItems->first()?->product?->name ?? 'Dịch vụ',
|
|
'product_billing' => 'N/A',
|
|
'product_price' => number_format((float) $invoice->total, 0, ',', '.'),
|
|
'service_host' => 'N/A',
|
|
'service_username' => 'N/A',
|
|
'service_port' => 'N/A',
|
|
'service_password' => 'N/A',
|
|
'os_name' => 'N/A',
|
|
'service_due_exp' => $invoice->due_date ? $invoice->due_date->format('d/m/Y') : 'N/A',
|
|
];
|
|
|
|
SendInformationVirtualMachine::dispatch($emailData, $customer, $invoicePdf, $invoicePdfFileName);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Đã gửi email hóa đơn (không có VM liên kết, sử dụng thông tin hóa đơn).',
|
|
'data' => [
|
|
'invoice_id' => $invoice->id,
|
|
'invoice_number' => $invoice->invoice_number,
|
|
'customer_email' => $customer->email,
|
|
'pdf_file_name' => $invoicePdfFileName,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function previewInvoicePdf(Request $request, string $invoiceNumber)
|
|
{
|
|
$invoice = Invoice::with(['invoiceItems.product', 'customer', 'order.orderItems'])
|
|
->where('invoice_number', $invoiceNumber)
|
|
->first();
|
|
|
|
if (!$invoice) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Không tìm thấy hóa đơn.',
|
|
], 404);
|
|
}
|
|
|
|
$setting = Setting::where('key', 'setting')->first();
|
|
$settingValue = $setting ? json_decode((string) $setting->value) : null;
|
|
|
|
return Pdf::loadView('pdf.invoice', [
|
|
'invoice' => $invoice,
|
|
'invoiceItems' => $invoice->invoiceItems,
|
|
'customer' => $invoice->customer,
|
|
'order' => $invoice->order,
|
|
'companyName' => $settingValue->company ?? 'Thế Giới Số',
|
|
'companyAddress' => $settingValue->address ?? '',
|
|
'companyPhone' => $settingValue->phone ?? '',
|
|
'companyEmail' => $settingValue->email ?? '',
|
|
])->stream($invoice->invoice_number . '.pdf');
|
|
}
|
|
} |