150 lines
4.6 KiB
PHP
150 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Services;
|
|
|
|
use App\Models\Addon;
|
|
use App\Models\Cart as CartModel;
|
|
use App\Models\Hosting;
|
|
use App\Models\OsGroup;
|
|
use App\Models\VPS;
|
|
|
|
class Cart
|
|
{
|
|
|
|
public function addToCart($user_id = 0, $price = 0, $product = null): static
|
|
{
|
|
$cart = session()->get('cart') ?? [];
|
|
|
|
$userKey = "user_$user_id";
|
|
|
|
if (!isset($cart[$userKey])) {
|
|
$cart[$userKey] = [];
|
|
}
|
|
|
|
foreach ($cart[$userKey] as $item) {
|
|
if ($item['product'] == $product->id && $item['product_type'] == $product->type_id) {
|
|
return $this;
|
|
}
|
|
}
|
|
|
|
$cart[$userKey][] = [
|
|
'product' => $product->id,
|
|
'product_type' => $product->type_id,
|
|
'price' => $price
|
|
];
|
|
|
|
session()->put('cart', $cart);
|
|
|
|
return $this;
|
|
}
|
|
|
|
|
|
public function cartBK($user_id = 0): ?array
|
|
{
|
|
if (session()->get('cart')) {
|
|
// $cart = session()->get('cart')["user_$user_id"];
|
|
$cart = session()->get("cart.user_$user_id", []);
|
|
|
|
return array_map(function ($item) {
|
|
if ($item['product_type'] === 4) {
|
|
$product = VPS::find($item['product']);
|
|
$addOn = Addon::where('type_id', 1)->get();
|
|
$group = OsGroup::with('Os')->where('module_host_id', $product->ModuleHost_id)->get();
|
|
$item['product'] = $product;
|
|
$item['addon'] = $addOn;
|
|
$item['os_group'] = $group;
|
|
}
|
|
if ($item['product_type'] === 3) {
|
|
$product = Hosting::find($item['product']);
|
|
$item['product'] = $product;
|
|
}
|
|
return $item;
|
|
}, $cart);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function cart($user_id = 0): ?array
|
|
{
|
|
$cartItems = CartModel::where('customer_id', $user_id)->get();
|
|
|
|
if ($cartItems->isEmpty()) {
|
|
return null;
|
|
}
|
|
|
|
return $cartItems->map(function ($item) {
|
|
$data = [
|
|
'id' => $item->id,
|
|
'product_type' => $item->type_product,
|
|
'quantity' => $item->quantity,
|
|
'price' => $item->price,
|
|
'months' => $item->months,
|
|
];
|
|
|
|
if ($item->type_product === 4) {
|
|
$product = VPS::with('operatingSystems.osGroup')->find($item->product_id);
|
|
$addons = Addon::where('type_id', 1)->get();
|
|
|
|
// Get OS groups that are specifically assigned to this VPS product
|
|
$osGroups = collect();
|
|
if ($product && $product->operatingSystems->isNotEmpty()) {
|
|
$osGroupIds = $product->operatingSystems->pluck('osGroup.id')->filter()->unique();
|
|
$osGroups = OsGroup::with(['os' => function($query) use ($product) {
|
|
// Only get OS versions that are assigned to this specific VPS product
|
|
$query->whereHas('vpsProducts', function($q) use ($product) {
|
|
$q->where('product_id', $product->id)
|
|
->where('product_type', 4);
|
|
});
|
|
}])
|
|
->whereIn('id', $osGroupIds)
|
|
->get();
|
|
}
|
|
|
|
|
|
$data['product'] = $product;
|
|
$data['addon'] = $addons;
|
|
$data['os_group'] = $osGroups;
|
|
} elseif ($item->type_product === 3) {
|
|
$product = Hosting::find($item->product_id);
|
|
$addons = Addon::where('type_id', 1)->get();
|
|
$osGroups = OsGroup::with('os')
|
|
->where('module_host_id', $product->module_host_id)
|
|
->get();
|
|
$data['product'] = $product;
|
|
$data['addon'] = $addons;
|
|
$data['os_group'] = $osGroups;
|
|
}
|
|
|
|
return $data;
|
|
})->toArray();
|
|
}
|
|
|
|
|
|
public function removeCartItem($user_id = 0, $key = 0): bool
|
|
{
|
|
if (session()->get('cart')) {
|
|
$cart = session()->get('cart');
|
|
|
|
unset($cart["user_$user_id"][$key]);
|
|
|
|
session()->forget('coupon');
|
|
|
|
session()->put('cart', $cart);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public function getItem($user_id = 0, $key = 0)
|
|
{
|
|
if (session()->get('cart')) {
|
|
$cart = session()->get('cart')["user_$user_id"];
|
|
|
|
if ($cart[$key]) {
|
|
return $cart[$key];
|
|
}
|
|
return null;
|
|
}
|
|
return null;
|
|
}
|
|
}
|