License.php
Go to the documentation of this file.
1 <?php
2 namespace App\Models;
3 
5 use DB;
7 
9 
10 class License extends Depreciable
11 {
12  use SoftDeletes;
13  use CompanyableTrait;
14  protected $injectUniqueIdentifier = true;
15  use ValidatingTrait;
16 
17  protected $dates = ['deleted_at'];
18 
19  public $timestamps = true;
20 
21  protected $guarded = 'id';
22  protected $table = 'licenses';
23  protected $rules = array(
24  'name' => 'required|string|min:3|max:255',
25  'serial' => 'required|min:5',
26  'seats' => 'required|min:1|max:10000|integer',
27  'license_email' => 'email|min:0|max:120',
28  'license_name' => 'string|min:0|max:100',
29  'note' => 'string',
30  'notes' => 'string|min:0',
31  'company_id' => 'integer',
32  );
33 
34  public function company()
35  {
36  return $this->belongsTo('\App\Models\Company', 'company_id');
37  }
38 
42  public function assignedusers()
43  {
44  return $this->belongsToMany('\App\Models\User', 'license_seats', 'assigned_to', 'license_id');
45  }
46 
50  public function assetlog()
51  {
52  return $this->hasMany('\App\Models\Actionlog', 'asset_id')
53  ->where('asset_type', '=', 'software')
54  ->orderBy('created_at', 'desc');
55  }
56 
60  public function uploads()
61  {
62  return $this->hasMany('\App\Models\Actionlog', 'asset_id')
63  ->where('asset_type', '=', 'software')
64  ->where('action_type', '=', 'uploaded')
65  ->whereNotNull('filename')
66  ->orderBy('created_at', 'desc');
67  }
68 
69 
73  public function adminuser()
74  {
75  return $this->belongsTo('\App\Models\User', 'user_id');
76  }
77 
81  public static function assetcount()
82  {
83  return LicenseSeat::whereNull('deleted_at')
84  ->count();
85  }
86 
87 
91  public function totalSeatsByLicenseID()
92  {
93  return LicenseSeat::where('license_id', '=', $this->id)
94  ->whereNull('deleted_at')
95  ->count();
96  }
97 
98 
102  public static function availassetcount()
103  {
104  return LicenseSeat::whereNull('assigned_to')
105  ->whereNull('asset_id')
106  ->whereNull('deleted_at')
107  ->count();
108  }
109 
113  public function availcount()
114  {
115  return LicenseSeat::whereNull('assigned_to')
116  ->whereNull('asset_id')
117  ->where('license_id', '=', $this->id)
118  ->whereNull('deleted_at')
119  ->count();
120  }
121 
126  public function assignedcount()
127  {
128 
129  return \App\Models\LicenseSeat::where('license_id', '=', $this->id)
130  ->where(function ($query) {
131 
132  $query->whereNotNull('assigned_to')
133  ->orWhereNotNull('asset_id');
134  })
135  ->count();
136 
137 
138  }
139 
140  public function remaincount()
141  {
142  $total = $this->totalSeatsByLicenseID();
143  $taken = $this->assignedcount();
144  $diff = ($total - $taken);
145  return $diff;
146  }
147 
151  public function totalcount()
152  {
153  $avail = $this->availcount();
154  $taken = $this->assignedcount();
155  $diff = ($avail + $taken);
156  return $diff;
157  }
158 
162  public function licenseseats()
163  {
164  return $this->hasMany('\App\Models\LicenseSeat');
165  }
166 
167  public function supplier()
168  {
169  return $this->belongsTo('\App\Models\Supplier', 'supplier_id');
170  }
171 
172  public function freeSeat()
173  {
174  $seat = LicenseSeat::where('license_id', '=', $this->id)
175  ->whereNull('deleted_at')
176  ->whereNull('assigned_to')
177  ->whereNull('asset_id')
178  ->first();
179  return $seat->id;
180  }
181 
182  public static function getExpiringLicenses($days = 60)
183  {
184 
185  return License::whereNotNull('expiration_date')
186  ->whereNull('deleted_at')
187  ->whereRaw(DB::raw('DATE_SUB(`expiration_date`,INTERVAL '.$days.' DAY) <= DATE(NOW()) '))
188  ->where('expiration_date', '>', date("Y-m-d"))
189  ->orderBy('expiration_date', 'ASC')
190  ->get();
191 
192  }
193 
202  public function scopeTextSearch($query, $search)
203  {
204 
205  return $query->where(function ($query) use ($search) {
206 
207  $query->where('name', 'LIKE', '%'.$search.'%')
208  ->orWhere('serial', 'LIKE', '%'.$search.'%')
209  ->orWhere('notes', 'LIKE', '%'.$search.'%')
210  ->orWhere('order_number', 'LIKE', '%'.$search.'%')
211  ->orWhere('purchase_date', 'LIKE', '%'.$search.'%')
212  ->orWhere('purchase_cost', 'LIKE', '%'.$search.'%');
213  });
214  }
215 }
uploads()
Get uploads for this asset.
Definition: License.php:60
totalcount()
Get the total number of seats.
Definition: License.php:151
adminuser()
Get admin user for this asset.
Definition: License.php:73
assignedusers()
Get the assigned user.
Definition: License.php:42
scopeTextSearch($query, $search)
Query builder scope to search on text.
Definition: License.php:202
static assetcount()
Get total licenses.
Definition: License.php:81
availcount()
Get the number of available seats.
Definition: License.php:113
static availassetcount()
Get total licenses not checked out.
Definition: License.php:102
totalSeatsByLicenseID()
Get total licenses.
Definition: License.php:91
assignedcount()
Get the number of assigned seats.
Definition: License.php:126
static getExpiringLicenses($days=60)
Definition: License.php:182
licenseseats()
Get license seat data.
Definition: License.php:162
assetlog()
Get asset logs for this asset.
Definition: License.php:50