diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php index 1eb206d219..d283486a6e 100644 --- a/app/Http/Controllers/Api/AssetModelsController.php +++ b/app/Http/Controllers/Api/AssetModelsController.php @@ -148,6 +148,11 @@ class AssetModelsController extends Controller $this->authorize('delete', AssetModel::class); $assetmodel = AssetModel::findOrFail($id); $this->authorize('delete', $assetmodel); + + if ($assetmodel->assets()->count() > 0) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/models/message.assoc_users'))); + } + $assetmodel->delete(); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/assetmodels/message.delete.success'))); diff --git a/app/Http/Controllers/Api/CategoriesController.php b/app/Http/Controllers/Api/CategoriesController.php index 4be6eb4ed1..f299d09e92 100644 --- a/app/Http/Controllers/Api/CategoriesController.php +++ b/app/Http/Controllers/Api/CategoriesController.php @@ -113,7 +113,16 @@ class CategoriesController extends Controller { $this->authorize('delete', Category::class); $category = Category::findOrFail($id); - $this->authorize('delete', $category); + + if ($category->has_models() > 0) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type'=>'model']))); + } elseif ($category->accessories()->count() > 0) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type'=>'accessory']))); + } elseif ($category->consumables()->count() > 0) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type'=>'consumable']))); + } elseif ($category->components()->count() > 0) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type'=>'component']))); + } $category->delete(); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/categories/message.delete.success'))); diff --git a/app/Http/Controllers/Api/CompaniesController.php b/app/Http/Controllers/Api/CompaniesController.php index 63048f3ba1..ee63e34b0a 100644 --- a/app/Http/Controllers/Api/CompaniesController.php +++ b/app/Http/Controllers/Api/CompaniesController.php @@ -117,9 +117,24 @@ class CompaniesController extends Controller $this->authorize('delete', Company::class); $company = Company::findOrFail($id); $this->authorize('delete', $company); + + try { $company->delete(); return response() ->json(Helper::formatStandardApiResponse('success', null, trans('admin/companies/message.delete.success'))); + } catch (\Illuminate\Database\QueryException $exception) { + /* + * NOTE: This happens when there's a foreign key constraint violation + * For example when rows in other tables are referencing this company + */ + if ($exception->getCode() == 23000) { + return response() + ->json(Helper::formatStandardApiResponse('error', null, trans('admin/companies/message.assoc_users'))); + + } else { + throw $exception; + } + } } } diff --git a/app/Http/Controllers/Api/CustomFieldsController.php b/app/Http/Controllers/Api/CustomFieldsController.php index 7a4fa59345..2cae7e1af1 100644 --- a/app/Http/Controllers/Api/CustomFieldsController.php +++ b/app/Http/Controllers/Api/CustomFieldsController.php @@ -35,4 +35,26 @@ class CustomFieldsController extends Controller return $fieldset->fields()->sync($fields); } + + + /** + * Delete a custom field. + * + * @author [Brady Wetherington] [] + * @since [v1.8] + * @return Redirect + */ + public function destroy($field_id) + { + $field = CustomField::find($field_id); + + if ($field->fieldset->count() >0) { + return response()->json(Helper::formatStandardApiResponse('error', null, 'Field is in use.')); + } else { + $field->delete(); + return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/custom_fields/message.field.delete.success'))); + + } + } + } diff --git a/app/Http/Controllers/Api/DepreciationsController.php b/app/Http/Controllers/Api/DepreciationsController.php index 30ef61bc67..f8643f9d9c 100644 --- a/app/Http/Controllers/Api/DepreciationsController.php +++ b/app/Http/Controllers/Api/DepreciationsController.php @@ -6,8 +6,7 @@ use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Helpers\Helper; use App\Models\Depreciation; -use App\Models\Asset; -use App\Http\Transformers\DatatablesTransformer; +use App\Http\Transformers\DepreciationsTransformer; class DepreciationsController extends Controller { @@ -23,7 +22,7 @@ class DepreciationsController extends Controller $this->authorize('view', Depreciation::class); $allowed_columns = ['id','name','created_at']; - $depreciations = Depreciation::select('id','name','months'); + $depreciations = Depreciation::select('id','name','months','user_id','created_at','updated_at'); if ($request->has('search')) { $depreciations = $depreciations->TextSearch($request->input('search')); @@ -37,7 +36,7 @@ class DepreciationsController extends Controller $total = $depreciations->count(); $depreciations = $depreciations->skip($offset)->take($limit)->get(); - return (new DatatablesTransformer)->transformDatatables($depreciations, $total); + return (new DepreciationsTransformer)->transformDepreciations($depreciations, $total); } @@ -74,7 +73,7 @@ class DepreciationsController extends Controller { $this->authorize('view', Depreciation::class); $depreciation = Depreciation::findOrFail($id); - return $depreciation; + return (new DepreciationsTransformer)->transformDepreciation($depreciation); } @@ -113,6 +112,11 @@ class DepreciationsController extends Controller $this->authorize('delete', Depreciation::class); $depreciation = Depreciation::findOrFail($id); $this->authorize('delete', $depreciation); + + if ($depreciation->has_models() > 0) { + return response()->json(Helper::formatStandardApiResponse('error', trans('admin/depreciations/message.assoc_users'))); + } + $depreciation->delete(); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/depreciations/message.delete.success'))); diff --git a/app/Http/Transformers/DepreciationsTransformer.php b/app/Http/Transformers/DepreciationsTransformer.php new file mode 100644 index 0000000000..be2c431722 --- /dev/null +++ b/app/Http/Transformers/DepreciationsTransformer.php @@ -0,0 +1,43 @@ +transformDatatables($array); + } + + public function transformDepreciation (Depreciation $depreciation) + { + $array = [ + 'id' => e($depreciation->id), + 'name' => e($depreciation->name), + 'months' => $depreciation->months . ' '. trans('general.months'), + 'created_at' => Helper::getFormattedDateObject($depreciation->created_at, 'datetime'), + 'updated_at' => Helper::getFormattedDateObject($depreciation->updated_at, 'datetime'), + ]; + + $permissions_array['available_actions'] = [ + 'update' => Gate::allows('update', Depreciation::class) ? true : false, + 'delete' => Gate::allows('delete', Depreciation::class) ? true : false, + ]; + + $array += $permissions_array; + + return $array; + } + + + +}