2024-10-30 11:48:14 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Actions\Assets;
|
|
|
|
|
2024-11-12 12:46:56 -08:00
|
|
|
use App\Events\CheckoutableCheckedIn;
|
2024-11-19 15:45:35 -08:00
|
|
|
use App\Exceptions\CustomFieldPermissionException;
|
2024-11-12 16:08:29 -08:00
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2024-11-12 12:46:56 -08:00
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Company;
|
2024-11-18 22:13:28 -08:00
|
|
|
use App\Models\Location;
|
2024-11-12 12:46:56 -08:00
|
|
|
use App\Models\Statuslabel;
|
2024-11-18 22:13:28 -08:00
|
|
|
use App\Models\User;
|
2024-11-12 12:46:56 -08:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-11-19 12:45:54 -08:00
|
|
|
use Watson\Validating\ValidationException;
|
2024-11-12 12:46:56 -08:00
|
|
|
|
2024-10-30 11:48:14 -07:00
|
|
|
class UpdateAssetAction
|
|
|
|
{
|
2024-11-19 12:45:54 -08:00
|
|
|
/**
|
|
|
|
* @throws ValidationException
|
2024-11-19 15:45:35 -08:00
|
|
|
* @throws CustomFieldPermissionException
|
2024-11-19 12:45:54 -08:00
|
|
|
*/
|
2024-11-12 12:46:56 -08:00
|
|
|
public static function run(
|
|
|
|
Asset $asset,
|
2024-11-18 22:13:28 -08:00
|
|
|
ImageUploadRequest $request, //very much would like this to go away
|
2024-11-12 12:46:56 -08:00
|
|
|
$status_id = null,
|
|
|
|
$warranty_months = null,
|
|
|
|
$purchase_cost = null,
|
|
|
|
$purchase_date = null,
|
2024-11-18 22:13:28 -08:00
|
|
|
$last_audit_date = null,
|
2024-11-12 12:46:56 -08:00
|
|
|
$next_audit_date = null,
|
|
|
|
$asset_eol_date = null,
|
|
|
|
$supplier_id = null,
|
|
|
|
$expected_checkin = null,
|
2024-11-12 16:08:29 -08:00
|
|
|
$requestable = false,
|
2024-11-18 22:13:28 -08:00
|
|
|
$location_id = null,
|
2024-11-12 12:46:56 -08:00
|
|
|
$rtd_location_id = null,
|
2024-11-18 22:13:28 -08:00
|
|
|
$assigned_location = null, // derp, make these work
|
|
|
|
$assigned_asset = null,
|
|
|
|
$assigned_user = null,
|
2024-11-12 16:08:29 -08:00
|
|
|
$byod = false,
|
|
|
|
$image_delete = false,
|
2024-11-14 14:54:23 -08:00
|
|
|
$serial = null,
|
2024-11-12 16:08:29 -08:00
|
|
|
$name = null,
|
|
|
|
$company_id = null,
|
|
|
|
$model_id = null,
|
|
|
|
$order_number = null,
|
2024-11-14 14:54:23 -08:00
|
|
|
$asset_tag = null,
|
2024-11-12 16:08:29 -08:00
|
|
|
$notes = null,
|
2024-11-18 22:13:28 -08:00
|
|
|
$isBulk = false,
|
2024-11-14 14:54:23 -08:00
|
|
|
): \App\Models\SnipeModel {
|
2024-11-12 12:46:56 -08:00
|
|
|
|
2024-11-18 22:13:28 -08:00
|
|
|
$asset->status_id = $status_id ?? $asset->status_id;
|
|
|
|
$asset->warranty_months = $warranty_months ?? $asset->warranty_months;
|
|
|
|
$asset->purchase_cost = $purchase_cost ?? $asset->purchase_cost;
|
|
|
|
$asset->purchase_date = $purchase_date ?? $asset->purchase_date?->format('Y-m-d');
|
|
|
|
$asset->next_audit_date = $next_audit_date ?? $asset->next_audit_date;
|
|
|
|
$asset->last_audit_date = $last_audit_date ?? $asset->last_audit_date;
|
2024-11-12 16:08:29 -08:00
|
|
|
if ($purchase_date && !$asset_eol_date && ($asset->model->eol > 0)) {
|
2024-11-18 22:13:28 -08:00
|
|
|
$asset->purchase_date = $purchase_date ?? $asset->purchase_date?->format('Y-m-d');
|
2024-11-12 16:08:29 -08:00
|
|
|
$asset->asset_eol_date = Carbon::parse($purchase_date)->addMonths($asset->model->eol)->format('Y-m-d');
|
2024-11-12 12:46:56 -08:00
|
|
|
$asset->eol_explicit = false;
|
2024-11-12 16:08:29 -08:00
|
|
|
} elseif ($asset_eol_date) {
|
|
|
|
$asset->asset_eol_date = $asset_eol_date ?? null;
|
2024-11-12 12:46:56 -08:00
|
|
|
$months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date);
|
|
|
|
if ($asset->model->eol) {
|
|
|
|
if ($months != $asset->model->eol > 0) {
|
|
|
|
$asset->eol_explicit = true;
|
|
|
|
} else {
|
|
|
|
$asset->eol_explicit = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$asset->eol_explicit = true;
|
|
|
|
}
|
2024-11-12 16:08:29 -08:00
|
|
|
} elseif (!$asset_eol_date && (($asset->model->eol) == 0)) {
|
2024-11-12 12:46:56 -08:00
|
|
|
$asset->asset_eol_date = null;
|
|
|
|
$asset->eol_explicit = false;
|
|
|
|
}
|
2024-11-12 16:08:29 -08:00
|
|
|
$asset->supplier_id = $supplier_id;
|
|
|
|
$asset->expected_checkin = $expected_checkin;
|
|
|
|
$asset->requestable = $requestable;
|
2024-11-18 22:13:28 -08:00
|
|
|
|
|
|
|
$asset->location_id = $location_id;
|
|
|
|
|
|
|
|
!$isBulk ?? $asset->rtd_location_id = $rtd_location_id;
|
2024-11-12 16:08:29 -08:00
|
|
|
$asset->byod = $byod;
|
2024-11-12 12:46:56 -08:00
|
|
|
|
|
|
|
$status = Statuslabel::find($status_id);
|
|
|
|
|
|
|
|
// This is a non-deployable status label - we should check the asset back in.
|
|
|
|
if (($status && $status->getStatuslabelType() != 'deployable') && ($target = $asset->assignedTo)) {
|
|
|
|
|
|
|
|
$originalValues = $asset->getRawOriginal();
|
|
|
|
$asset->assigned_to = null;
|
|
|
|
$asset->assigned_type = null;
|
|
|
|
$asset->accepted = null;
|
|
|
|
|
|
|
|
event(new CheckoutableCheckedIn($asset, $target, auth()->user(), 'Checkin on asset update', date('Y-m-d H:i:s'), $originalValues));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($asset->assigned_to == '') {
|
2024-11-12 16:08:29 -08:00
|
|
|
$asset->location_id = $rtd_location_id;
|
2024-11-12 12:46:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-12 16:08:29 -08:00
|
|
|
if ($image_delete) {
|
2024-11-12 12:46:56 -08:00
|
|
|
try {
|
|
|
|
unlink(public_path().'/uploads/assets/'.$asset->image);
|
|
|
|
$asset->image = '';
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::info($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 14:54:23 -08:00
|
|
|
$asset->serial = $serial;
|
2024-11-12 12:46:56 -08:00
|
|
|
|
2024-11-19 12:45:54 -08:00
|
|
|
if ($request->filled('null_name')) {
|
|
|
|
$asset->name = null;
|
|
|
|
} else {
|
|
|
|
$asset->name = $name ?? $asset->name;
|
|
|
|
}
|
2024-11-12 16:08:29 -08:00
|
|
|
$asset->company_id = Company::getIdForCurrentUser($company_id);
|
2024-11-18 22:13:28 -08:00
|
|
|
$asset->model_id = $model_id ?? $asset->model_id;
|
|
|
|
$asset->order_number = $order_number ?? $asset->order_number;
|
2024-11-12 12:46:56 -08:00
|
|
|
|
2024-11-18 22:13:28 -08:00
|
|
|
$asset->asset_tag = $asset_tag ?? $asset->asset_tag;
|
2024-11-12 12:46:56 -08:00
|
|
|
|
2024-11-12 16:08:29 -08:00
|
|
|
$asset->notes = $notes;
|
2024-11-12 12:46:56 -08:00
|
|
|
|
|
|
|
$asset = $request->handleImages($asset);
|
|
|
|
|
|
|
|
// Update custom fields in the database.
|
|
|
|
// Validation for these fields is handlded through the AssetRequest form request
|
|
|
|
// FIXME: No idea why this is returning a Builder error on db_column_name.
|
|
|
|
// Need to investigate and fix. Using static method for now.
|
2024-11-18 22:13:28 -08:00
|
|
|
|
2024-11-19 17:57:26 -08:00
|
|
|
// the gui method
|
2024-11-19 15:45:35 -08:00
|
|
|
//if (($model) && ($model->fieldset)) {
|
|
|
|
// dump($model->fieldset->fields);
|
|
|
|
// foreach ($model->fieldset->fields as $field) {
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// if ($field->field_encrypted == '1') {
|
|
|
|
// if (Gate::allows('assets.view.encrypted_custom_fields')) {
|
|
|
|
// if (is_array($request->input($field->db_column))) {
|
|
|
|
// $asset->{$field->db_column} = Crypt::encrypt(implode(', ', $request->input($field->db_column)));
|
|
|
|
// } else {
|
|
|
|
// $asset->{$field->db_column} = Crypt::encrypt($request->input($field->db_column));
|
|
|
|
// }
|
|
|
|
// throw new CustomFieldPermissionException();
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// if (is_array($request->input($field->db_column))) {
|
|
|
|
// $asset->{$field->db_column} = implode(', ', $request->input($field->db_column));
|
|
|
|
// } else {
|
|
|
|
// $asset->{$field->db_column} = $request->input($field->db_column);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
2024-11-19 17:57:26 -08:00
|
|
|
// the api method
|
2024-11-19 12:45:54 -08:00
|
|
|
$model = $asset->model;
|
2024-11-19 15:45:35 -08:00
|
|
|
if (($model) && (isset($model->fieldset))) {
|
2024-11-12 12:46:56 -08:00
|
|
|
foreach ($model->fieldset->fields as $field) {
|
2024-11-19 15:45:35 -08:00
|
|
|
$field_val = $request->input($field->db_column, null);
|
2024-11-12 12:46:56 -08:00
|
|
|
|
2024-11-19 15:45:35 -08:00
|
|
|
if ($request->has($field->db_column)) {
|
|
|
|
if ($field->element == 'checkbox') {
|
|
|
|
if (is_array($field_val)) {
|
|
|
|
$field_val = implode(',', $field_val);
|
2024-11-12 12:46:56 -08:00
|
|
|
}
|
|
|
|
}
|
2024-11-19 15:45:35 -08:00
|
|
|
if ($field->field_encrypted == '1') {
|
2024-11-19 17:57:26 -08:00
|
|
|
dump(Gate::allows('assets.view.encrypted_custom_fields'));
|
|
|
|
dump(auth()->user()->can('assets.view.encrypted_custom_fields'));
|
2024-11-19 15:45:35 -08:00
|
|
|
if (Gate::allows('assets.view.encrypted_custom_fields')) {
|
|
|
|
$field_val = Crypt::encrypt($field_val);
|
|
|
|
} else {
|
|
|
|
throw new CustomFieldPermissionException();
|
|
|
|
}
|
2024-11-12 12:46:56 -08:00
|
|
|
}
|
2024-11-19 15:45:35 -08:00
|
|
|
$asset->{$field->db_column} = $field_val;
|
2024-11-12 12:46:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);
|
|
|
|
|
2024-11-18 22:13:28 -08:00
|
|
|
if ($isBulk) {
|
|
|
|
self::bulkUpdate($asset, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($asset->save()) {
|
|
|
|
// check out stuff
|
2024-11-19 15:56:56 -08:00
|
|
|
$location = Location::find($asset->location_id);
|
2024-11-18 22:13:28 -08:00
|
|
|
if (!is_null($assigned_user) && ($target = User::find($assigned_user))) {
|
|
|
|
$location = $target->location_id;
|
|
|
|
} elseif (!is_null($assigned_asset) && ($target = Asset::find($assigned_asset))) {
|
|
|
|
$location = $target->location_id;
|
|
|
|
Asset::where('assigned_type', \App\Models\Asset::class)->where('assigned_to', $asset->id)
|
|
|
|
->update(['location_id' => $target->location_id]);
|
|
|
|
} elseif (!is_null($assigned_location) && ($target = Location::find($assigned_location))) {
|
|
|
|
$location = $target->id;
|
|
|
|
}
|
2024-11-19 08:02:04 -08:00
|
|
|
|
2024-11-18 22:13:28 -08:00
|
|
|
if (isset($target)) {
|
|
|
|
$asset->checkOut($target, auth()->user(), date('Y-m-d H:i:s'), '', 'Checked out on asset update', e($request->get('name')), $location);
|
|
|
|
}
|
2024-11-19 08:02:04 -08:00
|
|
|
|
|
|
|
if ($asset->image) {
|
|
|
|
$asset->image = $asset->getImageUrl();
|
|
|
|
}
|
2024-11-18 22:13:28 -08:00
|
|
|
}
|
2024-11-12 12:46:56 -08:00
|
|
|
|
2024-11-14 14:54:23 -08:00
|
|
|
return $asset;
|
2024-10-30 11:48:14 -07:00
|
|
|
}
|
2024-11-18 22:13:28 -08:00
|
|
|
|
|
|
|
private static function bulkUpdate($asset, $request): void
|
|
|
|
{
|
|
|
|
if ($request->filled('rtd_location_id')) {
|
|
|
|
|
|
|
|
if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '0')) {
|
|
|
|
$asset->rtd_location_id = $request->input('rtd_location_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '1')) {
|
|
|
|
$asset->location_id = $request->input('rtd_location_id');
|
|
|
|
$asset->rtd_location_id = $request->input('rtd_location_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '2')) {
|
|
|
|
$asset->location_id = $request->input('rtd_location_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-10-30 11:48:14 -07:00
|
|
|
}
|