snipe-it/app/Actions/Assets/UpdateAssetAction.php

166 lines
6.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Actions\Assets;
2024-11-12 12:46:56 -08:00
use App\Events\CheckoutableCheckedIn;
use App\Helpers\Helper;
use App\Http\Requests\ImageUploadRequest;
use App\Http\Requests\Request;
2024-11-12 12:46:56 -08:00
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company;
use App\Models\Statuslabel;
use Carbon\Carbon;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
class UpdateAssetAction
{
2024-11-12 12:46:56 -08:00
public static function run(
Asset $asset,
ImageUploadRequest $request,
2024-11-12 12:46:56 -08:00
$status_id = null,
$warranty_months = null,
$purchase_cost = null,
$purchase_date = null,
$next_audit_date = null,
$asset_eol_date = null,
$supplier_id = null,
$expected_checkin = null,
$requestable = false,
2024-11-12 12:46:56 -08:00
$rtd_location_id = null,
$byod = false,
$image_delete = false,
$serials = null,
$name = null,
$company_id = null,
$model_id = null,
$order_number = null,
$asset_tags = null,
$notes = null,
2024-11-12 12:46:56 -08:00
)
{
2024-11-12 12:46:56 -08:00
$asset->status_id = $status_id;
$asset->warranty_months = $warranty_months;
$asset->purchase_cost = $purchase_cost;
$asset->purchase_date = $purchase_date;
$asset->next_audit_date = $next_audit_date;
if ($purchase_date && !$asset_eol_date && ($asset->model->eol > 0)) {
$asset->purchase_date = $purchase_date;
$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;
} 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;
}
} 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;
}
$asset->supplier_id = $supplier_id;
$asset->expected_checkin = $expected_checkin;
$asset->requestable = $requestable;
$asset->rtd_location_id = $rtd_location_id;
$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 == '') {
$asset->location_id = $rtd_location_id;
2024-11-12 12:46:56 -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);
}
}
// this is gonna be a whole issue with validation - i'm assuming it's because we're using the same blade
// to do both create (which allows multiple creates) and update. should be fixable.
// and why is the offset 1 and not 0? very confusing.
$serial = $serials;
$asset->serial = $serials;
2024-11-12 12:46:56 -08:00
if (is_array($serials)) {
2024-11-12 12:46:56 -08:00
$asset->serial = $serial[1];
}
$asset->name = $name;
$asset->company_id = Company::getIdForCurrentUser($company_id);
$asset->model_id = $model_id;
$asset->order_number = $order_number;
2024-11-12 12:46:56 -08:00
// same thing as serials above
$asset_tags = $asset_tags;
$asset->asset_tag = $asset_tags;
2024-11-12 12:46:56 -08:00
if (is_array($asset_tags)) {
2024-11-12 12:46:56 -08:00
$asset->asset_tag = $asset_tags[1];
}
$asset->notes = $notes;
2024-11-12 12:46:56 -08:00
// andddddddd the handleImages issue reappears - i guess we'll be passing the request through here as well
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.
$model = AssetModel::find($request->get('model_id'));
if (($model) && ($model->fieldset)) {
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));
}
}
} 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);
}
}
}
}
session()->put(['redirect_option' => $request->get('redirect_option'), 'checkout_to_type' => $request->get('checkout_to_type')]);
if ($asset->save()) {
return redirect()->to(Helper::getRedirectOption($request, $assetId, 'Assets'))
->with('success', trans('admin/hardware/message.update.success'));
}
return redirect()->back()->withInput()->withErrors($asset->getErrors());
}
}