Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions app/Http/Controllers/Api/LocationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ public function index(Request $request) : JsonResponse | array
'address',
'address2',
'assets_count',
'assets_count',
'assigned_accessories_count',
'assigned_assets_count',
'assigned_assets_count',
'rtd_assets_count',
'accessories_count',
'assigned_accessories_count',
'components_count',
'consumables_count',
'users_count',
'children_count',
'city',
'country',
'created_at',
Expand All @@ -54,7 +58,6 @@ public function index(Request $request) : JsonResponse | array
'rtd_assets_count',
'state',
'updated_at',
'users_count',
'zip',
'notes',
];
Expand All @@ -79,15 +82,18 @@ public function index(Request $request) : JsonResponse | array
'locations.currency',
'locations.company_id',
'locations.notes',
'locations.created_by',
'locations.deleted_at',
])
->withCount('assignedAssets as assigned_assets_count')
->withCount('assignedAssets as assigned_assets_count')
->withCount('assets as assets_count')
->withCount('assignedAccessories as assigned_accessories_count')
->withCount('accessories as accessories_count')
->withCount('rtd_assets as rtd_assets_count')
->withCount('children as children_count')
->withCount('users as users_count')
->withCount('consumables as consumables_count')
->withCount('components as components_count')
->with('adminuser');

// Only scope locations if the setting is enabled
Expand Down Expand Up @@ -131,6 +137,14 @@ public function index(Request $request) : JsonResponse | array
$locations->where('locations.company_id', '=', $request->input('company_id'));
}

if ($request->filled('parent_id')) {
$locations->where('locations.parent_id', '=', $request->input('parent_id'));
}

if ($request->input('status') == 'deleted') {
$locations->onlyTrashed();
}

// Make sure the offset and limit are actually integers and do not exceed system limits
$offset = ($request->input('offset') > $locations->count()) ? $locations->count() : app('api_offset_value');
$limit = app('api_limit_value');
Expand Down Expand Up @@ -224,8 +238,13 @@ public function show($id) : JsonResponse | array
])
->withCount('assignedAssets as assigned_assets_count')
->withCount('assets as assets_count')
->withCount('assignedAccessories as assigned_accessories_count')
->withCount('accessories as accessories_count')
->withCount('rtd_assets as rtd_assets_count')
->withCount('children as children_count')
->withCount('users as users_count')
->withCount('consumables as consumables_count')
->withCount('components as components_count')
->findOrFail($id);

