Merge pull request #26 from Godmartinz/checkin_non_reassignable_license_cleanup

moved methods to licenseSeat model, clean up code, removed unused namespaces etc
This commit is contained in:
Godfrey Martinez
2025-01-21 11:47:42 -08:00
committed by GitHub
9 changed files with 39 additions and 42 deletions

View File

@@ -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\Consumable;
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 @@ class Helper
}
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;
}
}
}

View File

@@ -138,11 +138,12 @@ class LicenseSeatsController extends Controller
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')));
}

View File

@@ -248,15 +248,8 @@ class LicensesController extends Controller
}
$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'))

View File

@@ -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 @@ class LicenseSeatsTransformer
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;
// }
// }
}

View File

@@ -4,6 +4,7 @@ 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;
@@ -37,7 +38,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 - 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,

View File

@@ -594,7 +594,7 @@ class License extends Depreciable
{
$total = $this->licenseSeatsCount;
$taken = $this->assigned_seats_count;
$unreassignable = Helper::unReassignableCount($this);
$unreassignable = LicenseSeat::unReassignableCount($this);
$diff = ($total - $taken - $unreassignable);
return (int) $diff;

View File

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

@@ -1,9 +1,6 @@
<?php
namespace App\Models\LicenseSeat;
use App\Models\LicenseSeat;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration

View File

@@ -32,7 +32,7 @@ class LicenseCheckinTest extends TestCase
$licenseSeat->refresh();
$this->assertEquals(1, $licenseSeat->unreassignable_seat);
$this->assertEquals(true, $licenseSeat->unreassignable_seat);
}
public function testCannotCheckinLicenseThatIsNotAssigned()