From 2143952a1eacd74bf066a8f4dc84c4539b9c9496 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 21 Jan 2025 11:43:47 -0800 Subject: [PATCH] removed unused models, castged unreassignable_seat, clean up in general --- app/Helpers/Helper.php | 11 ------- .../Api/LicenseSeatsController.php | 3 +- .../Licenses/LicensesController.php | 11 ++----- .../Transformers/LicenseSeatsTransformer.php | 15 --------- app/Http/Transformers/LicensesTransformer.php | 3 +- app/Models/License.php | 2 +- app/Models/LicenseSeat.php | 31 +++++++++++++++++++ ...ds_unavailable_to_license_seats_tables.php | 3 -- .../Checkins/Ui/LicenseCheckinTest.php | 2 +- 9 files changed, 39 insertions(+), 42 deletions(-) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index 26301c1010b0..8f653ff35303 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -2,7 +2,6 @@ namespace App\Helpers; use App\Models\Accessory; -use App\Models\Actionlog; use App\Models\Asset; use App\Models\AssetModel; use App\Models\Component; @@ -10,7 +9,6 @@ use App\Models\CustomField; use App\Models\CustomFieldset; use App\Models\Depreciation; -use App\Models\LicenseSeat; use App\Models\Setting; use App\Models\Statuslabel; use App\Models\License; @@ -1531,14 +1529,5 @@ static public function getRedirectOption($request, $id, $table, $item_id = null) } return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error')); } - public static function unReassignableCount($license) - { - if (!$license->reassignable) { - $count = LicenseSeat::where('unreassignable_seat', '=', true) - ->where('license_id', '=', $license->id) - ->count(); - return $count; - } - } } diff --git a/app/Http/Controllers/Api/LicenseSeatsController.php b/app/Http/Controllers/Api/LicenseSeatsController.php index 38f8babf9d70..08b42b5a21a3 100644 --- a/app/Http/Controllers/Api/LicenseSeatsController.php +++ b/app/Http/Controllers/Api/LicenseSeatsController.php @@ -138,11 +138,12 @@ public function update(Request $request, $licenseId, $seatId) : JsonResponse | a if ($licenseSeat->save()) { if ($is_checkin) { - $licenseSeat->logCheckin($target, $request->input('note')); if(!$licenseSeat->license->reassignable){ + $licenseSeat->notes .= "\n" .trans('admin/licenses/message.checkin.not_reassignable') . "."; $licenseSeat->unreassignable_seat = true; $licenseSeat->save(); } + $licenseSeat->logCheckin($target, $licenseSeat->notes); return response()->json(Helper::formatStandardApiResponse('success', $licenseSeat, trans('admin/licenses/message.update.success'))); } diff --git a/app/Http/Controllers/Licenses/LicensesController.php b/app/Http/Controllers/Licenses/LicensesController.php index 8ddac020b579..54a8cb276421 100755 --- a/app/Http/Controllers/Licenses/LicensesController.php +++ b/app/Http/Controllers/Licenses/LicensesController.php @@ -248,15 +248,8 @@ public function show($licenseId = null) } $users_count = User::where('autoassign_licenses', '1')->count(); - $total_seats_count = (int) $license->totalSeatsByLicenseID(); - $available_seats_count = $license->availCount()->count(); - $unreassignable_seats_count = Helper::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); - } + + [$checkedout_seats_count, $total_seats_count, $available_seats_count, $unreassignable_seats_count] = LicenseSeat::usedSeatCount($license); $this->authorize('view', $license); return view('licenses.view', compact('license')) diff --git a/app/Http/Transformers/LicenseSeatsTransformer.php b/app/Http/Transformers/LicenseSeatsTransformer.php index e882e74a8899..98688762956f 100644 --- a/app/Http/Transformers/LicenseSeatsTransformer.php +++ b/app/Http/Transformers/LicenseSeatsTransformer.php @@ -2,12 +2,10 @@ namespace App\Http\Transformers; -use App\Models\Actionlog; use App\Models\License; use App\Models\LicenseSeat; use Illuminate\Support\Facades\Gate; use Illuminate\Database\Eloquent\Collection; - class LicenseSeatsTransformer { public function transformLicenseSeats(Collection $seats, $total) @@ -68,17 +66,4 @@ public function transformLicenseSeat(LicenseSeat $seat, $seat_count = 0) return $array; } -// private function unReassignable($seat) -// { -// if (!$seat->license->reassignable) { -// $exists = Actionlog::where('action_type', '=', 'checkin from') -// ->where('item_id', '=', $seat->license->id) -// ->where('updated_at', '=', $seat->updated_at) -// ->exists(); -// if($exists) { -// return true; -// } -// return false; -// } -// } } diff --git a/app/Http/Transformers/LicensesTransformer.php b/app/Http/Transformers/LicensesTransformer.php index 6ae57e629273..86940a8deca8 100644 --- a/app/Http/Transformers/LicensesTransformer.php +++ b/app/Http/Transformers/LicensesTransformer.php @@ -4,6 +4,7 @@ use App\Helpers\Helper; use App\Models\License; +use App\Models\LicenseSeat; use Illuminate\Support\Facades\Gate; use Illuminate\Database\Eloquent\Collection; @@ -37,7 +38,7 @@ public function transformLicense(License $license) '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 - Helper::unReassignableCount($license), + 'free_seats_count' => (int) $license->free_seats_count - LicenseSeat::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 0b5eed097a5d..e082e6c2a07e 100755 --- a/app/Models/License.php +++ b/app/Models/License.php @@ -594,7 +594,7 @@ public function remaincount() { $total = $this->licenseSeatsCount; $taken = $this->assigned_seats_count; - $unreassignable = Helper::unReassignableCount($this); + $unreassignable = LicenseSeat::unReassignableCount($this); $diff = ($total - $taken - $unreassignable); return (int) $diff; diff --git a/app/Models/LicenseSeat.php b/app/Models/LicenseSeat.php index 397a14146870..f491e19c2572 100755 --- a/app/Models/LicenseSeat.php +++ b/app/Models/LicenseSeat.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Helpers\Helper; use App\Models\Traits\Acceptable; use App\Notifications\CheckinLicenseNotification; use App\Notifications\CheckoutLicenseNotification; @@ -21,6 +22,9 @@ class LicenseSeat extends SnipeModel implements ICompanyableChild protected $guarded = 'id'; protected $table = 'license_seats'; + protected $casts = [ + 'unreassignable_seat' => 'boolean', + ]; /** * The attributes that are mass assignable. @@ -113,6 +117,33 @@ public function location() 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/database/migrations/2025_01_15_190348_adds_unavailable_to_license_seats_tables.php b/database/migrations/2025_01_15_190348_adds_unavailable_to_license_seats_tables.php index 9aec0343ac06..40c8ad53679b 100644 --- a/database/migrations/2025_01_15_190348_adds_unavailable_to_license_seats_tables.php +++ b/database/migrations/2025_01_15_190348_adds_unavailable_to_license_seats_tables.php @@ -1,9 +1,6 @@ refresh(); - $this->assertEquals(1, $licenseSeat->unreassignable_seat); + $this->assertEquals(true, $licenseSeat->unreassignable_seat); } public function testCannotCheckinLicenseThatIsNotAssigned()