Disallow deleting suppliers with associated assets, licenses or maintenances

This commit is contained in:
snipe 2017-10-18 05:47:20 -07:00
parent 081a64223a
commit d9135a8aac
3 changed files with 39 additions and 16 deletions

View file

@ -113,8 +113,22 @@ class SuppliersController extends Controller
public function destroy($id) public function destroy($id)
{ {
$this->authorize('delete', Supplier::class); $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); $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(); $supplier->delete();
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/suppliers/message.delete.success'))); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/suppliers/message.delete.success')));

View file

@ -189,23 +189,29 @@ class SuppliersController extends Controller
*/ */
public function destroy($supplierId) public function destroy($supplierId)
{ {
// Check if the supplier exists if (is_null($supplier = Supplier::with('asset_maintenances', 'assets', 'licenses')->withCount('asset_maintenances','assets','licenses')->find($supplierId))) {
if (is_null($supplier = Supplier::find($supplierId))) {
// Redirect to the suppliers page
return redirect()->route('suppliers.index')->with('error', trans('admin/suppliers/message.not_found')); return redirect()->route('suppliers.index')->with('error', trans('admin/suppliers/message.not_found'));
} }
if ($supplier->num_assets() == 0) {
// Delete the supplier if ($supplier->assets_count > 0) {
$supplier->delete(); return redirect()->route('suppliers.index')->with('error', trans('admin/suppliers/message.delete.assoc_assets', ['asset_count' => (int) $supplier->assets_count]));
// Redirect to the suppliers management page
return redirect()->route('suppliers.index')->with(
'success',
trans('admin/suppliers/message.delete.success')
);
} }
// 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')
);
} }

View file

@ -3,7 +3,7 @@
return array( return array(
'does_not_exist' => 'Supplier does not exist.', '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( 'create' => array(
'error' => 'Supplier was not created, please try again.', 'error' => 'Supplier was not created, please try again.',
@ -18,7 +18,10 @@ return array(
'delete' => array( 'delete' => array(
'confirm' => 'Are you sure you wish to delete this supplier?', 'confirm' => 'Are you sure you wish to delete this supplier?',
'error' => 'There was an issue deleting the supplier. Please try again.', '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. ',
) )
); );