+84
-71
@@ -123,32 +123,36 @@ class License extends Depreciable
|
||||
* Update seat counts when the license is updated
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v3.0]
|
||||
* @since [v3.0]
|
||||
*/
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
// We need to listen for created for the initial setup so that we have a license ID.
|
||||
static::created(function ($license) {
|
||||
$newSeatCount = $license->getAttributes()['seats'];
|
||||
static::created(
|
||||
function ($license) {
|
||||
$newSeatCount = $license->getAttributes()['seats'];
|
||||
|
||||
return static::adjustSeatCount($license, 0, $newSeatCount);
|
||||
});
|
||||
return static::adjustSeatCount($license, 0, $newSeatCount);
|
||||
}
|
||||
);
|
||||
// However, we listen for updating to be able to prevent the edit if we cannot delete enough seats.
|
||||
static::updating(function ($license) {
|
||||
$newSeatCount = $license->getAttributes()['seats'];
|
||||
//$oldSeatCount = isset($license->getOriginal()['seats']) ? $license->getOriginal()['seats'] : 0;
|
||||
/*
|
||||
That previous method *did* mostly work, but if you ever managed to get your $license->seats value out of whack
|
||||
with your actual count of license_seats *records*, you would never manage to get back 'into whack'.
|
||||
The below method actually grabs a count of existing license_seats records, so it will be more accurate.
|
||||
This means that if your license_seats are out of whack, you can change the quantity and hit 'save' and it
|
||||
will manage to 'true up' and make your counts line up correctly.
|
||||
*/
|
||||
$oldSeatCount = $license->license_seats_count;
|
||||
static::updating(
|
||||
function ($license) {
|
||||
$newSeatCount = $license->getAttributes()['seats'];
|
||||
//$oldSeatCount = isset($license->getOriginal()['seats']) ? $license->getOriginal()['seats'] : 0;
|
||||
/*
|
||||
That previous method *did* mostly work, but if you ever managed to get your $license->seats value out of whack
|
||||
with your actual count of license_seats *records*, you would never manage to get back 'into whack'.
|
||||
The below method actually grabs a count of existing license_seats records, so it will be more accurate.
|
||||
This means that if your license_seats are out of whack, you can change the quantity and hit 'save' and it
|
||||
will manage to 'true up' and make your counts line up correctly.
|
||||
*/
|
||||
$oldSeatCount = $license->license_seats_count;
|
||||
|
||||
return static::adjustSeatCount($license, $oldSeatCount, $newSeatCount);
|
||||
});
|
||||
return static::adjustSeatCount($license, $oldSeatCount, $newSeatCount);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function prepareLimitChangeRule($parameters, $field)
|
||||
@@ -163,7 +167,7 @@ class License extends Depreciable
|
||||
* Balance seat counts
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v3.0]
|
||||
* @since [v3.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public static function adjustSeatCount($license, $oldSeats, $newSeats)
|
||||
@@ -210,11 +214,15 @@ class License extends Depreciable
|
||||
}
|
||||
//Chunk and use DB transactions to prevent timeouts.
|
||||
|
||||
collect($licenseInsert)->chunk(1000)->each(function ($chunk) {
|
||||
DB::transaction(function () use ($chunk) {
|
||||
LicenseSeat::insert($chunk->toArray());
|
||||
});
|
||||
});
|
||||
collect($licenseInsert)->chunk(1000)->each(
|
||||
function ($chunk) {
|
||||
DB::transaction(
|
||||
function () use ($chunk) {
|
||||
LicenseSeat::insert($chunk->toArray());
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
// On initial create, we shouldn't log the addition of seats.
|
||||
if ($license->id) {
|
||||
@@ -235,7 +243,7 @@ class License extends Depreciable
|
||||
* Sets the attribute for whether or not the license is maintained
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v1.0]
|
||||
* @since [v1.0]
|
||||
* @return mixed
|
||||
*/
|
||||
public function setMaintainedAttribute($value)
|
||||
@@ -247,7 +255,7 @@ class License extends Depreciable
|
||||
* Sets the reassignable attribute
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v1.0]
|
||||
* @since [v1.0]
|
||||
* @return mixed
|
||||
*/
|
||||
public function setReassignableAttribute($value)
|
||||
@@ -259,7 +267,7 @@ class License extends Depreciable
|
||||
* Sets expiration date attribute
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v1.0]
|
||||
* @since [v1.0]
|
||||
* @return mixed
|
||||
*/
|
||||
public function setExpirationDateAttribute($value)
|
||||
@@ -276,7 +284,7 @@ class License extends Depreciable
|
||||
* Sets termination date attribute
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return mixed
|
||||
*/
|
||||
public function setTerminationDateAttribute($value)
|
||||
@@ -292,10 +300,11 @@ class License extends Depreciable
|
||||
* Sets free_seat_count attribute
|
||||
*
|
||||
* @author G. Martinez
|
||||
* @since [v6.3]
|
||||
* @since [v6.3]
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFreeSeatCountAttribute(){
|
||||
public function getFreeSeatCountAttribute()
|
||||
{
|
||||
return $this->attributes['free_seat_count'] = $this->remaincount();
|
||||
}
|
||||
|
||||
@@ -303,7 +312,7 @@ class License extends Depreciable
|
||||
* Establishes the license -> company relationship
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function company()
|
||||
@@ -315,7 +324,7 @@ class License extends Depreciable
|
||||
* Establishes the license -> category relationship
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v4.4.0]
|
||||
* @since [v4.4.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function category()
|
||||
@@ -327,7 +336,7 @@ class License extends Depreciable
|
||||
* Establishes the license -> manufacturer relationship
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function manufacturer()
|
||||
@@ -339,7 +348,7 @@ class License extends Depreciable
|
||||
* Determine whether the user should be emailed on checkin/checkout
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return bool
|
||||
*/
|
||||
public function checkin_email()
|
||||
@@ -354,7 +363,7 @@ class License extends Depreciable
|
||||
* Determine whether the user should be required to accept the license
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v4.0]
|
||||
* @since [v4.0]
|
||||
* @return bool
|
||||
*/
|
||||
public function requireAcceptance()
|
||||
@@ -371,12 +380,12 @@ class License extends Depreciable
|
||||
* checks for a settings level EULA
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @since [v4.0]
|
||||
* @since [v4.0]
|
||||
* @return string | false
|
||||
*/
|
||||
public function getEula()
|
||||
{
|
||||
if ($this->category){
|
||||
if ($this->category) {
|
||||
if ($this->category->eula_text) {
|
||||
return Helper::parseEscapedMarkedown($this->category->eula_text);
|
||||
} elseif ($this->category->use_default_eula == '1') {
|
||||
@@ -392,7 +401,7 @@ class License extends Depreciable
|
||||
* Establishes the license -> assigned user relationship
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function assignedusers()
|
||||
@@ -404,7 +413,7 @@ class License extends Depreciable
|
||||
* Establishes the license -> action logs relationship
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function assetlog()
|
||||
@@ -420,7 +429,7 @@ class License extends Depreciable
|
||||
* Establishes the license -> admin user relationship
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function adminuser()
|
||||
@@ -434,7 +443,7 @@ class License extends Depreciable
|
||||
* @todo this can probably be refactored at some point. We don't need counting methods.
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return int
|
||||
*/
|
||||
public static function assetcount()
|
||||
@@ -450,7 +459,7 @@ class License extends Depreciable
|
||||
* @todo this can also probably be refactored at some point.
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function totalSeatsByLicenseID()
|
||||
@@ -467,7 +476,7 @@ class License extends Depreciable
|
||||
* Otherwise calling "count()" on each model results in n+1 sadness.
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function licenseSeatsRelation()
|
||||
@@ -479,7 +488,7 @@ class License extends Depreciable
|
||||
* Sets the license seat count attribute
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return int
|
||||
*/
|
||||
public function getLicenseSeatsCountAttribute()
|
||||
@@ -495,7 +504,7 @@ class License extends Depreciable
|
||||
* Returns the number of total available seats across all licenses
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return int
|
||||
*/
|
||||
public static function availassetcount()
|
||||
@@ -509,7 +518,7 @@ class License extends Depreciable
|
||||
* Returns the available seats remaining
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return int
|
||||
*/
|
||||
|
||||
@@ -517,7 +526,7 @@ class License extends Depreciable
|
||||
* Returns the number of total available seats for this license
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v2.0]
|
||||
* @since [v2.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function availCount()
|
||||
@@ -532,7 +541,7 @@ class License extends Depreciable
|
||||
* Sets the available seats attribute
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v3.0]
|
||||
* @since [v3.0]
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAvailSeatsCountAttribute()
|
||||
@@ -548,22 +557,24 @@ class License extends Depreciable
|
||||
* Retuns the number of assigned seats for this asset
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v3.0]
|
||||
* @since [v3.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function assignedCount()
|
||||
{
|
||||
return $this->licenseSeatsRelation()->where(function ($query) {
|
||||
$query->whereNotNull('assigned_to')
|
||||
->orWhereNotNull('asset_id');
|
||||
});
|
||||
return $this->licenseSeatsRelation()->where(
|
||||
function ($query) {
|
||||
$query->whereNotNull('assigned_to')
|
||||
->orWhereNotNull('asset_id');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the assigned seats attribute
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v1.0]
|
||||
* @since [v1.0]
|
||||
* @return int
|
||||
*/
|
||||
public function getAssignedSeatsCountAttribute()
|
||||
@@ -579,7 +590,7 @@ class License extends Depreciable
|
||||
* Calculates the number of remaining seats
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v1.0]
|
||||
* @since [v1.0]
|
||||
* @return int
|
||||
*/
|
||||
public function remaincount()
|
||||
@@ -595,7 +606,7 @@ class License extends Depreciable
|
||||
* Returns the total number of seats for this license
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v1.0]
|
||||
* @since [v1.0]
|
||||
* @return int
|
||||
*/
|
||||
public function totalcount()
|
||||
@@ -611,7 +622,7 @@ class License extends Depreciable
|
||||
* Establishes the license -> seats relationship
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v1.0]
|
||||
* @since [v1.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function licenseseats()
|
||||
@@ -623,7 +634,7 @@ class License extends Depreciable
|
||||
* Establishes the license -> supplier relationship
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v1.0]
|
||||
* @since [v1.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function supplier()
|
||||
@@ -637,17 +648,19 @@ class License extends Depreciable
|
||||
* the API to populate next_seat
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v3.0]
|
||||
* @since [v3.0]
|
||||
* @return mixed
|
||||
*/
|
||||
public function freeSeat()
|
||||
{
|
||||
return $this->licenseseats()
|
||||
->whereNull('deleted_at')
|
||||
->where(function ($query) {
|
||||
$query->whereNull('assigned_to')
|
||||
->whereNull('asset_id');
|
||||
})
|
||||
->where(
|
||||
function ($query) {
|
||||
$query->whereNull('assigned_to')
|
||||
->whereNull('asset_id');
|
||||
}
|
||||
)
|
||||
->orderBy('id', 'asc')
|
||||
->first();
|
||||
}
|
||||
@@ -657,7 +670,7 @@ class License extends Depreciable
|
||||
* Establishes the license -> free seats relationship
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v1.0]
|
||||
* @since [v1.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public function freeSeats()
|
||||
@@ -671,7 +684,7 @@ class License extends Depreciable
|
||||
* @todo should refactor. I don't like get() in model methods
|
||||
*
|
||||
* @author A. Gianotto <snipe@snipe.net>
|
||||
* @since [v1.0]
|
||||
* @since [v1.0]
|
||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||
*/
|
||||
public static function getExpiringLicenses($days = 60)
|
||||
@@ -689,8 +702,8 @@ class License extends Depreciable
|
||||
/**
|
||||
* Query builder scope to order on manufacturer
|
||||
*
|
||||
* @param \Illuminate\Database\Query\Builder $query Query builder instance
|
||||
* @param string $order Order
|
||||
* @param \Illuminate\Database\Query\Builder $query Query builder instance
|
||||
* @param string $order Order
|
||||
*
|
||||
* @return \Illuminate\Database\Query\Builder Modified query builder
|
||||
*/
|
||||
@@ -703,8 +716,8 @@ class License extends Depreciable
|
||||
/**
|
||||
* Query builder scope to order on supplier
|
||||
*
|
||||
* @param \Illuminate\Database\Query\Builder $query Query builder instance
|
||||
* @param string $order Order
|
||||
* @param \Illuminate\Database\Query\Builder $query Query builder instance
|
||||
* @param string $order Order
|
||||
*
|
||||
* @return \Illuminate\Database\Query\Builder Modified query builder
|
||||
*/
|
||||
@@ -717,8 +730,8 @@ class License extends Depreciable
|
||||
/**
|
||||
* Query builder scope to order on company
|
||||
*
|
||||
* @param \Illuminate\Database\Query\Builder $query Query builder instance
|
||||
* @param text $order Order
|
||||
* @param \Illuminate\Database\Query\Builder $query Query builder instance
|
||||
* @param text $order Order
|
||||
*
|
||||
* @return \Illuminate\Database\Query\Builder Modified query builder
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user