2018-07-24 19:35:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Consumables;
|
|
|
|
|
2018-09-10 07:40:26 -07:00
|
|
|
use App\Events\CheckoutableCheckedOut;
|
2024-07-26 04:48:07 -07:00
|
|
|
use App\Helpers\Helper;
|
2018-07-25 01:02:06 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Models\Consumable;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Http\Request;
|
2024-07-11 08:00:08 -07:00
|
|
|
use \Illuminate\Contracts\View\View;
|
|
|
|
use \Illuminate\Http\RedirectResponse;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
class ConsumableCheckoutController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Return a view to checkout a consumable to a user.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ConsumableCheckoutController::store() method that stores the data.
|
|
|
|
* @since [v1.0]
|
2023-11-23 08:18:28 -08:00
|
|
|
* @param int $id
|
2018-07-24 19:35:26 -07:00
|
|
|
*/
|
2024-07-11 08:00:08 -07:00
|
|
|
public function create($id) : View | RedirectResponse
|
2018-07-24 19:35:26 -07:00
|
|
|
{
|
2023-04-06 20:38:02 -07:00
|
|
|
|
2024-07-11 08:00:08 -07:00
|
|
|
if ($consumable = Consumable::find($id)) {
|
2023-04-06 20:38:02 -07:00
|
|
|
|
2023-11-23 08:18:28 -08:00
|
|
|
$this->authorize('checkout', $consumable);
|
|
|
|
|
|
|
|
// Make sure the category is valid
|
|
|
|
if ($consumable->category) {
|
|
|
|
|
|
|
|
// Make sure there is at least one available to checkout
|
|
|
|
if ($consumable->numRemaining() <= 0){
|
|
|
|
return redirect()->route('consumables.index')
|
2024-07-26 04:48:07 -07:00
|
|
|
->with('error', trans('admin/consumables/message.checkout.unavailable', ['requested' => 1, 'remaining' => $consumable->numRemaining()]));
|
2023-11-23 08:18:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the checkout view
|
|
|
|
return view('consumables/checkout', compact('consumable'));
|
|
|
|
}
|
2023-04-06 20:38:02 -07:00
|
|
|
|
2023-11-23 08:18:28 -08:00
|
|
|
// Invalid category
|
|
|
|
return redirect()->route('consumables.edit', ['consumable' => $consumable->id])
|
|
|
|
->with('error', trans('general.invalid_item_category_single', ['type' => trans('general.consumable')]));
|
2023-11-20 07:28:44 -08:00
|
|
|
}
|
|
|
|
|
2023-11-23 08:18:28 -08:00
|
|
|
// Not found
|
|
|
|
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.does_not_exist'));
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the checkout information
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ConsumableCheckoutController::create() method that returns the form.
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $consumableId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
2018-07-27 15:05:45 -07:00
|
|
|
public function store(Request $request, $consumableId)
|
2018-07-24 19:35:26 -07:00
|
|
|
{
|
2023-04-06 20:38:02 -07:00
|
|
|
if (is_null($consumable = Consumable::with('users')->find($consumableId))) {
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->authorize('checkout', $consumable);
|
|
|
|
|
2023-05-21 06:41:03 -07:00
|
|
|
// If the quantity is not present in the request or is not a positive integer, set it to 1
|
|
|
|
$quantity = $request->input('qty');
|
|
|
|
if (!isset($quantity) || !ctype_digit((string)$quantity) || $quantity <= 0) {
|
|
|
|
$quantity = 1;
|
|
|
|
}
|
|
|
|
|
2023-04-06 20:38:02 -07:00
|
|
|
// Make sure there is at least one available to checkout
|
2023-05-21 06:41:03 -07:00
|
|
|
if ($consumable->numRemaining() <= 0 || $quantity > $consumable->numRemaining()) {
|
2024-07-26 04:48:07 -07:00
|
|
|
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.checkout.unavailable', ['requested' => $quantity, 'remaining' => $consumable->numRemaining() ]));
|
2023-04-06 20:38:02 -07:00
|
|
|
}
|
|
|
|
|
2024-07-04 12:49:22 -07:00
|
|
|
$admin_user = auth()->user();
|
2019-12-11 11:09:54 -08:00
|
|
|
$assigned_to = e($request->input('assigned_to'));
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
// Check if the user exists
|
|
|
|
if (is_null($user = User::find($assigned_to))) {
|
|
|
|
// Redirect to the consumable management page with error
|
2022-08-10 15:15:09 -07:00
|
|
|
return redirect()->route('consumables.checkout.show', $consumable)->with('error', trans('admin/consumables/message.checkout.user_does_not_exist'))->withInput();
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the consumable data
|
2019-12-11 11:09:54 -08:00
|
|
|
$consumable->assigned_to = e($request->input('assigned_to'));
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2023-05-21 06:41:03 -07:00
|
|
|
for($i = 0; $i < $quantity; $i++){
|
2018-07-24 19:35:26 -07:00
|
|
|
$consumable->users()->attach($consumable->id, [
|
|
|
|
'consumable_id' => $consumable->id,
|
|
|
|
'user_id' => $admin_user->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'assigned_to' => e($request->input('assigned_to')),
|
2022-08-10 15:04:59 -07:00
|
|
|
'note' => $request->input('note'),
|
2018-07-24 19:35:26 -07:00
|
|
|
]);
|
2023-05-21 06:19:00 -07:00
|
|
|
}
|
2024-07-04 12:49:22 -07:00
|
|
|
event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note')));
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2024-07-26 04:48:07 -07:00
|
|
|
$request->request->add(['checkout_to_type' => 'user']);
|
|
|
|
$request->request->add(['assigned_user' => $user->id]);
|
|
|
|
|
|
|
|
session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);
|
|
|
|
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
// Redirect to the new consumable page
|
2024-07-26 04:48:07 -07:00
|
|
|
return redirect()->to(Helper::getRedirectOption($request, $consumable->id, 'Consumables'))->with('success', trans('admin/consumables/message.checkout.success'));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
}
|