2018-07-24 19:35:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Accessories;
|
|
|
|
|
2018-09-10 07:40:26 -07:00
|
|
|
use App\Events\CheckoutableCheckedOut;
|
2024-07-26 04:48:07 -07:00
|
|
|
use App\Helpers\Helper;
|
2024-07-29 01:54:53 -07:00
|
|
|
use App\Http\Controllers\CheckInOutRequest;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2024-07-17 16:26:42 -07:00
|
|
|
use App\Http\Requests\AccessoryCheckoutRequest;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Models\Accessory;
|
2024-07-29 01:54:53 -07:00
|
|
|
use App\Models\AccessoryCheckout;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Models\User;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2024-07-04 16:54:50 -07:00
|
|
|
use \Illuminate\Contracts\View\View;
|
|
|
|
use \Illuminate\Http\RedirectResponse;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
class AccessoryCheckoutController extends Controller
|
|
|
|
{
|
2024-07-29 01:54:53 -07:00
|
|
|
|
|
|
|
use CheckInOutRequest;
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
/**
|
|
|
|
* Return the form to checkout an Accessory to a user.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2023-11-23 08:18:28 -08:00
|
|
|
* @param int $id
|
2018-07-24 19:35:26 -07:00
|
|
|
*/
|
2024-07-04 16:54:50 -07:00
|
|
|
public function create($id) : View | RedirectResponse
|
2018-07-24 19:35:26 -07:00
|
|
|
{
|
|
|
|
|
2024-07-29 01:54:53 -07:00
|
|
|
if ($accessory = Accessory::withCount('checkouts as checkouts_count')->find($id)) {
|
2023-11-23 08:18:28 -08:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$this->authorize('checkout', $accessory);
|
|
|
|
|
2023-11-23 08:18:28 -08:00
|
|
|
if ($accessory->category) {
|
|
|
|
// Make sure there is at least one available to checkout
|
|
|
|
if ($accessory->numRemaining() <= 0){
|
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.checkout.unavailable'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the checkout view
|
|
|
|
return view('accessories/checkout', compact('accessory'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid category
|
|
|
|
return redirect()->route('accessories.edit', ['accessory' => $accessory->id])
|
|
|
|
->with('error', trans('general.invalid_item_category_single', ['type' => trans('general.accessory')]));
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
2023-11-23 08:18:28 -08:00
|
|
|
// Not found
|
|
|
|
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found'));
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the Accessory checkout information.
|
|
|
|
*
|
|
|
|
* If Slack is enabled and/or asset acceptance is enabled, it will also
|
|
|
|
* trigger a Slack message and send an email.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param Request $request
|
2024-07-29 01:54:53 -07:00
|
|
|
* @param Accessory $accessory
|
2018-07-24 19:35:26 -07:00
|
|
|
*/
|
2024-07-17 16:26:42 -07:00
|
|
|
public function store(AccessoryCheckoutRequest $request, Accessory $accessory) : RedirectResponse
|
2018-07-24 19:35:26 -07:00
|
|
|
{
|
2024-07-29 01:54:53 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$this->authorize('checkout', $accessory);
|
2024-07-17 16:26:42 -07:00
|
|
|
|
2024-07-29 01:54:53 -07:00
|
|
|
$target = $this->determineCheckoutTarget();
|
|
|
|
|
|
|
|
$accessory->checkout_qty = $request->input('checkout_qty', 1);
|
|
|
|
|
2024-07-17 16:26:42 -07:00
|
|
|
for ($i = 0; $i < $accessory->checkout_qty; $i++) {
|
2024-07-29 01:54:53 -07:00
|
|
|
AccessoryCheckout::create([
|
2024-07-17 16:26:42 -07:00
|
|
|
'accessory_id' => $accessory->id,
|
|
|
|
'created_at' => Carbon::now(),
|
2024-09-17 14:16:41 -07:00
|
|
|
'created_by' => auth()->id(),
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_to' => $target->id,
|
|
|
|
'assigned_type' => $target::class,
|
2024-07-17 16:26:42 -07:00
|
|
|
'note' => $request->input('note'),
|
|
|
|
]);
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
2024-07-29 01:54:53 -07:00
|
|
|
event(new CheckoutableCheckedOut($accessory, $target, auth()->user(), $request->input('note')));
|
2018-07-25 01:02:06 -07:00
|
|
|
|
2024-07-26 04:48:07 -07:00
|
|
|
// Set this as user since we only allow checkout to user for this item type
|
2024-07-29 01:54:53 -07:00
|
|
|
$request->request->add(['checkout_to_type' => request('checkout_to_type')]);
|
|
|
|
$request->request->add(['assigned_user' => $target->id]);
|
2024-07-26 04:48:07 -07:00
|
|
|
|
|
|
|
session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);
|
|
|
|
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
// Redirect to the new accessory page
|
2024-07-26 04:48:07 -07:00
|
|
|
return redirect()->to(Helper::getRedirectOption($request, $accessory->id, 'Accessories'))
|
2024-07-17 16:26:42 -07:00
|
|
|
->with('success', trans('admin/accessories/message.checkout.success'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
}
|