feat: add GeneratesUniqueSlug class for unique slug generation

feat: create PostCategoryFactory for generating post category data

feat: create PostFactory for generating post data

feat: add retry_requested_at column to services table

feat: add vcenter provisioning fields to products table

feat: add module_host_id to os table

feat: create email_notification_templates table for email templates

feat: add scenario and format fields to template table

feat: add admin fields to transactions table

fix: update ticket table columns and types

feat: add uuid to ticket table and backfill existing records

fix: make ticket_reply user_id nullable

feat: create notifications table for user notifications

fix: make ticket uuid nullable

feat: create post_categories table for blog categories

feat: create posts table for blog posts

feat: add invoice email template for PDF generation
This commit is contained in:
baodev0
2026-05-25 15:39:42 +07:00
parent a31d238135
commit 520f21cb11
73 changed files with 4682 additions and 440 deletions
@@ -14,10 +14,12 @@ use App\Http\Requests\Api\Order\PayOrderRequest;
use App\Http\Requests\Api\Order\StoreOrderRequest;
use App\Models\Invoice;
use App\Models\InvoiceItem;
use App\Models\ModuleVmware;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\Service;
use App\Models\VirtualMachines;
use App\Repositories\Api\Order\OrderRepository;
use App\Services\ActivityLogger;
use App\Services\Api\Order\OrderService;
@@ -57,6 +59,35 @@ class OrderController extends Controller
}
}
/**
* GET /v1/orders/check-server-name?name=xxx
* Kiểm tra tên server đã tồn tại chưa (order_items, services, module_vmware, virtual_machines)
*/
public function checkServerName(Request $request): JsonResponse
{
$name = trim($request->query('name', ''));
if ($name === '') {
return response()->json(['available' => false, 'message' => 'Tên server không được để trống.']);
}
$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();
if ($exists) {
return response()->json([
'available' => false,
'message' => 'Tên server này đã được sử dụng, vui lòng chọn tên khác.',
]);
}
return response()->json(['available' => true]);
}
/**
* GET /v1/client/orders — customer's order list
*/
@@ -97,7 +128,7 @@ class OrderController extends Controller
{
try {
$order = Order::query()
->with(['invoice', 'orderItems.product', 'customer'])
->with(['invoice', 'orderItems.product', 'orderItems.service', 'customer'])
->where('customer_id', $request->user()->id)
->where('id', $id)
->first();
@@ -227,6 +258,7 @@ class OrderController extends Controller
{
try {
$order = Order::where('id', $id)
->with(['invoice', 'orderItems.service'])
->where('customer_id', $request->user()->id)
->first();
@@ -238,6 +270,26 @@ class OrderController extends Controller
return $this->error('Đơn hàng chưa được thanh toán.');
}
$paidAt = $order->invoice->paid_at ? Carbon::parse($order->invoice->paid_at) : null;
if (!$paidAt || $paidAt->diffInMinutes(now()) < 180) {
return $this->error('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ụ.');
}
$service = $order->orderItems->first()?->service;
if (!$service || (int) $service->customer_id !== (int) $request->user()->id) {
return $this->error('Không tìm thấy dịch vụ hợp lệ để khởi tạo lại.');
}
if ($service->status !== 'pending') {
return $this->error('Dịch vụ đã được khởi tạo hoặc không ở trạng thái chờ.');
}
if ($service->retry_requested_at) {
return $this->error('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');
}
$service->update(['retry_requested_at' => now()]);
RetryProvisioningOrchestrator::dispatch($order->id, $request->user()->id);
$this->activityLogger->log(
@@ -302,7 +354,6 @@ class OrderController extends Controller
'order.orderItems.product',
'order.vmWare.ipAddress',
'order.vmWare.os',
'invoiceItems',
'customer',
'attachments',
])
@@ -691,6 +742,13 @@ class OrderController extends Controller
'server_name' => $item->server_name,
'os_id' => $item->os_id,
'price' => (float) $item->price,
'service' => $item->service ? [
'id' => $item->service->id,
'status' => $item->service->status,
'status_text' => $item->service->status_label,
'retry_requested_at' => $this->isoDate($item->service->retry_requested_at),
'can_retry_provisioning' => $this->canRetryProvisioning($item->service),
] : null,
'product' => $item->product ? [
'id' => $item->product->id,
'uuid' => $item->product->uuid,
@@ -737,6 +795,24 @@ class OrderController extends Controller
];
}
private function canRetryProvisioning(Service $service): bool
{
if ($service->status !== 'pending') {
return false;
}
if ($service->retry_requested_at) {
return false;
}
$paidAt = $service->invoice?->paid_at;
if (!$paidAt) {
return false;
}
return Carbon::parse($paidAt)->diffInMinutes(now()) >= 180;
}
private function formatInvoiceDetail(Invoice $invoice): array
{
$order = $invoice->order;
@@ -780,15 +856,15 @@ class OrderController extends Controller
'disk' => $firstItem->product?->disk,
],
] : null,
'items' => ($invoice->relationLoaded('invoiceItems') ? $invoice->invoiceItems : collect())->map(fn($item) => [
'id' => $item->id,
'type' => $item->type,
'description' => $item->description,
'months' => $item->months,
'quantity' => $item->quantity,
'unit_price' => (float) $item->unit_price,
'total_price' => (float) $item->total_price,
])->all(),
'items' => $order ? $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' => ($invoice->relationLoaded('attachments') ? $invoice->attachments : collect())->map(fn($a) => [
'id' => $a->id,
'filename' => $a->filename,
@@ -166,69 +166,7 @@ class ProductCategoryController extends Controller
])
->get();
$productResources = $products->map(function ($product) {
$billingCycleLabels = [
'monthly' => 'Hàng tháng',
'quarterly' => 'Hàng quý',
'semi_annually' => 'Nửa năm',
'annually' => 'Hàng năm',
'onetime' => 'Một lần',
];
$periodUnits = [
'monthly' => 'month',
'quarterly' => 'quarter',
'semi_annually' => 'semi_year',
'annually' => 'year',
'onetime' => 'none',
];
return [
'uuid' => $product->uuid,
'name' => $product->name,
'slug' => $product->slug,
'short_description' => $product->short_description,
'description' => $product->description,
'thumbnail' => $product->thumbnail ? asset($product->thumbnail) : null,
'product_type' => $product->product_type,
'specs' => [
'cpu' => (int) $product->cpu,
'ram' => (int) $product->ram,
'disk' => (int) $product->disk,
'bandwidth' => (int) $product->bandwidth,
'gpu' => $product->gpu
],
'stock' => $product->stock,
'price' => (int) $product->price,
'price_sale' => $product->price_sale ? (int) $product->price_sale : null,
'group' => $product->category->name ?? null,
'os' => $product->osVersions->groupBy('os_group_id')->map(function ($versions) {
$group = $versions->first()->osGroup;
return [
'name' => $group?->name,
'thumbnail' => $group?->thumbnail,
'versions' => $versions->map(fn($os) => [
'version' => $os->version,
'name' => $os->name,
])->values(),
];
})->values(),
'pricing' => $product->pricings->map(fn($p) => [
'id' => $p->id,
'name' => $billingCycleLabels[$p->billing_cycle] ?? $p->billing_cycle,
'price' => (int) $p->price,
'price_sale' => $p->price_sale ? (int) $p->price_sale : null,
'period' => $p->billing_cycle ? (int) match($p->billing_cycle) {
'monthly' => 1,
'quarterly' => 3,
'semi_annually' => 6,
'annually' => 12,
default => 1,
} : 1,
'period_unit' => $periodUnits[$p->billing_cycle] ?? 'month',
]),
];
});
$productResources = $products->map(fn($product) => $this->formatProduct($product));
$categoryArray = $category->toArray();
$categoryArray['products'] = $productResources;
@@ -332,69 +270,7 @@ class ProductCategoryController extends Controller
])
->get();
$billingCycleLabels = [
'monthly' => 'Hàng tháng',
'quarterly' => 'Hàng quý',
'semi_annually' => 'Nửa năm',
'annually' => 'Hàng năm',
'onetime' => 'Một lần',
];
$periodUnits = [
'monthly' => 'month',
'quarterly' => 'quarter',
'semi_annually' => 'semi_year',
'annually' => 'year',
'onetime' => 'none',
];
$productResources = $products->map(function ($product) use ($billingCycleLabels, $periodUnits) {
return [
'uuid' => $product->uuid,
'name' => $product->name,
'slug' => $product->slug,
'short_description' => $product->short_description,
'description' => $product->description,
'thumbnail' => $product->thumbnail ? asset($product->thumbnail) : null,
'product_type' => $product->product_type,
'specs' => [
'cpu' => (int) $product->cpu,
'ram' => (int) $product->ram,
'disk' => (int) $product->disk,
'bandwidth' => (int) $product->bandwidth,
'gpu' => $product->gpu
],
'stock' => $product->stock,
'price' => (int) $product->price,
'price_sale' => $product->price_sale ? (int) $product->price_sale : null,
'group' => $product->category->name ?? null,
'os' => $product->osVersions->groupBy('os_group_id')->map(function ($versions) {
$group = $versions->first()->osGroup;
return [
'name' => $group?->name,
'thumbnail' => $group?->thumbnail,
'versions' => $versions->map(fn($os) => [
'version' => $os->version,
'name' => $os->name,
])->values(),
];
})->values(),
'pricing' => $product->pricings->map(fn($p) => [
'id' => $p->id,
'name' => $billingCycleLabels[$p->billing_cycle] ?? $p->billing_cycle,
'price' => (int) $p->price,
'price_sale' => $p->price_sale ? (int) $p->price_sale : null,
'period' => $p->billing_cycle ? (int) match($p->billing_cycle) {
'monthly' => 1,
'quarterly' => 3,
'semi_annually' => 6,
'annually' => 12,
default => 1,
} : 1,
'period_unit' => $periodUnits[$p->billing_cycle] ?? 'month',
]),
];
});
$productResources = $products->map(fn($product) => $this->formatProduct($product));
$childArray = $child->toArray();
$childArray['parent'] = $child->parent ? $child->parent->toArray() : null;
@@ -406,4 +282,81 @@ class ProductCategoryController extends Controller
'data' => $childArray,
]);
}
private function formatProduct($product): array
{
$billingCycleLabels = [
'monthly' => 'Hàng tháng',
'quarterly' => 'Hàng quý',
'semiannually' => 'Nửa năm',
'annually' => 'Hàng năm',
'biennially' => '2 năm',
'triennially' => '3 năm',
'onetime' => 'Một lần',
];
$periodMonths = [
'monthly' => 1,
'quarterly' => 3,
'semiannually' => 6,
'annually' => 12,
'biennially' => 24,
'triennially' => 36,
'onetime' => 0,
];
$periodUnits = [
'monthly' => 'month',
'quarterly' => 'quarter',
'semiannually' => 'semi_year',
'annually' => 'year',
'biennially' => 'biennially',
'triennially' => 'triennially',
'onetime' => 'none',
];
return [
'uuid' => $product->uuid,
'name' => $product->name,
'slug' => $product->slug,
'short_description' => $product->short_description,
'description' => $product->description,
'thumbnail' => $product->thumbnail ? asset($product->thumbnail) : null,
'product_type' => $product->product_type,
'specs' => [
'cpu' => (int) $product->cpu,
'ram' => (int) $product->ram,
'disk' => (int) $product->disk,
'bandwidth' => (int) $product->bandwidth,
'gpu' => $product->gpu,
],
'stock' => $product->stock,
'price' => (int) $product->price,
'price_sale' => $product->price_sale ? (int) $product->price_sale : null,
'group' => $product->category->name ?? null,
'os' => $product->osVersions->groupBy('os_group_id')->map(function ($versions) {
$group = $versions->first()->osGroup;
return [
'name' => $group?->name,
'thumbnail' => $group?->thumbnail,
'versions' => $versions->map(fn($os) => [
'id' => $os->id,
'version' => $os->version,
'name' => $os->name,
])->values(),
];
})->values(),
'pricing' => $product->pricings
->sortBy(fn($p) => $periodMonths[$p->billing_cycle] ?? 999)
->values()
->map(fn($p) => [
'id' => $p->id,
'name' => $billingCycleLabels[$p->billing_cycle] ?? $p->billing_cycle,
'price' => (int) $p->price,
'price_sale' => $p->price_sale ? (int) $p->price_sale : null,
'period' => $periodMonths[$p->billing_cycle] ?? 1,
'period_unit' => $periodUnits[$p->billing_cycle] ?? 'month',
]),
];
}
}