moves counts to licenses, allows bulk check in of unreassignable licenses

This commit is contained in:
Godfrey M 2025-01-22 10:46:28 -08:00
parent 2143952a1e
commit 34e8360b10
7 changed files with 39 additions and 55 deletions

View file

@ -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();
}

View file

@ -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'));

View file

@ -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'))

View file

@ -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,

View file

@ -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;

View file

@ -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

View file

@ -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>
@ -573,13 +573,6 @@
{{ 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" />