Skip to content

Commit 09e843a

Browse files
committed
Merge remote-tracking branch 'origin/develop'
Signed-off-by: snipe <[email protected]> # Conflicts: # public/css/build/app.css # public/css/build/app.css.map # public/css/build/overrides.css # public/css/build/overrides.css.map # public/css/dist/all.css # public/mix-manifest.json
2 parents 77b79db + 6731e44 commit 09e843a

File tree

27 files changed

+890
-222
lines changed

27 files changed

+890
-222
lines changed

app/Http/Controllers/Api/LocationsController.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ public function index(Request $request) : JsonResponse | array
3737
'address',
3838
'address2',
3939
'assets_count',
40-
'assets_count',
41-
'assigned_accessories_count',
42-
'assigned_assets_count',
4340
'assigned_assets_count',
41+
'rtd_assets_count',
42+
'accessories_count',
43+
'assigned_accessories_count',
44+
'components_count',
45+
'consumables_count',
46+
'users_count',
47+
'children_count',
4448
'city',
4549
'country',
4650
'created_at',
@@ -54,7 +58,6 @@ public function index(Request $request) : JsonResponse | array
5458
'rtd_assets_count',
5559
'state',
5660
'updated_at',
57-
'users_count',
5861
'zip',
5962
'notes',
6063
];
@@ -79,15 +82,18 @@ public function index(Request $request) : JsonResponse | array
7982
'locations.currency',
8083
'locations.company_id',
8184
'locations.notes',
85+
'locations.created_by',
86+
'locations.deleted_at',
8287
])
83-
->withCount('assignedAssets as assigned_assets_count')
8488
->withCount('assignedAssets as assigned_assets_count')
8589
->withCount('assets as assets_count')
8690
->withCount('assignedAccessories as assigned_accessories_count')
8791
->withCount('accessories as accessories_count')
8892
->withCount('rtd_assets as rtd_assets_count')
8993
->withCount('children as children_count')
9094
->withCount('users as users_count')
95+
->withCount('consumables as consumables_count')
96+
->withCount('components as components_count')
9197
->with('adminuser');
9298

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

140+
if ($request->filled('parent_id')) {
141+
$locations->where('locations.parent_id', '=', $request->input('parent_id'));
142+
}
143+
144+
if ($request->input('status') == 'deleted') {
145+
$locations->onlyTrashed();
146+
}
147+
134148
// Make sure the offset and limit are actually integers and do not exceed system limits
135149
$offset = ($request->input('offset') > $locations->count()) ? $locations->count() : app('api_offset_value');
136150
$limit = app('api_limit_value');
@@ -224,8 +238,13 @@ public function show($id) : JsonResponse | array
224238
])
225239
->withCount('assignedAssets as assigned_assets_count')
226240
->withCount('assets as assets_count')
241+
->withCount('assignedAccessories as assigned_accessories_count')
242+
->withCount('accessories as accessories_count')
227243
->withCount('rtd_assets as rtd_assets_count')
244+
->withCount('children as children_count')
228245
->withCount('users as users_count')
246+
->withCount('consumables as consumables_count')
247+
->withCount('components as components_count')
229248
->findOrFail($id);
230249

