Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 19 additions & 79 deletions app/Http/Controllers/Assets/BulkAssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function edit(Request $request) : View | RedirectResponse
$asset_ids = $request->input('ids');

if ($request->input('bulk_actions') === 'checkout') {
$undeployable_assets = Asset::findMany($asset_ids)->filter(fn(Asset $asset) => !$asset->availableForCheckout());

if($undeployable_assets->count() > 0) {
return redirect()->back()->with('error', trans_choice('admin/hardware/form.bulk_asset_undeployable', $undeployable_assets->count()));
}

$status_check =$this->hasUndeployableStatus($asset_ids);
if($status_check && $status_check['status'] === true){

Expand Down Expand Up @@ -229,21 +235,6 @@ public function update(Request $request) : RedirectResponse

$custom_field_columns = CustomField::all()->pluck('db_column')->toArray();

// find custom field input attributes that start with 'null_'
$null_custom_fields_inputs = array_filter($request->all(), function ($key) {
// filter out all keys that start with 'null_'
return (strpos($key, 'null_') === 0);
}, ARRAY_FILTER_USE_KEY);;
// remove 'null' from the keys
$custom_fields_to_null = [];
foreach ($null_custom_fields_inputs as $key => $value) {
$custom_fields_to_null[str_replace('null', '', $key)] = $value;
}






if (! $request->filled('ids') || count($request->input('ids')) == 0) {
return redirect($bulk_back_url)->with('error', trans('admin/hardware/message.update.no_assets_selected'));
Expand Down Expand Up @@ -281,9 +272,7 @@ public function update(Request $request) : RedirectResponse
|| ($request->filled('null_expected_checkin_date'))
|| ($request->filled('null_next_audit_date'))
|| ($request->filled('null_asset_eol_date'))
|| ($request->filled('null_notes'))
|| ($request->anyFilled($custom_field_columns))
|| ($request->anyFilled(array_keys($null_custom_fields_inputs)))

) {
// Let's loop through those assets and build an update array
Expand All @@ -306,14 +295,10 @@ public function update(Request $request) : RedirectResponse
->conditionallyAddItem('supplier_id')
->conditionallyAddItem('warranty_months')
->conditionallyAddItem('next_audit_date')
->conditionallyAddItem('asset_eol_date')
->conditionallyAddItem('notes');
->conditionallyAddItem('asset_eol_date');
foreach ($custom_field_columns as $key => $custom_field_column) {
$this->conditionallyAddItem($custom_field_column);
}
foreach ($custom_fields_to_null as $key => $custom_field_to_null) {
$this->conditionallyAddItem($key);
}

if (!($asset->eol_explicit)) {
if ($request->filled('model_id')) {
Expand Down Expand Up @@ -364,10 +349,6 @@ public function update(Request $request) : RedirectResponse
}
}

if ($request->input('null_notes')=='1') {
$this->update_array['notes'] = null;
}



if ($request->filled('purchase_cost')) {
Expand Down Expand Up @@ -408,12 +389,10 @@ public function update(Request $request) : RedirectResponse
// This could probably be added to a form request.
// If the asset isn't assigned, we don't care what the status is.
// Otherwise we need to make sure the status type is still a deployable one.

$unassigned = $asset->assigned_to == '';
$deployable = $updated_status->deployable == '1' && $asset->assetstatus?->deployable == '1';
$pending = $updated_status->pending === 1;

if ($unassigned || $deployable || $pending) {
if (
($asset->assigned_to == '')
|| ($updated_status->deployable == '1') && ($asset->assetstatus?->deployable == '1')
) {
$this->update_array['status_id'] = $updated_status->id;
}

Expand Down Expand Up @@ -465,23 +444,13 @@ public function update(Request $request) : RedirectResponse
}

/**
*
* Start all the custom fields shenanigans
*/

// Does the model have a fieldset?
if ($asset->model->fieldset) {
foreach ($asset->model->fieldset->fields as $field) {

// null custom fields
if ($custom_fields_to_null) {
foreach ($custom_fields_to_null as $key => $custom_field_to_null) {
if ($field->db_column == $key) {
$this->update_array[$field->db_column] = null;
}
}
}

if ((array_key_exists($field->db_column, $this->update_array)) && ($field->field_encrypted == '1')) {
if (Gate::allows('admin')) {
$decrypted_old = Helper::gracefulDecrypt($field, $asset->{$field->db_column});
Expand Down Expand Up @@ -614,17 +583,15 @@ public function destroy(Request $request) : RedirectResponse
public function showCheckout() : View
{
$this->authorize('checkout', Asset::class);

$do_not_change = ['' => trans('general.do_not_change')];
$status_label_list = $do_not_change + Helper::deployableStatusLabelList();
return view('hardware/bulk-checkout')->with('statusLabel_list', $status_label_list);
return view('hardware/bulk-checkout');
}

/**
* Process Multiple Checkout Request
*/
public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse | ModelNotFoundException
{

$this->authorize('checkout', Asset::class);

try {
Expand All @@ -638,8 +605,6 @@ public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse

$asset_ids = array_filter($request->get('selected_assets'));

$assets = Asset::findOrFail($asset_ids);

if (request('checkout_to_type') == 'asset') {
foreach ($asset_ids as $asset_id) {
if ($target->id == $asset_id) {
Expand All @@ -649,25 +614,21 @@ public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse
}
$checkout_at = date('Y-m-d H:i:s');
if (($request->filled('checkout_at')) && ($request->get('checkout_at') != date('Y-m-d'))) {
$checkout_at = $request->get('checkout_at');
$checkout_at = e($request->get('checkout_at'));
}

$expected_checkin = '';

if ($request->filled('expected_checkin')) {
$expected_checkin = $request->get('expected_checkin');
$expected_checkin = e($request->get('expected_checkin'));
}

$errors = [];
DB::transaction(function () use ($target, $admin, $checkout_at, $expected_checkin, &$errors, $assets, $request) { //NOTE: $errors is passsed by reference!
foreach ($assets as $asset) {
DB::transaction(function () use ($target, $admin, $checkout_at, $expected_checkin, &$errors, $asset_ids, $request) { //NOTE: $errors is passsed by reference!
foreach ($asset_ids as $asset_id) {
$asset = Asset::findOrFail($asset_id);
$this->authorize('checkout', $asset);

// See if there is a status label passed
if ($request->filled('status_id')) {
$asset->status_id = $request->get('status_id');
}

$checkout_success = $asset->checkOut($target, $admin, $checkout_at, $expected_checkin, e($request->get('note')), $asset->name, null);

//TODO - I think this logic is duplicated in the checkOut method?
Expand All @@ -692,7 +653,7 @@ public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse
// Redirect to the asset management page with error
return redirect()->route('hardware.bulkcheckout.show')->withInput()->with('error', trans_choice('admin/hardware/message.multi-checkout.error', $asset_ids))->withErrors($errors);
} catch (ModelNotFoundException $e) {
return redirect()->route('hardware.bulkcheckout.show')->withInput()->with('error', trans_choice('admin/hardware/message.multi-checkout.error', $request->input('selected_assets')));
return redirect()->route('hardware.bulkcheckout.show')->with('error', $e->getErrors());
}

}
Expand All @@ -711,25 +672,4 @@ public function restore(Request $request) : RedirectResponse
return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.restore.success'));
}
}
public function hasUndeployableStatus (array $asset_ids)
{
$undeployable = Asset::whereIn('id', $asset_ids)
->undeployable()
->get();

$undeployableTags = $undeployable->map(function ($asset) {
return [
'id' => $asset->id,
'asset_tag' => $asset->asset_tag,
];
})->toArray();

$undeployableIds = array_column($undeployableTags, 'id');
$filtered_ids = array_diff($asset_ids, $undeployableIds);

if($undeployable->isNotEmpty()) {
return ['status' => true, 'tags' => $undeployableTags, 'asset_ids' => $filtered_ids];
}
return false;
}
}
1 change: 1 addition & 0 deletions resources/lang/en-US/admin/hardware/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
'asset_location_update_actual' => 'Update only actual location',
'asset_not_deployable' => 'That asset status is not deployable. This asset cannot be checked out.',
'asset_not_deployable_checkin' => 'That asset status is not deployable. Using this status label will checkin the asset.',
'bulk_asset_undeployable' => 'Asset in this bulk checkout request is undeployable.|[2,*]Assets in this bulk checkout request are undeployable.',
'asset_deployable' => 'This asset can be checked out.',
'processing_spinner' => 'Processing... (This might take a bit of time on large files)',
'processing' => 'Processing... ',
Expand Down
Loading