diff --git a/app/Http/Controllers/Api/SuppliersController.php b/app/Http/Controllers/Api/SuppliersController.php index 22f35f47de..99a60c6a93 100644 --- a/app/Http/Controllers/Api/SuppliersController.php +++ b/app/Http/Controllers/Api/SuppliersController.php @@ -113,8 +113,22 @@ class SuppliersController extends Controller public function destroy($id) { $this->authorize('delete', Supplier::class); - $supplier = Supplier::findOrFail($id); + $supplier = Supplier::with('asset_maintenances', 'assets', 'licenses')->withCount('asset_maintenances','assets', 'licenses')->findOrFail($id); $this->authorize('delete', $supplier); + + + if ($supplier->assets_count > 0) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/suppliers/message.delete.assoc_assets', ['asset_count' => (int) $supplier->assets_count]))); + } + + if ($supplier->asset_maintenances_count > 0) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/suppliers/message.delete.assoc_maintenances', ['asset_maintenances_count' => $supplier->asset_maintenances_count]))); + } + + if ($supplier->licenses_count > 0) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/suppliers/message.delete.assoc_licenses', ['licenses_count' => (int) $supplier->licenses_count]))); + } + $supplier->delete(); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/suppliers/message.delete.success'))); diff --git a/app/Http/Controllers/SuppliersController.php b/app/Http/Controllers/SuppliersController.php index a6e8552df9..04657e06a2 100755 --- a/app/Http/Controllers/SuppliersController.php +++ b/app/Http/Controllers/SuppliersController.php @@ -189,23 +189,29 @@ class SuppliersController extends Controller */ public function destroy($supplierId) { - // Check if the supplier exists - if (is_null($supplier = Supplier::find($supplierId))) { - // Redirect to the suppliers page + if (is_null($supplier = Supplier::with('asset_maintenances', 'assets', 'licenses')->withCount('asset_maintenances','assets','licenses')->find($supplierId))) { return redirect()->route('suppliers.index')->with('error', trans('admin/suppliers/message.not_found')); } - if ($supplier->num_assets() == 0) { - // Delete the supplier - $supplier->delete(); - // Redirect to the suppliers management page - return redirect()->route('suppliers.index')->with( - 'success', - trans('admin/suppliers/message.delete.success') - ); + + if ($supplier->assets_count > 0) { + return redirect()->route('suppliers.index')->with('error', trans('admin/suppliers/message.delete.assoc_assets', ['asset_count' => (int) $supplier->assets_count])); } - // Redirect to the asset management page - return redirect()->route('suppliers.index')->with('error', trans('admin/suppliers/message.assoc_users')); + + if ($supplier->asset_maintenances_count > 0) { + return redirect()->route('suppliers.index')->with('error', trans('admin/suppliers/message.delete.assoc_maintenances', ['asset_maintenances_count' => $supplier->asset_maintenances_count])); + } + + if ($supplier->licenses_count > 0) { + return redirect()->route('suppliers.index')->with('error', trans('admin/suppliers/message.delete.assoc_licenses', ['licenses_count' => (int) $supplier->licenses_count])); + } + + $supplier->delete(); + return redirect()->route('suppliers.index')->with('success', + trans('admin/suppliers/message.delete.success') + ); + + } diff --git a/resources/lang/en/admin/suppliers/message.php b/resources/lang/en/admin/suppliers/message.php index df4bc41af3..cea332748f 100644 --- a/resources/lang/en/admin/suppliers/message.php +++ b/resources/lang/en/admin/suppliers/message.php @@ -3,7 +3,7 @@ return array( 'does_not_exist' => 'Supplier does not exist.', - 'assoc_users' => 'This supplier is currently associated with at least one model and cannot be deleted. Please update your models to no longer reference this supplier and try again. ', + 'create' => array( 'error' => 'Supplier was not created, please try again.', @@ -18,7 +18,10 @@ return array( 'delete' => array( 'confirm' => 'Are you sure you wish to delete this supplier?', 'error' => 'There was an issue deleting the supplier. Please try again.', - 'success' => 'Supplier was deleted successfully.' + 'success' => 'Supplier was deleted successfully.', + 'assoc_assets' => 'This supplier is currently associated with :asset_count asset(s) and cannot be deleted. Please update your assets to no longer reference this supplier and try again. ', + 'assoc_licenses' => 'This supplier is currently associated with :licenses_count licences(s) and cannot be deleted. Please update your licenses to no longer reference this supplier and try again. ', + 'assoc_maintenances' => 'This supplier is currently associated with :asset_maintenances_count asset maintenances(s) and cannot be deleted. Please update your asset maintenances to no longer reference this supplier and try again. ', ) );