231250
return (new LocationsTransformer)->transformLocation($location);
@@ -320,11 +339,15 @@ public function destroy($id) : JsonResponse
320339
{
321340
$this->authorize('delete', Location::class);
322341
$location = Location::withCount('assignedAssets as assigned_assets_count')
342+
->withCount('assignedAssets as assigned_assets_count')
323343
->withCount('assets as assets_count')
344+
->withCount('assignedAccessories as assigned_accessories_count')
345+
->withCount('accessories as accessories_count')
324346
->withCount('rtd_assets as rtd_assets_count')
325347
->withCount('children as children_count')
326348
->withCount('users as users_count')
327-
->withCount('accessories as accessories_count')
349+
->withCount('consumables as consumables_count')
350+
->withCount('components as components_count')
328351
->findOrFail($id);
329352

330353
if (! $location->isDeletable()) {

app/Http/Controllers/LocationsController.php

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -189,30 +189,36 @@ public function destroy($locationId) : RedirectResponse
189189
{
190190
$this->authorize('delete', Location::class);
191191

192-
if (is_null($location = Location::find($locationId))) {
192+
$location = Location::withCount('assignedAssets as assigned_assets_count')
193+
->withCount('assets as assets_count')
194+
->withCount('assignedAccessories as assigned_accessories_count')
195+
->withCount('accessories as accessories_count')
196+
->withCount('rtd_assets as rtd_assets_count')
197+
->withCount('children as children_count')
198+
->withCount('users as users_count')
199+
->withCount('consumables as consumables_count')
200+
->withCount('components as components_count')
201+
->find($locationId);
202+
203+
if (!$location) {
193204
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.does_not_exist'));
194205
}
195206

196-
if ($location->users()->count() > 0) {
197-
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_users'));
198-
} elseif ($location->children()->count() > 0) {
199-
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_child_loc'));
200-
} elseif ($location->assets()->count() > 0) {
201-
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_assets'));
202-
} elseif ($location->assignedassets()->count() > 0) {
203-
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_assets'));
204-
}
207+
if ($location->isDeletable()) {
205208

206-
if ($location->image) {
207-
try {
208-
Storage::disk('public')->delete('locations/'.$location->image);
209-
} catch (\Exception $e) {
210-
Log::error($e);
209+
if ($location->image) {
210+
try {
211+
Storage::disk('public')->delete('locations/'.$location->image);
212+
} catch (\Exception $e) {
213+
Log::error($e);
214+
}
211215
}
216+
$location->delete();
217+
return redirect()->to(route('locations.index'))->with('success', trans('admin/locations/message.delete.success'));
218+
} else {
219+
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_users'));
212220
}
213-
$location->delete();
214221

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

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

249255
if ($location = Location::where('id', $id)->first()) {
250-
$parent = Location::where('id', $location->parent_id)->first();
251-
$manager = User::where('id', $location->manager_id)->first();
252-
$company = Company::where('id', $location->company_id)->first();
253-
$users = User::where('location_id', $id)->with('company', 'department', 'location')->get();
254-
$assets = Asset::where('assigned_to', $id)->where('assigned_type', Location::class)->with('model', 'model.category')->get();
255256
return view('locations/print')
256-
->with('assets', $assets)
257-
->with('users',$users)
257+
->with('assigned', false)
258+
->with('assets', $location->assets)
259+
->with('assignedAssets', $location->assignedAssets)
260+
->with('accessories', $location->accessories)
261+
->with('assignedAccessories', $location->assignedAccessories)
262+
->with('users',$location->users)
258263
->with('location', $location)
259-
->with('parent', $parent)
260-
->with('manager', $manager)
261-
->with('company', $company);
264+
->with('consumables', $location->consumables)
265+
->with('components', $location->components)
266+
->with('children', $location->children);
262267
}
263268

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

272+
public function print_all_assigned($id) : View | RedirectResponse
273+
{
274+
$this->authorize('view', Location::class);
275+
if ($location = Location::where('id', $id)->first()) {
276+
return view('locations/print')
277+
->with('assigned', true)
278+
->with('assets', $location->assets)
279+
->with('assignedAssets', $location->assignedAssets)
280+
->with('accessories', $location->accessories)
281+
->with('assignedAccessories', $location->assignedAccessories)
282+
->with('users',$location->users)
283+
->with('location', $location)
284+
->with('consumables', $location->consumables)
285+
->with('components', $location->components)
286+
->with('children', $location->children);
287+
}
288+
return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist'));
289+
}
290+
267291

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

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

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

330353
}
331-
public function print_all_assigned($id) : View | RedirectResponse
332-
{
333-
$this->authorize('view', Location::class);
334-
if ($location = Location::where('id', $id)->first()) {
335-
$parent = Location::where('id', $location->parent_id)->first();
336-
$manager = User::where('id', $location->manager_id)->first();
337-
$company = Company::where('id', $location->company_id)->first();
338-
$users = User::where('location_id', $id)->with('company', 'department', 'location')->get();
339-
$assets = Asset::where('location_id', $id)->with('model', 'model.category')->get();
340-
return view('locations/print')
341-
->with('assets', $assets)
342-
->with('users',$users)
343-
->with('location', $location)
344-
->with('parent', $parent)
345-
->with('manager', $manager)
346-
->with('company', $company);
347-
}
348-
return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist'));
349-
}
350-
351354

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

373380
$valid_count = 0;
@@ -400,9 +407,13 @@ public function postBulkDeleteStore(Request $request) : RedirectResponse
400407
$locations = Location::whereIn('id', $locations_raw_array)
401408
->withCount('assignedAssets as assigned_assets_count')
402409
->withCount('assets as assets_count')
410+
->withCount('assignedAccessories as assigned_accessories_count')
411+
->withCount('accessories as accessories_count')
403412
->withCount('rtd_assets as rtd_assets_count')
404413
->withCount('children as children_count')
405-
->withCount('users as users_count')->get();
414+
->withCount('users as users_count')
415+
->withCount('consumables as consumables_count')
416+
->withCount('components as components_count')->get();
406417

407418
$success_count = 0;
408419
$error_count = 0;

app/Http/Transformers/LocationsTransformer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public function transformLocation(Location $location = null)
5353
'assets_count' => (int) $location->assets_count,
5454
'rtd_assets_count' => (int) $location->rtd_assets_count,
5555
'users_count' => (int) $location->users_count,
56+
'consumables_count' => (int) $location->consumables_count,
57+
'components_count' => (int) $location->components_count,
58+
'children_count' => (int) $location->children_count,
5659
'currency' => ($location->currency) ? e($location->currency) : null,
5760
'ldap_ou' => ($location->ldap_ou) ? e($location->ldap_ou) : null,
5861
'notes' => Helper::parseEscapedMarkedownInline($location->notes),
@@ -76,12 +79,13 @@ public function transformLocation(Location $location = null)
7679
];
7780

7881
$permissions_array['available_actions'] = [
79-
'update' => Gate::allows('update', Location::class) ? true : false,
82+
'update' => (Gate::allows('update', Location::class) && ($location->deleted_at == '')),
8083
'delete' => $location->isDeletable(),
8184
'bulk_selectable' => [
8285
'delete' => $location->isDeletable()
8386
],
8487
'clone' => (Gate::allows('create', Location::class) && ($location->deleted_at == '')),
88+
'restore' => (Gate::allows('create', Location::class) && ($location->deleted_at != '')),
8589
];
8690

8791
$array += $permissions_array;

app/Listeners/CheckoutableListener.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,25 @@ public function onCheckedOut($event)
9696

9797
if (!empty($to)) {
9898
try {
99-
Mail::to(array_flatten($to))->send($mailable->locale($notifiable->locale));
100-
Mail::to(array_flatten($cc))->send($mailable->locale(Setting::getSettings()->locale));
99+
$toMail = (clone $mailable)->locale($notifiable->locale);
100+
Mail::to(array_flatten($to))->send($toMail);
101101
Log::info('Checkout Mail sent to checkout target');
102102
} catch (ClientException $e) {
103103
Log::debug("Exception caught during checkout email: " . $e->getMessage());
104104
} catch (Exception $e) {
105105
Log::debug("Exception caught during checkout email: " . $e->getMessage());
106106
}
107107
}
108+
if (!empty($cc)) {
109+
try {
110+
$ccMail = (clone $mailable)->locale(Setting::getSettings()->locale);
111+
Mail::to(array_flatten($cc))->send($ccMail);
112+
} catch (ClientException $e) {
113+
Log::debug("Exception caught during checkout email: " . $e->getMessage());
114+
} catch (Exception $e) {
115+
Log::debug("Exception caught during checkout email: " . $e->getMessage());
116+
}
117+
}
108118
}
109119

110120
if ($shouldSendWebhookNotification) {
@@ -179,16 +189,26 @@ public function onCheckedIn($event)
179189

180190
[$to, $cc] = $this->generateEmailRecipients($shouldSendEmailToUser, $shouldSendEmailToAlertAddress, $notifiable);
181191

182-
try {
183-
if (!empty($to)) {
184-
Mail::to(array_flatten($to))->send($mailable->locale($notifiable->locale));
185-
Mail::to(array_flatten($cc))->send($mailable->locale(Setting::getSettings()->locale));
186-
Log::info('Checkin Mail sent to CC addresses');
192+
if (!empty($to)) {
193+
try {
194+
$toMail = (clone $mailable)->locale($notifiable->locale);
195+
Mail::to(array_flatten($to))->send($toMail);
196+
Log::info('Checkin Mail sent to checkin target');
197+
} catch (ClientException $e) {
198+
Log::debug("Exception caught during checkin email: " . $e->getMessage());
199+
} catch (Exception $e) {
200+
Log::debug("Exception caught during checkin email: " . $e->getMessage());
201+
}
202+
}
203+
if (!empty($cc)) {
204+
try {
205+
$ccMail = (clone $mailable)->locale(Setting::getSettings()->locale);
206+
Mail::to(array_flatten($cc))->send($ccMail);
207+
} catch (ClientException $e) {
208+
Log::debug("Exception caught during checkin email: " . $e->getMessage());
209+
} catch (Exception $e) {
210+
Log::debug("Exception caught during checkin email: " . $e->getMessage());
187211
}
188-
} catch (ClientException $e) {
189-
Log::debug("Exception caught during checkin email: " . $e->getMessage());
190-
} catch (Exception $e) {
191-
Log::debug("Exception caught during checkin email: " . $e->getMessage());
192212
}
193213
}
194214

app/Models/Location.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Location extends SnipeModel
4343
'company_id' => 'integer',
4444
];
4545

46+
4647
/**
4748
* Whether the model should inject its identifier to the unique
4849
* validation rules before attempting validation. If this property
@@ -113,13 +114,19 @@ public function isDeletable()
113114
{
114115

115116
return Gate::allows('delete', $this)
116-
&& ($this->assets_count == 0)
117-
&& ($this->assigned_assets_count == 0)
118-
&& ($this->children_count == 0)
119-
&& ($this->accessories_count == 0)
120-
&& ($this->users_count == 0);
117+
&& ($this->deleted_at == '')
118+
&& (($this->assets_count ?? $this->assets()->count()) === 0)
119+
&& (($this->assigned_assets_count ?? $this->assignedAssets()->count()) === 0)
120+
&& (($this->accessories_count ?? $this->accessories()->count()) === 0)
121+
&& (($this->assigned_accessories_count ?? $this->assignedAccessories()->count()) === 0)
122+
&& (($this->children_count ?? $this->children()->count()) === 0)
123+
&& (($this->components_count ?? $this->components()->count()) === 0)
124+
&& (($this->consumables_count ?? $this->consumables()->count()) === 0)
125+
&& (($this->rtd_assets_count ?? $this->rtd_assets()->count()) === 0)
126+
&& (($this->users_count ?? $this->users()->count()) === 0);
121127
}
122128

129+
123130
/**
124131
* Establishes the user -> location relationship
125132
*

app/Notifications/AuditNotification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function toSlack()
6161
->from(($this->settings->webhook_botname) ? $this->settings->webhook_botname : 'Snipe-Bot')
6262
->to($channel)
6363
->attachment(function ($attachment) {
64-
$item = $this->params['item'];
64+
$item = $this->params['item'] ?? null;
6565
$admin_user = $this->params['admin'];
6666
$fields = [
6767
'By' => '<'.$admin_user->present()->viewUrl().'|'.$admin_user->display_name.'>',

0 commit comments

Comments
 (0)