mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
hm, lots to think about 🤔
This commit is contained in:
parent
1fd945c2d8
commit
95a32864cf
19
app/Actions/BaseAction.php
Normal file
19
app/Actions/BaseAction.php
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Facade;
|
||||||
|
|
||||||
|
abstract class BaseAction extends Facade
|
||||||
|
{
|
||||||
|
public static function __callStatic($method, $args)
|
||||||
|
{
|
||||||
|
return (new static)->$method(...$args);
|
||||||
|
}
|
||||||
|
|
||||||
|
//abstract static function run($parameters = null)
|
||||||
|
//{
|
||||||
|
// return call_user_func($this->run(...$parameters));
|
||||||
|
//}
|
||||||
|
|
||||||
|
}
|
75
app/Actions/CheckoutRequests/CreateCheckoutRequestNew.php
Normal file
75
app/Actions/CheckoutRequests/CreateCheckoutRequestNew.php
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Actions\CheckoutRequests;
|
||||||
|
|
||||||
|
use App\Actions\BaseAction;
|
||||||
|
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;
|
||||||
|
|
||||||
|
class CreateCheckoutRequestNew extends BaseAction
|
||||||
|
{
|
||||||
|
//public string $status = '';
|
||||||
|
|
||||||
|
static function run($assetId): string
|
||||||
|
{
|
||||||
|
dump($assetId);
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
|
// Check if the asset exists and is requestable
|
||||||
|
if (is_null($asset = Asset::RequestableAssets()->find($assetId))) {
|
||||||
|
return $status = 'doesNotExist';
|
||||||
|
}
|
||||||
|
if (!Company::isCurrentUserHasAccess($asset)) {
|
||||||
|
return $status = 'accessDenied';
|
||||||
|
}
|
||||||
|
|
||||||
|
$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) {
|
||||||
|
\Log::warning($e);
|
||||||
|
}
|
||||||
|
return $status = 'cancelled';
|
||||||
|
}
|
||||||
|
|
||||||
|
$logaction->logaction('requested');
|
||||||
|
$asset->request();
|
||||||
|
$asset->increment('requests_counter', 1);
|
||||||
|
try {
|
||||||
|
$settings->notify(new RequestAssetNotification($data));
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
\Log::warning($e);
|
||||||
|
}
|
||||||
|
dump('handle end');
|
||||||
|
|
||||||
|
return $status = 'success';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Actions\CheckoutRequests\CreateCheckoutRequest;
|
use App\Actions\CheckoutRequests\CreateCheckoutRequest;
|
||||||
|
use App\Actions\CheckoutRequests\CreateCheckoutRequestNew;
|
||||||
use App\Models\Actionlog;
|
use App\Models\Actionlog;
|
||||||
use App\Models\Asset;
|
use App\Models\Asset;
|
||||||
use App\Models\AssetModel;
|
use App\Models\AssetModel;
|
||||||
|
@ -143,9 +144,10 @@ class ViewAssetsController extends Controller
|
||||||
* Process a specific requested asset
|
* Process a specific requested asset
|
||||||
* @param null $assetId
|
* @param null $assetId
|
||||||
*/
|
*/
|
||||||
public function getRequestAsset($assetId = null): RedirectResponse
|
public function getRequestAsset(CreateCheckoutRequestNew $checkoutRequestNew, $assetId = null): RedirectResponse
|
||||||
{
|
{
|
||||||
$status = CreateCheckoutRequest::run($assetId);
|
$status = CreateCheckoutRequestNew::run($assetId);
|
||||||
|
//$status = $checkoutRequestNew->run($assetId);
|
||||||
|
|
||||||
return match ($status) {
|
return match ($status) {
|
||||||
'doesNotExist' => redirect()->route('requestable-assets')->with('error', trans('admin/hardware/message.does_not_exist_or_not_requestable')),
|
'doesNotExist' => redirect()->route('requestable-assets')->with('error', trans('admin/hardware/message.does_not_exist_or_not_requestable')),
|
||||||
|
|
Loading…
Reference in a new issue