Skip to content

Commit

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

moved methods to licenseSeat model, clean up code, removed unused namespaces etc
  • Loading branch information
Godmartinz authored Jan 21, 2025
2 parents 6bab6e7 + 2143952 commit ca259ee
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 42 deletions.
11 changes: 0 additions & 11 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace App\Helpers;
use App\Models\Accessory;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Component;
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;
Expand Down Expand Up @@ -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;
}
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/LicenseSeatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}
Expand Down
11 changes: 2 additions & 9 deletions app/Http/Controllers/Licenses/LicensesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
15 changes: 0 additions & 15 deletions app/Http/Transformers/LicenseSeatsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
// }
// }
}
3 changes: 2 additions & 1 deletion app/Http/Transformers/LicensesTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/Models/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions app/Models/LicenseSeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Helpers\Helper;
use App\Models\Traits\Acceptable;
use App\Notifications\CheckinLicenseNotification;
use App\Notifications\CheckoutLicenseNotification;
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Checkins/Ui/LicenseCheckinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testNonReassignableLicenseSeatIsUnavailable()

$licenseSeat->refresh();

$this->assertEquals(1, $licenseSeat->unreassignable_seat);
$this->assertEquals(true, $licenseSeat->unreassignable_seat);
}

public function testCannotCheckinLicenseThatIsNotAssigned()
Expand Down

0 comments on commit ca259ee

Please sign in to comment.