feat: Enhance product support in Cart and OrderItem models

- Added new fields 'billing_cycle' and 'product_pricing_id' to Cart and OrderItem models.
- Updated product retrieval logic to support both new and legacy systems.
- Introduced relationships for Product and ProductPricings in Cart and OrderItem models.
- Created migration to add new fields to the carts and order_item tables.
- Updated frontend service index to handle new pricing structure and billing cycles.
- Improved UI components for better user experience in product selection and pricing display.
- Refactored action buttons in various views for consistency and improved styling.
This commit is contained in:
pbao9
2026-02-09 08:04:56 +07:00
parent 4d4bf020d4
commit 057353a2e1
15 changed files with 590 additions and 250 deletions
@@ -14,7 +14,10 @@ use App\Models\InvoiceItem;
use App\Models\ModuleHosting; use App\Models\ModuleHosting;
use App\Models\ModuleVmware; use App\Models\ModuleVmware;
use App\Models\Order; use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Os; use App\Models\Os;
use App\Models\Product;
use App\Models\ProductPricings;
use App\Models\VPS; use App\Models\VPS;
use Exception; use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@@ -36,36 +39,85 @@ class CartController extends Controller
public function addCart(Request $request) public function addCart(Request $request)
{ {
$userId = customer()?->id; $userId = customer()?->id;
$months = $request->input('months');
$productId = $request->product_id;
$typeId = $request->type_id;
if (!$userId) { if (!$userId) {
return back()->with('error', 'Bạn cần đăng nhập để thêm sản phẩm vào giỏ hàng.'); return back()->with('error', 'Bạn cần đăng nhập để thêm sản phẩm vào giỏ hàng.');
} }
// Validate input data // Get request data
if (!$productId || !$typeId || !$months) { $productId = $request->product_id;
return back()->with('error', 'Dữ liệu không hợp lệ.'); $typeId = $request->type_id; // Can be null for new Product system
$months = $request->input('months');
$billingCycle = $request->input('billing_cycle'); // New: billing cycle
$productPrice = $request->input('product_price'); // Price from form
// Validate basic input
if (!$productId) {
return back()->with('error', 'Sản phẩm không hợp lệ.');
} }
// Use updateOrCreate instead of delete + create for better performance
try { try {
CartModel::updateOrCreate( // Check if this is new Product system or legacy (Hosting/VPS)
['customer_id' => $userId], // Only one cart item per user $product = Product::find($productId);
[
'product_id' => $productId,
'type_product' => $typeId,
'months' => $months,
]
);
return redirect()->route('cart.index')->with('success', 'Sản phẩm đã được thêm vào giỏ hàng!'); if ($product) {
// NEW FLOW: Using Product table
$pricingId = null;
$finalPrice = $productPrice;
// Find the pricing based on billing_cycle
if ($billingCycle) {
$pricing = ProductPricings::where('product_id', $productId)
->where('billing_cycle', $billingCycle)
->where('is_active', 1)
->first();
if ($pricing) {
$pricingId = $pricing->id;
$finalPrice = $pricing->price_sale ?: $pricing->price;
}
}
// Add to cart
CartModel::updateOrCreate(
['customer_id' => $userId],
[
'product_id' => $productId,
'type_product' => null, // New system doesn't use type_product
'months' => $months,
'billing_cycle' => $billingCycle,
'product_pricing_id' => $pricingId,
'price' => $finalPrice,
'quantity' => 1,
]
);
return redirect()->route('cart.index')->with('success', 'Sản phẩm đã được thêm vào giỏ hàng!');
} else {
// LEGACY FLOW: Using old Hosting/VPS system
if (!$typeId || !$months) {
return back()->with('error', 'Dữ liệu không hợp lệ.');
}
CartModel::updateOrCreate(
['customer_id' => $userId],
[
'product_id' => $productId,
'type_product' => $typeId,
'months' => $months,
'price' => $productPrice,
]
);
return redirect()->route('cart.index')->with('success', 'Sản phẩm đã được thêm vào giỏ hàng!');
}
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error('Cart add error: ' . $e->getMessage(), [ Log::error('Cart add error: ' . $e->getMessage(), [
'user_id' => $userId, 'user_id' => $userId,
'product_id' => $productId, 'product_id' => $productId,
'type_id' => $typeId 'type_id' => $typeId,
'billing_cycle' => $billingCycle ?? null,
'trace' => $e->getTraceAsString()
]); ]);
return back()->with('error', 'Có lỗi xảy ra khi thêm sản phẩm vào giỏ hàng.'); return back()->with('error', 'Có lỗi xảy ra khi thêm sản phẩm vào giỏ hàng.');
} }
@@ -11,6 +11,7 @@ use App\Models\Hosting;
use App\Models\Group; use App\Models\Group;
use App\Models\Type; use App\Models\Type;
use App\Models\VPS; use App\Models\VPS;
use App\Models\ProductCategories;
use App\Repositories\Addon\AddonRepositoryInterface; use App\Repositories\Addon\AddonRepositoryInterface;
use App\Repositories\Domain\DomainRepositoryInterface; use App\Repositories\Domain\DomainRepositoryInterface;
use App\Repositories\Group\GroupRepositoryInterface; use App\Repositories\Group\GroupRepositoryInterface;
@@ -18,7 +19,6 @@ use App\Repositories\Hosting\HostingRepositoryInterface;
use App\Repositories\Type\TypeRepositoryInterface; use App\Repositories\Type\TypeRepositoryInterface;
use App\Repositories\VPS\VPSRepositoryInterface; use App\Repositories\VPS\VPSRepositoryInterface;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
class ShopController extends Controller class ShopController extends Controller
{ {
@@ -34,24 +34,22 @@ class ShopController extends Controller
} }
/** /**
* Display the shop index page with optimized queries * Display the shop index page
*/ */
public function index(Request $request) public function index(Request $request)
{ {
$title = "Dịch vụ của chúng tôi"; $title = "Dịch vụ của chúng tôi";
// Cache types with groups for better performance // Get types with groups
$type = Cache::remember('shop_types_with_groups', 3600, function () { $type = Type::with(['group' => function ($query) {
return Type::with(['group' => function ($query) { $query->where('status', 1); // Only active groups
$query->where('status', 1); // Only active groups }])
}])
->whereNotIn('id', [1, 2]) ->whereNotIn('id', [1, 2])
->where('status', 1) // Only active types ->where('status', 1) // Only active types
->latest('id') ->latest('id')
->get(); ->get();
});
// Handle product filtering with optimized queries // Handle product filtering
if ($request->type && $request->group) { if ($request->type && $request->group) {
$product = $this->getProductsByTypeAndGroup($request->type, $request->group); $product = $this->getProductsByTypeAndGroup($request->type, $request->group);
$group = $this->getGroupsByType($request->type); $group = $this->getGroupsByType($request->type);
@@ -84,76 +82,67 @@ class ShopController extends Controller
} }
/** /**
* Display products by group slug with pagination and optimization * Display products by category slug (ProductCategories)
*/ */
public function show(Request $request, string $slug) public function show(Request $request, string $slug)
{ {
// Cache group data for better performance // Find category by slug
$page = (int) $request->get('page', 1); $category = ProductCategories::with(['parent', 'children'])
$cacheKey = "group_products_{$slug}_page_{$page}"; ->where('slug', $slug)
->where('is_active', 1)
->first();
$data = Cache::remember($cacheKey, 1800, function () use ($slug, $page) { if (!$category) {
// Find group with optimized query abort(404, 'Danh mục sản phẩm không tồn tại');
$group = Group::with(['type'])
->where('slug', $slug)
->where('status', 1) // Only active groups
->first();
if (!$group) {
return null;
}
// Get products with pagination based on group type
$products = $this->getPaginatedProducts($group, $page);
return [
'group' => $group,
'products' => $products,
];
});
if (!$data) {
abort(404, 'Nhóm sản phẩm không tồn tại');
} }
// Get products with pagination and pricing
$products = $category->products()
->with([
'pricings' => function ($query) {
$query->where('is_active', 1)->orderBy('price');
},
'osVersions.osGroup',
'controlPanels'
])
->where('is_active', 1)
->orderBy('created_at', 'desc')
->paginate(12);
return view('frontend.service.index', [ return view('frontend.service.index', [
'group' => $data['group'], 'category' => $category,
'products' => $data['products'], 'products' => $products,
'breadcrumbs' => $this->crums->add(__('Dashboard'))->add('Sản phẩm thuộc ' . $data['group']->name), 'breadcrumbs' => $this->crums->add(__('Dashboard'))->add('Sản phẩm thuộc ' . $category->name),
]); ]);
} }
/** /**
* Get products by type and group with optimization * Get products by type and group
*/ */
private function getProductsByTypeAndGroup(int $typeId, int $groupId) private function getProductsByTypeAndGroup(int $typeId, int $groupId)
{ {
return Cache::remember("products_type_{$typeId}_group_{$groupId}", 1800, function () use ($typeId, $groupId) { return match ($typeId) {
return match ($typeId) { 3 => Hosting::where('group_id', $groupId)
3 => Hosting::where('group_id', $groupId) ->where('status', 1)
->where('status', 1) ->with(['group', 'type'])
->with(['group', 'type']) ->get(),
->get(), 4 => VPS::where('group_id', $groupId)
4 => VPS::where('group_id', $groupId) ->where('status', 1)
->where('status', 1) ->with(['group', 'type', 'operatingSystems.osGroup'])
->with(['group', 'type', 'operatingSystems.osGroup']) ->get(),
->get(), default => collect()
default => collect() };
};
});
} }
/** /**
* Get groups by type with optimization * Get groups by type
*/ */
private function getGroupsByType(int $typeId) private function getGroupsByType(int $typeId)
{ {
return Cache::remember("groups_type_{$typeId}", 3600, function () use ($typeId) { return Group::with('type')
return Group::with('type') ->where('type_id', $typeId)
->where('type_id', $typeId) ->where('status', 1)
->where('status', 1) ->get();
->get();
});
} }
/** /**
@@ -171,34 +160,4 @@ class ShopController extends Controller
default => null default => null
}; };
} }
/**
* Get paginated products based on group type
*/
private function getPaginatedProducts(Group $group, int $page = 1, int $perPage = 12)
{
$query = null;
// Determine which model to query based on group type
switch ($group->type_id) {
case 3: // Hosting
$query = Hosting::where('group_id', $group->id)
->where('status', 1)
->with(['group', 'type'])
->orderBy('created_at', 'desc');
break;
case 4: // VPS
$query = VPS::where('group_id', $group->id)
->where('status', 1)
->with(['group', 'type'])
->orderBy('created_at', 'desc');
break;
default:
return collect()->paginate($perPage);
}
return $query->paginate($perPage, ['*'], 'page', $page);
}
} }
+38
View File
@@ -16,14 +16,52 @@ class Cart extends Model
'price', 'price',
'quantity', 'quantity',
'months', 'months',
'billing_cycle', // Add new field for billing cycle
'product_pricing_id', // Add new field to link to ProductPricings
]; ];
/**
* Get product - support both new and legacy system
*/
public function product() public function product()
{ {
// New flow: Check if product exists in Product table first
if ($this->product_id) {
$product = Product::find($this->product_id);
if ($product) {
return $product;
}
}
// Legacy flow: fallback to old system based on type_product
return match ($this->type_product) { return match ($this->type_product) {
3 => $this->belongsTo(Hosting::class, 'product_id'), 3 => $this->belongsTo(Hosting::class, 'product_id'),
4 => $this->belongsTo(VPS::class, 'product_id'), 4 => $this->belongsTo(VPS::class, 'product_id'),
default => null, default => null,
}; };
} }
/**
* Relationship: Cart belongs to Product (new system)
*/
public function productNew()
{
return $this->belongsTo(Product::class, 'product_id');
}
/**
* Relationship: Cart belongs to Customer
*/
public function customer()
{
return $this->belongsTo(Customer::class, 'customer_id');
}
/**
* Relationship: Cart has pricing info
*/
public function pricing()
{
return $this->belongsTo(ProductPricings::class, 'product_pricing_id');
}
} }
+25
View File
@@ -25,6 +25,15 @@ class OrderItem extends Model
public function getProductAttribute() public function getProductAttribute()
{ {
// New flow: Check if product exists in Product table first
if ($this->product_id) {
$product = Product::find($this->product_id);
if ($product) {
return $product;
}
}
// Legacy flow: fallback to old system
return match ($this->product_type) { return match ($this->product_type) {
3 => Hosting::find($this->product_id), 3 => Hosting::find($this->product_id),
4 => Vps::find($this->product_id), 4 => Vps::find($this->product_id),
@@ -32,6 +41,22 @@ class OrderItem extends Model
}; };
} }
/**
* Relationship: OrderItem belongs to Product (new system)
*/
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
/**
* Relationship: OrderItem has pricing info
*/
public function pricing()
{
return $this->belongsTo(ProductPricings::class, 'product_pricing_id');
}
public function os() public function os()
+20
View File
@@ -39,4 +39,24 @@ class Os extends Model
->withTimestamps() ->withTimestamps()
->withPivot('product_type'); ->withPivot('product_type');
} }
/**
* Many-to-many relationship với Products (new table: os_product)
*/
public function products()
{
return $this->belongsToMany(\App\Models\Product::class, 'os_product', 'os_id', 'product_id')
->withTimestamps();
}
/**
* Get display name with OS Group
*/
public function getNameAttribute()
{
if ($this->osGroup) {
return $this->osGroup->name . ' ' . $this->version;
}
return $this->version;
}
} }
+43
View File
@@ -79,6 +79,22 @@ class Product extends Model
return $this->hasMany(ProductPricings::class, 'product_id'); return $this->hasMany(ProductPricings::class, 'product_id');
} }
/**
* Relationship: Product has many order items
*/
public function orderItems()
{
return $this->hasMany(OrderItem::class, 'product_id');
}
/**
* Relationship: Product has many orders through order items
*/
public function orders()
{
return $this->hasManyThrough(Order::class, OrderItem::class, 'product_id', 'id', 'id', 'order_id');
}
/** /**
* Legacy: Many-to-many categories (if still needed) * Legacy: Many-to-many categories (if still needed)
*/ */
@@ -86,4 +102,31 @@ class Product extends Model
{ {
return $this->belongsToMany(ProductCategories::class, 'product_product_category', 'product_id', 'product_category_id'); return $this->belongsToMany(ProductCategories::class, 'product_product_category', 'product_id', 'product_category_id');
} }
/**
* Get monthly pricing (or first available pricing)
*/
public function getMonthlyPricingAttribute()
{
return $this->pricings()
->where('is_active', 1)
->where('billing_cycle', 'monthly')
->first()
?? $this->pricings()->where('is_active', 1)->orderBy('price')->first();
}
/**
* Get display price (sale price if available, otherwise regular price)
*/
public function getPriceAttribute()
{
$pricing = $this->monthlyPricing;
return $pricing ? $pricing->price : 0;
}
public function getPriceSaleAttribute()
{
$pricing = $this->monthlyPricing;
return $pricing ? $pricing->price_sale : null;
}
} }
+3 -1
View File
@@ -10,6 +10,8 @@ class ProductCategories extends Model
{ {
use HasFactory, SoftDeletes; use HasFactory, SoftDeletes;
protected $table = 'product_categories';
protected $fillable = [ protected $fillable = [
'name', 'name',
'slug', 'slug',
@@ -30,6 +32,6 @@ class ProductCategories extends Model
public function products() public function products()
{ {
return $this->belongsToMany(Product::class, 'product_product_category', 'product_category_id', 'product_id'); return $this->hasMany(Product::class, 'product_categories_id');
} }
} }
@@ -0,0 +1,60 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// Add support for new Product system to carts table
Schema::table('carts', function (Blueprint $table) {
if (!Schema::hasColumn('carts', 'billing_cycle')) {
$table->string('billing_cycle', 50)->nullable()->after('months')->comment('Billing cycle: monthly, semi_annually, annually, etc.');
}
if (!Schema::hasColumn('carts', 'product_pricing_id')) {
$table->unsignedBigInteger('product_pricing_id')->nullable()->after('billing_cycle')->comment('Link to product_pricings table');
$table->index('product_pricing_id');
}
// Make type_product nullable for new Product system
$table->integer('type_product')->nullable()->change();
});
// Add support for new Product system to order__order_item table
Schema::table('order__order_item', function (Blueprint $table) {
if (!Schema::hasColumn('order__order_item', 'billing_cycle')) {
$table->string('billing_cycle', 50)->nullable()->after('months')->comment('Billing cycle: monthly, semi_annually, annually, etc.');
}
if (!Schema::hasColumn('order__order_item', 'product_pricing_id')) {
$table->unsignedBigInteger('product_pricing_id')->nullable()->after('billing_cycle')->comment('Link to product_pricings table');
$table->index('product_pricing_id');
}
// Make product_type nullable for new Product system
if (Schema::hasColumn('order__order_item', 'product_type')) {
$table->integer('product_type')->nullable()->change();
}
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('carts', function (Blueprint $table) {
$table->dropColumn(['billing_cycle', 'product_pricing_id']);
$table->dropIndex(['product_pricing_id']);
});
Schema::table('order__order_item', function (Blueprint $table) {
$table->dropColumn(['billing_cycle', 'product_pricing_id']);
$table->dropIndex(['product_pricing_id']);
});
}
};
+23 -14
View File
@@ -27,15 +27,22 @@ $(document).ready(function () {
$(document).on("click", ".dropdown-option", function (e) { $(document).on("click", ".dropdown-option", function (e) {
e.stopPropagation(); e.stopPropagation();
const $option = $(this); const $option = $(this);
const $dropdown = $option.closest(".custom-dropdown");
const $selected = $dropdown.find(".dropdown-selected"); // Ignore disabled options
const $options = $dropdown.find(".dropdown-options"); if ($option.hasClass('disabled')) {
const $selectedText = $selected.find(".selected-text"); return;
}
const $dropdown = $option.closest('.custom-dropdown');
const $selected = $dropdown.find('.dropdown-selected');
const $options = $dropdown.find('.dropdown-options');
const $selectedText = $selected.find('.selected-text');
// Get data from selected option // Get data from selected option
const value = $option.data("value"); const value = $option.data('value');
const months = $option.data("months"); const months = $option.data('months');
const price = $option.data("price"); const price = $option.data('price');
const cycle = $option.data('cycle'); // Get billing cycle
// Get text from the first span element to avoid getting badge text // Get text from the first span element to avoid getting badge text
const text = $option.find('span').first().text(); const text = $option.find('span').first().text();
@@ -43,19 +50,21 @@ $(document).ready(function () {
$selectedText.text(text); $selectedText.text(text);
// Update hidden inputs // Update hidden inputs
$dropdown.closest("form").find(".hiddenMonths").val(months); const $form = $dropdown.closest('form');
$dropdown.closest("form").find(".hiddenTotalPrice").val(price); $form.find('.hiddenMonths').val(months);
$form.find('.hiddenProductPrice').val(price); // Update product price
$form.find('.hiddenBillingCycle').val(cycle); // Update billing cycle
// Update selected state // Update selected state
$options.find(".dropdown-option").removeClass("selected"); $options.find('.dropdown-option').removeClass('selected');
$option.addClass("selected"); $option.addClass('selected');
// Close dropdown // Close dropdown
$options.removeClass("show"); $options.removeClass('show');
$selected.removeClass("active"); $selected.removeClass('active');
// Trigger custom event for other components // Trigger custom event for other components
$dropdown.trigger("dropdown:changed", { $dropdown.trigger('dropdown:changed', {
value: value, value: value,
months: months, months: months,
price: price, price: price,
@@ -1,12 +1,12 @@
<x-button-crm type="button" variant="primary" size="sm" route="module.vcenter.create" icon="plus-circle">Add <x-button-crm type="button" variant="outline-primary" size="sm" route="module.vcenter.create" icon="plus-circle">Add
Server</x-button-crm> Server</x-button-crm>
<form action="{{ route('module.vcenter.import') }}" class="d-lg-inline" enctype="multipart/form-data" method="post"> <form action="{{ route('module.vcenter.import') }}" class="d-lg-inline" enctype="multipart/form-data" method="post">
@csrf @csrf
<input type="file" name="file" class="d-none file-selector" <input type="file" name="file" class="d-none file-selector"
accept=".csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" onchange="form.submit()" /> accept=".csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" onchange="form.submit()" />
<x-button-crm type="button" variant="success" size="sm" route="module.vcenter.import" icon="plus-circle">Import <x-button-crm type="button" variant="outline-info" size="sm" route="module.vcenter.import" icon="plus-circle">Import
From From
Excel</x-button-crm> Excel</x-button-crm>
</form> </form>
<x-button-crm type="button" variant="secondary" size="sm" url="file/vCenter.xlsx" icon="download">File Import <x-button-crm type="button" variant="outline-secondary" size="sm" url="file/vCenter.xlsx" icon="download">File Import
Demo</x-button-crm> Demo</x-button-crm>
@@ -44,7 +44,7 @@
@section('content') @section('content')
@if (auth()->guard('admins')->user()->can('manager module hosting')) @if (auth()->guard('admins')->user()->can('manager module hosting'))
<div class="card card-primary card-outline"> <div class="card card-primary card-outline">
<div class="card-header"> <div class="card-header d-flex justify-content-end gap-2">
@include('administrator.pages.block.ModuleHosting') @include('administrator.pages.block.ModuleHosting')
</div> </div>
<div class="card-body"> <div class="card-body">
@@ -1,2 +1,7 @@
<x-button-crm type="button" variant="primary" size="sm" route="module.vcenter.edit" :params="['id' => $row->id]" icon="pencil">Edit</x-button-crm> <div class="d-flex gap-2">
<x-button-crm type="button" variant="danger" size="sm" route="module.vcenter.destroy" :params="['id' => $row->id]" icon="trash">Delete</x-button-crm>
<x-button-crm type="button" variant="primary" size="sm" route="module.vcenter.edit" :params="['id' => $row->id]"
icon="pencil">Edit</x-button-crm>
<x-button-crm type="button" variant="danger" size="sm" route="module.vcenter.destroy" :params="['id' => $row->id]"
icon="trash">Delete</x-button-crm>
</div>
@@ -1,2 +1,6 @@
<x-button-crm type="button" variant="primary" size="sm" route="products.edit" :params="['id' => $row->id]" icon="pencil">Chỉnh sửa</x-button-crm> <div class="d-flex gap-2">
<x-button-crm type="button" variant="danger" size="sm" route="products.destroy" :params="['id' => $row->id]" icon="trash">Xóa</x-button-crm> <x-button-crm type="button" variant="outline-primary" size="sm" route="products.edit" :params="['id' => $row->id]"
icon="pencil">Chỉnh sửa</x-button-crm>
<x-button-crm type="button" variant="outline-danger" size="sm" route="products.destroy" :params="['id' => $row->id]"
icon="trash">Xóa</x-button-crm>
</div>
+239 -116
View File
@@ -1,42 +1,65 @@
@extends('frontend.layouts.master') @extends('frontend.layouts.master')
@section('content') @section('content')
@if (!empty($group)) @if (!empty($category))
<!-- Products Grid --> <!-- Products Grid -->
<div class="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-4 gap-8" x-data> <div class="grid grid-cols-1 gap-8 lg:grid-cols-2 xl:grid-cols-4" x-data>
@forelse ($products as $index => $item) @forelse ($products as $index => $item)
<form action="{{ route('cart.add') }}" method="post"> <form action="{{ route('cart.add') }}" method="post">
@csrf @csrf
<input name="product_id" value="{{ $item->id }}" type="hidden" /> <input name="product_id" value="{{ $item->id }}" type="hidden" />
<input name="type_id" value="{{ $item->type_id }}" type="hidden" /> {{-- Remove type_id for new Product system --}}
<input name="product_price" value="{{ $item->price_sale ?: $item->price }}" type="hidden" /> @php
$defaultPricingForCart =
$item->pricings->firstWhere('billing_cycle', 'semi_annually') ?? $item->pricings->first();
$cartPrice = $defaultPricingForCart
? floatval($defaultPricingForCart->price_sale ?: $defaultPricingForCart->price)
: 0;
@endphp
<input name="product_price" value="{{ $cartPrice }}" type="hidden" class="hiddenProductPrice" />
<div class="group relative bg-white rounded-md hover:shadow-2xl transition-all duration-500 transform overflow-hidden border border-gray-100" <div class="group relative transform overflow-hidden rounded-md border border-gray-100 bg-white transition-all duration-500 hover:shadow-2xl"
x-init="$el.style.opacity = 0; x-init="$el.style.opacity = 0;
setTimeout(() => { $el.style.opacity = 1 }, {{ (int) $index * 150 }})" style="opacity: 0"> setTimeout(() => { $el.style.opacity = 1 }, {{ (int) $index * 150 }})" style="opacity: 0">
<!-- Header with gradient --> <!-- Header with gradient -->
<div class="p-2 border-b border-dashed border-gray-200"> <div class="border-b border-dashed border-gray-200 p-2">
<h3 class="text-xl text-primary font-bold mb-2 text-center"> <h3 class="text-primary mb-2 text-center text-xl font-bold">
{{ $item->name }} {{ $item->name }}
</h3> </h3>
</div> </div>
@php @php
$basePrice = floatval($item->price_sale ?: $item->price); // Get pricing for different billing cycles
// Note: If billing_cycle is 'onetime', treat it as monthly
$monthlyPricing =
$item->pricings->firstWhere('billing_cycle', 'monthly') ??
$item->pricings->firstWhere('billing_cycle', 'onetime');
$semiAnnuallyPricing = $item->pricings->firstWhere('billing_cycle', 'semi_annually');
$annuallyPricing = $item->pricings->firstWhere('billing_cycle', 'annually');
$bienniallyPricing = $item->pricings->firstWhere('billing_cycle', 'biennially');
$trienniallyPricing = $item->pricings->firstWhere('billing_cycle', 'triennially');
// Use monthly or first available pricing for display
$displayPricing = $monthlyPricing ?? $item->pricings->first();
$basePrice = $displayPricing
? floatval($displayPricing->price_sale ?: $displayPricing->price)
: 0;
@endphp @endphp
<div class="p-6" data-product-id="{{ $item->id }}" data-base-price="{{ $basePrice }}"> <div class="p-6" data-product-id="{{ $item->id }}" data-base-price="{{ $basePrice }}">
<!-- Price Display --> <!-- Price Display -->
<div class="text-center"> <div class="text-center">
<div <div
class="inline-flex items-center justify-center gap-2 bg-orange-50 rounded-md w-full mb-3 p-2"> class="mb-3 inline-flex w-full items-center justify-center gap-2 rounded-md bg-orange-50 p-2">
<span class="text-sm font-bold text-primary"> <span class="text-primary text-sm font-bold">
≈ {{ format_price($item->price_sale ?: $item->price) }} ≈
{{ $displayPricing ? format_price($displayPricing->price_sale ?: $displayPricing->price) : 'Liên hệ' }}
</span> </span>
<span class="text-primary font-medium">/ tháng</span> @if ($displayPricing)
<span class="text-primary font-medium">/ tháng</span>
@endif
</div> </div>
</div> </div>
<!-- Duration Selector --> <!-- Duration Selector -->
@@ -44,143 +67,243 @@
<div class="mb-6"> <div class="mb-6">
<!-- Custom Dropdown --> <!-- Custom Dropdown -->
<div class="custom-dropdown" id="durationDropdown{{ $item->id }}"> <div class="custom-dropdown" id="durationDropdown{{ $item->id }}">
@php
// Default to semi_annually (6 months) if available, otherwise first available
$defaultPricing = $semiAnnuallyPricing ?? $item->pricings->first();
$defaultPrice = $defaultPricing
? floatval($defaultPricing->price_sale ?: $defaultPricing->price)
: 0;
$defaultMonths = match ($defaultPricing?->billing_cycle) {
'semi_annually' => 6,
'annually' => 12,
'biennially' => 24,
'triennially' => 36,
default => 1,
};
@endphp
<div class="dropdown-selected"> <div class="dropdown-selected">
<span class="selected-text">{{ format_price($basePrice * 6) }} / 6 tháng</span> <span class="selected-text">
@if ($defaultPricing)
{{ format_price($defaultPrice) }} / {{ $defaultMonths }}
{{ $defaultMonths > 1 ? 'tháng' : 'tháng' }}
@else
Chọn thời gian
@endif
</span>
<i class="dropdown-arrow">▼</i> <i class="dropdown-arrow">▼</i>
</div> </div>
<div class="dropdown-options"> <div class="dropdown-options">
<div class="dropdown-option selected" data-value="6" data-months="6" {{-- Monthly --}}
data-price="{{ $basePrice * 6 }}"> <div class="dropdown-option {{ !$monthlyPricing ? 'disabled opacity-50 cursor-not-allowed' : '' }} {{ $defaultPricing?->billing_cycle === 'monthly' ? 'selected' : '' }}"
<div class="flex justify-between items-center"> @if ($monthlyPricing) data-value="monthly"
<span>{{ format_price($basePrice * 6) }} / 6 tháng</span> data-months="1"
data-price="{{ floatval($monthlyPricing->price_sale ?: $monthlyPricing->price) }}" @endif>
<div class="flex items-center justify-between">
<span>
@if ($monthlyPricing)
{{ format_price($monthlyPricing->price_sale ?: $monthlyPricing->price) }}
/ 1 tháng
@else
<span class="text-gray-400">1 tháng - Liên hệ</span>
@endif
</span>
</div> </div>
</div> </div>
<div class="dropdown-option" data-value="12" data-months="12"
data-price="{{ $basePrice * 12 }}"> {{-- Semi-annually --}}
<div class="flex justify-between items-center"> <div class="dropdown-option {{ !$semiAnnuallyPricing ? 'disabled opacity-50 cursor-not-allowed' : '' }} {{ $defaultPricing?->billing_cycle === 'semi_annually' ? 'selected' : '' }}"
<span>{{ format_price($basePrice * 12) }} / 1 năm</span> @if ($semiAnnuallyPricing) data-value="semi_annually"
<span class="text-xs text-green-600 bg-green-100 px-2 py-1 rounded">Tiết data-months="6"
kiệm 10%</span> data-price="{{ floatval($semiAnnuallyPricing->price_sale ?: $semiAnnuallyPricing->price) }}" @endif>
<div class="flex items-center justify-between">
<span>
@if ($semiAnnuallyPricing)
{{ format_price($semiAnnuallyPricing->price_sale ?: $semiAnnuallyPricing->price) }}
/ 6 tháng
@else
<span class="text-gray-400">6 tháng - Liên hệ</span>
@endif
</span>
@if ($monthlyPricing && $semiAnnuallyPricing && $semiAnnuallyPricing->price < $monthlyPricing->price * 6)
<span class="rounded bg-green-100 px-2 py-1 text-xs text-green-600">Tiết
kiệm</span>
@endif
</div> </div>
</div> </div>
<div class="dropdown-option" data-value="24" data-months="24"
data-price="{{ $basePrice * 24 }}"> {{-- Annually --}}
<div class="flex justify-between items-center"> <div class="dropdown-option {{ !$annuallyPricing ? 'disabled opacity-50 cursor-not-allowed' : '' }} {{ $defaultPricing?->billing_cycle === 'annually' ? 'selected' : '' }}"
<span>{{ format_price($basePrice * 24) }} / 2 năm</span> @if ($annuallyPricing) data-value="annually"
<span class="text-xs text-green-600 bg-green-100 px-2 py-1 rounded">Tiết data-months="12"
kiệm 10%</span> data-price="{{ floatval($annuallyPricing->price_sale ?: $annuallyPricing->price) }}" @endif>
<div class="flex items-center justify-between">
<span>
@if ($annuallyPricing)
{{ format_price($annuallyPricing->price_sale ?: $annuallyPricing->price) }}
/ 1 năm
@else
<span class="text-gray-400">1 năm - Liên hệ</span>
@endif
</span>
@if ($semiAnnuallyPricing && $annuallyPricing && $annuallyPricing->price < $semiAnnuallyPricing->price * 2)
<span class="rounded bg-green-100 px-2 py-1 text-xs text-green-600">Tiết
kiệm</span>
@endif
</div> </div>
</div> </div>
<div class="dropdown-option" data-value="36" data-months="36"
data-price="{{ $basePrice * 36 }}"> {{-- Biennially --}}
<div class="flex justify-between items-center"> <div class="dropdown-option {{ !$bienniallyPricing ? 'disabled opacity-50 cursor-not-allowed' : '' }} {{ $defaultPricing?->billing_cycle === 'biennially' ? 'selected' : '' }}"
<span>{{ format_price($basePrice * 36) }} / 3 năm</span> @if ($bienniallyPricing) data-value="biennially"
<span class="text-xs text-green-600 bg-green-100 px-2 py-1 rounded">Tiết data-months="24"
kiệm 10%</span> data-price="{{ floatval($bienniallyPricing->price_sale ?: $bienniallyPricing->price) }}" @endif>
<div class="flex items-center justify-between">
<span>
@if ($bienniallyPricing)
{{ format_price($bienniallyPricing->price_sale ?: $bienniallyPricing->price) }}
/ 2 năm
@else
<span class="text-gray-400">2 năm - Liên hệ</span>
@endif
</span>
@if ($annuallyPricing && $bienniallyPricing && $bienniallyPricing->price < $annuallyPricing->price * 2)
<span class="rounded bg-green-100 px-2 py-1 text-xs text-green-600">Tiết
kiệm</span>
@endif
</div>
</div>
{{-- Triennially --}}
<div class="dropdown-option {{ !$trienniallyPricing ? 'disabled opacity-50 cursor-not-allowed' : '' }} {{ $defaultPricing?->billing_cycle === 'triennially' ? 'selected' : '' }}"
@if ($trienniallyPricing) data-value="triennially"
data-months="36"
data-price="{{ floatval($trienniallyPricing->price_sale ?: $trienniallyPricing->price) }}" @endif>
<div class="flex items-center justify-between">
<span>
@if ($trienniallyPricing)
{{ format_price($trienniallyPricing->price_sale ?: $trienniallyPricing->price) }}
/ 3 năm
@else
<span class="text-gray-400">3 năm - Liên hệ</span>
@endif
</span>
@if ($bienniallyPricing && $trienniallyPricing && $trienniallyPricing->price < $bienniallyPricing->price * 1.5)
<span class="rounded bg-green-100 px-2 py-1 text-xs text-green-600">Tiết
kiệm</span>
@endif
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<input type="hidden" name="months" class="hiddenMonths" value="6"> <input type="hidden" name="months" class="hiddenMonths" value="{{ $defaultMonths }}">
{{-- <input type="hidden" name="total_price" class="hiddenTotalPrice" <input type="hidden" name="billing_cycle" class="hiddenBillingCycle"
value="{{ $basePrice }}"> --}} value="{{ $defaultPricing?->billing_cycle ?? 'semi_annually' }}">
</div> </div>
<!-- Specifications --> <!-- Specifications -->
<div class="space-y-4 mb-6"> <div class="mb-6 space-y-4">
<ul class="space-y-1"> <ul class="space-y-1">
<!-- CPU --> <!-- CPU -->
<li class="flex items-center justify-center gap-3"> @if ($item->cpu)
<div class="w-10 h-10 bg-orange-100 rounded-full flex items-center justify-center"> <li class="flex items-center justify-center gap-3">
<i class="fas fa-microchip text-orange-600"></i> <div
</div> class="flex h-10 w-10 items-center justify-center rounded-full bg-orange-100">
<div class="flex-1"> <i class="fas fa-microchip text-orange-600"></i>
<span class="text-sm text-gray-600">CPU:</span> </div>
<span class="font-bold text-gray-800 ml-2 vm-cpu" <div class="flex-1">
data-template-id="{{ $item->template_id }}">{{ $item->cpu }} <span class="text-sm text-gray-600">CPU:</span>
vCore</span> <span class="ml-2 font-bold text-gray-800">{{ $item->cpu }} vCore</span>
</div> </div>
</li> </li>
@endif
<!-- RAM --> <!-- RAM -->
<li class="flex items-center justify-center gap-3"> @if ($item->ram)
<div class="w-10 h-10 bg-orange-100 rounded-full flex items-center justify-center"> <li class="flex items-center justify-center gap-3">
<i class="fas fa-memory text-orange-600"></i> <div
</div> class="flex h-10 w-10 items-center justify-center rounded-full bg-orange-100">
<div class="flex-1"> <i class="fas fa-memory text-orange-600"></i>
<span class="text-sm text-gray-600">RAM:</span> </div>
<span class="font-bold text-gray-800 ml-2 vm-ram" <div class="flex-1">
data-template-id="{{ $item->template_id }}">{{ $item->ram }} GB</span> <span class="text-sm text-gray-600">RAM:</span>
</div> <span class="ml-2 font-bold text-gray-800">{{ $item->ram }} GB</span>
</li> </div>
</li>
@endif
<!-- SSD --> <!-- SSD -->
<li class="flex items-center justify-center gap-3"> @if ($item->disk)
<div class="w-10 h-10 bg-orange-100 rounded-full flex items-center justify-center"> <li class="flex items-center justify-center gap-3">
<i class="fas fa-hdd text-orange-600"></i> <div
</div> class="flex h-10 w-10 items-center justify-center rounded-full bg-orange-100">
<div class="flex-1"> <i class="fas fa-hdd text-orange-600"></i>
<span class="text-sm text-gray-600">SSD:</span> </div>
<span class="font-bold text-gray-800 ml-2 vm-disk" <div class="flex-1">
data-template-id="{{ $item->template_id }}">{{ $item->disk }} GB</span> <span class="text-sm text-gray-600">SSD:</span>
</div> <span class="ml-2 font-bold text-gray-800">{{ $item->disk }} GB</span>
</li> </div>
</li>
@endif
<!-- GPU --> <!-- GPU -->
<li class="flex items-center justify-center gap-3"> @if ($item->gpu)
<div class="w-10 h-10 bg-orange-100 rounded-full flex items-center justify-center"> <li class="flex items-center justify-center gap-3">
<i class="fas fa-tv text-orange-600"></i> <div
</div> class="flex h-10 w-10 items-center justify-center rounded-full bg-orange-100">
<div class="flex-1"> <i class="fas fa-tv text-orange-600"></i>
<span class="text-sm text-gray-600">GPU:</span> </div>
<span class="font-bold text-gray-800 ml-2">{{ $item->gpu }} GB</span> <div class="flex-1">
</div> <span class="text-sm text-gray-600">GPU:</span>
</li> <span class="ml-2 font-bold text-gray-800">{{ $item->gpu }} GB</span>
</div>
</li>
@endif
<!-- Bandwidth --> <!-- Bandwidth -->
<li class="flex items-center justify-center gap-3"> @if ($item->bandwidth)
<div class="w-10 h-10 bg-orange-100 rounded-full flex items-center justify-center"> <li class="flex items-center justify-center gap-3">
<i class="fas fa-tachometer-alt text-orange-600"></i> <div
</div> class="flex h-10 w-10 items-center justify-center rounded-full bg-orange-100">
<div class="flex-1"> <i class="fas fa-tachometer-alt text-orange-600"></i>
<span class="text-sm text-gray-600">Băng thông:</span> </div>
<span class="font-bold text-gray-800 ml-2">{{ $item->bandwidth }} MB</span> <div class="flex-1">
</div> <span class="text-sm text-gray-600">Băng thông:</span>
</li> <span class="ml-2 font-bold text-gray-800">{{ $item->bandwidth }} MB</span>
</div>
</li>
@endif
<!-- OS --> <!-- OS -->
<li class="flex items-center justify-center gap-3"> @if ($item->osVersions && $item->osVersions->isNotEmpty())
<div class="w-10 h-10 bg-orange-100 rounded-full flex items-center justify-center"> <li class="flex items-center justify-center gap-3">
<i class="fab fa-linux text-orange-600 os-icon" <div
data-template-id="{{ $item->template_id }}"></i> class="flex h-10 w-10 items-center justify-center rounded-full bg-orange-100">
</div> <i class="fab fa-linux os-icon text-orange-600"></i>
<div class="flex-1"> </div>
<span class="text-sm text-gray-600">Hệ điều hành:</span> <div class="flex-1">
<span class="font-bold text-gray-800 ml-2 guest-os" <span class="text-sm text-gray-600">Hệ điều hành:</span>
data-template-id="{{ $item->template_id }}"> <span class="guest-os ml-2 font-bold text-gray-800">
@if ($item->type_id == 4 && $item->operatingSystems->isNotEmpty()) {{ $item->osVersions->pluck('name')->unique()->implode(', ') }}
{{ $item->operatingSystems->pluck('osGroup.name')->filter()->unique()->implode(', ') }} </span>
@else </div>
{{ $item->os ?? 'Linux' }} </li>
@endif @endif
</span>
</div>
</li>
</ul> </ul>
</div> </div>
<!-- Action Button --> <!-- Action Button -->
<flux:button variant="primary" color="orange" type="submit" class="w-full" <flux:button variant="primary" color="orange" type="submit" class="w-full" icon="shopping-bag">
icon="shopping-bag">
Đăng ký ngay</flux:button> Đăng ký ngay</flux:button>
</div> </div>
</div> </div>
</form> </form>
@empty @empty
<div class="col-span-full"> <div class="col-span-full">
<div class="text-center py-16"> <div class="py-16 text-center">
<div class="w-24 h-24 bg-orange-100 rounded-full flex items-center justify-center mx-auto mb-6"> <div class="mx-auto mb-6 flex h-24 w-24 items-center justify-center rounded-full bg-orange-100">
<i class="fas fa-box-open text-4xl text-orange-500"></i> <i class="fas fa-box-open text-4xl text-orange-500"></i>
</div> </div>
<h3 class="text-2xl font-bold text-gray-800 mb-2">Không có sản phẩm</h3> <h3 class="mb-2 text-2xl font-bold text-gray-800">Không có sản phẩm</h3>
<p class="text-gray-600">Danh mục này hiện chưa có sản phẩm nào.</p> <p class="text-gray-600">Danh mục này hiện chưa có sản phẩm nào.</p>
</div> </div>
</div> </div>
@@ -188,15 +311,15 @@
@endforelse @endforelse
</div> </div>
@else @else
<div class="min-h-screen flex items-center justify-center"> <div class="flex min-h-screen items-center justify-center">
<div class="text-center"> <div class="text-center">
<div class="w-32 h-32 bg-orange-100 rounded-full flex items-center justify-center mx-auto mb-6"> <div class="mx-auto mb-6 flex h-32 w-32 items-center justify-center rounded-full bg-orange-100">
<i class="fas fa-exclamation-triangle text-5xl text-orange-500"></i> <i class="fas fa-exclamation-triangle text-5xl text-orange-500"></i>
</div> </div>
<h2 class="text-3xl font-bold text-gray-800 mb-4">Không tìm thấy</h2> <h2 class="mb-4 text-3xl font-bold text-gray-800">Không tìm thấy</h2>
<p class="text-gray-600 text-lg">Danh mục này không tồn tại hoặc đã bị xóa.</p> <p class="text-lg text-gray-600">Danh mục này không tồn tại hoặc đã bị xóa.</p>
<a href="{{ route('home') }}" <a href="{{ route('home') }}"
class="inline-block mt-6 bg-orange-500 hover:bg-orange-600 text-white font-bold py-3 px-6 rounded-xl transition-colors"> class="mt-6 inline-block rounded-xl bg-orange-500 px-6 py-3 font-bold text-white transition-colors hover:bg-orange-600">
Về trang chủ Về trang chủ
</a> </a>
</div> </div>
+1 -1
View File
@@ -12,7 +12,7 @@ use App\Http\Controllers\frontend\ShopController;
use App\Http\Controllers\frontend\TicketController; use App\Http\Controllers\frontend\TicketController;
use App\Http\Controllers\frontend\Order\OrderController as ClientOrderController; use App\Http\Controllers\frontend\Order\OrderController as ClientOrderController;
use App\Http\Controllers\frontend\UserController as ClientController; use App\Http\Controllers\frontend\UserController as ClientController;
use Illuminate\Support\Facades\Route;
Route::middleware("IsLoginCheck") Route::middleware("IsLoginCheck")
->controller(ClientController::class) ->controller(ClientController::class)