2018-07-16 17:44:03 -07:00
|
|
|
<?php
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
namespace App\Http\Controllers\Assets;
|
2018-07-16 17:44:03 -07:00
|
|
|
|
2022-02-01 10:10:46 -08:00
|
|
|
use App\Models\Actionlog;
|
2018-07-16 17:44:03 -07:00
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Http\Controllers\CheckInOutRequest;
|
2018-07-24 19:35:26 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2018-07-16 17:44:03 -07:00
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-05-06 05:14:08 -07:00
|
|
|
use Illuminate\Support\Facades\Session;
|
2022-08-02 06:14:16 -07:00
|
|
|
use App\Http\Requests\AssetCheckoutRequest;
|
2023-03-28 18:31:24 -07:00
|
|
|
use App\Models\CustomField;
|
|
|
|
use Illuminate\Support\Collection;
|
2018-07-16 17:44:03 -07:00
|
|
|
|
2023-05-09 12:58:59 -07:00
|
|
|
use function Amp\Promise\wait;
|
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
class BulkAssetsController extends Controller
|
|
|
|
{
|
|
|
|
use CheckInOutRequest;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
/**
|
|
|
|
* Display the bulk edit page.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @return View
|
|
|
|
* @internal param int $assetId
|
|
|
|
* @since [v2.0]
|
2018-07-24 19:35:26 -07:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2018-07-16 17:44:03 -07:00
|
|
|
*/
|
|
|
|
public function edit(Request $request)
|
|
|
|
{
|
2023-05-02 16:54:20 -07:00
|
|
|
// dd($request->all());
|
2023-05-09 12:58:59 -07:00
|
|
|
// dd(Session::get('ids'));
|
2018-07-16 17:44:03 -07:00
|
|
|
|
2023-06-22 16:00:31 -07:00
|
|
|
$this->authorize('view', Asset::class);
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $request->filled('ids')) {
|
2022-05-06 05:41:52 -07:00
|
|
|
return redirect()->back()->with('error', trans('admin/hardware/message.update.no_assets_selected'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
2022-05-06 05:30:21 -07:00
|
|
|
|
|
|
|
// Figure out where we need to send the user after the update is complete, and store that in the session
|
2022-05-06 05:14:08 -07:00
|
|
|
$bulk_back_url = request()->headers->get('referer');
|
|
|
|
session(['bulk_back_url' => $bulk_back_url]);
|
|
|
|
|
2021-10-04 17:18:07 -07:00
|
|
|
$asset_ids = array_values(array_unique($request->input('ids')));
|
2023-03-28 18:31:24 -07:00
|
|
|
|
2023-04-25 21:26:16 -07:00
|
|
|
//custom fields logic
|
|
|
|
$asset_custom_field = Asset::with(['model.fieldset.fields', 'model'])->whereIn('id', $asset_ids)->whereHas('model', function ($query) {
|
2023-03-28 18:31:24 -07:00
|
|
|
return $query->where('fieldset_id', '!=', null);
|
|
|
|
})->get();
|
2023-03-28 19:45:31 -07:00
|
|
|
|
2023-04-12 12:46:48 -07:00
|
|
|
$models = $asset_custom_field->unique('model_id');
|
2023-04-17 11:57:48 -07:00
|
|
|
$modelNames = [];
|
2023-05-09 12:58:59 -07:00
|
|
|
foreach($models as $model) {
|
|
|
|
$modelNames[] = $model->model->name;
|
|
|
|
}
|
2023-03-28 19:45:31 -07:00
|
|
|
|
2023-04-12 12:46:48 -07:00
|
|
|
// $custom_fields = new Collection();
|
|
|
|
// foreach ($models as $asset_key => $asset) {
|
|
|
|
// $custom_fields->push($asset->model->customFields);
|
|
|
|
// }
|
|
|
|
$custom_fields = $asset_custom_field->pluck('model.fieldset.fields')->flatten()->unique('id');
|
2018-07-16 17:44:03 -07:00
|
|
|
|
2018-07-24 22:51:31 -07:00
|
|
|
if ($request->filled('bulk_actions')) {
|
2021-06-10 13:15:52 -07:00
|
|
|
switch ($request->input('bulk_actions')) {
|
2018-07-16 17:44:03 -07:00
|
|
|
case 'labels':
|
2023-04-25 08:06:45 -07:00
|
|
|
$this->authorize('view', Asset::class);
|
2018-07-16 17:44:03 -07:00
|
|
|
return view('hardware/labels')
|
|
|
|
->with('assets', Asset::find($asset_ids))
|
|
|
|
->with('settings', Setting::getSettings())
|
2019-09-03 11:02:55 -07:00
|
|
|
->with('bulkedit', true)
|
2018-07-16 17:44:03 -07:00
|
|
|
->with('count', 0);
|
2023-03-29 16:24:56 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
case 'delete':
|
2023-04-25 08:06:45 -07:00
|
|
|
$this->authorize('delete', Asset::class);
|
2018-07-16 17:44:03 -07:00
|
|
|
$assets = Asset::with('assignedTo', 'location')->find($asset_ids);
|
|
|
|
$assets->each(function ($asset) {
|
|
|
|
$this->authorize('delete', $asset);
|
|
|
|
});
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
return view('hardware/bulk-delete')->with('assets', $assets);
|
2023-03-29 15:34:36 -07:00
|
|
|
|
2023-04-25 08:06:45 -07:00
|
|
|
case 'restore':
|
|
|
|
$this->authorize('update', Asset::class);
|
2023-03-29 16:24:56 -07:00
|
|
|
$assets = Asset::withTrashed()->find($asset_ids);
|
2023-03-29 15:34:36 -07:00
|
|
|
$assets->each(function ($asset) {
|
2023-03-29 16:24:56 -07:00
|
|
|
$this->authorize('delete', $asset);
|
2023-03-29 15:34:36 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return view('hardware/bulk-restore')->with('assets', $assets);
|
2023-03-29 16:24:56 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
case 'edit':
|
2023-04-25 08:06:45 -07:00
|
|
|
$this->authorize('update', Asset::class);
|
2018-07-16 17:44:03 -07:00
|
|
|
return view('hardware/bulk')
|
2021-10-04 17:18:07 -07:00
|
|
|
->with('assets', $asset_ids)
|
2023-03-28 18:31:24 -07:00
|
|
|
->with('statuslabel_list', Helper::statusLabelList())
|
2023-04-12 12:46:48 -07:00
|
|
|
// ->with('custom_fields', $custom_fields)
|
2023-04-25 21:26:16 -07:00
|
|
|
->with('models', $models->pluck(['model']))
|
2023-04-17 11:57:48 -07:00
|
|
|
->with('modelNames', $modelNames);
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
return redirect()->back()->with('error', 'No action selected');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save bulk edits
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @return Redirect
|
|
|
|
* @internal param array $assets
|
|
|
|
* @since [v2.0]
|
|
|
|
*/
|
|
|
|
public function update(Request $request)
|
|
|
|
{
|
2023-05-02 16:54:20 -07:00
|
|
|
// dd(request()->headers->get('referer'));
|
2018-07-16 17:44:03 -07:00
|
|
|
$this->authorize('update', Asset::class);
|
2023-04-25 21:26:16 -07:00
|
|
|
$error_bag = [];
|
2018-07-16 17:44:03 -07:00
|
|
|
|
2022-05-06 05:14:08 -07:00
|
|
|
// Get the back url from the session and then destroy the session
|
2022-05-06 15:46:25 -07:00
|
|
|
$bulk_back_url = route('hardware.index');
|
2022-05-06 14:10:47 -07:00
|
|
|
if ($request->session()->has('bulk_back_url')) {
|
|
|
|
$bulk_back_url = $request->session()->pull('bulk_back_url');
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
|
2023-03-29 12:46:31 -07:00
|
|
|
$custom_field_columns = CustomField::all()->pluck('db_column')->toArray();
|
2023-05-09 12:58:59 -07:00
|
|
|
|
|
|
|
if(Session::exists('ids')) {
|
|
|
|
$assets = Session::get('ids');
|
|
|
|
} elseif (! $request->filled('ids') || count($request->input('ids')) <= 0) {
|
2022-05-06 05:41:32 -07:00
|
|
|
return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.update.no_assets_selected'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
2023-05-09 12:58:59 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
$assets = array_keys($request->input('ids'));
|
2023-03-29 12:46:31 -07:00
|
|
|
|
|
|
|
if ($request->anyFilled($custom_field_columns)) {
|
|
|
|
$custom_fields_present = true;
|
|
|
|
} else {
|
|
|
|
$custom_fields_present = false;
|
|
|
|
}
|
2018-07-24 22:51:31 -07:00
|
|
|
if (($request->filled('purchase_date'))
|
2020-07-09 13:08:35 -07:00
|
|
|
|| ($request->filled('expected_checkin'))
|
2018-07-24 22:51:31 -07:00
|
|
|
|| ($request->filled('purchase_cost'))
|
|
|
|
|| ($request->filled('supplier_id'))
|
|
|
|
|| ($request->filled('order_number'))
|
|
|
|
|| ($request->filled('warranty_months'))
|
|
|
|
|| ($request->filled('rtd_location_id'))
|
|
|
|
|| ($request->filled('requestable'))
|
|
|
|
|| ($request->filled('company_id'))
|
|
|
|
|| ($request->filled('status_id'))
|
|
|
|
|| ($request->filled('model_id'))
|
2023-03-21 16:01:41 -07:00
|
|
|
|| ($request->filled('next_audit_date'))
|
2022-08-11 18:25:07 -07:00
|
|
|
|| ($request->filled('null_purchase_date'))
|
|
|
|
|| ($request->filled('null_expected_checkin_date'))
|
2023-03-21 16:01:41 -07:00
|
|
|
|| ($request->filled('null_next_audit_date'))
|
2023-03-29 12:46:31 -07:00
|
|
|
|| ($request->anyFilled($custom_field_columns))
|
2023-03-21 16:01:41 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
) {
|
2023-05-02 16:54:20 -07:00
|
|
|
// dd($assets);
|
2018-07-16 17:44:03 -07:00
|
|
|
foreach ($assets as $assetId) {
|
2021-10-04 17:18:07 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
$this->update_array = [];
|
|
|
|
|
|
|
|
$this->conditionallyAddItem('purchase_date')
|
2020-07-09 13:08:35 -07:00
|
|
|
->conditionallyAddItem('expected_checkin')
|
2018-07-16 17:44:03 -07:00
|
|
|
->conditionallyAddItem('model_id')
|
|
|
|
->conditionallyAddItem('order_number')
|
|
|
|
->conditionallyAddItem('requestable')
|
|
|
|
->conditionallyAddItem('status_id')
|
|
|
|
->conditionallyAddItem('supplier_id')
|
2023-03-21 16:01:41 -07:00
|
|
|
->conditionallyAddItem('warranty_months')
|
|
|
|
->conditionallyAddItem('next_audit_date');
|
2023-03-29 12:46:31 -07:00
|
|
|
foreach ($custom_field_columns as $key => $custom_field_column) {
|
|
|
|
$this->conditionallyAddItem($custom_field_column);
|
|
|
|
}
|
2018-07-16 17:44:03 -07:00
|
|
|
|
2022-08-11 18:25:07 -07:00
|
|
|
if ($request->input('null_purchase_date')=='1') {
|
|
|
|
$this->update_array['purchase_date'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->input('null_expected_checkin_date')=='1') {
|
|
|
|
$this->update_array['expected_checkin'] = null;
|
|
|
|
}
|
|
|
|
|
2023-03-21 16:01:41 -07:00
|
|
|
if ($request->input('null_next_audit_date')=='1') {
|
|
|
|
$this->update_array['next_audit_date'] = null;
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:51:31 -07:00
|
|
|
if ($request->filled('purchase_cost')) {
|
2023-03-29 00:56:34 -07:00
|
|
|
$this->update_array['purchase_cost'] = $request->input('purchase_cost');
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
|
2018-07-24 22:51:31 -07:00
|
|
|
if ($request->filled('company_id')) {
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->update_array['company_id'] = $request->input('company_id');
|
|
|
|
if ($request->input('company_id') == 'clear') {
|
2018-07-16 17:44:03 -07:00
|
|
|
$this->update_array['company_id'] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:51:31 -07:00
|
|
|
if ($request->filled('rtd_location_id')) {
|
2018-07-16 17:44:03 -07:00
|
|
|
$this->update_array['rtd_location_id'] = $request->input('rtd_location_id');
|
2018-07-24 22:51:31 -07:00
|
|
|
if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '1')) {
|
2018-07-16 17:44:03 -07:00
|
|
|
$this->update_array['location_id'] = $request->input('rtd_location_id');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 10:10:46 -08:00
|
|
|
$changed = [];
|
2023-04-26 13:06:50 -07:00
|
|
|
$assetCollection = Asset::where('id' ,$assetId)->get();
|
2022-02-01 10:10:46 -08:00
|
|
|
|
|
|
|
foreach ($this->update_array as $key => $value) {
|
2023-04-26 13:06:50 -07:00
|
|
|
if ($this->update_array[$key] != $assetCollection->toArray()[0][$key]) {
|
|
|
|
$changed[$key]['old'] = $assetCollection->toArray()[0][$key];
|
2022-02-01 10:10:46 -08:00
|
|
|
$changed[$key]['new'] = $this->update_array[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = Asset::class;
|
|
|
|
$logAction->item_id = $assetId;
|
|
|
|
$logAction->created_at = date("Y-m-d H:i:s");
|
|
|
|
$logAction->user_id = Auth::id();
|
|
|
|
$logAction->log_meta = json_encode($changed);
|
|
|
|
$logAction->logaction('update');
|
2023-03-29 12:46:31 -07:00
|
|
|
|
|
|
|
if($custom_fields_present) {
|
|
|
|
$asset = Asset::find($assetId);
|
|
|
|
$assetCustomFields = $asset->model()->first()->fieldset;
|
2023-05-10 12:08:12 -07:00
|
|
|
if($assetCustomFields?->fields) {
|
|
|
|
foreach ($assetCustomFields?->fields as $field) {
|
|
|
|
if (array_key_exists($field->db_column, $this->update_array)) {
|
|
|
|
$asset->{$field->db_column} = $this->update_array[$field->db_column];
|
|
|
|
$asset->save();
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
$array = $this->update_array;
|
|
|
|
array_except($array, $field->db_column);
|
|
|
|
// $asset->update($array);
|
|
|
|
$asset->save($array);
|
|
|
|
//call update on parent model
|
|
|
|
// $asset->save();
|
|
|
|
}
|
|
|
|
if (!$asset->save()) {
|
|
|
|
$error_bag[] = $asset->getErrors();
|
|
|
|
}
|
2023-05-09 12:58:59 -07:00
|
|
|
}
|
|
|
|
}
|
2023-03-29 12:46:31 -07:00
|
|
|
} else {
|
|
|
|
Asset::find($assetId)->update($this->update_array);
|
|
|
|
}
|
|
|
|
} // endforeach ($assets)
|
2023-05-09 12:58:59 -07:00
|
|
|
ray($error_bag);
|
2023-04-26 13:06:50 -07:00
|
|
|
if(!empty($error_bag)) {
|
2023-05-03 12:49:31 -07:00
|
|
|
// $errors = collect($error_bag)->unique();
|
|
|
|
// foreach ($errors as $key => $value) {
|
|
|
|
// ray($value->message);
|
|
|
|
// }
|
2023-05-09 12:58:59 -07:00
|
|
|
$errors = [];
|
|
|
|
foreach ($error_bag as $key => $value) {
|
|
|
|
foreach($value as $key => $value) {
|
|
|
|
$errors[] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ray($error_bag);
|
|
|
|
Session::save('ids', $assets);
|
|
|
|
|
|
|
|
// return redirect()->route('hardware/bulkedit');
|
|
|
|
return redirect()->back()->with('bulk_errors', $errors);
|
2023-04-26 13:06:50 -07:00
|
|
|
}
|
2022-05-06 05:14:08 -07:00
|
|
|
return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.update.success'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
2022-05-06 05:14:08 -07:00
|
|
|
// no values given, nothing to update
|
|
|
|
return redirect($bulk_back_url)->with('warning', trans('admin/hardware/message.update.nothing_updated'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array to store update data per item
|
2021-06-10 13:15:52 -07:00
|
|
|
* @var array
|
2018-07-16 17:44:03 -07:00
|
|
|
*/
|
|
|
|
private $update_array;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
/**
|
|
|
|
* Adds parameter to update array for an item if it exists in request
|
2021-06-10 13:15:52 -07:00
|
|
|
* @param string $field field name
|
2018-07-24 19:35:26 -07:00
|
|
|
* @return BulkAssetsController Model for Chaining
|
2018-07-16 17:44:03 -07:00
|
|
|
*/
|
|
|
|
protected function conditionallyAddItem($field)
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
if (request()->filled($field)) {
|
2018-07-16 17:44:03 -07:00
|
|
|
$this->update_array[$field] = request()->input($field);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save bulk deleted.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2018-07-24 19:35:26 -07:00
|
|
|
* @param Request $request
|
2018-07-16 17:44:03 -07:00
|
|
|
* @return View
|
2018-07-24 19:35:26 -07:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2018-07-16 17:44:03 -07:00
|
|
|
* @internal param array $assets
|
|
|
|
* @since [v2.0]
|
|
|
|
*/
|
|
|
|
public function destroy(Request $request)
|
|
|
|
{
|
|
|
|
$this->authorize('delete', Asset::class);
|
|
|
|
|
2022-05-06 15:46:25 -07:00
|
|
|
$bulk_back_url = route('hardware.index');
|
2022-05-06 14:10:47 -07:00
|
|
|
if ($request->session()->has('bulk_back_url')) {
|
|
|
|
$bulk_back_url = $request->session()->pull('bulk_back_url');
|
|
|
|
}
|
2022-05-06 05:14:08 -07:00
|
|
|
|
2018-07-24 22:51:31 -07:00
|
|
|
if ($request->filled('ids')) {
|
2018-07-16 17:44:03 -07:00
|
|
|
$assets = Asset::find($request->get('ids'));
|
|
|
|
foreach ($assets as $asset) {
|
|
|
|
$update_array['deleted_at'] = date('Y-m-d H:i:s');
|
|
|
|
$update_array['assigned_to'] = null;
|
|
|
|
|
|
|
|
DB::table('assets')
|
|
|
|
->where('id', $asset->id)
|
|
|
|
->update($update_array);
|
|
|
|
} // endforeach
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2022-05-06 05:14:08 -07:00
|
|
|
return redirect($bulk_back_url)->with('success', trans('admin/hardware/message.delete.success'));
|
2018-07-16 17:44:03 -07:00
|
|
|
// no values given, nothing to update
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2022-05-06 05:41:52 -07:00
|
|
|
return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.delete.nothing_updated'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show Bulk Checkout Page
|
|
|
|
* @return View View to checkout multiple assets
|
|
|
|
*/
|
|
|
|
public function showCheckout()
|
|
|
|
{
|
|
|
|
$this->authorize('checkout', Asset::class);
|
|
|
|
// Filter out assets that are not deployable.
|
|
|
|
|
|
|
|
return view('hardware/bulk-checkout');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process Multiple Checkout Request
|
|
|
|
* @return View
|
|
|
|
*/
|
2022-08-02 06:14:16 -07:00
|
|
|
public function storeCheckout(AssetCheckoutRequest $request)
|
2018-07-16 17:44:03 -07:00
|
|
|
{
|
2022-05-06 05:13:36 -07:00
|
|
|
|
|
|
|
$this->authorize('checkout', Asset::class);
|
|
|
|
|
2018-07-16 17:44:03 -07:00
|
|
|
try {
|
|
|
|
$admin = Auth::user();
|
|
|
|
|
|
|
|
$target = $this->determineCheckoutTarget();
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! is_array($request->get('selected_assets'))) {
|
2022-08-01 15:59:54 -07:00
|
|
|
return redirect()->route('hardware.bulkcheckout.show')->withInput()->with('error', trans('admin/hardware/message.checkout.no_assets_selected'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$asset_ids = array_filter($request->get('selected_assets'));
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (request('checkout_to_type') == 'asset') {
|
2020-05-23 08:48:02 -07:00
|
|
|
foreach ($asset_ids as $asset_id) {
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($target->id == $asset_id) {
|
2020-05-23 08:48:02 -07:00
|
|
|
return redirect()->back()->with('error', 'You cannot check an asset out to itself.');
|
|
|
|
}
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
$checkout_at = date('Y-m-d H:i:s');
|
|
|
|
if (($request->filled('checkout_at')) && ($request->get('checkout_at') != date('Y-m-d'))) {
|
2018-07-16 17:44:03 -07:00
|
|
|
$checkout_at = e($request->get('checkout_at'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$expected_checkin = '';
|
|
|
|
|
2018-07-24 22:51:31 -07:00
|
|
|
if ($request->filled('expected_checkin')) {
|
2018-07-16 17:44:03 -07:00
|
|
|
$expected_checkin = e($request->get('expected_checkin'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
DB::transaction(function () use ($target, $admin, $checkout_at, $expected_checkin, $errors, $asset_ids, $request) {
|
|
|
|
foreach ($asset_ids as $asset_id) {
|
|
|
|
$asset = Asset::findOrFail($asset_id);
|
|
|
|
$this->authorize('checkout', $asset);
|
2023-02-24 09:54:46 -08:00
|
|
|
|
|
|
|
$error = $asset->checkOut($target, $admin, $checkout_at, $expected_checkin, e($request->get('note')), $asset->name, null);
|
2018-07-16 17:44:03 -07:00
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($target->location_id != '') {
|
2018-07-16 17:44:03 -07:00
|
|
|
$asset->location_id = $target->location_id;
|
|
|
|
$asset->unsetEventDispatcher();
|
|
|
|
$asset->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($error) {
|
|
|
|
array_merge_recursive($errors, $asset->getErrors()->toArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $errors) {
|
|
|
|
// Redirect to the new asset page
|
|
|
|
return redirect()->to('hardware')->with('success', trans('admin/hardware/message.checkout.success'));
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
// Redirect to the asset management page with error
|
2022-08-01 15:59:54 -07:00
|
|
|
return redirect()->route('hardware.bulkcheckout.show')->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($errors);
|
2018-07-16 17:44:03 -07:00
|
|
|
} catch (ModelNotFoundException $e) {
|
2022-08-01 15:59:54 -07:00
|
|
|
return redirect()->route('hardware.bulkcheckout.show')->with('error', $e->getErrors());
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
2023-03-29 15:34:36 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
public function restore(Request $request) {
|
2023-04-25 08:09:16 -07:00
|
|
|
$this->authorize('update', Asset::class);
|
2023-03-29 15:34:36 -07:00
|
|
|
$assetIds = $request->get('ids');
|
2023-03-30 11:52:29 -07:00
|
|
|
if (empty($assetIds)) {
|
2023-03-30 11:57:18 -07:00
|
|
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.restore.nothing_updated'));
|
2023-03-30 11:52:29 -07:00
|
|
|
} else {
|
|
|
|
foreach ($assetIds as $key => $assetId) {
|
|
|
|
$asset = Asset::withTrashed()->find($assetId);
|
|
|
|
$asset->restore();
|
|
|
|
}
|
2023-03-30 11:57:18 -07:00
|
|
|
return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.restore.success'));
|
2023-03-30 11:52:29 -07:00
|
|
|
}
|
2018-07-16 17:44:03 -07:00
|
|
|
}
|
|
|
|
}
|