@@ -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 ;
0 commit comments