Merge branch 'develop' into checkin_non_reassignable_license

# Conflicts:
#	app/Models/License.php
#	resources/views/licenses/view.blade.php
#	tests/Feature/Checkins/Api/LicenseCheckInTest.php
This commit is contained in:
Godfrey M
2025-09-03 11:09:51 -07:00
3209 changed files with 96676 additions and 314180 deletions
+96 -93
View File
@@ -3,13 +3,14 @@
namespace App\Models;
use App\Helpers\Helper;
use App\Models\Traits\CompanyableTrait;
use App\Models\Traits\HasUploads;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Watson\Validating\ValidatingTrait;
@@ -21,6 +22,7 @@ class License extends Depreciable
use SoftDeletes;
use CompanyableTrait;
use HasUploads;
use Loggable, Presentable;
protected $injectUniqueIdentifier = true;
use ValidatingTrait;
@@ -43,13 +45,13 @@ class License extends Depreciable
protected $rules = [
'name' => 'required|string|min:3|max:255',
'seats' => 'required|min:1|integer',
'seats' => 'required|min:1|integer|limit_change:10000', // limit_change is a "pseudo-rule" that translates into 'between', see prepareLimitChangeRule() below
'license_email' => 'email|nullable|max:120',
'license_name' => 'string|nullable|max:100',
'notes' => 'string|nullable',
'category_id' => 'required|exists:categories,id',
'company_id' => 'integer|nullable',
'purchase_cost'=> 'numeric|nullable|gte:0',
'purchase_cost' => 'numeric|nullable|gte:0|max:99999999999999999.99',
'purchase_date' => 'date_format:Y-m-d|nullable|max:10|required_with:depreciation_id',
'expiration_date' => 'date_format:Y-m-d|nullable|max:10',
'termination_date' => 'date_format:Y-m-d|nullable|max:10',
@@ -113,6 +115,7 @@ class License extends Depreciable
'company' => ['name'],
'category' => ['name'],
'depreciation' => ['name'],
'supplier' => ['name'],
];
protected $appends = ['free_seat_count'];
@@ -120,39 +123,51 @@ 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)
{
$actual_seat_count = $this->licenseseats()->count(); //we use the *actual* seat count here, in case your license has gone wonky
$lower_bound = $actual_seat_count - $parameters[0];
$upper_bound = $actual_seat_count + $parameters[0];
return ["between", ($lower_bound <= 0 ? 1 : $lower_bound), $upper_bound];
}
/**
* 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)
@@ -164,21 +179,17 @@ class License extends Depreciable
// On Create, we just make one for each of the seats.
$change = abs($oldSeats - $newSeats);
if ($oldSeats > $newSeats) {
$license->load('licenseseats.user');
// Need to delete seats... lets see if if we have enough.
$seatsAvailableForDelete = $license->licenseseats->reject(function ($seat) {
return ((bool) $seat->assigned_to) || ((bool) $seat->asset_id);
});
$seatsAvailableForDelete = $license->licenseseats()->whereNull('assigned_to')->whereNull('asset_id')->limit($change);
if ($change > $seatsAvailableForDelete->count()) {
Session::flash('error', trans('admin/licenses/message.assoc_users'));
return false;
}
for ($i = 1; $i <= $change; $i++) {
$seatsAvailableForDelete->pop()->delete();
}
$seatsAvailableForDelete->delete();
// Log Deletion of seats.
$logAction = new Actionlog;
$logAction->item_type = self::class;
@@ -203,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) {
@@ -228,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)
@@ -240,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)
@@ -252,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)
@@ -269,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)
@@ -285,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();
}
@@ -296,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()
@@ -308,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()
@@ -320,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()
@@ -332,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()
@@ -347,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()
@@ -364,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') {
@@ -385,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()
@@ -397,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()
@@ -407,28 +423,13 @@ class License extends Depreciable
->orderBy('created_at', 'desc');
}
/**
* Establishes the license -> action logs -> uploads relationship
*
* @author A. Gianotto <snipe@snipe.net>
* @since [v2.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function uploads()
{
return $this->hasMany(\App\Models\Actionlog::class, 'item_id')
->where('item_type', '=', self::class)
->where('action_type', '=', 'uploaded')
->whereNotNull('filename')
->orderBy('created_at', 'desc');
}
/**
* 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()
@@ -442,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()
@@ -458,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()
@@ -475,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()
@@ -487,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()
@@ -503,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()
@@ -517,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
*/
@@ -525,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()
@@ -541,7 +542,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()
@@ -557,22 +558,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()
@@ -603,7 +606,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() : int
@@ -620,7 +623,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()
@@ -636,7 +639,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()
@@ -648,7 +651,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()
@@ -662,7 +665,7 @@ 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()
@@ -683,7 +686,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()
@@ -697,7 +700,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)
@@ -715,8 +718,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
*/
@@ -729,8 +732,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
*/
@@ -743,8 +746,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
*/