Skip to content

Commit

Permalink
Merge pull request #27 from Godmartinz/checkin_non_reassignable_licen…
Browse files Browse the repository at this point in the history
…se_cleanup

adds bulk check in of unreassinable licenses, cleans up methods used for counting.
  • Loading branch information
Godmartinz authored Jan 22, 2025
2 parents ca259ee + 34e8360 commit 10a2d59
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 55 deletions.
1 change: 0 additions & 1 deletion app/Http/Controllers/Api/LicenseSeatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public function update(Request $request, $licenseId, $seatId) : JsonResponse | a

if ($is_checkin) {
if(!$licenseSeat->license->reassignable){
$licenseSeat->notes .= "\n" .trans('admin/licenses/message.checkin.not_reassignable') . ".";
$licenseSeat->unreassignable_seat = true;
$licenseSeat->save();
}
Expand Down
20 changes: 8 additions & 12 deletions app/Http/Controllers/Licenses/LicenseCheckinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public function store(Request $request, $seatId = null, $backTo = null)
$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')]);
Expand Down Expand Up @@ -131,21 +129,17 @@ public function bulkCheckin(Request $request, $licenseId) {
$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'));
Expand All @@ -160,7 +154,9 @@ public function bulkCheckin(Request $request, $licenseId) {
$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'));
Expand Down
11 changes: 10 additions & 1 deletion app/Http/Controllers/Licenses/LicensesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,16 @@ public function show($licenseId = null)

$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'))
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Transformers/LicensesTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Helpers\Helper;
use App\Models\License;
use App\Models\LicenseSeat;
use Illuminate\Support\Facades\Gate;
use Illuminate\Database\Eloquent\Collection;

Expand Down Expand Up @@ -38,7 +37,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 - 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,
Expand Down
22 changes: 19 additions & 3 deletions app/Models/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ public function availCount()
return $this->licenseSeatsRelation()
->whereNull('asset_id')
->whereNull('assigned_to')
->where('unreassignable_seat', '=', false)
->whereNull('deleted_at');
}

Expand Down Expand Up @@ -582,19 +583,34 @@ public function getAssignedSeatsCountAttribute()

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
*
* @author A. Gianotto <[email protected]>
* @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;
Expand Down
28 changes: 0 additions & 28 deletions app/Models/LicenseSeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Models;

use App\Helpers\Helper;
use App\Models\Traits\Acceptable;
use App\Notifications\CheckinLicenseNotification;
use App\Notifications\CheckoutLicenseNotification;
Expand Down Expand Up @@ -117,33 +116,6 @@ 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
Expand Down
9 changes: 1 addition & 8 deletions resources/views/licenses/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<x-icon type="seats" class="fa-2x" />
</span>
<span class="hidden-xs hidden-sm">{{ trans('admin/licenses/form.seats') }}</span>
<span class="badge badge-secondary">{{ number_format($license->availCount()->count() - number_format($unreassignable_seats_count) ) }} / {{ number_format($license->seats)}}</span>
<span class="badge badge-secondary">{{ number_format($license->availCount()->count()) }} / {{ number_format($license->seats)}}</span>

</a>
</li>
Expand Down Expand Up @@ -573,13 +573,6 @@ class="table table-striped snipe-table"
{{ trans('admin/licenses/general.bulk.checkin_all.button') }}
</a>
</span>
@elseif (! $license->reassignable)
<span data-tooltip="true" title=" {{ trans('admin/licenses/general.bulk.checkin_all.disabled_tooltip_reassignable') }}">
<a href="#" class="btn btn-primary bg-purple btn-sm btn-social btn-block hidden-print disabled" style="margin-bottom: 25px;">
<x-icon type="checkin" />
{{ trans('admin/licenses/general.bulk.checkin_all.button') }}
</a>
</span>
@else
<a href="#" class="btn btn-primary bg-purple btn-sm btn-social btn-block hidden-print" style="margin-bottom: 25px;" data-toggle="modal" data-tooltip="true" data-target="#checkinFromAllModal" data-content="{{ trans('general.sure_to_delete') }} data-title="{{ trans('general.delete') }}" onClick="return false;">
<x-icon type="checkin" />
Expand Down

0 comments on commit 10a2d59

Please sign in to comment.