orderRepository->getOrders($customerId, $filters->toArray()); } public function checkServerName(string $name): array { $exists = OrderItem::where('server_name', $name)->exists() || Service::where('server_name', $name)->exists() || ModuleVmware::where('name', $name)->exists() || ModuleVmware::where('hostname', $name)->exists() || VirtualMachines::where('name', $name)->exists(); return [ 'available' => ! $exists, 'message' => $exists ? 'Tên server này đã được sử dụng, vui lòng chọn tên khác.' : null, ]; } public function findCustomerOrder(int $customerId, int $id): ?Order { return Order::query() ->with(['invoice', 'orderItems.product', 'orderItems.service', 'customer']) ->where('customer_id', $customerId) ->where('id', $id) ->first(); } public function getCustomerInvoices(int $customerId) { return Invoice::query() ->with(['order.orderItems.product']) ->where('customer_id', $customerId) ->orderByDesc('created_at') ->get(); } public function findCustomerInvoice(int $customerId, int $id): ?Invoice { return Invoice::query() ->with([ 'order.orderItems.product', 'order.vmWare.ipAddress', 'order.vmWare.os', 'customer', 'attachments', ]) ->where('customer_id', $customerId) ->where('id', $id) ->first(); } public function findCustomerInvoiceByNumber(int $customerId, string $invoiceNumber): ?Invoice { return Invoice::query() ->with([ 'order.orderItems.product', 'order.vmWare.ipAddress', 'order.vmWare.os', 'customer', 'attachments', 'invoiceItems.product', 'services.product', ]) ->where('customer_id', $customerId) ->where('invoice_number', $invoiceNumber) ->first(); } public function findCustomerInvoiceByOrderNum(int $customerId, string $orderNumber): ?Order { return Order::query() ->with([ 'invoice.invoiceItems', 'invoice.attachments', 'invoice.customer', 'orderItems.product', 'customer', 'vmWare.ipAddress', 'vmWare.os', ]) ->where('customer_id', $customerId) ->where('order_num', $orderNumber) ->first(); } public function getCustomerServices(int $customerId) { return Service::query() ->with(['product', 'addons', 'invoice', 'orderItem', 'vmware.ipAddress', 'vmware.os']) ->where('customer_id', $customerId) ->orderByDesc('created_at') ->get(); } public function findCustomerService(int $customerId, string $uuid): ?Service { return Service::query() ->with(['product', 'addons', 'invoice', 'orderItem', 'vmware.ipAddress', 'vmware.os', 'vmware.vCenter']) ->where('uuid', $uuid) ->where('customer_id', $customerId) ->first(); } public function canRetryProvisioning(Service $service): bool { if ($service->status !== 'pending' || $service->retry_requested_at) { return false; } $paidAt = $service->invoice?->paid_at; return $paidAt ? Carbon::parse($paidAt)->diffInMinutes(now()) >= 180 : false; } public function retryProvisioning(Order $order, int $customerId): array { $order = Order::where('id', $order->id) ->with(['invoice', 'orderItems.service']) ->where('customer_id', $customerId) ->first(); if (! $order) { return ['ok' => false, 'message' => 'Đơn hàng không tồn tại.', 'code' => 404]; } if (! $order->invoice || (int) $order->invoice->status !== InvoiceStatus::PAID->value) { return ['ok' => false, 'message' => 'Đơn hàng chưa được thanh toán.', 'code' => 400]; } $paidAt = $order->invoice->paid_at ? Carbon::parse($order->invoice->paid_at) : null; if (! $paidAt || $paidAt->diffInMinutes(now()) < 180) { return ['ok' => false, 'message' => 'Vui lòng chờ khoảng 3 tiếng sau khi thanh toán mới có thể khởi tạo lại dịch vụ.', 'code' => 400]; } $service = $order->orderItems->first()?->service; if (! $service || (int) $service->customer_id !== $customerId) { return ['ok' => false, 'message' => 'Không tìm thấy dịch vụ hợp lệ để khởi tạo lại.', 'code' => 400]; } if ($service->status !== 'pending') { return ['ok' => false, 'message' => 'Dịch vụ đã được khởi tạo hoặc không ở trạng thái chờ.', 'code' => 400]; } if ($service->retry_requested_at) { return ['ok' => false, 'message' => 'Dịch vụ đã được yêu cầu khởi tạo lại một lần. Vui lòng liên hệ kỹ thuật qua mailto:kythuat@tgs.com.vn', 'code' => 400]; } $service->update(['retry_requested_at' => now()]); RetryProvisioningOrchestrator::dispatch($order->id, $customerId); return ['ok' => true, 'message' => 'Đã yêu cầu khởi tạo lại dịch vụ. Hệ thống sẽ kiểm tra và xử lý trong vài phút.']; } public function renewService(int $serviceId, int $customerId, int $months): array { $service = Service::query() ->with(['orderItem.product', 'invoice']) ->where('id', $serviceId) ->where('customer_id', $customerId) ->whereNotIn('status', ['terminated', 'cancelled']) ->first(); if (! $service) { return ['ok' => false, 'message' => 'Không tìm thấy dịch vụ hoặc dịch vụ đã bị hủy.', 'code' => 404]; } $orderItem = $service->orderItem; if (! $orderItem || (int) $orderItem->price <= 0) { return ['ok' => false, 'message' => 'Không thể xác định giá gia hạn dịch vụ.', 'code' => 400]; } $price = (int) $orderItem->price * $months; if ($service->invoice_id && Invoice::where('id', $service->invoice_id) ->where('invoice_type', 2) ->whereIn('status', [InvoiceStatus::UNPAID->value, InvoiceStatus::OVERDUE->value]) ->exists()) { $invoice = Invoice::find($service->invoice_id); return [ 'ok' => true, 'message' => 'Dịch vụ đã có hóa đơn gia hạn đang chờ thanh toán.', 'data' => [ 'order_id' => $invoice?->order_id, 'invoice_id' => $invoice?->id, 'invoice_number' => $invoice?->invoice_number, 'total' => (float) ($invoice?->total ?? 0), 'status' => $invoice?->status, 'payment_url' => url('/client/invoices/' . $invoice?->invoice_number . '/payment'), ], ]; } $invoice = DB::transaction(function () use ($service, $orderItem, $price, $months) { $order = Order::create([ 'order_num' => OrderHelper::generateOrderNumber(), 'customer_id' => $service->customer_id, 'sub_total' => $price, 'discount_amount' => 0, 'total' => $price, 'status' => OrderStatus::PENDING->value, 'client_ip' => request()?->ip(), 'note' => 'Đơn hàng gia hạn dịch vụ #' . $service->id, ]); OrderItem::create([ 'order_id' => $order->id, 'product_id' => $service->product_id, 'billing_cycle' => $orderItem->billing_cycle, 'months' => $months, 'os_id' => $orderItem->os_id, 'quantity' => 1, 'server_name' => $service->server_name, 'price' => $price, ]); $invoice = Invoice::create([ 'invoice_number' => OrderHelper::generateOrderNumber('INV'), 'customer_id' => $service->customer_id, 'order_id' => $order->id, 'invoice_type' => 2, 'sub_total' => $price, 'discount_amount' => 0, 'tax' => 0, 'total' => $price, 'status' => InvoiceStatus::UNPAID->value, 'due_date' => now()->addDays(7)->toDateString(), ]); \App\Models\InvoiceItem::create([ 'invoice_id' => $invoice->id, 'product_id' => $service->product_id, 'type' => 'renewal', 'description' => 'Gia hạn dịch vụ: ' . ($orderItem->product?->name ?? $service->server_name ?? ''), 'months' => $months, 'quantity' => 1, 'unit_price' => $price, 'total_price' => $price, ]); $service->update(['invoice_id' => $invoice->id]); return $invoice; }); return [ 'ok' => true, 'message' => 'Đã tạo hóa đơn gia hạn. Vui lòng thanh toán để gia hạn dịch vụ.', 'data' => [ 'order_id' => $invoice->order_id, 'invoice_id' => $invoice->id, 'invoice_number' => $invoice->invoice_number, 'total' => (float) $invoice->total, 'status' => $invoice->status, 'payment_url' => url('/client/invoices/' . $invoice->invoice_number . '/payment'), ], ]; } }