2024-10-16 15:23:22 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Actions\CheckoutRequests;
|
|
|
|
|
2024-10-21 15:31:26 -07:00
|
|
|
use App\Exceptions\AssetNotRequestable;
|
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\RequestAssetNotification;
|
2024-10-21 15:31:26 -07:00
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
2024-10-22 23:41:27 -07:00
|
|
|
use Log;
|
2024-10-16 15:23:22 -07:00
|
|
|
|
2024-12-03 15:51:12 -08:00
|
|
|
class CreateCheckoutRequestAction
|
2024-10-16 15:23:22 -07:00
|
|
|
{
|
2024-10-21 15:31:26 -07:00
|
|
|
/**
|
|
|
|
* @throws AssetNotRequestable
|
|
|
|
* @throws AuthorizationException
|
|
|
|
*/
|
|
|
|
public static function run(Asset $asset, User $user): string
|
2024-10-16 15:23:22 -07:00
|
|
|
{
|
2024-10-21 15:31:26 -07:00
|
|
|
if (is_null(Asset::RequestableAssets()->find($asset->id))) {
|
|
|
|
throw new AssetNotRequestable($asset);
|
2024-10-16 15:23:22 -07:00
|
|
|
}
|
|
|
|
if (!Company::isCurrentUserHasAccess($asset)) {
|
2024-10-21 15:31:26 -07:00
|
|
|
throw new AuthorizationException();
|
2024-10-16 15:23:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$data['item'] = $asset;
|
2024-10-21 15:31:26 -07:00
|
|
|
$data['target'] = $user;
|
2024-10-16 15:23:22 -07:00
|
|
|
$data['item_quantity'] = 1;
|
|
|
|
$settings = Setting::getSettings();
|
|
|
|
|
2024-10-22 18:54:20 -07:00
|
|
|
$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');
|
|
|
|
$logaction->target_id = $data['user_id'] = auth()->id();
|
|
|
|
$logaction->target_type = User::class;
|
|
|
|
$logaction->location_id = $user->location_id ?? null;
|
2024-10-16 15:23:22 -07:00
|
|
|
$logaction->logaction('requested');
|
2024-10-22 18:48:27 -07:00
|
|
|
|
2024-10-16 15:23:22 -07:00
|
|
|
$asset->request();
|
|
|
|
$asset->increment('requests_counter', 1);
|
|
|
|
try {
|
|
|
|
$settings->notify(new RequestAssetNotification($data));
|
|
|
|
} catch (\Exception $e) {
|
2024-10-22 23:41:27 -07:00
|
|
|
Log::warning($e);
|
2024-10-16 15:23:22 -07:00
|
|
|
}
|
2024-10-17 10:45:49 -07:00
|
|
|
|
2024-10-22 23:41:27 -07:00
|
|
|
return true;
|
2024-10-22 13:09:35 -07:00
|
|
|
}
|
2024-10-21 15:31:26 -07:00
|
|
|
}
|