Renamed/refactorered licenses method

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2023-04-18 01:01:44 -07:00
parent 6ab4314221
commit de78f8d41f

View file

@ -455,40 +455,27 @@ class License extends Depreciable
* @since [v2.0] * @since [v2.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation * @return \Illuminate\Database\Eloquent\Relations\Relation
*/ */
public function licenseSeatsRelation() public function seats()
{ {
return $this->hasMany(LicenseSeat::class)->whereNull('deleted_at')->selectRaw('license_id, count(*) as count')->groupBy('license_id'); return $this->hasMany(LicenseSeat::class)->whereNull('deleted_at')->selectRaw('license_id, count(*) as count')->groupBy('license_id');
} }
/** /**
* Sets the license seat count attribute * Returns the number of total available seats for this license
* @todo - refactor all of these weird counters for licenses. The relationships are donked, yo.
* *
* @author A. Gianotto <snipe@snipe.net> * @author A. Gianotto <snipe@snipe.net>
* @since [v2.0] * @since [v2.0]
* @return int * @return int
*/ */
public function getLicenseSeatsCountAttribute() public function availableSeats()
{
if ($this->licenseSeatsRelation->first()) {
return $this->licenseSeatsRelation->first()->count;
}
return 0;
}
/**
* Returns the number of total available seats across all licenses
*
* @author A. Gianotto <snipe@snipe.net>
* @since [v2.0]
* @return int
*/
public static function availassetcount()
{ {
return LicenseSeat::whereNull('assigned_to') return LicenseSeat::whereNull('assigned_to')
->whereNull('asset_id') ->where('license_id', '=', $this->id)
->whereNull('deleted_at') ->whereNull('asset_id')
->count(); ->whereNull('deleted_at')
->count();
} }
/** /**
@ -500,7 +487,7 @@ class License extends Depreciable
*/ */
public function availCount() public function availCount()
{ {
return $this->licenseSeatsRelation() return $this->seats()
->whereNull('asset_id') ->whereNull('asset_id')
->whereNull('assigned_to') ->whereNull('assigned_to')
->whereNull('deleted_at'); ->whereNull('deleted_at');
@ -531,7 +518,7 @@ class License extends Depreciable
*/ */
public function assignedCount() public function assignedCount()
{ {
return $this->licenseSeatsRelation()->where(function ($query) { return $this->seats()->where(function ($query) {
$query->whereNotNull('assigned_to') $query->whereNotNull('assigned_to')
->orWhereNotNull('asset_id'); ->orWhereNotNull('asset_id');
}); });