return (new LocationsTransformer)->transformLocation($location);
Expand Down Expand Up @@ -320,11 +339,15 @@ public function destroy($id) : JsonResponse
{
$this->authorize('delete', Location::class);
$location = Location::withCount('assignedAssets as assigned_assets_count')
->withCount('assignedAssets as assigned_assets_count')
->withCount('assets as assets_count')
->withCount('assignedAccessories as assigned_accessories_count')
->withCount('accessories as accessories_count')
->withCount('rtd_assets as rtd_assets_count')
->withCount('children as children_count')
->withCount('users as users_count')
->withCount('accessories as accessories_count')
->withCount('consumables as consumables_count')
->withCount('components as components_count')
->findOrFail($id);

if (! $location->isDeletable()) {
Expand Down
109 changes: 60 additions & 49 deletions app/Http/Controllers/LocationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,30 +189,36 @@ public function destroy($locationId) : RedirectResponse
{
$this->authorize('delete', Location::class);

if (is_null($location = Location::find($locationId))) {
$location = Location::withCount('assignedAssets as assigned_assets_count')
->withCount('assets as assets_count')
->withCount('assignedAccessories as assigned_accessories_count')
->withCount('accessories as accessories_count')
->withCount('rtd_assets as rtd_assets_count')
->withCount('children as children_count')
->withCount('users as users_count')
->withCount('consumables as consumables_count')
->withCount('components as components_count')
->find($locationId);

if (!$location) {
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.does_not_exist'));
}

if ($location->users()->count() > 0) {
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_users'));
} elseif ($location->children()->count() > 0) {
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_child_loc'));
} elseif ($location->assets()->count() > 0) {
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_assets'));
} elseif ($location->assignedassets()->count() > 0) {
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_assets'));
}
if ($location->isDeletable()) {

if ($location->image) {
try {
Storage::disk('public')->delete('locations/'.$location->image);
} catch (\Exception $e) {
Log::error($e);
if ($location->image) {
try {
Storage::disk('public')->delete('locations/'.$location->image);
} catch (\Exception $e) {
Log::error($e);
}
}
$location->delete();
return redirect()->to(route('locations.index'))->with('success', trans('admin/locations/message.delete.success'));
} else {
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_users'));
}
$location->delete();

return redirect()->to(route('locations.index'))->with('success', trans('admin/locations/message.delete.success'));
}

/**
Expand Down Expand Up @@ -247,23 +253,41 @@ public function print_assigned($id) : View | RedirectResponse
$this->authorize('view', Location::class);

if ($location = Location::where('id', $id)->first()) {
$parent = Location::where('id', $location->parent_id)->first();
$manager = User::where('id', $location->manager_id)->first();
$company = Company::where('id', $location->company_id)->first();
$users = User::where('location_id', $id)->with('company', 'department', 'location')->get();
$assets = Asset::where('assigned_to', $id)->where('assigned_type', Location::class)->with('model', 'model.category')->get();
return view('locations/print')
->with('assets', $assets)
->with('users',$users)
->with('assigned', false)
->with('assets', $location->assets)
->with('assignedAssets', $location->assignedAssets)
->with('accessories', $location->accessories)
->with('assignedAccessories', $location->assignedAccessories)
->with('users',$location->users)
->with('location', $location)
->with('parent', $parent)
->with('manager', $manager)
->with('company', $company);
->with('consumables', $location->consumables)
->with('components', $location->components)
->with('children', $location->children);
}

return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist'));
}

public function print_all_assigned($id) : View | RedirectResponse
{
$this->authorize('view', Location::class);
if ($location = Location::where('id', $id)->first()) {
return view('locations/print')
->with('assigned', true)
->with('assets', $location->assets)
->with('assignedAssets', $location->assignedAssets)
->with('accessories', $location->accessories)
->with('assignedAccessories', $location->assignedAccessories)
->with('users',$location->users)
->with('location', $location)
->with('consumables', $location->consumables)
->with('components', $location->components)
->with('children', $location->children);
}
return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist'));
}


/**
* Returns a view that presents a form to clone a location.
Expand Down Expand Up @@ -321,33 +345,12 @@ public function postRestore($id) : RedirectResponse
return redirect()->route('locations.index')->with('success', trans('admin/locations/message.restore.success'));
}

// Check validation
return redirect()->back()->with('error', trans('general.could_not_restore', ['item_type' => trans('general.location'), 'error' => $location->getErrors()->first()]));
}

return redirect()->back()->with('error', trans('admin/models/message.does_not_exist'));

}
public function print_all_assigned($id) : View | RedirectResponse
{
$this->authorize('view', Location::class);
if ($location = Location::where('id', $id)->first()) {
$parent = Location::where('id', $location->parent_id)->first();
$manager = User::where('id', $location->manager_id)->first();
$company = Company::where('id', $location->company_id)->first();
$users = User::where('location_id', $id)->with('company', 'department', 'location')->get();
$assets = Asset::where('location_id', $id)->with('model', 'model.category')->get();
return view('locations/print')
->with('assets', $assets)
->with('users',$users)
->with('location', $location)
->with('parent', $parent)
->with('manager', $manager)
->with('company', $company);
}
return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist'));
}


/**
* Returns a view that allows the user to bulk delete locations
Expand All @@ -366,8 +369,12 @@ public function postBulkDelete(Request $request) : View | RedirectResponse
$locations = Location::whereIn('id', $locations_raw_array)
->withCount('assignedAssets as assigned_assets_count')
->withCount('assets as assets_count')
->withCount('assignedAccessories as assigned_accessories_count')
->withCount('accessories as accessories_count')
->withCount('rtd_assets as rtd_assets_count')
->withCount('children as children_count')
->withCount('consumables as consumables_count')
->withCount('components as components_count')
->withCount('users as users_count')->get();

$valid_count = 0;
Expand Down Expand Up @@ -400,9 +407,13 @@ public function postBulkDeleteStore(Request $request) : RedirectResponse
$locations = Location::whereIn('id', $locations_raw_array)
->withCount('assignedAssets as assigned_assets_count')
->withCount('assets as assets_count')
->withCount('assignedAccessories as assigned_accessories_count')
->withCount('accessories as accessories_count')
->withCount('rtd_assets as rtd_assets_count')
->withCount('children as children_count')
->withCount('users as users_count')->get();
->withCount('users as users_count')
->withCount('consumables as consumables_count')
->withCount('components as components_count')->get();

$success_count = 0;
$error_count = 0;
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Transformers/LocationsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function transformLocation(Location $location = null)
'assets_count' => (int) $location->assets_count,
'rtd_assets_count' => (int) $location->rtd_assets_count,
'users_count' => (int) $location->users_count,
'consumables_count' => (int) $location->consumables_count,
'components_count' => (int) $location->components_count,
'children_count' => (int) $location->children_count,
'currency' => ($location->currency) ? e($location->currency) : null,
'ldap_ou' => ($location->ldap_ou) ? e($location->ldap_ou) : null,
'notes' => Helper::parseEscapedMarkedownInline($location->notes),
Expand All @@ -76,12 +79,13 @@ public function transformLocation(Location $location = null)
];

$permissions_array['available_actions'] = [
'update' => Gate::allows('update', Location::class) ? true : false,
'update' => (Gate::allows('update', Location::class) && ($location->deleted_at == '')),
'delete' => $location->isDeletable(),
'bulk_selectable' => [
'delete' => $location->isDeletable()
],
'clone' => (Gate::allows('create', Location::class) && ($location->deleted_at == '')),
'restore' => (Gate::allows('create', Location::class) && ($location->deleted_at != '')),
];

$array += $permissions_array;
Expand Down
17 changes: 12 additions & 5 deletions app/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Location extends SnipeModel
'company_id' => 'integer',
];


/**
* Whether the model should inject its identifier to the unique
* validation rules before attempting validation. If this property
Expand Down Expand Up @@ -113,13 +114,19 @@ public function isDeletable()
{

return Gate::allows('delete', $this)
&& ($this->assets_count == 0)
&& ($this->assigned_assets_count == 0)
&& ($this->children_count == 0)
&& ($this->accessories_count == 0)
&& ($this->users_count == 0);
&& ($this->deleted_at == '')
&& (($this->assets_count ?? $this->assets()->count()) === 0)
&& (($this->assigned_assets_count ?? $this->assignedAssets()->count()) === 0)
&& (($this->accessories_count ?? $this->accessories()->count()) === 0)
&& (($this->assigned_accessories_count ?? $this->assignedAccessories()->count()) === 0)
&& (($this->children_count ?? $this->children()->count()) === 0)
&& (($this->components_count ?? $this->components()->count()) === 0)
&& (($this->consumables_count ?? $this->consumables()->count()) === 0)
&& (($this->rtd_assets_count ?? $this->rtd_assets()->count()) === 0)
&& (($this->users_count ?? $this->users()->count()) === 0);
}


/**
* Establishes the user -> location relationship
*
Expand Down
39 changes: 34 additions & 5 deletions app/Presenters/LocationPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public static function dataTableLayout()
'title' => trans('admin/locations/table.parent'),
'visible' => true,
'formatter' => 'locationsLinkObjFormatter',
], [
'field' => 'users_count',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('general.people'),
'titleTooltip' => trans('general.people'),
'visible' => true,
'class' => 'css-house-user',
], [
'field' => 'assets_count',
'searchable' => false,
Expand Down Expand Up @@ -98,7 +107,7 @@ public static function dataTableLayout()
'titleTooltip' => trans('general.accessories'),
'visible' => true,
'class' => 'css-accessory',
], [
],[
'field' => 'assigned_accessories_count',
'searchable' => false,
'sortable' => true,
Expand All @@ -108,14 +117,34 @@ public static function dataTableLayout()
'visible' => true,
'class' => 'css-accessory-alt',
], [
'field' => 'users_count',
'field' => 'components_count',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('general.people'),
'titleTooltip' => trans('general.people'),
'title' => trans('general.components'),
'titleTooltip' => trans('general.components'),
'visible' => true,
'class' => 'css-house-user',
'class' => 'css-component',
],
[
'field' => 'consumables_count',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('general.consumables'),
'titleTooltip' => trans('general.consumables'),
'visible' => true,
'class' => 'css-consumable',
],
[
'field' => 'children_count',
'searchable' => false,
'sortable' => true,
'switchable' => true,
'title' => trans('general.child_locations'),
'titleTooltip' => trans('general.child_locations'),
'visible' => true,
'class' => 'css-child-locations',
], [
'field' => 'currency',
'searchable' => true,
Expand Down
Loading
Loading