update logic
This commit is contained in:
@@ -575,6 +575,119 @@ class CustomerController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a service for a customer. For VPS products, accepts a
|
||||
* `virtual_machine_id` from the vCenter inventory and auto-creates the
|
||||
* matching module_vmware row so `services.vm_id` can be linked.
|
||||
*/
|
||||
public function createService(string $uuid, Request $request): JsonResponse
|
||||
{
|
||||
$customer = $this->customerRepository->findBy('uuid', $uuid);
|
||||
if (!$customer) {
|
||||
return response()->json(['error' => true, 'message' => "Can't find customer"], 404);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'product_id' => 'required|integer|exists:products,id',
|
||||
'virtual_machine_id' => 'nullable|integer|exists:virtual_machines,id',
|
||||
'server_name' => 'nullable|string|max:255',
|
||||
'status' => 'nullable|in:pending,active,suspended,expired,cancelled,terminated',
|
||||
'auto_renew' => 'nullable|boolean',
|
||||
'started_at' => 'nullable|date',
|
||||
'expires_at' => 'nullable|date',
|
||||
'note' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$product = \App\Models\Product::find($data['product_id']);
|
||||
if ($product->product_type === 'vps' && empty($data['virtual_machine_id'])) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'VPS service requires a virtual_machine_id',
|
||||
], 422);
|
||||
}
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$vmId = null;
|
||||
if (!empty($data['virtual_machine_id'])) {
|
||||
$vm = \App\Models\VirtualMachines::find($data['virtual_machine_id']);
|
||||
$moduleVm = \App\Models\ModuleVmware::create([
|
||||
'customer_id' => $customer->id,
|
||||
'name' => $vm->name,
|
||||
'vmware_id' => $vm->vm_id,
|
||||
'virtual_machine_id' => $vm->id,
|
||||
'hostname' => $data['server_name'] ?? $vm->name,
|
||||
'status' => 1,
|
||||
]);
|
||||
$vmId = $moduleVm->id;
|
||||
}
|
||||
|
||||
$service = \App\Models\Service::create([
|
||||
'customer_id' => $customer->id,
|
||||
'product_id' => $data['product_id'],
|
||||
'vm_id' => $vmId,
|
||||
'server_name' => $data['server_name'] ?? null,
|
||||
'status' => $data['status'] ?? 'active',
|
||||
'auto_renew' => (bool) ($data['auto_renew'] ?? false),
|
||||
'started_at' => $data['started_at'] ?? now(),
|
||||
'expires_at' => $data['expires_at'] ?? null,
|
||||
'note' => $data['note'] ?? null,
|
||||
]);
|
||||
|
||||
DB::commit();
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Service created',
|
||||
'data' => $service->load(['product']),
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List virtual machines from vCenter inventory for the VPS picker.
|
||||
* Annotates each VM with `assigned_service_id` if already linked to a service.
|
||||
*/
|
||||
public function listAvailableVms(Request $request): JsonResponse
|
||||
{
|
||||
$search = trim((string) $request->get('q', ''));
|
||||
|
||||
$assignments = DB::table('module_vmware as mv')
|
||||
->join('services as s', 's.vm_id', '=', 'mv.id')
|
||||
->whereNotNull('mv.virtual_machine_id')
|
||||
->select(
|
||||
'mv.virtual_machine_id',
|
||||
's.id as service_id',
|
||||
's.customer_id as service_customer_id',
|
||||
)
|
||||
->get()
|
||||
->keyBy('virtual_machine_id');
|
||||
|
||||
$vms = DB::table('virtual_machines')
|
||||
->select('id', 'vm_id', 'name', 'power_state', 'cpu', 'ram_mb', 'ip')
|
||||
->where('is_template', 0)
|
||||
->where('is_system', 0)
|
||||
->when($search, fn ($q) => $q->where(function ($w) use ($search) {
|
||||
$w->where('name', 'like', "%{$search}%")
|
||||
->orWhere('vm_id', 'like', "%{$search}%")
|
||||
->orWhere('ip', 'like', "%{$search}%");
|
||||
}))
|
||||
->orderBy('name')
|
||||
->limit(200)
|
||||
->get()
|
||||
->map(function ($vm) use ($assignments) {
|
||||
$a = $assignments->get($vm->id);
|
||||
$vm->assigned_service_id = $a->service_id ?? null;
|
||||
$vm->assigned_customer_id = $a->service_customer_id ?? null;
|
||||
return $vm;
|
||||
});
|
||||
|
||||
return response()->json(['error' => false, 'data' => $vms]);
|
||||
}
|
||||
|
||||
public function getOrders(string $uuid, Request $request): JsonResponse
|
||||
{
|
||||
$customer = $this->customerRepository->findBy('uuid', $uuid);
|
||||
|
||||
@@ -189,6 +189,7 @@ class VCenterController extends Controller
|
||||
'db_id' => $vm->id,
|
||||
'uuid' => $vm->uuid,
|
||||
'vm_id' => $vm->vm_id,
|
||||
'ip' => $vm->ip,
|
||||
'power_state' => $vm->power_state,
|
||||
'folder' => $vm->folder,
|
||||
'cpu' => $vm->cpu,
|
||||
@@ -263,6 +264,7 @@ class VCenterController extends Controller
|
||||
'db_id' => $vm->id,
|
||||
'uuid' => $vm->uuid,
|
||||
'vm_id' => $vm->vm_id,
|
||||
'ip' => $vm->ip,
|
||||
'power_state' => $vm->power_state,
|
||||
'folder' => $vm->folder,
|
||||
'cpu' => $vm->cpu,
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Service;
|
||||
|
||||
use App\DTOs\Order\ClientOrderFilters;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\Order\CheckServerNameRequest;
|
||||
use App\Http\Requests\Api\Order\PayOrderRequest;
|
||||
use App\Http\Requests\Api\Order\RenewServiceRequest;
|
||||
use App\Http\Requests\Api\Order\StoreOrderRequest;
|
||||
use App\Http\Resources\Api\Service\Order\InvoiceDetailResource;
|
||||
use App\Http\Resources\Api\Service\Order\InvoiceSummaryResource;
|
||||
use App\Http\Resources\Api\Service\Order\OrderDetailResource;
|
||||
use App\Http\Resources\Api\Service\Order\OrderItemResource;
|
||||
use App\Http\Resources\Api\Service\Order\OrderSummaryResource;
|
||||
use App\Http\Resources\Api\Service\Order\ServiceDetailResource;
|
||||
use App\Http\Resources\Api\Service\Order\ServiceSummaryResource;
|
||||
use App\Repositories\Api\Order\OrderRepository;
|
||||
use App\Services\ActivityLogger;
|
||||
use App\Services\Api\Order\OrderQueryService;
|
||||
use App\Services\Api\Order\OrderService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OrderControllerRefactor extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly OrderService $orderService,
|
||||
private readonly OrderRepository $orderRepository,
|
||||
private readonly OrderQueryService $orderQueryService,
|
||||
private readonly ActivityLogger $activityLogger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getList(Request $request): JsonResponse
|
||||
{
|
||||
$orders = $this->orderQueryService->getOrdersForCustomer(
|
||||
(int) $request->user()->id,
|
||||
ClientOrderFilters::fromArray($request->only(['total_price', 'pending']))
|
||||
);
|
||||
|
||||
return response()->json(['success' => true, 'data' => $orders]);
|
||||
}
|
||||
|
||||
public function checkServerName(CheckServerNameRequest $request): JsonResponse
|
||||
{
|
||||
$result = $this->orderQueryService->checkServerName($request->string('name')->toString());
|
||||
|
||||
return response()->json($result['available'] ? ['available' => true] : $result);
|
||||
}
|
||||
|
||||
public function getClientOrders(Request $request): JsonResponse
|
||||
{
|
||||
$orders = $this->orderRepository->getOrders((int) $request->user()->id, []);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => OrderSummaryResource::collection($orders->getCollection()->load('invoice')),
|
||||
]);
|
||||
}
|
||||
|
||||
public function getClientOrderDetail(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$order = $this->orderQueryService->findCustomerOrder((int) $request->user()->id, $id);
|
||||
|
||||
if (! $order) {
|
||||
return response()->json(['success' => false, 'message' => 'Đơn hàng không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true, 'data' => new OrderDetailResource($order)]);
|
||||
}
|
||||
|
||||
public function storeOrder(StoreOrderRequest $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$customer = $request->user();
|
||||
$data = $request->validated();
|
||||
|
||||
$product = \App\Models\Product::where('uuid', $data['product_id'])->first();
|
||||
if (! $product) {
|
||||
return response()->json(['success' => false, 'message' => 'Sản phẩm không tìm thấy hoặc đã bị xóa.'], 404);
|
||||
}
|
||||
|
||||
$data['product_id'] = $product->id;
|
||||
$result = $this->orderService->create($data, $customer);
|
||||
|
||||
$order = $result['order']->load(['orderItems.product', 'invoice', 'customer']);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Tạo đơn hàng thành công.',
|
||||
'notice' => 'Dịch vụ VPS sẽ được khởi tạo trong 5-10 phút sau khi thanh toán.',
|
||||
'data' => [
|
||||
'order' => new OrderDetailResource($order),
|
||||
'invoice' => new InvoiceDetailResource($result['invoice']->load(['customer', 'order'])),
|
||||
'items' => OrderItemResource::collection($order->orderItems),
|
||||
],
|
||||
], 201);
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json(['success' => false, 'message' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function payOrder(int $id, PayOrderRequest $request): JsonResponse
|
||||
{
|
||||
$order = $this->orderRepository->find($id);
|
||||
|
||||
if (! $order) {
|
||||
return response()->json(['success' => false, 'message' => 'Đơn hàng không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
if ($order->customer_id !== $request->user()->id) {
|
||||
return response()->json(['success' => false, 'message' => 'Không có quyền truy cập.'], 403);
|
||||
}
|
||||
|
||||
$this->orderService->pay($order, $request->payment_method);
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'payment',
|
||||
logName: 'Order',
|
||||
description: "Customer {$request->user()->email} thanh toán đơn hàng #{$order->order_num} qua {$request->payment_method}",
|
||||
subject: $order,
|
||||
newValues: ['payment_method' => $request->payment_method, 'total' => $order->total],
|
||||
);
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Thanh toán thành công.']);
|
||||
}
|
||||
|
||||
public function retryOrderProvisioning(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$order = $this->orderRepository->find($id);
|
||||
|
||||
if (! $order) {
|
||||
return response()->json(['success' => false, 'message' => 'Đơn hàng không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
$result = $this->orderQueryService->retryProvisioning($order, (int) $request->user()->id);
|
||||
|
||||
if (! $result['ok']) {
|
||||
return response()->json(['success' => false, 'message' => $result['message']], $result['code']);
|
||||
}
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'retry_provisioning',
|
||||
logName: 'Order',
|
||||
description: "Customer {$request->user()->email} yêu cầu khởi tạo lại dịch vụ cho đơn hàng #{$order->order_num}",
|
||||
subject: $order,
|
||||
);
|
||||
|
||||
return response()->json(['success' => true, 'message' => $result['message']]);
|
||||
}
|
||||
|
||||
public function getClientInvoices(Request $request): JsonResponse
|
||||
{
|
||||
$invoices = $this->orderQueryService->getCustomerInvoices((int) $request->user()->id);
|
||||
|
||||
return response()->json(['success' => true, 'data' => InvoiceSummaryResource::collection($invoices)]);
|
||||
}
|
||||
|
||||
public function getClientInvoiceDetail(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$invoice = $this->orderQueryService->findCustomerInvoice((int) $request->user()->id, $id);
|
||||
|
||||
if (! $invoice) {
|
||||
return response()->json(['success' => false, 'message' => 'Hóa đơn không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true, 'data' => new InvoiceDetailResource($invoice)]);
|
||||
}
|
||||
|
||||
public function getClientInvoiceDetailByOrderNum(Request $request, string $orderNumber): JsonResponse
|
||||
{
|
||||
$order = $this->orderQueryService->findCustomerInvoiceByOrderNum((int) $request->user()->id, $orderNumber);
|
||||
|
||||
if (! $order?->invoice) {
|
||||
return response()->json(['success' => false, 'message' => 'Hóa đơn không tồn tại.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true, 'data' => new InvoiceDetailResource($order->invoice->setRelation('order', $order))]);
|
||||
}
|
||||
|
||||
public function getClientServices(Request $request): JsonResponse
|
||||
{
|
||||
$services = $this->orderQueryService->getCustomerServices((int) $request->user()->id);
|
||||
|
||||
return response()->json(['success' => true, 'data' => ServiceSummaryResource::collection($services)]);
|
||||
}
|
||||
|
||||
public function getClientServiceDetail(Request $request, string $uuid): JsonResponse
|
||||
{
|
||||
$service = $this->orderQueryService->findCustomerService((int) $request->user()->id, $uuid);
|
||||
|
||||
if (! $service) {
|
||||
return response()->json(['success' => false, 'message' => 'Không tìm thấy dịch vụ.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true, 'data' => new ServiceDetailResource($service)]);
|
||||
}
|
||||
|
||||
public function updateClientServiceAutoRenew(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$validated = $request->validate(['auto_renew' => ['required', 'boolean']]);
|
||||
|
||||
$service = \App\Models\Service::query()
|
||||
->where('id', $id)
|
||||
->where('customer_id', $request->user()->id)
|
||||
->first();
|
||||
|
||||
if (! $service) {
|
||||
return response()->json(['success' => false, 'message' => 'Không tìm thấy dịch vụ.'], 404);
|
||||
}
|
||||
|
||||
$service->update(['auto_renew' => (bool) $validated['auto_renew']]);
|
||||
|
||||
$this->activityLogger->log(
|
||||
event: 'updated',
|
||||
logName: 'Service',
|
||||
description: "Customer {$request->user()->email} " . ($validated['auto_renew'] ? 'bật' : 'tắt') . " tự động gia hạn dịch vụ #{$service->id}",
|
||||
subject: $service,
|
||||
newValues: ['auto_renew' => (bool) $validated['auto_renew']],
|
||||
);
|
||||
|
||||
return response()->json(['success' => true, 'message' => 'Cập nhật tự động gia hạn thành công.', 'data' => ['id' => $service->id, 'auto_renew' => (bool) $service->auto_renew]]);
|
||||
}
|
||||
|
||||
public function renewService(RenewServiceRequest $request, int $id): JsonResponse
|
||||
{
|
||||
$result = $this->orderQueryService->renewService($id, (int) $request->user()->id, (int) $request->validated()['months']);
|
||||
|
||||
if (! $result['ok']) {
|
||||
return response()->json(['success' => false, 'message' => $result['message']], $result['code']);
|
||||
}
|
||||
|
||||
return response()->json(['success' => true, 'message' => $result['message'], 'data' => $result['data']]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\Order;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class CheckServerNameRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'name.required' => 'Tên server không được để trống.',
|
||||
'name.string' => 'Tên server không hợp lệ.',
|
||||
'name.max' => 'Tên server tối đa 255 ký tự.',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\Order;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class RenewServiceRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'months' => ['required', 'integer', 'min:1', 'max:24'],
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'months.required' => 'Vui lòng chọn số tháng gia hạn.',
|
||||
'months.integer' => 'Số tháng gia hạn không hợp lệ.',
|
||||
'months.min' => 'Số tháng gia hạn tối thiểu là 1.',
|
||||
'months.max' => 'Số tháng gia hạn tối đa là 24.',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Service\Order;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class InvoiceDetailResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'invoice_number' => $this->invoice_number,
|
||||
'invoice_type' => $this->invoice_type,
|
||||
'status' => $this->status,
|
||||
'status_text' => InvoiceStatus::tryFrom((int) $this->status)?->label() ?? '-',
|
||||
'sub_total' => (float) $this->sub_total,
|
||||
'discount_amount' => (float) ($this->discount_amount ?? 0),
|
||||
'tax' => (float) $this->tax,
|
||||
'total' => (float) $this->total,
|
||||
'payment_method' => $this->payment_method,
|
||||
'auto_renew' => (bool) $this->auto_renew,
|
||||
'created_at' => $this->created_at?->toISOString(),
|
||||
'due_date' => $this->due_date?->toISOString(),
|
||||
'paid_at' => $this->paid_at?->toISOString(),
|
||||
'customer' => $this->whenLoaded('customer', fn () => [
|
||||
'id' => $this->customer->id,
|
||||
'name' => $this->customer->fullname,
|
||||
'email' => $this->customer->email,
|
||||
'address' => $this->customer->address,
|
||||
]),
|
||||
'order' => $this->whenLoaded('order', fn () => [
|
||||
'id' => $this->order->id,
|
||||
'order_num' => $this->order->order_num,
|
||||
'total' => (float) $this->order->total,
|
||||
]),
|
||||
'items' => $this->whenLoaded('order', fn () => $this->order->orderItems->map(fn ($item) => [
|
||||
'id' => $item->id,
|
||||
'product_name' => $item->product?->name,
|
||||
'server_name' => $item->server_name,
|
||||
'billing_cycle' => $item->billing_cycle,
|
||||
'price' => (float) $item->price,
|
||||
'months' => $item->months,
|
||||
'os_id' => $item->os_id,
|
||||
])->values()->all()),
|
||||
'attachments' => $this->whenLoaded('attachments', fn () => $this->attachments->map(fn ($a) => [
|
||||
'id' => $a->id,
|
||||
'filename' => $a->filename,
|
||||
'url' => $a->url,
|
||||
'mime_type' => $a->mime_type,
|
||||
])->all()),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Service\Order;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class InvoiceSummaryResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'invoice_number' => $this->invoice_number,
|
||||
'service_name' => $this->order?->orderItems->first()?->product?->name ?? ('Đơn hàng ' . $this->order?->order_num),
|
||||
'total' => (float) $this->total,
|
||||
'status' => $this->status,
|
||||
'status_text' => InvoiceStatus::tryFrom((int) $this->status)?->label() ?? '-',
|
||||
'created_at' => $this->created_at?->toISOString(),
|
||||
'order_id' => $this->order?->id,
|
||||
'order_num' => $this->order?->order_num,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Service\Order;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use App\Enums\Order\OrderStatus;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OrderDetailResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'order_num' => $this->order_num,
|
||||
'status' => $this->status,
|
||||
'status_text' => OrderStatus::from((int) $this->status)->label(),
|
||||
'total' => (float) $this->total,
|
||||
'created_at' => $this->created_at?->toISOString(),
|
||||
'customer' => $this->whenLoaded('customer', fn () => [
|
||||
'id' => $this->customer->id,
|
||||
'name' => $this->customer->fullname,
|
||||
'email' => $this->customer->email,
|
||||
]),
|
||||
'invoice' => $this->whenLoaded('invoice', fn () => [
|
||||
'id' => $this->invoice->id,
|
||||
'invoice_number' => $this->invoice->invoice_number,
|
||||
'status' => $this->invoice->status,
|
||||
'status_text' => InvoiceStatus::tryFrom((int) $this->invoice->status)?->label() ?? '-',
|
||||
'sub_total' => (float) $this->invoice->sub_total,
|
||||
'tax' => (float) $this->invoice->tax,
|
||||
'total' => (float) $this->invoice->total,
|
||||
'due_date' => $this->invoice->due_date?->toISOString(),
|
||||
'paid_at' => $this->invoice->paid_at?->toISOString(),
|
||||
]),
|
||||
'items' => $this->whenLoaded('orderItems') ? OrderItemResource::collection($this->orderItems) : [],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Service\Order;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OrderItemResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'billing_cycle' => $this->billing_cycle,
|
||||
'months' => $this->months,
|
||||
'server_name' => $this->server_name,
|
||||
'os_id' => $this->os_id,
|
||||
'price' => (float) $this->price,
|
||||
'service' => $this->whenLoaded('service', fn () => [
|
||||
'id' => $this->service->id,
|
||||
'status' => $this->service->status,
|
||||
'status_text' => $this->service->status_label,
|
||||
'retry_requested_at' => $this->service->retry_requested_at?->toISOString(),
|
||||
'can_retry_provisioning' => $this->service->status === 'pending' && ! $this->service->retry_requested_at,
|
||||
]),
|
||||
'product' => $this->whenLoaded('product', fn () => [
|
||||
'id' => $this->product->id,
|
||||
'uuid' => $this->product->uuid,
|
||||
'name' => $this->product->name,
|
||||
'product_type' => $this->product->product_type,
|
||||
'specs' => [
|
||||
'cpu' => $this->product->cpu,
|
||||
'ram' => $this->product->ram,
|
||||
'disk' => $this->product->disk,
|
||||
'bandwidth' => $this->product->bandwidth,
|
||||
],
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Service\Order;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use App\Enums\Order\OrderStatus;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OrderSummaryResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'order_num' => $this->order_num,
|
||||
'status' => $this->status,
|
||||
'status_text' => OrderStatus::from((int) $this->status)->label(),
|
||||
'total' => (float) $this->total,
|
||||
'created_at' => $this->created_at?->toISOString(),
|
||||
'invoice' => $this->whenLoaded('invoice', fn () => [
|
||||
'id' => $this->invoice->id,
|
||||
'invoice_number' => $this->invoice->invoice_number,
|
||||
'status' => $this->invoice->status,
|
||||
'status_text' => InvoiceStatus::tryFrom((int) $this->invoice->status)?->label() ?? '-',
|
||||
'total' => (float) $this->invoice->total,
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Service\Order;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ServiceDetailResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'service_name' => $this->product?->name ?? $this->server_name,
|
||||
'server_name' => $this->server_name,
|
||||
'product_type' => $this->product?->product_type,
|
||||
'billing_cycle' => $this->orderItem?->billing_cycle,
|
||||
'status' => $this->status,
|
||||
'status_text' => $this->status_label,
|
||||
'auto_renew' => (bool) $this->auto_renew,
|
||||
'started_at' => $this->started_at?->toISOString(),
|
||||
'expires_at' => $this->expires_at?->toISOString(),
|
||||
'note' => $this->note,
|
||||
'invoice_id' => $this->invoice_id,
|
||||
'invoice_number' => $this->invoice?->invoice_number,
|
||||
'product' => $this->whenLoaded('product', fn () => [
|
||||
'id' => $this->product->id,
|
||||
'uuid' => $this->product->uuid,
|
||||
'name' => $this->product->name,
|
||||
'product_type' => $this->product->product_type,
|
||||
'specs' => [
|
||||
'cpu' => $this->product->cpu,
|
||||
'ram' => $this->product->ram,
|
||||
'disk' => $this->product->disk,
|
||||
'bandwidth' => $this->product->bandwidth,
|
||||
],
|
||||
]),
|
||||
'addons' => $this->whenLoaded('addons', fn () => $this->addons->map(fn ($a) => [
|
||||
'type' => $a->addon_type,
|
||||
'value' => $a->addon_value,
|
||||
'price' => $a->price,
|
||||
])->all()),
|
||||
'vmware' => $this->whenLoaded('vmware', fn () => $this->vmware ? [
|
||||
'id' => $this->vmware->id,
|
||||
'name' => $this->vmware->name,
|
||||
'hostname' => $this->vmware->hostname,
|
||||
'ip_address' => $this->vmware->ipAddress?->ip,
|
||||
'os_name' => $this->vmware->os?->name,
|
||||
'status' => $this->vmware->status,
|
||||
'next_due_date' => $this->vmware->next_due_date?->toISOString(),
|
||||
] : null),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Service\Order;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ServiceSummaryResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'service_name' => $this->product?->name ?? $this->server_name,
|
||||
'server_name' => $this->server_name,
|
||||
'product_type' => $this->product?->product_type,
|
||||
'billing_cycle' => $this->orderItem?->billing_cycle,
|
||||
'status' => $this->status,
|
||||
'status_text' => $this->status_label,
|
||||
'auto_renew' => (bool) $this->auto_renew,
|
||||
'started_at' => $this->started_at?->toISOString(),
|
||||
'expires_at' => $this->expires_at?->toISOString(),
|
||||
'invoice_id' => $this->invoice_id,
|
||||
'invoice_number' => $this->invoice?->invoice_number,
|
||||
'addons' => $this->whenLoaded('addons', fn () => $this->addons->map(fn ($a) => [
|
||||
'type' => $a->addon_type,
|
||||
'value' => $a->addon_value,
|
||||
'price' => $a->price,
|
||||
])->all()),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user