2019-02-19 11:19:00 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Events\CheckoutableCheckedOut;
|
2019-02-19 11:19:00 -08:00
|
|
|
use App\Models\PredefinedKit;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Http\Request;
|
2019-02-19 11:19:00 -08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
2019-02-23 11:44:03 -08:00
|
|
|
/**
|
|
|
|
* Class incapsulates checkout logic for reuse in different controllers
|
|
|
|
* @author [D. Minaev.] [<dmitriy.minaev.v@gmail.com>]
|
|
|
|
*/
|
2019-02-19 11:19:00 -08:00
|
|
|
class PredefinedKitCheckoutService
|
|
|
|
{
|
|
|
|
use AuthorizesRequests;
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2019-02-19 11:19:00 -08:00
|
|
|
/**
|
2019-02-23 11:44:03 -08:00
|
|
|
* @param Request $request, this function works with fields: checkout_at, expected_checkin, note
|
|
|
|
* @param PredefinedKit $kit kit for checkout
|
|
|
|
* @param User $user checkout target
|
|
|
|
* @return array Empty array if all ok, else [string_error1, string_error2...]
|
2019-02-19 11:19:00 -08:00
|
|
|
*/
|
2019-02-27 14:37:58 -08:00
|
|
|
public function checkout(Request $request, PredefinedKit $kit, User $user)
|
|
|
|
{
|
2019-02-19 11:19:00 -08:00
|
|
|
try {
|
|
|
|
|
|
|
|
// Check if the user exists
|
2019-02-27 14:37:58 -08:00
|
|
|
if (is_null($user)) {
|
2019-05-23 19:39:30 -07:00
|
|
|
return ['errors' => trans('admin/users/message.user_not_found')];
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
|
|
|
|
$assets_to_add = $this->getAssetsToAdd($kit, $user, $errors);
|
2019-02-27 14:37:58 -08:00
|
|
|
$license_seats_to_add = $this->getLicenseSeatsToAdd($kit, $errors);
|
|
|
|
$consumables_to_add = $this->getConsumablesToAdd($kit, $errors);
|
|
|
|
$accessories_to_add = $this->getAccessoriesToAdd($kit, $errors);
|
2019-02-19 11:19:00 -08:00
|
|
|
|
2019-02-27 14:37:58 -08:00
|
|
|
if (count($errors) > 0) {
|
2019-05-23 19:39:30 -07:00
|
|
|
return ['errors' => $errors];
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$checkout_at = date('Y-m-d H:i:s');
|
|
|
|
if (($request->filled('checkout_at')) && ($request->get('checkout_at') != date('Y-m-d'))) {
|
2019-02-19 11:19:00 -08:00
|
|
|
$checkout_at = $request->get('checkout_at');
|
|
|
|
}
|
|
|
|
|
|
|
|
$expected_checkin = '';
|
|
|
|
if ($request->filled('expected_checkin')) {
|
|
|
|
$expected_checkin = $request->get('expected_checkin');
|
|
|
|
}
|
|
|
|
|
|
|
|
$admin = Auth::user();
|
|
|
|
|
|
|
|
$note = e($request->get('note'));
|
|
|
|
|
2019-02-27 14:37:58 -08:00
|
|
|
$errors = $this->saveToDb($user, $admin, $checkout_at, $expected_checkin, $errors, $assets_to_add, $license_seats_to_add, $consumables_to_add, $accessories_to_add, $note);
|
2019-05-23 19:39:30 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return ['errors' => $errors, 'assets' => $assets_to_add, 'accessories' => $accessories_to_add, 'consumables' => $consumables_to_add];
|
2019-02-19 11:19:00 -08:00
|
|
|
} catch (ModelNotFoundException $e) {
|
2019-05-23 19:39:30 -07:00
|
|
|
return ['errors' => [$e->getMessage()]];
|
2019-02-19 11:19:00 -08:00
|
|
|
} catch (CheckoutNotAllowed $e) {
|
2019-05-23 19:39:30 -07:00
|
|
|
return ['errors' => [$e->getMessage()]];
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:37:58 -08:00
|
|
|
protected function getAssetsToAdd($kit, $user, &$errors)
|
|
|
|
{
|
2019-02-19 11:19:00 -08:00
|
|
|
$models = $kit->models()
|
2019-02-27 14:37:58 -08:00
|
|
|
->with(['assets' => function ($hasMany) {
|
|
|
|
$hasMany->RTD();
|
|
|
|
}])
|
|
|
|
->get();
|
2019-02-19 11:19:00 -08:00
|
|
|
$assets_to_add = [];
|
2019-02-27 14:37:58 -08:00
|
|
|
foreach ($models as $model) {
|
2019-02-19 11:19:00 -08:00
|
|
|
$assets = $model->assets;
|
|
|
|
$quantity = $model->pivot->quantity;
|
2019-02-27 14:37:58 -08:00
|
|
|
foreach ($assets as $asset) {
|
|
|
|
if (
|
|
|
|
$asset->availableForCheckout()
|
2021-06-10 13:15:52 -07:00
|
|
|
&& ! $asset->is($user)
|
2019-02-27 14:37:58 -08:00
|
|
|
) {
|
2019-02-19 11:19:00 -08:00
|
|
|
$this->authorize('checkout', $asset);
|
|
|
|
$quantity -= 1;
|
2019-02-27 14:37:58 -08:00
|
|
|
$assets_to_add[] = $asset;
|
|
|
|
if ($quantity <= 0) {
|
2019-02-19 11:19:00 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 14:37:58 -08:00
|
|
|
if ($quantity > 0) {
|
2019-03-13 12:15:48 -07:00
|
|
|
$errors[] = trans('admin/kits/general.none_models', ['model'=> $model->name, 'qty' => $model->pivot->quantity]);
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $assets_to_add;
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:37:58 -08:00
|
|
|
protected function getLicenseSeatsToAdd($kit, &$errors)
|
|
|
|
{
|
2019-02-19 11:19:00 -08:00
|
|
|
$seats_to_add = [];
|
|
|
|
$licenses = $kit->licenses()
|
2019-02-27 14:37:58 -08:00
|
|
|
->with('freeSeats')
|
|
|
|
->get();
|
|
|
|
foreach ($licenses as $license) {
|
2019-02-19 11:19:00 -08:00
|
|
|
$quantity = $license->pivot->quantity;
|
2019-02-27 14:37:58 -08:00
|
|
|
if ($quantity > count($license->freeSeats)) {
|
2019-03-13 12:15:48 -07:00
|
|
|
$errors[] = trans('admin/kits/general.none_licenses', ['license'=> $license->name, 'qty' => $license->pivot->quantity]);
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
2019-02-27 14:37:58 -08:00
|
|
|
for ($i = 0; $i < $quantity; $i++) {
|
|
|
|
$seats_to_add[] = $license->freeSeats[$i];
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2019-02-19 11:19:00 -08:00
|
|
|
return $seats_to_add;
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:37:58 -08:00
|
|
|
protected function getConsumablesToAdd($kit, &$errors)
|
|
|
|
{
|
2019-02-19 11:19:00 -08:00
|
|
|
$consumables = $kit->consumables()->with('users')->get();
|
2019-02-27 14:37:58 -08:00
|
|
|
foreach ($consumables as $consumable) {
|
|
|
|
if ($consumable->numRemaining() < $consumable->pivot->quantity) {
|
2019-03-13 12:15:48 -07:00
|
|
|
$errors[] = trans('admin/kits/general.none_consumables', ['consumable'=> $consumable->name, 'qty' => $consumable->pivot->quantity]);
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2019-02-19 11:19:00 -08:00
|
|
|
return $consumables;
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:37:58 -08:00
|
|
|
protected function getAccessoriesToAdd($kit, &$errors)
|
|
|
|
{
|
2019-02-19 11:19:00 -08:00
|
|
|
$accessories = $kit->accessories()->with('users')->get();
|
2019-03-13 12:15:48 -07:00
|
|
|
foreach ($accessories as $accessory) {
|
|
|
|
if ($accessory->numRemaining() < $accessory->pivot->quantity) {
|
2019-05-23 19:39:30 -07:00
|
|
|
$errors[] = trans('admin/kits/general.none_accessory', ['accessory'=> $accessory->name, 'qty' => $accessory->pivot->quantity]);
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2019-02-19 11:19:00 -08:00
|
|
|
return $accessories;
|
|
|
|
}
|
2019-02-27 14:37:58 -08:00
|
|
|
|
|
|
|
protected function saveToDb($user, $admin, $checkout_at, $expected_checkin, $errors, $assets_to_add, $license_seats_to_add, $consumables_to_add, $accessories_to_add, $note)
|
|
|
|
{
|
|
|
|
$errors = DB::transaction(
|
|
|
|
function () use ($user, $admin, $checkout_at, $expected_checkin, $errors, $assets_to_add, $license_seats_to_add, $consumables_to_add, $accessories_to_add, $note) {
|
|
|
|
// assets
|
|
|
|
foreach ($assets_to_add as $asset) {
|
|
|
|
$asset->location_id = $user->location_id;
|
|
|
|
$error = $asset->checkOut($user, $admin, $checkout_at, $expected_checkin, $note, null);
|
|
|
|
if ($error) {
|
|
|
|
array_merge_recursive($errors, $asset->getErrors()->toArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// licenses
|
|
|
|
foreach ($license_seats_to_add as $licenseSeat) {
|
|
|
|
$licenseSeat->user_id = $admin->id;
|
|
|
|
$licenseSeat->assigned_to = $user->id;
|
|
|
|
if ($licenseSeat->save()) {
|
|
|
|
event(new CheckoutableCheckedOut($licenseSeat, $user, $admin, $note));
|
|
|
|
} else {
|
|
|
|
$errors[] = 'Something went wrong saving a license seat';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// consumables
|
|
|
|
foreach ($consumables_to_add as $consumable) {
|
|
|
|
$consumable->assigned_to = $user->id;
|
|
|
|
$consumable->users()->attach($consumable->id, [
|
|
|
|
'consumable_id' => $consumable->id,
|
|
|
|
'user_id' => $admin->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'assigned_to' => $user->id,
|
2019-02-27 14:37:58 -08:00
|
|
|
]);
|
|
|
|
event(new CheckoutableCheckedOut($consumable, $user, $admin, $note));
|
|
|
|
}
|
|
|
|
//accessories
|
|
|
|
foreach ($accessories_to_add as $accessory) {
|
|
|
|
$accessory->assigned_to = $user->id;
|
|
|
|
$accessory->users()->attach($accessory->id, [
|
|
|
|
'accessory_id' => $accessory->id,
|
|
|
|
'user_id' => $admin->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'assigned_to' => $user->id,
|
2019-02-27 14:37:58 -08:00
|
|
|
]);
|
|
|
|
event(new CheckoutableCheckedOut($accessory, $user, $admin, $note));
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2019-02-27 14:37:58 -08:00
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
2019-02-19 11:19:00 -08:00
|
|
|
}
|