2018-07-24 19:35:26 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Models\AssetModel;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
|
|
|
|
class BulkAssetModelsController extends Controller
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view that allows the user to bulk edit model attrbutes
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.7]
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function edit(Request $request)
|
|
|
|
{
|
2019-12-11 11:09:54 -08:00
|
|
|
$models_raw_array = $request->input('ids');
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
// Make sure some IDs have been selected
|
|
|
|
if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) {
|
|
|
|
$models = AssetModel::whereIn('id', $models_raw_array)
|
2018-07-24 22:40:05 -07:00
|
|
|
->withCount('assets as assets_count')
|
2018-07-24 19:35:26 -07:00
|
|
|
->orderBy('assets_count', 'ASC')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
// If deleting....
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($request->input('bulk_actions') == 'delete') {
|
2018-07-24 19:35:26 -07:00
|
|
|
$valid_count = 0;
|
|
|
|
foreach ($models as $model) {
|
|
|
|
if ($model->assets_count == 0) {
|
|
|
|
$valid_count++;
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return view('models/bulk-delete', compact('models'))->with('valid_count', $valid_count);
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
// Otherwise display the bulk edit screen
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$nochange = ['NC' => 'No Change'];
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return view('models/bulk-edit', compact('models'))
|
|
|
|
->with('fieldset_list', $nochange + Helper::customFieldsetList())
|
|
|
|
->with('depreciation_list', $nochange + Helper::depreciationList());
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('error', 'You must select at least one model to edit.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view that allows the user to bulk edit model attrbutes
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.7]
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function update(Request $request)
|
|
|
|
{
|
2019-12-11 11:09:54 -08:00
|
|
|
$models_raw_array = $request->input('ids');
|
2021-06-10 13:15:52 -07:00
|
|
|
$update_array = [];
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($request->filled('manufacturer_id') && ($request->input('manufacturer_id') != 'NC'))) {
|
2018-07-24 19:35:26 -07:00
|
|
|
$update_array['manufacturer_id'] = $request->input('manufacturer_id');
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
if (($request->filled('category_id') && ($request->input('category_id') != 'NC'))) {
|
2018-07-24 19:35:26 -07:00
|
|
|
$update_array['category_id'] = $request->input('category_id');
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($request->input('fieldset_id') != 'NC') {
|
2018-07-24 19:35:26 -07:00
|
|
|
$update_array['fieldset_id'] = $request->input('fieldset_id');
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($request->input('depreciation_id') != 'NC') {
|
2018-07-24 19:35:26 -07:00
|
|
|
$update_array['depreciation_id'] = $request->input('depreciation_id');
|
|
|
|
}
|
|
|
|
|
2021-10-28 15:15:54 -07:00
|
|
|
if ($request->filled('requestable') != '') {
|
|
|
|
$update_array['requestable'] = $request->input('requestable');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
if (count($update_array) > 0) {
|
|
|
|
AssetModel::whereIn('id', $models_raw_array)->update($update_array);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('success', trans('admin/models/message.bulkedit.success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('warning', trans('admin/models/message.bulkedit.error'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate and delete the given Asset Models. An Asset Model
|
|
|
|
* cannot be deleted if there are associated assets.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2019-12-11 11:09:54 -08:00
|
|
|
public function destroy(Request $request)
|
2018-07-24 19:35:26 -07:00
|
|
|
{
|
2019-12-11 11:09:54 -08:00
|
|
|
$models_raw_array = $request->input('ids');
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) {
|
2018-07-24 22:40:05 -07:00
|
|
|
$models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets as assets_count')->get();
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
$del_error_count = 0;
|
|
|
|
$del_count = 0;
|
|
|
|
|
|
|
|
foreach ($models as $model) {
|
|
|
|
if ($model->assets_count > 0) {
|
|
|
|
$del_error_count++;
|
|
|
|
} else {
|
|
|
|
$model->delete();
|
|
|
|
$del_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($del_error_count == 0) {
|
|
|
|
return redirect()->route('models.index')
|
2021-06-10 13:15:52 -07:00
|
|
|
->with('success', trans('admin/models/message.bulkdelete.success', ['success_count'=> $del_count]));
|
2018-07-24 19:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('warning', trans('admin/models/message.bulkdelete.success_partial', ['fail_count'=>$del_error_count, 'success_count'=> $del_count]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('error', trans('admin/models/message.bulkdelete.error'));
|
|
|
|
}
|
|
|
|
}
|