2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Models\Company;
|
2016-09-06 19:39:42 -07:00
|
|
|
use App\Models\Loggable;
|
2016-03-25 01:18:05 -07:00
|
|
|
use DB;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2016-09-06 19:39:42 -07:00
|
|
|
use Watson\Validating\ValidatingTrait;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
class License extends Depreciable
|
|
|
|
{
|
|
|
|
use SoftDeletes;
|
|
|
|
use CompanyableTrait;
|
2016-09-06 19:39:42 -07:00
|
|
|
use Loggable;
|
2016-03-25 01:18:05 -07:00
|
|
|
protected $injectUniqueIdentifier = true;
|
|
|
|
use ValidatingTrait;
|
|
|
|
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
|
|
|
|
public $timestamps = true;
|
|
|
|
|
|
|
|
protected $guarded = 'id';
|
|
|
|
protected $table = 'licenses';
|
|
|
|
protected $rules = array(
|
|
|
|
'name' => 'required|string|min:3|max:255',
|
|
|
|
'serial' => 'required|min:5',
|
|
|
|
'seats' => 'required|min:1|max:10000|integer',
|
|
|
|
'license_email' => 'email|min:0|max:120',
|
|
|
|
'license_name' => 'string|min:0|max:100',
|
|
|
|
'note' => 'string',
|
|
|
|
'notes' => 'string|min:0',
|
|
|
|
'company_id' => 'integer',
|
|
|
|
);
|
|
|
|
|
|
|
|
public function company()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('\App\Models\Company', 'company_id');
|
|
|
|
}
|
|
|
|
|
2016-08-16 18:49:54 -07:00
|
|
|
public function manufacturer()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('\App\Models\Manufacturer', 'manufacturer_id');
|
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
/**
|
|
|
|
* Get the assigned user
|
|
|
|
*/
|
|
|
|
public function assignedusers()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany('\App\Models\User', 'license_seats', 'assigned_to', 'license_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get asset logs for this asset
|
|
|
|
*/
|
|
|
|
public function assetlog()
|
|
|
|
{
|
2016-09-06 19:39:42 -07:00
|
|
|
return $this->hasMany('\App\Models\Actionlog', 'item_id')
|
|
|
|
->where('item_type', '=', License::class)
|
2016-03-25 01:18:05 -07:00
|
|
|
->orderBy('created_at', 'desc');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get uploads for this asset
|
|
|
|
*/
|
|
|
|
public function uploads()
|
|
|
|
{
|
2016-09-06 19:39:42 -07:00
|
|
|
return $this->hasMany('\App\Models\Actionlog', 'item_id')
|
|
|
|
->where('item_type', '=', License::class)
|
2016-03-25 01:18:05 -07:00
|
|
|
->where('action_type', '=', 'uploaded')
|
|
|
|
->whereNotNull('filename')
|
|
|
|
->orderBy('created_at', 'desc');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get admin user for this asset
|
|
|
|
*/
|
|
|
|
public function adminuser()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('\App\Models\User', 'user_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get total licenses
|
|
|
|
*/
|
|
|
|
public static function assetcount()
|
|
|
|
{
|
|
|
|
return LicenseSeat::whereNull('deleted_at')
|
|
|
|
->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get total licenses
|
|
|
|
*/
|
|
|
|
public function totalSeatsByLicenseID()
|
|
|
|
{
|
|
|
|
return LicenseSeat::where('license_id', '=', $this->id)
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->count();
|
|
|
|
}
|
|
|
|
|
2016-10-28 14:15:13 -07:00
|
|
|
// We do this to eager load the "count" of seats from the controller. Otherwise calling "count()" on each model results in n+1
|
|
|
|
public function licenseSeatsRelation()
|
|
|
|
{
|
|
|
|
return $this->hasMany(LicenseSeat::class)->whereNull('deleted_at')->selectRaw('license_id, count(*) as count')->groupBy('license_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLicenseSeatsCountAttribute()
|
|
|
|
{
|
|
|
|
if ($this->licenseSeatsRelation->first()) {
|
|
|
|
return $this->licenseSeatsRelation->first()->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get total licenses not checked out
|
|
|
|
*/
|
|
|
|
public static function availassetcount()
|
|
|
|
{
|
|
|
|
return LicenseSeat::whereNull('assigned_to')
|
|
|
|
->whereNull('asset_id')
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of available seats
|
|
|
|
*/
|
2016-10-28 14:15:13 -07:00
|
|
|
public function availCount()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-10-28 14:15:13 -07:00
|
|
|
return $this->licenseSeatsRelation()
|
|
|
|
->whereNull('asset_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAvailSeatsCountAttribute()
|
|
|
|
{
|
|
|
|
if ($this->availCount->first()) {
|
|
|
|
return $this->availCount->first()->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of assigned seats
|
|
|
|
*
|
|
|
|
*/
|
2016-10-28 14:15:13 -07:00
|
|
|
public function assignedCount()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-10-28 14:15:13 -07:00
|
|
|
return $this->licenseSeatsRelation()->where(function ($query) {
|
|
|
|
$query->whereNotNull('assigned_to')
|
|
|
|
->orWhereNotNull('asset_id');
|
|
|
|
});
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-10-28 14:15:13 -07:00
|
|
|
public function getAssignedSeatsCountAttribute()
|
|
|
|
{
|
|
|
|
// dd($this->licenseSeatsRelation->first());
|
|
|
|
if ($this->assignedCount->first()) {
|
|
|
|
return $this->assignedCount->first()->count;
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-10-28 14:15:13 -07:00
|
|
|
return 0;
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function remaincount()
|
|
|
|
{
|
2016-10-28 14:15:13 -07:00
|
|
|
$total = $this->licenseSeatsCount;
|
|
|
|
$taken = $this->assigned_seats;
|
2016-03-25 01:18:05 -07:00
|
|
|
$diff = ($total - $taken);
|
|
|
|
return $diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the total number of seats
|
|
|
|
*/
|
|
|
|
public function totalcount()
|
|
|
|
{
|
2016-10-28 14:15:13 -07:00
|
|
|
$avail = $this->availSeatsCount;
|
2016-03-25 01:18:05 -07:00
|
|
|
$taken = $this->assignedcount();
|
|
|
|
$diff = ($avail + $taken);
|
|
|
|
return $diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get license seat data
|
|
|
|
*/
|
|
|
|
public function licenseseats()
|
|
|
|
{
|
|
|
|
return $this->hasMany('\App\Models\LicenseSeat');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function supplier()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('\App\Models\Supplier', 'supplier_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function freeSeat()
|
|
|
|
{
|
|
|
|
$seat = LicenseSeat::where('license_id', '=', $this->id)
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->whereNull('assigned_to')
|
|
|
|
->whereNull('asset_id')
|
|
|
|
->first();
|
|
|
|
return $seat->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getExpiringLicenses($days = 60)
|
|
|
|
{
|
|
|
|
|
|
|
|
return License::whereNotNull('expiration_date')
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->whereRaw(DB::raw('DATE_SUB(`expiration_date`,INTERVAL '.$days.' DAY) <= DATE(NOW()) '))
|
|
|
|
->where('expiration_date', '>', date("Y-m-d"))
|
|
|
|
->orderBy('expiration_date', 'ASC')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query builder scope to search on text
|
|
|
|
*
|
|
|
|
* @param Illuminate\Database\Query\Builder $query Query builder instance
|
|
|
|
* @param text $search Search term
|
|
|
|
*
|
|
|
|
* @return Illuminate\Database\Query\Builder Modified query builder
|
|
|
|
*/
|
|
|
|
public function scopeTextSearch($query, $search)
|
|
|
|
{
|
|
|
|
|
|
|
|
return $query->where(function ($query) use ($search) {
|
|
|
|
|
|
|
|
$query->where('name', 'LIKE', '%'.$search.'%')
|
|
|
|
->orWhere('serial', 'LIKE', '%'.$search.'%')
|
|
|
|
->orWhere('notes', 'LIKE', '%'.$search.'%')
|
|
|
|
->orWhere('order_number', 'LIKE', '%'.$search.'%')
|
2016-09-27 01:29:31 -07:00
|
|
|
->orWhere('purchase_order', 'LIKE', '%'.$search.'%')
|
2016-03-25 01:18:05 -07:00
|
|
|
->orWhere('purchase_date', 'LIKE', '%'.$search.'%')
|
|
|
|
->orWhere('purchase_cost', 'LIKE', '%'.$search.'%');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|