161 lines
6.0 KiB
PHP
161 lines
6.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs\VirtualMachine\Provision;
|
|
|
|
use App\Jobs\Mail\Admin\AdminOrderNotificationJob;
|
|
use App\Jobs\Mail\SendInformationVirtualMachine;
|
|
use App\Models\Invoice;
|
|
use App\Models\ModuleVmware;
|
|
use App\Models\Setting;
|
|
use App\Services\InvoicePdfService;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
/**
|
|
* SendVmCredentialsEmailJob
|
|
*
|
|
* Sends the final VM credential email to the customer with an attached PDF invoice.
|
|
* Always loads fresh data from DB so it gets the real IP and
|
|
* the final password saved by ChangePasswordJob.
|
|
*
|
|
* This is step [11] in the provisioning flow.
|
|
*/
|
|
class SendVmCredentialsEmailJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 3;
|
|
public int $timeout = 60;
|
|
|
|
public function __construct(
|
|
public readonly int $moduleVmwareId,
|
|
) {}
|
|
|
|
public function handle(): void
|
|
{
|
|
$vm = ModuleVmware::with(['customer', 'os.osGroup', 'order.orderItems'])->find($this->moduleVmwareId);
|
|
|
|
if (!$vm) {
|
|
Log::error('SendVmCredentialsEmailJob: ModuleVmware not found', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$customer = $vm->customer;
|
|
|
|
if (!$customer?->email) {
|
|
Log::error('SendVmCredentialsEmailJob: customer or email missing', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$setting = Setting::where('key', 'setting')->first();
|
|
$settingValue = $setting ? json_decode((string) $setting->value) : null;
|
|
|
|
// Resolve billing info from order items
|
|
$orderItem = $vm->order?->orderItems?->first();
|
|
$months = (int) ($orderItem?->months ?? 0);
|
|
|
|
$emailData = [
|
|
'company_name' => $settingValue->company ?? 'Thế Giới Số',
|
|
'company_logo' => $settingValue->logo ?? '',
|
|
'client_name' => trim($customer->firstname . ' ' . $customer->lastname),
|
|
'product_name' => $orderItem?->getProductAttribute()?->name ?? 'Cloud VPS',
|
|
'product_billing' => $this->billingLabel($months),
|
|
'product_price' => $orderItem ? number_format((float) $orderItem->price, 0, ',', '.') : 'N/A',
|
|
'service_host' => $vm->ip ?? 'N/A',
|
|
'service_username' => $vm->user,
|
|
'service_port' => $vm->port,
|
|
'service_password' => $vm->plain_password ?? '(xem tài khoản quản lý)',
|
|
'os_name' => $vm->os
|
|
? (($vm->os->osGroup?->name ?? '') . ' ' . $vm->os->version)
|
|
: 'Unknown OS',
|
|
'service_due_exp' => $vm->next_due_date
|
|
? date('d/m/Y', (int) $vm->next_due_date)
|
|
: 'N/A',
|
|
];
|
|
|
|
// Generate invoice PDF attachment (synchronous, no queue)
|
|
$invoicePdf = null;
|
|
$invoicePdfFileName = null;
|
|
|
|
$invoice = Invoice::where('order_id', $vm->order_id)
|
|
->with(['invoiceItems.product', 'customer', 'order.orderItems'])
|
|
->first();
|
|
|
|
if ($invoice) {
|
|
try {
|
|
$pdfService = app(InvoicePdfService::class);
|
|
$invoicePdf = $pdfService->generate($invoice);
|
|
$invoicePdfFileName = $pdfService->generateFileName($invoice);
|
|
} catch (\Throwable $e) {
|
|
Log::error('SendVmCredentialsEmailJob: failed to generate invoice PDF', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
'invoice_id' => $invoice->id,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
SendInformationVirtualMachine::dispatch($emailData, $customer, $invoicePdf, $invoicePdfFileName);
|
|
|
|
Log::info('SendVmCredentialsEmailJob: email dispatched', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
'customer_email' => $customer->email,
|
|
'ip' => $vm->ip,
|
|
'invoice_attached' => $invoicePdf !== null,
|
|
]);
|
|
|
|
// Notify admin with VM credentials after successful provisioning
|
|
$order = $vm->order;
|
|
AdminOrderNotificationJob::dispatch([
|
|
'customer_name' => trim($customer->firstname . ' ' . $customer->lastname),
|
|
'customer_email' => $customer->email,
|
|
'client_ip' => null,
|
|
'order_num' => $order?->order_num ?? $order?->id ?? 'N/A',
|
|
'product_name' => $orderItem?->getProductAttribute()?->name ?? 'Cloud VPS',
|
|
'product_type' => 'Cloud VPS',
|
|
'billing_cycle' => $this->billingLabel($months),
|
|
'total' => $orderItem?->price ?? 0,
|
|
'created_at' => $order?->created_at?->format('d/m/Y H:i:s') ?? now()->format('d/m/Y H:i:s'),
|
|
'ip' => $vm->ip ?? '',
|
|
'username' => $vm->user ?? '',
|
|
'password' => $vm->plain_password ?? '',
|
|
]);
|
|
|
|
Log::info('SendVmCredentialsEmailJob: admin notification dispatched', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
]);
|
|
}
|
|
|
|
public function failed(\Throwable $e): void
|
|
{
|
|
Log::error('SendVmCredentialsEmailJob: failed', [
|
|
'module_vmware_id' => $this->moduleVmwareId,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
private function billingLabel(?int $months): string
|
|
{
|
|
return match ($months) {
|
|
1 => 'Hàng tháng',
|
|
3 => '3 Tháng',
|
|
6 => '6 Tháng',
|
|
12 => 'Hàng năm',
|
|
24 => '2 Năm',
|
|
default => ($months ?? 0) . ' Tháng',
|
|
};
|
|
}
|
|
}
|