Skip to content

Commit

Permalink
Merge pull request #15185 from arne-kroeger/feat/accesspories-checkou…
Browse files Browse the repository at this point in the history
…t-to-location-or-asset

Added #14979: add checkout to location and assets functionality to accessories
  • Loading branch information
snipe authored Jul 29, 2024
2 parents 33b8605 + 3c3b922 commit 4eccb5f
Show file tree
Hide file tree
Showing 21 changed files with 411 additions and 123 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/RestoreFromBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static function guess_prefix($input):string
$parser->line_aware_piping(); // <----- THIS is doing the heavy lifting!

$check_tables = ['settings' => null, 'migrations' => null /* 'assets' => null */]; //TODO - move to statics?
//can't use 'users' because the 'accessories_users' table?
//can't use 'users' because the 'accessories_checkout' table?
// can't use 'assets' because 'ver1_components_assets'
foreach($check_tables as $check_table => $_ignore) {
foreach ($parser->tablenames as $tablename => $_count) {
Expand Down
4 changes: 2 additions & 2 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ public static function checkLowInventory()
{
$alert_threshold = \App\Models\Setting::getSettings()->alert_threshold;
$consumables = Consumable::withCount('consumableAssignments as consumable_assignments_count')->whereNotNull('min_amt')->get();
$accessories = Accessory::withCount('users as users_count')->whereNotNull('min_amt')->get();
$accessories = Accessory::withCount('checkouts as checkouts_count')->whereNotNull('min_amt')->get();
$components = Component::whereNotNull('min_amt')->get();
$asset_models = AssetModel::where('min_amt', '>', 0)->get();
$licenses = License::where('min_amt', '>', 0)->get();
Expand Down Expand Up @@ -749,7 +749,7 @@ public static function checkLowInventory()
}

foreach ($accessories as $accessory) {
$avail = $accessory->qty - $accessory->users_count;
$avail = $accessory->qty - $accessory->checkouts_count;
if ($avail < ($accessory->min_amt) + $alert_threshold) {
if ($accessory->qty > 0) {
$percent = number_format((($avail / $accessory->qty) * 100), 0);
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Accessories/AccessoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ public function getClone($accessoryId = null) : View | RedirectResponse
*/
public function update(ImageUploadRequest $request, $accessoryId = null) : RedirectResponse
{
if ($accessory = Accessory::withCount('users as users_count')->find($accessoryId)) {
if ($accessory = Accessory::withCount('checkouts as checkouts_count')->find($accessoryId)) {

$this->authorize($accessory);

$validator = Validator::make($request->all(), [
"qty" => "required|numeric|min:$accessory->users_count"
"qty" => "required|numeric|min:$accessory->checkouts_count"
]);

if ($validator->fails()) {
Expand Down Expand Up @@ -233,7 +233,7 @@ public function destroy($accessoryId) : RedirectResponse
*/
public function show($accessoryID = null) : View | RedirectResponse
{
$accessory = Accessory::withCount('users as users_count')->find($accessoryID);
$accessory = Accessory::withCount('checkouts as checkouts_count')->find($accessoryID);
$this->authorize('view', $accessory);
if (isset($accessory->id)) {
return view('accessories/view', compact('accessory'));
Expand Down
17 changes: 8 additions & 9 deletions app/Http/Controllers/Accessories/AccessoryCheckinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Models\Accessory;
use App\Models\AccessoryCheckout;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
Expand All @@ -24,7 +25,7 @@ class AccessoryCheckinController extends Controller
*/
public function create($accessoryUserId = null, $backto = null) : View | RedirectResponse
{
if (is_null($accessory_user = DB::table('accessories_users')->find($accessoryUserId))) {
if (is_null($accessory_user = DB::table('accessories_checkout')->find($accessoryUserId))) {
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found'));
}

Expand All @@ -39,16 +40,16 @@ public function create($accessoryUserId = null, $backto = null) : View | Redirec
*
* @uses Accessory::checkin_email() to determine if an email can and should be sent
* @author [A. Gianotto] [<[email protected]>]
* @param null $accessoryUserId
* @param null $accessoryCheckoutId
* @param string $backto
*/
public function store(Request $request, $accessoryUserId = null, $backto = null) : RedirectResponse
public function store(Request $request, $accessoryCheckoutId = null, $backto = null) : RedirectResponse
{
if (is_null($accessory_user = DB::table('accessories_users')->find($accessoryUserId))) {
if (is_null($accessory_checkout = AccessoryCheckout::find($accessoryCheckoutId))) {
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist'));
}

$accessory = Accessory::find($accessory_user->accessory_id);
$accessory = Accessory::find($accessory_checkout->accessory_id);

$this->authorize('checkin', $accessory);

Expand All @@ -59,10 +60,8 @@ public function store(Request $request, $accessoryUserId = null, $backto = null)
}

// Was the accessory updated?
if (DB::table('accessories_users')->where('id', '=', $accessory_user->id)->delete()) {
$return_to = e($accessory_user->assigned_to);

event(new CheckoutableCheckedIn($accessory, User::find($return_to), auth()->user(), $request->input('note'), $checkin_at));
if ($accessory_checkout->delete()) {
event(new CheckoutableCheckedIn($accessory, $accessory_checkout->assignedTo, auth()->user(), $request->input('note'), $checkin_at));

session()->put(['redirect_option' => $request->get('redirect_option')]);

Expand Down
29 changes: 18 additions & 11 deletions app/Http/Controllers/Accessories/AccessoryCheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use App\Events\CheckoutableCheckedOut;
use App\Helpers\Helper;
use App\Http\Controllers\CheckInOutRequest;
use App\Http\Controllers\Controller;
use App\Http\Requests\AccessoryCheckoutRequest;
use App\Models\Accessory;
use App\Models\AccessoryCheckout;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
Expand All @@ -16,6 +18,9 @@

class AccessoryCheckoutController extends Controller
{

use CheckInOutRequest;

/**
* Return the form to checkout an Accessory to a user.
*
Expand All @@ -25,7 +30,7 @@ class AccessoryCheckoutController extends Controller
public function create($id) : View | RedirectResponse
{

if ($accessory = Accessory::withCount('users as users_count')->find($id)) {
if ($accessory = Accessory::withCount('checkouts as checkouts_count')->find($id)) {

$this->authorize('checkout', $accessory);

Expand Down Expand Up @@ -58,30 +63,32 @@ public function create($id) : View | RedirectResponse
*
* @author [A. Gianotto] [<[email protected]>]
* @param Request $request
* @param int $accessory
* @param Accessory $accessory
*/
public function store(AccessoryCheckoutRequest $request, Accessory $accessory) : RedirectResponse
{

$this->authorize('checkout', $accessory);
$accessory->assigned_to = $request->input('assigned_to');
$user = User::find($request->input('assigned_to'));
$accessory->checkout_qty = $request->input('checkout_qty', 1);

$target = $this->determineCheckoutTarget();

$accessory->checkout_qty = $request->input('checkout_qty', 1);

for ($i = 0; $i < $accessory->checkout_qty; $i++) {
$accessory->users()->attach($accessory->id, [
AccessoryCheckout::create([
'accessory_id' => $accessory->id,
'created_at' => Carbon::now(),
'user_id' => Auth::id(),
'assigned_to' => $request->input('assigned_to'),
'assigned_to' => $target->id,
'assigned_type' => $target::class,
'note' => $request->input('note'),
]);
}
event(new CheckoutableCheckedOut($accessory, $user, auth()->user(), $request->input('note')));
event(new CheckoutableCheckedOut($accessory, $target, auth()->user(), $request->input('note')));

// Set this as user since we only allow checkout to user for this item type
$request->request->add(['checkout_to_type' => 'user']);
$request->request->add(['assigned_user' => $user->id]);
$request->request->add(['checkout_to_type' => request('checkout_to_type')]);
$request->request->add(['assigned_user' => $target->id]);

session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);

Expand Down
54 changes: 26 additions & 28 deletions app/Http/Controllers/Api/AccessoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Events\CheckoutableCheckedOut;
use App\Helpers\Helper;
use App\Http\Controllers\CheckInOutRequest;
use App\Http\Controllers\Controller;
use App\Http\Requests\AccessoryCheckoutRequest;
use App\Http\Requests\StoreAccessoryRequest;
Expand All @@ -17,10 +18,12 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Requests\ImageUploadRequest;

use App\Models\AccessoryCheckout;

class AccessoriesController extends Controller
{
use CheckInOutRequest;

/**
* Display a listing of the resource.
*
Expand Down Expand Up @@ -48,13 +51,13 @@ public function index(Request $request)
'min_amt',
'company_id',
'notes',
'users_count',
'checkouts_count',
'qty',
];


$accessories = Accessory::select('accessories.*')->with('category', 'company', 'manufacturer', 'users', 'location', 'supplier')
->withCount('users as users_count');
$accessories = Accessory::select('accessories.*')->with('category', 'company', 'manufacturer', 'checkouts', 'location', 'supplier')
->withCount('checkouts as checkouts_count');

if ($request->filled('search')) {
$accessories = $accessories->TextSearch($request->input('search'));
Expand Down Expand Up @@ -154,7 +157,7 @@ public function store(StoreAccessoryRequest $request)
public function show($id)
{
$this->authorize('view', Accessory::class);
$accessory = Accessory::withCount('users as users_count')->findOrFail($id);
$accessory = Accessory::withCount('checkouts as checkouts_count')->findOrFail($id);

return (new AccessoriesTransformer)->transformAccessory($accessory);
}
Expand Down Expand Up @@ -197,28 +200,23 @@ public function checkedout($id, Request $request)
$offset = request('offset', 0);
$limit = request('limit', 50);

$accessory_users = $accessory->users;
$total = $accessory_users->count();
$accessory_checkouts = $accessory->checkouts;
$total = $accessory_checkouts->count();

if ($total < $offset) {
$offset = 0;
}

$accessory_users = $accessory->users()->skip($offset)->take($limit)->get();
$accessory_checkouts = $accessory->checkouts()->skip($offset)->take($limit)->get();

if ($request->filled('search')) {
$accessory_users = $accessory->users()
->where(function ($query) use ($request) {
$search_str = '%' . $request->input('search') . '%';
$query->where('first_name', 'like', $search_str)
->orWhere('last_name', 'like', $search_str)
->orWhere('note', 'like', $search_str);
})

$accessory_checkouts = $accessory->checkouts()->TextSearch($request->input('search'))
->get();
$total = $accessory_users->count();
$total = $accessory_checkouts->count();
}

return (new AccessoriesTransformer)->transformCheckedoutAccessory($accessory, $accessory_users, $total);
return (new AccessoriesTransformer)->transformCheckedoutAccessory($accessory, $accessory_checkouts, $total);
}


Expand Down Expand Up @@ -282,22 +280,22 @@ public function destroy($id)
public function checkout(AccessoryCheckoutRequest $request, Accessory $accessory)
{
$this->authorize('checkout', $accessory);
$accessory->assigned_to = $request->input('assigned_to');
$user = User::find($request->input('assigned_to'));
$target = $this->determineCheckoutTarget();
$accessory->checkout_qty = $request->input('checkout_qty', 1);

for ($i = 0; $i < $accessory->checkout_qty; $i++) {
$accessory->users()->attach($accessory->id, [
AccessoryCheckout::create([
'accessory_id' => $accessory->id,
'created_at' => Carbon::now(),
'user_id' => Auth::id(),
'assigned_to' => $request->input('assigned_to'),
'assigned_to' => $target->id,
'assigned_type' => $target::class,
'note' => $request->input('note'),
]);
}

// Set this value to be able to pass the qty through to the event
event(new CheckoutableCheckedOut($accessory, $user, auth()->user(), $request->input('note')));
event(new CheckoutableCheckedOut($accessory, $target, auth()->user(), $request->input('note')));

return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/accessories/message.checkout.success')));

Expand All @@ -316,19 +314,19 @@ public function checkout(AccessoryCheckoutRequest $request, Accessory $accessory
*/
public function checkin(Request $request, $accessoryUserId = null)
{
if (is_null($accessory_user = DB::table('accessories_users')->find($accessoryUserId))) {
if (is_null($accessory_checkout = AccessoryCheckout::find($accessoryUserId))) {
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/accessories/message.does_not_exist')));
}

$accessory = Accessory::find($accessory_user->accessory_id);
$accessory = Accessory::find($accessory_checkout->accessory_id);
$this->authorize('checkin', $accessory);

$logaction = $accessory->logCheckin(User::find($accessory_user->assigned_to), $request->input('note'));
$logaction = $accessory->logCheckin(User::find($accessory_checkout->assigned_to), $request->input('note'));

// Was the accessory updated?
if (DB::table('accessories_users')->where('id', '=', $accessory_user->id)->delete()) {
if (! is_null($accessory_user->assigned_to)) {
$user = User::find($accessory_user->assigned_to);
if ($accessory_checkout->delete()) {
if (! is_null($accessory_checkout->assigned_to)) {
$user = User::find($accessory_checkout->assigned_to);
}

$data['log_id'] = $logaction->id;
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Users/BulkUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function destroy(Request $request)

$users = User::whereIn('id', $user_raw_array)->get();
$assets = Asset::whereIn('assigned_to', $user_raw_array)->where('assigned_type', \App\Models\User::class)->get();
$accessories = DB::table('accessories_users')->whereIn('assigned_to', $user_raw_array)->get();
$accessories = DB::table('accessories_checkout')->whereIn('assigned_to', $user_raw_array)->get();
$licenses = DB::table('license_seats')->whereIn('assigned_to', $user_raw_array)->get();
$consumables = DB::table('consumables_users')->whereIn('assigned_to', $user_raw_array)->get();

Expand Down
11 changes: 4 additions & 7 deletions app/Http/Requests/AccessoryCheckoutRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ public function rules(): array

return array_merge(
[
'assigned_to' => [
'required',
'integer',
'exists:users,id,deleted_at,NULL',
'not_array'
],

'assigned_user' => 'required_without_all:assigned_asset,assigned_location',
'assigned_asset' => 'required_without_all:assigned_user,assigned_location',
'assigned_location' => 'required_without_all:assigned_user,assigned_asset',

'number_remaining_after_checkout' => [
'min:0',
'required',
Expand Down
Loading

0 comments on commit 4eccb5f

Please sign in to comment.