feat: Implement VM restart, auto-renew invoice functionality, and client invoice detail API, alongside admin UI improvements.

This commit is contained in:
pbao9
2026-03-02 10:26:01 +07:00
parent 2c8c3d14cd
commit 0c260a7e1c
9 changed files with 842 additions and 44 deletions
@@ -9,6 +9,7 @@ use App\Repositories\Api\Order\OrderRepository;
use App\Services\Api\Order\OrderService;
use App\Enums\Order\OrderStatus;
use App\Enums\Invoice\InvoiceStatus;
use App\Models\Invoice as InvoiceModel;
use App\Models\Order as OrderModel;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
@@ -279,6 +280,8 @@ class OrderController extends Controller
'amount' => (float) $order->amount,
],
'service' => [
'product_id' => $firstItem?->product?->id,
'product_uuid' => $firstItem?->product?->uuid,
'name' => $firstItem?->product?->name,
'billing_cycle' => $firstItem?->billing_cycle,
'server_name' => $firstItem?->server_name,
@@ -338,9 +341,11 @@ class OrderController extends Controller
'id' => $invoice->id,
'invoice_num' => $invoice->order_num,
'service_name' => $firstItem?->product?->name ?? ('Đơn hàng ' . $order?->order_num),
'product_type' => $firstItem?->product?->product_type,
'server_name' => $firstItem?->server_name,
'billing_cycle'=> $firstItem?->billing_cycle,
'amount' => (float) ($invoice->total ?? 0),
'auto_renew' => (bool) ($invoice->auto_renew ?? false),
'vm_id' => $invoice->vm_id,
'ip_address' => $vmware?->ipAddress?->ip,
'os_name' => $vmware?->os?->name,
@@ -365,6 +370,135 @@ class OrderController extends Controller
}
}
public function getClientInvoiceDetailByOrderNum(Request $request, string $orderNum): JsonResponse
{
try {
$customer = $request->user();
$order = OrderModel::query()
->with(['invoice', 'orderItems.product', 'customer', 'vmWare.ipAddress', 'vmWare.os'])
->where('customer_id', $customer->id)
->where('order_num', $orderNum)
->first();
if (!$order || !$order->invoice) {
return response()->json([
'success' => false,
'message' => 'Hóa đơn không tồn tại',
], 404);
}
$invoice = $order->invoice;
$firstItem = $order->orderItems->first();
$vmware = $order->vmWare->first();
return response()->json([
'success' => true,
'data' => [
'id' => $invoice->id,
'invoice_num' => $invoice->invoice_num,
'status' => $invoice->status,
'status_text' => InvoiceStatus::from((int) $invoice->status)->label(),
'sub_total' => (float) ($invoice->sub_total ?? 0),
'tax' => (float) ($invoice->tax ?? 0),
'total' => (float) ($invoice->total ?? 0),
'created_at' => $this->toIsoDate($invoice->created_at),
'due_date' => $this->toIsoDate($invoice->due_date),
'paid_at' => $this->toIsoDate($invoice->date_paid),
'payment_method' => $order->payment_method ?? null,
'order' => [
'id' => $order->id,
'order_num' => $order->order_num,
'amount' => (float) $order->amount,
],
'service' => [
'product_id' => $firstItem?->product?->id,
'product_uuid' => $firstItem?->product?->uuid,
'name' => $firstItem?->product?->name,
'billing_cycle' => $firstItem?->billing_cycle,
'server_name' => $firstItem?->server_name,
'product_type' => $firstItem?->product?->product_type,
'specs' => [
'cpu' => $firstItem?->product?->cpu,
'ram' => $firstItem?->product?->ram,
'disk' => $firstItem?->product?->disk,
],
],
'customer' => [
'id' => $order->customer?->id,
'name' => $order->customer?->fullname,
'email' => $order->customer?->email,
'address' => $order->customer?->address,
],
'items' => $order->orderItems->map(function ($item) {
return [
'id' => $item->id,
'product_name' => $item->product?->name,
'billing_cycle' => $item->billing_cycle,
'server_name' => $item->server_name,
'price' => (float) $item->price,
];
}),
'vmware' => $vmware ? [
'id' => $vmware->id,
'name' => $vmware->name,
'hostname' => $vmware->hostname,
'ip_address' => $vmware->ipAddress?->ip,
'os_name' => $vmware->os?->name,
'status' => $vmware->status,
'username' => $vmware->user,
'port' => $vmware->port,
'next_due_date' => $this->toIsoDate($vmware->next_due_date),
] : null,
],
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => $e->getMessage(),
], 400);
}
}
public function updateClientServiceAutoRenew(Request $request, int $id): JsonResponse
{
try {
$customer = $request->user();
$validated = $request->validate([
'auto_renew' => ['required', 'boolean'],
]);
$invoice = InvoiceModel::query()
->where('id', $id)
->where('customer_id', $customer->id)
->first();
if (!$invoice) {
return response()->json([
'success' => false,
'message' => 'Không tìm thấy dịch vụ tương ứng',
], 404);
}
$invoice->auto_renew = (bool) $validated['auto_renew'];
$invoice->save();
return response()->json([
'success' => true,
'message' => 'Cập nhật tự động gia hạn thành công',
'data' => [
'id' => $invoice->id,
'invoice_num' => $invoice->order_num,
'auto_renew' => (bool) $invoice->auto_renew,
],
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => $e->getMessage(),
], 400);
}
}
private function toIsoDate($value): ?string
{
if (empty($value)) {