good progress on update action

This commit is contained in:
spencerrlongg 2024-11-12 14:46:56 -06:00
parent a57fffe696
commit 916c0c0394
3 changed files with 209 additions and 38 deletions

View file

@ -2,10 +2,157 @@
namespace App\Actions\Assets; namespace App\Actions\Assets;
use App\Events\CheckoutableCheckedIn;
use App\Helpers\Helper;
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 class UpdateAssetAction
{ {
public static function run() public static function run(
Asset $asset,
$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 = null,
$rtd_location_id = null,
$byod = null
)
{ {
// stuff // Check if the asset exists
//if (!$asset = Asset::find($assetId)) {
// // Redirect to the asset management page with error
// return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
//}
//$this->authorize($asset);
$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 ($request->filled('purchase_date') && !$request->filled('asset_eol_date') && ($asset->model->eol > 0)) {
$asset->purchase_date = $request->input('purchase_date', null);
$asset->asset_eol_date = Carbon::parse($request->input('purchase_date'))->addMonths($asset->model->eol)->format('Y-m-d');
$asset->eol_explicit = false;
} elseif ($request->filled('asset_eol_date')) {
$asset->asset_eol_date = $request->input('asset_eol_date', null);
$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 (!$request->filled('asset_eol_date') && (($asset->model->eol) == 0)) {
$asset->asset_eol_date = null;
$asset->eol_explicit = false;
}
$asset->supplier_id = $request->input('supplier_id', null);
$asset->expected_checkin = $request->input('expected_checkin', null);
$asset->requestable = $request->input('requestable', 0);
$asset->rtd_location_id = $request->input('rtd_location_id', null);
$asset->byod = $request->input('byod', 0);
$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 = $request->input('rtd_location_id', null);
}
if ($request->filled('image_delete')) {
try {
unlink(public_path().'/uploads/assets/'.$asset->image);
$asset->image = '';
} catch (\Exception $e) {
Log::info($e);
}
}
// Update the asset data
$serial = $request->input('serials');
$asset->serial = $request->input('serials');
if (is_array($request->input('serials'))) {
$asset->serial = $serial[1];
}
$asset->name = $request->input('name');
$asset->company_id = Company::getIdForCurrentUser($request->input('company_id'));
$asset->model_id = $request->input('model_id');
$asset->order_number = $request->input('order_number');
$asset_tags = $request->input('asset_tags');
$asset->asset_tag = $request->input('asset_tags');
if (is_array($request->input('asset_tags'))) {
$asset->asset_tag = $asset_tags[1];
}
$asset->notes = $request->input('notes');
$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());
} }
} }

View file

