285 lines
10 KiB
PHP
285 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Order;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\OrderRequest;
|
|
use App\Http\Requests\UpdateOrderRequest;
|
|
use App\Http\Resources\Api\Admin\OrderResource;
|
|
use App\Jobs\Hosting\CreateHostingJobs;
|
|
use App\Jobs\VirtualMachine\Create\CreateVMJobs;
|
|
use App\Jobs\VirtualMachine\Upgrade\UpgradeVirtualMachine;
|
|
use App\Models\Order;
|
|
use App\Repositories\Addon\AddonRepositoryInterface;
|
|
use App\Repositories\Invoice\InvoiceRepositoryInterface;
|
|
use App\Repositories\InvoiceItem\InvoiceItemRepositoryInterface;
|
|
use App\Repositories\Module\Hosting\HostingRepositoryInterface as ModuleHostingRepositoryInterface;
|
|
use App\Repositories\Module\HostingServer\HostingServerRepositoryInterface;
|
|
use App\Repositories\Module\IpAddress\IpAddressRepositoryInterface;
|
|
use App\Repositories\Module\Vmware\VmwareRepositoryInterface;
|
|
use App\Repositories\Order\OrderRepositoryInterface;
|
|
use App\Repositories\Os\OsRepositoryInterface;
|
|
use App\Services\OrderService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class OrderController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly OrderRepositoryInterface $orderRepository,
|
|
private readonly InvoiceRepositoryInterface $invoiceRepository,
|
|
private readonly InvoiceItemRepositoryInterface $invoiceItemRepository,
|
|
private readonly ModuleHostingRepositoryInterface $moduleHostingRepository,
|
|
private readonly VmwareRepositoryInterface $moduleVmwareRepository,
|
|
private readonly HostingServerRepositoryInterface $moduleHostingServerRepository,
|
|
private readonly OsRepositoryInterface $osRepository,
|
|
private readonly IpAddressRepositoryInterface $ipAddressRepository,
|
|
private readonly AddonRepositoryInterface $addonRepository,
|
|
private readonly OrderService $orderService,
|
|
) {}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) $request->get('per_page', 15), 100);
|
|
|
|
$data = $this->orderRepository->filter(
|
|
$request->all(),
|
|
['payment', 'customer', 'invoice'],
|
|
[],
|
|
['*'],
|
|
true
|
|
)->paginate($perPage);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => [
|
|
'items' => OrderResource::collection($data->items())->resolve(),
|
|
'total' => $data->total(),
|
|
'per_page' => $data->perPage(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function store(OrderRequest $request): JsonResponse
|
|
{
|
|
try {
|
|
$order = $this->orderService->createOrder($request->validated());
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Order created successfully',
|
|
'data' => new OrderResource($order),
|
|
], 201);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return response()->json(['error' => true, 'message' => $e->getMessage()], 422);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function show(string $order_num): JsonResponse
|
|
{
|
|
$order = $this->orderRepository->findByOrderNum($order_num);
|
|
|
|
if (!$order) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find order by ID #" . $order_num,
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => new OrderResource($order),
|
|
]);
|
|
}
|
|
|
|
public function update(int $id, UpdateOrderRequest $request): JsonResponse
|
|
{
|
|
$order = $this->orderRepository->findWithRelationships($id, ['invoice', 'orderItem']);
|
|
|
|
if (!$order) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find order by ID #" . $id,
|
|
], 404);
|
|
}
|
|
|
|
$status = match ($request->action) {
|
|
"accept" => 1,
|
|
"cancel" => 2,
|
|
default => 0
|
|
};
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$this->orderRepository->update($id, ['status' => $status]);
|
|
|
|
if ($status === 1 && $request->module == 1) {
|
|
$jobs = [];
|
|
$addon = [];
|
|
$product = 0;
|
|
|
|
foreach ($order->orderItem as $key => $item) {
|
|
switch ((int) $item->type_id) {
|
|
case 1:
|
|
$itemData = $this->addonRepository->find($item->relid);
|
|
if ($itemData) {
|
|
$price = match ((int) $item->billing) {
|
|
2 => $item->amount / 1,
|
|
3 => $item->amount / 3,
|
|
4 => $item->amount / 6,
|
|
5 => $item->amount / 12,
|
|
default => 0
|
|
};
|
|
$addon[strtolower($itemData->name)] = (int) $price / $itemData->price;
|
|
$product = $item->parent_id;
|
|
}
|
|
break;
|
|
case 3:
|
|
$hostingServer = $this->moduleHostingServerRepository->get_active();
|
|
if ($hostingServer) {
|
|
$this->moduleHostingRepository->update($item->relid, ['module_server_id' => $hostingServer->first()->id]);
|
|
$jobs[] = new CreateHostingJobs($item->relid);
|
|
}
|
|
break;
|
|
case 4:
|
|
$product = $this->moduleVmwareRepository->find($item->relid);
|
|
|
|
if ($product && $product->os_id) {
|
|
$os = $this->osRepository->findWithRelationships($product->os_id, ['osGroup']);
|
|
if (!$os || !$os->osGroup->isNotEmpty()) break;
|
|
|
|
$ip_address = $this->ipAddressRepository->filter([
|
|
'module_host_id' => $os->osGroup->module_host_id,
|
|
'status' => 0,
|
|
]);
|
|
|
|
if (count($ip_address) > 0) {
|
|
$this->moduleVmwareRepository->update($item->relid, [
|
|
'module_ipaddress_id' => $ip_address->first()->id,
|
|
]);
|
|
$this->ipAddressRepository->update($ip_address->first()->id, ['status' => 1]);
|
|
$jobs[] = new CreateVMJobs($product->id);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (count($order->orderItem) === count($addon)) {
|
|
dispatch(new UpgradeVirtualMachine($product, $addon, $order->id))->delay(now()->addSeconds(10));
|
|
}
|
|
|
|
foreach ($jobs as $item) {
|
|
dispatch($item)->delay(now()->addSeconds(10));
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return response()->json(['error' => false, 'message' => 'Order updated successfully']);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function destroy(int $id): JsonResponse
|
|
{
|
|
$order = Order::with('Invoice', 'Invoice.InvoiceItem')->find($id);
|
|
|
|
if (!$order) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find order by ID #" . $id,
|
|
], 404);
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
if ($order->Invoice) {
|
|
$order->Invoice->delete();
|
|
|
|
if ($order->Invoice->InvoiceItem->count()) {
|
|
foreach ($order->Invoice->InvoiceItem->all() as $item) {
|
|
$item->delete();
|
|
}
|
|
}
|
|
}
|
|
|
|
$order->delete();
|
|
|
|
DB::commit();
|
|
|
|
return response()->json(['error' => false, 'message' => 'Order deleted successfully']);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function accept(int $id): JsonResponse
|
|
{
|
|
$order = $this->orderRepository->findWithRelationships($id, ['invoice', 'orderItem']);
|
|
|
|
if (!$order) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find order by ID #" . $id,
|
|
], 404);
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$this->orderRepository->update($id, ['status' => 1]);
|
|
DB::commit();
|
|
|
|
return response()->json(['error' => false, 'message' => 'Order accepted successfully']);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function cancel(int $id): JsonResponse
|
|
{
|
|
$order = $this->orderRepository->find($id);
|
|
|
|
if (!$order) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => "Can't find order by ID #" . $id,
|
|
], 404);
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$this->orderRepository->update($id, ['status' => 2]);
|
|
DB::commit();
|
|
|
|
return response()->json(['error' => false, 'message' => 'Order cancelled successfully']);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return response()->json(['error' => true, 'message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function uploadProof(Request $request): JsonResponse
|
|
{
|
|
$request->validate(['file' => 'required|image|max:5120']);
|
|
|
|
$path = $request->file('file')->store('payment-proofs', 'public');
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'url' => Storage::disk('public')->url($path),
|
|
'path' => $path,
|
|
]);
|
|
}
|
|
}
|