From 34e8360b1060f8c451a4e5df4133a6034736f6cd Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Wed, 22 Jan 2025 10:46:28 -0800 Subject: [PATCH] moves counts to licenses, allows bulk check in of unreassignable licenses --- .../Api/LicenseSeatsController.php | 1 - .../Licenses/LicenseCheckinController.php | 20 ++++++------- .../Licenses/LicensesController.php | 11 +++++++- app/Http/Transformers/LicensesTransformer.php | 3 +- app/Models/License.php | 22 +++++++++++++-- app/Models/LicenseSeat.php | 28 ------------------- resources/views/licenses/view.blade.php | 9 +----- 7 files changed, 39 insertions(+), 55 deletions(-) diff --git a/app/Http/Controllers/Api/LicenseSeatsController.php b/app/Http/Controllers/Api/LicenseSeatsController.php index 08b42b5a21..0f752cbb83 100644 --- a/app/Http/Controllers/Api/LicenseSeatsController.php +++ b/app/Http/Controllers/Api/LicenseSeatsController.php @@ -139,7 +139,6 @@ class LicenseSeatsController extends Controller if ($is_checkin) { if(!$licenseSeat->license->reassignable){ - $licenseSeat->notes .= "\n" .trans('admin/licenses/message.checkin.not_reassignable') . "."; $licenseSeat->unreassignable_seat = true; $licenseSeat->save(); } diff --git a/app/Http/Controllers/Licenses/LicenseCheckinController.php b/app/Http/Controllers/Licenses/LicenseCheckinController.php index 8bd3e34f11..30fcd1cffd 100644 --- a/app/Http/Controllers/Licenses/LicenseCheckinController.php +++ b/app/Http/Controllers/Licenses/LicenseCheckinController.php @@ -97,8 +97,6 @@ class LicenseCheckinController extends Controller $licenseSeat->notes = $request->input('notes'); if (! $licenseSeat->license->reassignable) { $licenseSeat->unreassignable_seat = true; - $licenseSeat->notes .= "\n" . trans('admin/licenses/message.checkin.not_reassignable') . '.'; - } session()->put(['redirect_option' => $request->get('redirect_option')]); @@ -131,21 +129,17 @@ class LicenseCheckinController extends Controller $license = License::findOrFail($licenseId); $this->authorize('checkin', $license); - if (! $license->reassignable) { - // Not allowed to checkin - Session::flash('error', 'License not reassignable.'); - - return redirect()->back()->withInput(); - } - $licenseSeatsByUser = LicenseSeat::where('license_id', '=', $licenseId) ->whereNotNull('assigned_to') - ->with('user') + ->with('user', 'license') ->get(); + $license = $licenseSeatsByUser->first()?->license; foreach ($licenseSeatsByUser as $user_seat) { $user_seat->assigned_to = null; - + if ($license && ! $license->reassignable) { + $user_seat->unreassignable_seat = true; + } if ($user_seat->save()) { Log::debug('Checking in '.$license->name.' from user '.$user_seat->username); $user_seat->logCheckin($user_seat->user, trans('admin/licenses/general.bulk.checkin_all.log_msg')); @@ -160,7 +154,9 @@ class LicenseCheckinController extends Controller $count = 0; foreach ($licenseSeatsByAsset as $asset_seat) { $asset_seat->asset_id = null; - + if ($license && ! $license->reassignable) { + $asset_seat->unreassignable_seat = true; + } if ($asset_seat->save()) { Log::debug('Checking in '.$license->name.' from asset '.$asset_seat->asset_tag); $asset_seat->logCheckin($asset_seat->asset, trans('admin/licenses/general.bulk.checkin_all.log_msg')); diff --git a/app/Http/Controllers/Licenses/LicensesController.php b/app/Http/Controllers/Licenses/LicensesController.php index 54a8cb2764..7ef6eeb30b 100755 --- a/app/Http/Controllers/Licenses/LicensesController.php +++ b/app/Http/Controllers/Licenses/LicensesController.php @@ -249,7 +249,16 @@ class LicensesController extends Controller $users_count = User::where('autoassign_licenses', '1')->count(); - [$checkedout_seats_count, $total_seats_count, $available_seats_count, $unreassignable_seats_count] = LicenseSeat::usedSeatCount($license); + $total_seats_count = (int) $license->totalSeatsByLicenseID(); + $available_seats_count = $license->availCount()->count(); + $unreassignable_seats_count = License::unReassignableCount($license); + + if(!$license->reassignable){ + $checkedout_seats_count = ($total_seats_count - $available_seats_count - $unreassignable_seats_count ); + } + else { + $checkedout_seats_count = ($total_seats_count - $available_seats_count); + } $this->authorize('view', $license); return view('licenses.view', compact('license')) diff --git a/app/Http/Transformers/LicensesTransformer.php b/app/Http/Transformers/LicensesTransformer.php index 86940a8dec..3be7b256da 100644 --- a/app/Http/Transformers/LicensesTransformer.php +++ b/app/Http/Transformers/LicensesTransformer.php @@ -4,7 +4,6 @@ namespace App\Http\Transformers; use App\Helpers\Helper; use App\Models\License; -use App\Models\LicenseSeat; use Illuminate\Support\Facades\Gate; use Illuminate\Database\Eloquent\Collection; @@ -38,7 +37,7 @@ class LicensesTransformer 'notes' => Helper::parseEscapedMarkedownInline($license->notes), 'expiration_date' => Helper::getFormattedDateObject($license->expiration_date, 'date'), 'seats' => (int) $license->seats, - 'free_seats_count' => (int) $license->free_seats_count - LicenseSeat::unReassignableCount($license), + 'free_seats_count' => (int) $license->free_seats_count - License::unReassignableCount($license), 'min_amt' => ($license->min_amt) ? (int) ($license->min_amt) : null, 'license_name' => ($license->license_name) ? e($license->license_name) : null, 'license_email' => ($license->license_email) ? e($license->license_email) : null, diff --git a/app/Models/License.php b/app/Models/License.php index e082e6c2a0..490a8401a4 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -533,6 +533,7 @@ class License extends Depreciable return $this->licenseSeatsRelation() ->whereNull('asset_id') ->whereNull('assigned_to') + ->where('unreassignable_seat', '=', false) ->whereNull('deleted_at'); } @@ -582,7 +583,22 @@ class License extends Depreciable return 0; } - + /** + * Calculates the number of unreassignable seats + * + * @author G. Martinez + * @since [v7.1.15] + */ + public static function unReassignableCount($license) : int + { + $count = 0; + if (!$license->reassignable) { + $count = licenseSeat::query()->where('unreassignable_seat', '=', true) + ->where('license_id', '=', $license->id) + ->count(); + } + return $count; + } /** * Calculates the number of remaining seats * @@ -590,11 +606,11 @@ class License extends Depreciable * @since [v1.0] * @return int */ - public function remaincount() + public function remaincount() : int { $total = $this->licenseSeatsCount; $taken = $this->assigned_seats_count; - $unreassignable = LicenseSeat::unReassignableCount($this); + $unreassignable = self::unReassignableCount($this); $diff = ($total - $taken - $unreassignable); return (int) $diff; diff --git a/app/Models/LicenseSeat.php b/app/Models/LicenseSeat.php index f491e19c25..6243d7d020 100755 --- a/app/Models/LicenseSeat.php +++ b/app/Models/LicenseSeat.php @@ -2,7 +2,6 @@ namespace App\Models; -use App\Helpers\Helper; use App\Models\Traits\Acceptable; use App\Notifications\CheckinLicenseNotification; use App\Notifications\CheckoutLicenseNotification; @@ -117,33 +116,6 @@ class LicenseSeat extends SnipeModel implements ICompanyableChild return false; } - public static function usedSeatCount($license): array { - $total_seats_count = (int) $license->totalSeatsByLicenseID(); - $available_seats_count = $license->availCount()->count(); - $unreassignable_seats_count = self::unReassignableCount($license); - if(!$license->reassignable){ - $checkedout_seats_count = ($total_seats_count - $available_seats_count - $unreassignable_seats_count ); - } - else { - $checkedout_seats_count = ($total_seats_count - $available_seats_count); - } - return [ - $checkedout_seats_count, - $total_seats_count, - $available_seats_count, - $unreassignable_seats_count, - ]; - } - public static function unReassignableCount($license) - { - - if (!$license->reassignable) { - $count = static::query()->where('unreassignable_seat', '=', true) - ->where('license_id', '=', $license->id) - ->count(); - return $count; - } - } /** * Query builder scope to order on department diff --git a/resources/views/licenses/view.blade.php b/resources/views/licenses/view.blade.php index 7af8676156..cc2f10b980 100755 --- a/resources/views/licenses/view.blade.php +++ b/resources/views/licenses/view.blade.php @@ -32,7 +32,7 @@ - {{ number_format($license->availCount()->count() - number_format($unreassignable_seats_count) ) }} / {{ number_format($license->seats)}} + {{ number_format($license->availCount()->count()) }} / {{ number_format($license->seats)}} @@ -573,13 +573,6 @@ {{ trans('admin/licenses/general.bulk.checkin_all.button') }} - @elseif (! $license->reassignable) - - - - {{ trans('admin/licenses/general.bulk.checkin_all.button') }} - - @else