@ -4,11 +4,13 @@ namespace App\Http\Controllers\Assets;
use App\Actions\Assets\DestroyAssetAction; use App\Actions\Assets\DestroyAssetAction;
use App\Actions\Assets\StoreAssetAction; use App\Actions\Assets\StoreAssetAction;
use App\Actions\Assets\UpdateAssetAction;
use App\Events\CheckoutableCheckedIn; use App\Events\CheckoutableCheckedIn;
use App\Exceptions\CheckoutNotAllowed; use App\Exceptions\CheckoutNotAllowed;
use App\Helpers\Helper; use App\Helpers\Helper;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests\Assets\DestroyAssetRequest; use App\Http\Requests\Assets\DestroyAssetRequest;
use App\Http\Requests\Assets\UpdateAssetRequest;
use App\Http\Requests\ImageUploadRequest; use App\Http\Requests\ImageUploadRequest;
use App\Http\Requests\Assets\StoreAssetRequest; use App\Http\Requests\Assets\StoreAssetRequest;
use App\Models\Actionlog; use App\Models\Actionlog;
@ -228,46 +230,66 @@ class AssetsController extends Controller
* @since [v1.0] * @since [v1.0]
* @author [A. Gianotto] [<snipe@snipe.net>] * @author [A. Gianotto] [<snipe@snipe.net>]
*/ */
public function update(ImageUploadRequest $request, $assetId = null) : RedirectResponse public function update(UpdateAssetRequest $request, Asset $asset): RedirectResponse
{ {
// Check if the asset exists // Check if the asset exists
if (! $asset = Asset::find($assetId)) { //if (! $asset = Asset::find($assetId)) {
// Redirect to the asset management page with error // // Redirect to the asset management page with error
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); // return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
//}
//$this->authorize($asset);
try {
$asset = UpdateAssetAction::run(
$asset,
status_id: $request->validated('status_id'),
warranty_months: $request->validated('warranty_months'),
purchase_cost: $request->validated('purchase_cost'),
purchase_date: $request->validated('purchase_date'),
next_audit_date: $request->validated('next_audit_date'),
asset_eol_date: $request->validated('asset_eol_date'),
supplier_id: $request->validated('supplier_id'),
expected_checkin: $request->validated('expected_checkin'),
requestable: $request->validated('requestable'),
rtd_location_id: $request->validated('rtd_location_id'),
byod: $request->validated('byod')
);
return redirect()->back()->with('success', trans('admin/hardware/message.update.success'));
} catch (\Exception $e) {
report($e);
return redirect()->back()->with('error', trans('admin/hardware/message.update.error'));
} }
$this->authorize($asset);
$asset->status_id = $request->input('status_id', null); //$asset->status_id = $request->input('status_id', null);
$asset->warranty_months = $request->input('warranty_months', null); //$asset->warranty_months = $request->input('warranty_months', null);
$asset->purchase_cost = $request->input('purchase_cost', null); //$asset->purchase_cost = $request->input('purchase_cost', null);
$asset->purchase_date = $request->input('purchase_date', null); //$asset->purchase_date = $request->input('purchase_date', null);
$asset->next_audit_date = $request->input('next_audit_date', null); //$asset->next_audit_date = $request->input('next_audit_date', null);
if ($request->filled('purchase_date') && !$request->filled('asset_eol_date') && ($asset->model->eol > 0)) { //if ($request->filled('purchase_date') && !$request->filled('asset_eol_date') && ($asset->model->eol > 0)) {
$asset->purchase_date = $request->input('purchase_date', null); // $asset->purchase_date = $request->input('purchase_date', null);
$asset->asset_eol_date = Carbon::parse($request->input('purchase_date'))->addMonths($asset->model->eol)->format('Y-m-d'); // $asset->asset_eol_date = Carbon::parse($request->input('purchase_date'))->addMonths($asset->model->eol)->format('Y-m-d');
$asset->eol_explicit = false; // $asset->eol_explicit = false;
} elseif ($request->filled('asset_eol_date')) { //} elseif ($request->filled('asset_eol_date')) {
$asset->asset_eol_date = $request->input('asset_eol_date', null); // $asset->asset_eol_date = $request->input('asset_eol_date', null);
$months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date); // $months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date);
if($asset->model->eol) { // if($asset->model->eol) {
if($months != $asset->model->eol > 0) { // if($months != $asset->model->eol > 0) {
$asset->eol_explicit = true; // $asset->eol_explicit = true;
} else { // } else {
$asset->eol_explicit = false; // $asset->eol_explicit = false;
} // }
} else { // } else {
$asset->eol_explicit = true; // $asset->eol_explicit = true;
} // }
} elseif (!$request->filled('asset_eol_date') && (($asset->model->eol) == 0)) { //} elseif (!$request->filled('asset_eol_date') && (($asset->model->eol) == 0)) {
$asset->asset_eol_date = null; // $asset->asset_eol_date = null;
$asset->eol_explicit = false; // $asset->eol_explicit = false;
} //}
$asset->supplier_id = $request->input('supplier_id', null); //$asset->supplier_id = $request->input('supplier_id', null);
$asset->expected_checkin = $request->input('expected_checkin', null); //$asset->expected_checkin = $request->input('expected_checkin', null);
$asset->requestable = $request->input('requestable', 0); //$asset->requestable = $request->input('requestable', 0);
$asset->rtd_location_id = $request->input('rtd_location_id', null); //$asset->rtd_location_id = $request->input('rtd_location_id', null);
$asset->byod = $request->input('byod', 0); //$asset->byod = $request->input('byod', 0);
$status = Statuslabel::find($request->input('status_id')); $status = Statuslabel::find($request->input('status_id'));

View file

@ -165,6 +165,8 @@ Route::group(
Route::delete('/hardware/{asset}', [AssetsController::class, 'destroy'])->name('hardware.destroy'); Route::delete('/hardware/{asset}', [AssetsController::class, 'destroy'])->name('hardware.destroy');
Route::match(['put', 'patch'], '/hardware/{asset}', [AssetsController::class, 'update'])->name('hardware.update');
Route::resource('hardware', Route::resource('hardware',
AssetsController::class, AssetsController::class,
[ [
@ -174,7 +176,7 @@ Route::resource('hardware',
'show' => 'view', 'show' => 'view',
], ],
], ],
'except' => ['destroy'], 'except' => ['destroy', 'update'],
]); ]);
Route::get('ht/{any?}', Route::get('ht/{any?}',