diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php index 176033453..2a6b19eef 100644 --- a/app/Http/Controllers/Api/LocationsController.php +++ b/app/Http/Controllers/Api/LocationsController.php @@ -27,7 +27,7 @@ class LocationsController extends Controller $allowed_columns = [ 'id', 'name', 'address', 'address2', 'city', 'state', 'country', 'zip', 'created_at', 'updated_at', 'manager_id', 'image', - 'assigned_assets_count', 'users_count', 'assets_count', 'currency', 'ldap_ou', ]; + 'assigned_assets_count', 'users_count', 'assets_count','assigned_assets_count', 'assets_location_count', 'rtd_assets_count', 'currency', 'ldap_ou', ]; $locations = Location::with('parent', 'manager', 'children')->select([ 'locations.id', @@ -46,7 +46,8 @@ class LocationsController extends Controller 'locations.ldap_ou', 'locations.currency', ])->withCount('assignedAssets as assigned_assets_count') - ->withCount('assets as assets_count') + ->withCount('assets as assets_location_count') + ->withCount('rtd_assets as rtd_assets_count') ->withCount('users as users_count'); if ($request->filled('search')) { @@ -156,8 +157,10 @@ class LocationsController extends Controller 'locations.currency', ]) ->withCount('assignedAssets as assigned_assets_count') - ->withCount('assets as assets_count') - ->withCount('users as users_count')->findOrFail($id); + ->withCount('assets as assets_location_count') + ->withCount('rtd_assets as rtd_assets_count') + ->withCount('users as users_count') + ->findOrFail($id); return (new LocationsTransformer)->transformLocation($location); } diff --git a/app/Http/Transformers/LocationsTransformer.php b/app/Http/Transformers/LocationsTransformer.php index 78d8cc809..8d6a1315c 100644 --- a/app/Http/Transformers/LocationsTransformer.php +++ b/app/Http/Transformers/LocationsTransformer.php @@ -44,7 +44,8 @@ class LocationsTransformer 'country' => ($location->country) ? e($location->country) : null, 'zip' => ($location->zip) ? e($location->zip) : null, 'assigned_assets_count' => (int) $location->assigned_assets_count, - 'assets_count' => (int) $location->assets_count, + 'assets_location_count' => (int) $location->assets_location_count, + 'rtd_assets_count' => (int) $location->rtd_assets_count, 'users_count' => (int) $location->users_count, 'currency' => ($location->currency) ? e($location->currency) : null, 'ldap_ou' => ($location->ldap_ou) ? e($location->ldap_ou) : null, diff --git a/app/Models/Location.php b/app/Models/Location.php index 07ca6c8a1..8181f406c 100755 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -90,6 +90,14 @@ class Location extends SnipeModel 'parent' => ['name'], ]; + + /** + * Determine whether or not this location can be deleted + * + * @author A. Gianotto + * @since [v3.0] + * @return bool + */ public function isDeletable() { return Gate::allows('delete', $this) @@ -98,12 +106,25 @@ class Location extends SnipeModel && ($this->users()->count() === 0); } + /** + * Establishes the user -> location relationship + * + * @author A. Gianotto + * @since [v3.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ public function users() { return $this->hasMany(\App\Models\User::class, 'location_id'); } - + /** + * Find assets with this location as their location_id + * + * @author A. Gianotto + * @since [v3.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ public function assets() { return $this->hasMany(\App\Models\Asset::class, 'location_id') @@ -114,6 +135,14 @@ class Location extends SnipeModel }); } + + /** + * Establishes the asset -> rtd_location relationship + * + * @author A. Gianotto + * @since [v3.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ public function rtd_assets() { /* This used to have an ...->orHas() clause that referred to @@ -123,48 +152,93 @@ class Location extends SnipeModel It is arguable that we should have a '...->whereNull('assigned_to') bit in there, but that isn't always correct either (in the case where a user has no location, for example). - - In all likelyhood, we need to denorm an "effective_location" column - into Assets to make this slightly less miserable. */ return $this->hasMany(\App\Models\Asset::class, 'rtd_location_id'); } + /** + * Establishes the consumable -> location relationship + * + * @author A. Gianotto + * @since [v3.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ public function consumables() { return $this->hasMany(\App\Models\Consumable::class, 'location_id'); } + /** + * Establishes the component -> location relationship + * + * @author A. Gianotto + * @since [v3.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ public function components() { return $this->hasMany(\App\Models\Component::class, 'location_id'); } + /** + * Establishes the component -> accessory relationship + * + * @author A. Gianotto + * @since [v3.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ public function accessories() { return $this->hasMany(\App\Models\Accessory::class, 'location_id'); } - - + /** + * Find the parent of a location + * + * @author A. Gianotto + * @since [v2.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ public function parent() { return $this->belongsTo(self::class, 'parent_id', 'id') ->with('parent'); } + + /** + * Find the manager of a location + * + * @author A. Gianotto + * @since [v2.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ public function manager() { return $this->belongsTo(\App\Models\User::class, 'manager_id'); } + + /** + * Find children of a location + * + * @author A. Gianotto + * @since [v2.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ public function children() { return $this->hasMany(self::class, 'parent_id') ->with('children'); } - // I don't think we need this anymore since we de-normed location_id in assets? + /** + * Establishes the asset -> location assignment relationship + * + * @author A. Gianotto + * @since [v3.0] + * @return \Illuminate\Database\Eloquent\Relations\Relation + */ public function assignedAssets() { return $this->morphMany(\App\Models\Asset::class, 'assigned', 'assigned_type', 'assigned_to')->withTrashed(); diff --git a/app/Presenters/LocationPresenter.php b/app/Presenters/LocationPresenter.php index 2501677bd..892775ede 100644 --- a/app/Presenters/LocationPresenter.php +++ b/app/Presenters/LocationPresenter.php @@ -51,21 +51,32 @@ class LocationPresenter extends Presenter ], [ - 'field' => 'assets_count', + 'field' => 'assets_location_count', 'searchable' => false, 'sortable' => true, 'switchable' => true, - 'title' => trans('admin/locations/table.assets_rtd'), + 'title' => trans('admin/locations/message.current_location'), 'visible' => true, ], + + [ + 'field' => 'rtd_assets_count', + 'searchable' => false, + 'sortable' => true, + 'switchable' => true, + 'title' => trans('admin/hardware/form.default_location'), + 'visible' => false, + ], + [ 'field' => 'assigned_assets_count', 'searchable' => false, 'sortable' => true, 'switchable' => true, - 'title' => trans('admin/locations/table.assets_checkedout'), + 'title' => trans('admin/locations/message.assigned_assets'), 'visible' => true, ], + [ 'field' => 'users_count', 'searchable' => false, diff --git a/resources/lang/en/admin/locations/message.php b/resources/lang/en/admin/locations/message.php index 3ba1eed3b..22c7fe8f7 100644 --- a/resources/lang/en/admin/locations/message.php +++ b/resources/lang/en/admin/locations/message.php @@ -6,6 +6,8 @@ return array( 'assoc_users' => 'This location is currently associated with at least one user and cannot be deleted. Please update your users to no longer reference this location and try again. ', 'assoc_assets' => 'This location is currently associated with at least one asset and cannot be deleted. Please update your assets to no longer reference this location and try again. ', 'assoc_child_loc' => 'This location is currently the parent of at least one child location and cannot be deleted. Please update your locations to no longer reference this location and try again. ', + 'assigned_assets' => 'Assigned Assets', + 'current_location' => 'Current Location', 'create' => array( diff --git a/resources/views/accessories/index.blade.php b/resources/views/accessories/index.blade.php index 52e970e9f..3b82bfdb6 100755 --- a/resources/views/accessories/index.blade.php +++ b/resources/views/accessories/index.blade.php @@ -8,7 +8,7 @@ @section('header_right') @can('create', \App\Models\Accessory::class) - {{ trans('general.create') }} + {{ trans('general.create') }} @endcan @stop diff --git a/resources/views/components/index.blade.php b/resources/views/components/index.blade.php index e92d49a01..0442b5de0 100644 --- a/resources/views/components/index.blade.php +++ b/resources/views/components/index.blade.php @@ -8,7 +8,7 @@ @section('header_right') @can('create', \App\Models\Component::class) - {{ trans('general.create') }} + {{ trans('general.create') }} @endcan @stop diff --git a/resources/views/consumables/index.blade.php b/resources/views/consumables/index.blade.php index 5bf82ccb9..b654aa884 100644 --- a/resources/views/consumables/index.blade.php +++ b/resources/views/consumables/index.blade.php @@ -8,7 +8,7 @@ @section('header_right') @can('create', \App\Models\Consumable::class) - {{ trans('general.create') }} + {{ trans('general.create') }} @endcan @stop diff --git a/resources/views/hardware/index.blade.php b/resources/views/hardware/index.blade.php index 6c4cb6936..ef095c1ce 100755 --- a/resources/views/hardware/index.blade.php +++ b/resources/views/hardware/index.blade.php @@ -45,7 +45,7 @@ {{ trans('admin/hardware/general.custom_export') }} @can('create', \App\Models\Asset::class) - {{ trans('general.create') }} + {{ trans('general.create') }} @endcan @stop diff --git a/resources/views/layouts/default.blade.php b/resources/views/layouts/default.blade.php index b74239454..4899e4b8a 100644 --- a/resources/views/layouts/default.blade.php +++ b/resources/views/layouts/default.blade.php @@ -131,7 +131,7 @@