Skip to content

A free open source IT asset/license management system

Merge branch 'develop' into bug/sc-24884

Spencer Long013463aafc7d
diff --cc app/Http/Controllers/Api/AssetsController.php
index 1b671386ab,dd375d0823..38f1356812
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@@ -625,70 -629,90 +630,79 @@@ class AssetsController extends Controll
* Accepts a POST request to update an asset
*
* @author [A. Gianotto] [<[email protected]>]
- * @param \App\Http\Requests\ImageUploadRequest $request
* @since [v4.0]
- * @return \Illuminate\Http\JsonResponse
*/
- public function update(ImageUploadRequest $request, $id)
+ public function update(UpdateAssetRequest $request, Asset $asset): JsonResponse
{
- $this->authorize('update', Asset::class);
+ $asset->fill($request->validated());
- if ($asset = Asset::find($id)) {
- $asset->fill($request->all());
-
- ($request->filled('model_id')) ?
- $asset->model()->associate(AssetModel::find($request->get('model_id'))) : null;
- ($request->filled('rtd_location_id')) ?
- $asset->location_id = $request->get('rtd_location_id') : '';
- ($request->filled('company_id')) ?
- $asset->company_id = Company::getIdForCurrentUser($request->get('company_id')) : '';
-
- ($request->filled('rtd_location_id')) ?
- $asset->location_id = $request->get('rtd_location_id') : null;
-
- /**
- * this is here just legacy reasons. Api\AssetController
- * used image_source once to allow encoded image uploads.
- */
- if ($request->has('image_source')) {
- $request->offsetSet('image', $request->offsetGet('image_source'));
- }
-
- $asset = $request->handleImages($asset);
- $model = AssetModel::find($asset->model_id);
+ if ($request->has('model_id')) {
+ $asset->model()->associate(AssetModel::find($request->validated()['model_id']));
+ }
+ if ($request->has('company_id')) {
+ $asset->company_id = Company::getIdForCurrentUser($request->validated()['company_id']);
+ }
+ if ($request->has('rtd_location_id') && !$request->has('location_id')) {
+ $asset->location_id = $request->validated()['rtd_location_id'];
+ }
+
+ /**
- * this is here just legacy reasons. Api\AssetController
- * used image_source once to allow encoded image uploads.
- */
++ * this is here just legacy reasons. Api\AssetController
++ * used image_source once to allow encoded image uploads.
++ */
+ if ($request->has('image_source')) {
+ $request->offsetSet('image', $request->offsetGet('image_source'));
- }
++ }
+
+ $asset = $request->handleImages($asset);
+ $model = $asset->model;
-
+
- // Update custom fields
- if (($model) && (isset($model->fieldset))) {
- foreach ($model->fieldset->fields as $field) {
- $field_val = $request->input($field->db_column, null);
-
- if ($request->has($field->db_column)) {
- if ($field->field_encrypted == '1') {
- if (Gate::allows('admin')) {
- $asset->{$field->db_column} = Crypt::encrypt($field_val);
- }
- }
- if ($field->element == 'checkbox') {
- if(is_array($field_val)) {
- $field_val = implode(',', $field_val);
- $asset->{$field->db_column} = $field_val;
- }
+ // Update custom fields
+ if (($model) && (isset($model->fieldset))) {
+ foreach ($model->fieldset->fields as $field) {
++ $field_val = $request->input($field->db_column, null);
++
+ if ($request->has($field->db_column)) {
+ if ($field->field_encrypted == '1') {
+ if (Gate::allows('admin')) {
- $asset->{$field->db_column} = Crypt::encrypt($request->input($field->db_column));
++ $asset->{$field->db_column} = Crypt::encrypt($field_val);
}
- } else {
- $asset->{$field->db_column} = $request->input($field->db_column);
- else {
++ }
++ if ($field->element == 'checkbox') {
++ if(is_array($field_val)) {
++ $field_val = implode(',', $field_val);
+ $asset->{$field->db_column} = $field_val;
+ }
+ }
++ else {
++ $asset->{$field->db_column} = $field_val;
+ }
}
}
+ }
+ if ($asset->save()) {
+ if (($request->filled('assigned_user')) && ($target = User::find($request->get('assigned_user')))) {
+ $location = $target->location_id;
+ } elseif (($request->filled('assigned_asset')) && ($target = Asset::find($request->get('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 (($request->filled('assigned_location')) && ($target = Location::find($request->get('assigned_location')))) {
+ $location = $target->id;
+ }
- if ($asset->save()) {
- if (($request->filled('assigned_user')) && ($target = User::find($request->get('assigned_user')))) {
- $location = $target->location_id;
- } elseif (($request->filled('assigned_asset')) && ($target = Asset::find($request->get('assigned_asset')))) {
- $location = $target->location_id;
-
- Asset::where('assigned_type', \App\Models\Asset::class)->where('assigned_to', $id)
- ->update(['location_id' => $target->location_id]);
- } elseif (($request->filled('assigned_location')) && ($target = Location::find($request->get('assigned_location')))) {
- $location = $target->id;
- }
-
- 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);
- }
-
- if ($asset->image) {
- $asset->image = $asset->getImageUrl();
- }
-
- return response()->json(Helper::formatStandardApiResponse('success', (new AssetsTransformer)->transformAsset($asset), trans('admin/hardware/message.update.success')));
+ 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);
}
- return response()->json(Helper::formatStandardApiResponse('error', null, $asset->getErrors()), 200);
+ if ($asset->image) {
+ $asset->image = $asset->getImageUrl();
+ }
- return response()->json(Helper::formatStandardApiResponse('success', $asset, trans('admin/hardware/message.update.success')));
++ return response()->json(Helper::formatStandardApiResponse('success', (new AssetsTransformer)->transformAsset($asset), trans('admin/hardware/message.update.success')));
}
-
- return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200);
+ return response()->json(Helper::formatStandardApiResponse('error', null, $asset->getErrors()), 200);
}
diff --cc app/Models/Asset.php
index 112c79aee6,2830eca7e5..f9c9ce15ee
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@@ -90,29 -89,32 +90,31 @@@ class Asset extends Depreciabl
];
protected $rules = [
- 'model_id' => 'required|integer|exists:models,id,deleted_at,NULL|not_array',
- 'status_id' => 'required|integer|exists:status_labels,id',
- 'asset_tag' => 'required|min:1|max:255|unique_undeleted:assets,asset_tag|not_array',
- 'name' => 'nullable|max:255',
- 'company_id' => 'nullable|integer|exists:companies,id',
- 'warranty_months' => 'nullable|numeric|digits_between:0,240',
- 'last_checkout' => 'nullable|date_format:Y-m-d H:i:s',
+ 'model_id' => 'required|integer|exists:models,id,deleted_at,NULL|not_array',
+ 'status_id' => 'required|integer|exists:status_labels,id',
+ 'asset_tag' => 'required|min:1|max:255|unique_undeleted:assets,asset_tag|not_array',
+ 'name' => 'nullable|max:255',
+ 'company_id' => 'nullable|integer|exists:companies,id',
+ 'warranty_months' => 'nullable|numeric|digits_between:0,240',
+ 'last_checkout' => 'nullable|date_format:Y-m-d H:i:s',
'expected_checkin' => 'nullable|date',
- 'location_id' => 'nullable|exists:locations,id',
- 'rtd_location_id' => 'nullable|exists:locations,id',
- 'purchase_date' => 'nullable|date|date_format:Y-m-d',
- 'serial' => 'nullable|unique_undeleted:assets,serial',
- 'purchase_cost' => 'nullable|numeric|gte:0',
- 'supplier_id' => 'nullable|exists:suppliers,id',
- 'asset_eol_date' => 'nullable|date',
- 'eol_explicit' => 'nullable|boolean',
- 'byod' => 'nullable|boolean',
- 'order_number' => 'nullable|string|max:191',
- 'notes' => 'nullable|string|max:65535',
- 'assigned_to' => 'nullable|integer',
- 'requestable' => 'nullable|boolean',
+ 'last_audit_date' => 'nullable|date_format:Y-m-d H:i:s',
+ 'next_audit_date' => 'nullable|date|after:last_audit_date',
+ 'location_id' => 'nullable|exists:locations,id',
+ 'rtd_location_id' => 'nullable|exists:locations,id',
+ 'purchase_date' => 'nullable|date|date_format:Y-m-d',
+ 'serial' => 'nullable|unique_undeleted:assets,serial',
+ 'purchase_cost' => 'nullable|numeric|gte:0',
+ 'supplier_id' => 'nullable|exists:suppliers,id',
+ 'asset_eol_date' => 'nullable|date',
+ 'eol_explicit' => 'nullable|boolean',
+ 'byod' => 'nullable|boolean',
+ 'order_number' => 'nullable|string|max:191',
+ 'notes' => 'nullable|string|max:65535',
+ 'assigned_to' => 'nullable|integer',
+ 'requestable' => 'nullable|boolean',
];
-
/**
* The attributes that are mass assignable.
*