A free open source IT asset/license management system
Added scopes
snipe58cda5ae6d75
diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.phpindex db39f987aa..105a20bd86 100644--- a/app/Http/Controllers/Api/LicensesController.php+++ b/app/Http/Controllers/Api/LicensesController.php@@ -26,6 +26,12 @@ class LicensesController extends Controller$licenses = License::with('company', 'manufacturer', 'supplier','category', 'adminuser')->withCount('freeSeats as free_seats_count');+ if ($request->input('status')=='inactive') {+ $licenses->ExpiredLicenses();+ } else {+ $licenses->ActiveLicenses();+ }+if ($request->filled('company_id')) {$licenses->where('licenses.company_id', '=', $request->input('company_id'));}@@ -94,6 +100,8 @@ class LicensesController extends Controller$licenses->onlyTrashed();}++// Make sure the offset and limit are actually integers and do not exceed system limits$offset = ($request->input('offset') > $licenses->count()) ? $licenses->count() : app('api_offset_value');$limit = app('api_limit_value');diff --git a/app/Http/Controllers/Licenses/LicenseCheckoutController.php b/app/Http/Controllers/Licenses/LicenseCheckoutController.phpindex 5b2d344ba5..90aff29627 100644--- a/app/Http/Controllers/Licenses/LicenseCheckoutController.php+++ b/app/Http/Controllers/Licenses/LicenseCheckoutController.php@@ -25,7 +25,7 @@ class LicenseCheckoutController extends Controller* @author [A. Gianotto] [<[email protected]>]* @since [v1.0]* @param $id- * @return \Illuminate\Contracts\View\View+ * @return \Illuminate\Contracts\View\View |\Illuminate\Http\RedirectResponse* @throws \Illuminate\Auth\Access\AuthorizationException*/public function create(License $license)@@ -39,6 +39,11 @@ class LicenseCheckoutController extends Controllerreturn redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkout.not_enough_seats'));}+ // Make sure the license is expired or terminated+ if ($license->isInactive()){+ return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkout.license_is_inactive'));+ }+// We don't currently allow checking out licenses to locations, so we'll reset that to user if neededif (session()->get('checkout_to_type') == 'location') {session()->put(['checkout_to_type' => 'user']);@@ -70,8 +75,19 @@ class LicenseCheckoutController extends Controllerreturn redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.not_found'));}+$this->authorize('checkout', $license);+ // Make sure there is at least one available to checkout+ if ($license->availCount()->count() < 1) {+ return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkout.not_enough_seats'));+ }++ // Make sure the license is expired or terminated+ if ($license->isInactive()) {+ return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkout.license_is_inactive'));+ }+$licenseSeat = $this->findLicenseSeatToCheckout($license, $seatId);$licenseSeat->created_by = auth()->id();$licenseSeat->notes = $request->input('notes');@@ -114,6 +130,12 @@ class LicenseCheckoutController extends Controllerthrow new \Illuminate\Http\Exceptions\HttpResponseException(redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkout.not_enough_seats')));}++ \Log::debug('Seat ID: '.$seatId);+ \Log::debug('License: '.$license);+ \Log::debug('$licenseSeat->license: '.$licenseSeat->license);+ // dd($licenseSeat->license);+ // dd($license);if (! $licenseSeat->license->is($license)) {throw new \Illuminate\Http\Exceptions\HttpResponseException(redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.checkout.mismatch')));}diff --git a/app/Http/Transformers/LicensesTransformer.php b/app/Http/Transformers/LicensesTransformer.phpindex 39079c6ec5..c360834c1c 100644--- a/app/Http/Transformers/LicensesTransformer.php+++ b/app/Http/Transformers/LicensesTransformer.php@@ -31,11 +31,11 @@ class LicensesTransformer'purchase_order' => ($license->purchase_order) ? e($license->purchase_order) : null,'purchase_date' => Helper::getFormattedDateObject($license->purchase_date, 'date'),'termination_date' => Helper::getFormattedDateObject($license->termination_date, 'date'),+ 'expiration_date' => Helper::getFormattedDateObject($license->expiration_date, 'date'),'depreciation' => ($license->depreciation) ? ['id' => (int) $license->depreciation->id,'name'=> e($license->depreciation->name)] : null,'purchase_cost' => Helper::formatCurrencyOutput($license->purchase_cost),'purchase_cost_numeric' => $license->purchase_cost,'notes' => Helper::parseEscapedMarkedownInline($license->notes),- 'expiration_date' => Helper::getFormattedDateObject($license->expiration_date, 'date'),'seats' => (int) $license->seats,'free_seats_count' => (int) $license->free_seats_count - License::unReassignableCount($license),'remaining' => (int) $license->free_seats_count,diff --git a/app/Models/License.php b/app/Models/License.phpindex d0a0faf8d9..3edc7a10a9 100755--- a/app/Models/License.php+++ b/app/Models/License.php@@ -299,16 +299,17 @@ class License extends Depreciable}public function isInactive(): bool-{- $day = now()->startOfDay();+ {+ $day = now()->startOfDay();- $expired = $this->expiration_date && $this->asDateTime($this->expiration_date)->startofDay()->lessThanOrEqualTo($day);+ $expired = $this->expiration_date && $this->asDateTime($this->expiration_date)->startofDay()->lessThanOrEqualTo($day);- $terminated = $this->termination_date && $this->asDateTime($this->termination_date)->startofDay()->lessThanOrEqualTo($day);+ $terminated = $this->termination_date && $this->asDateTime($this->termination_date)->startofDay()->lessThanOrEqualTo($day);- return $expired || $terminated;-}+ return $expired || $terminated;+ }+/*** Sets free_seat_count attribute*@@ -750,6 +751,38 @@ class License extends Depreciable->get();}+ public function scopeActiveLicenses($query)+ {++ return $query->whereNull('deleted_at')++ // The termination date is null or within range+ ->where(function ($query) {+ $query->whereNull('termination_date')+ ->orWhereDate('termination_date', '>', [Carbon::now()]);+ })+ ->where(function ($query) {+ $query->whereNull('expiration_date')+ ->orWhereDate('expiration_date', '>', [Carbon::now()]);+ });+ }++ public function scopeExpiredLicenses($query)+ {++ return $query->whereNull('deleted_at')++ // The termination date is null or within range+ ->where(function ($query) {+ $query->whereNull('termination_date')+ ->orWhereDate('termination_date', '<=', [Carbon::now()]);+ })+ ->orWhere(function ($query) {+ $query->whereNull('expiration_date')+ ->orWhereDate('expiration_date', '<=', [Carbon::now()]);+ });+ }+/*** Query builder scope to order on manufacturer*