snipe-it/app/Actions/CheckoutRequests/CreateCheckoutRequest.php

111 lines
3.9 KiB
PHP
Raw Normal View History

2024-10-16 15:23:22 -07:00
<?php
namespace App\Actions\CheckoutRequests;
2024-10-16 16:48:15 -07:00
use App\Helpers\Helper;
2024-10-16 15:23:22 -07:00
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Company;
use App\Models\Setting;
use App\Models\User;
use App\Notifications\RequestAssetCancelation;
use App\Notifications\RequestAssetNotification;
2024-10-16 16:48:15 -07:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
2024-10-16 15:23:22 -07:00
use Lorisleiva\Actions\Concerns\AsAction;
2024-10-16 22:02:15 -07:00
use Lorisleiva\Actions\Concerns\AsController;
use Lorisleiva\Actions\Concerns\WithAttributes;
2024-10-16 15:23:22 -07:00
class CreateCheckoutRequest
{
use AsAction;
2024-10-16 22:02:15 -07:00
use WithAttributes;
2024-10-16 15:23:22 -07:00
2024-10-16 22:02:15 -07:00
public string $status = '';
2024-10-16 16:48:15 -07:00
2024-10-16 15:23:22 -07:00
public function handle($assetId)
{
2024-10-16 22:02:15 -07:00
dump($assetId);
2024-10-16 15:23:22 -07:00
$user = auth()->user();
// Check if the asset exists and is requestable
if (is_null($asset = Asset::RequestableAssets()->find($assetId))) {
2024-10-17 10:45:49 -07:00
return $this->status = 'doesNotExist';
2024-10-16 15:23:22 -07:00
}
if (!Company::isCurrentUserHasAccess($asset)) {
2024-10-17 10:45:49 -07:00
return $this->status = 'accessDenied';
2024-10-16 15:23:22 -07:00
}
$data['item'] = $asset;
$data['target'] = auth()->user();
$data['item_quantity'] = 1;
$settings = Setting::getSettings();
$logaction = new Actionlog();
$logaction->item_id = $data['asset_id'] = $asset->id;
$logaction->item_type = $data['item_type'] = Asset::class;
$logaction->created_at = $data['requested_date'] = date('Y-m-d H:i:s');
if ($user->location_id) {
$logaction->location_id = $user->location_id;
}
$logaction->target_id = $data['user_id'] = auth()->id();
$logaction->target_type = User::class;
// If it's already requested, cancel the request.
if ($asset->isRequestedBy(auth()->user())) {
$asset->cancelRequest();
$asset->decrement('requests_counter', 1);
$logaction->logaction('request canceled');
try {
$settings->notify(new RequestAssetCancelation($data));
} catch (\Exception $e) {
2024-10-16 22:02:15 -07:00
\Log::warning($e);
2024-10-16 15:23:22 -07:00
}
2024-10-17 10:45:49 -07:00
return $this->status = 'cancelled';
2024-10-16 15:23:22 -07:00
}
$logaction->logaction('requested');
$asset->request();
$asset->increment('requests_counter', 1);
try {
$settings->notify(new RequestAssetNotification($data));
} catch (\Exception $e) {
2024-10-16 16:48:15 -07:00
\Log::warning($e);
2024-10-16 15:23:22 -07:00
}
2024-10-16 22:02:15 -07:00
dump('handle end');
2024-10-17 10:45:49 -07:00
return $this->status = 'success';
2024-10-16 22:02:15 -07:00
}
2024-10-16 15:23:22 -07:00
2024-10-16 22:02:15 -07:00
public function asController($assetId)
{
dump('asController');
$asset = $this->handle($assetId);
//return $asset;
2024-10-16 16:48:15 -07:00
}
2024-10-17 10:45:49 -07:00
//public function jsonResponse(): JsonResponse
//{
// dump('json');
// return match ($this->status) {
// 'doesNotExist' => response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist_or_not_requestable'))),
// 'accessDenied' => response()->json(Helper::formatStandardApiResponse('error', null, trans('general.insufficient_permissions'))),
// default => response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.request_successfully_created'))),
// };
//}
2024-10-16 16:48:15 -07:00
2024-10-17 10:45:49 -07:00
//public function htmlResponse(): RedirectResponse
//{
// dump('redirects');
// return match ($this->status) {
// dump('redirects'),
// 'doesNotExist' => redirect()->route('requestable-assets')->with('error', trans('admin/hardware/message.does_not_exist_or_not_requestable')),
// 'accessDenied' => redirect()->route('requestable-assets')->with('error', trans('general.insufficient_permissions')),
// 'cancelled' => redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.canceled')),
// default => redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success')),
// };
//}
2024-10-16 15:23:22 -07:00
}