some more good progress on UpdateAssetAction.php

This commit is contained in:
spencerrlongg 2024-11-12 18:08:29 -06:00
parent 916c0c0394
commit 47d132703e
4 changed files with 58 additions and 107 deletions

View file

@ -3,6 +3,7 @@
namespace App\Actions\Assets; namespace App\Actions\Assets;
use App\Exceptions\CheckoutNotAllowed; use App\Exceptions\CheckoutNotAllowed;
use App\Http\Requests\ImageUploadRequest;
use App\Models\Asset; use App\Models\Asset;
use App\Models\AssetModel; use App\Models\AssetModel;
use App\Models\Company; use App\Models\Company;
@ -23,6 +24,7 @@ class StoreAssetAction
public static function run( public static function run(
$model_id,//gonna make these two optional for now... we can either make them required here or use the spread operator when calling... $model_id,//gonna make these two optional for now... we can either make them required here or use the spread operator when calling...
$status_id,// $status_id,//
ImageUploadRequest $request, //temp for handleImages - i'd like to see that moved to a helper or something - or maybe just invoked at the extended request level so that it doesn't need to be done in the action?
$name = null, $name = null,
$serial = null, $serial = null,
$company_id = null, $company_id = null,
@ -45,7 +47,7 @@ class StoreAssetAction
$assigned_asset = null, $assigned_asset = null,
$assigned_location = null, $assigned_location = null,
$custom_fields = null, $custom_fields = null,
$request = null, //temp for handleImages - i'd like to see that moved to a helper or something - or maybe just invoked at the extended request level so that it doesn't need to be done in the action?
$last_audit_date = null, $last_audit_date = null,
) )
{ {

View file

@ -4,6 +4,8 @@ namespace App\Actions\Assets;
use App\Events\CheckoutableCheckedIn; use App\Events\CheckoutableCheckedIn;
use App\Helpers\Helper; use App\Helpers\Helper;
use App\Http\Requests\ImageUploadRequest;
use App\Http\Requests\Request;
use App\Models\Asset; use App\Models\Asset;
use App\Models\AssetModel; use App\Models\AssetModel;
use App\Models\Company; use App\Models\Company;
@ -17,6 +19,7 @@ class UpdateAssetAction
{ {
public static function run( public static function run(
Asset $asset, Asset $asset,
ImageUploadRequest $request,
$status_id = null, $status_id = null,
$warranty_months = null, $warranty_months = null,
$purchase_cost = null, $purchase_cost = null,
@ -25,29 +28,31 @@ class UpdateAssetAction
$asset_eol_date = null, $asset_eol_date = null,
$supplier_id = null, $supplier_id = null,
$expected_checkin = null, $expected_checkin = null,
$requestable = null, $requestable = false,
$rtd_location_id = null, $rtd_location_id = null,
$byod = null $byod = false,
$image_delete = false,
$serials = null,
$name = null,
$company_id = null,
$model_id = null,
$order_number = null,
$asset_tags = null,
$notes = null,
) )
{ {
// 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->status_id = $status_id;
$asset->warranty_months = $warranty_months; $asset->warranty_months = $warranty_months;
$asset->purchase_cost = $purchase_cost; $asset->purchase_cost = $purchase_cost;
$asset->purchase_date = $purchase_date; $asset->purchase_date = $purchase_date;
$asset->next_audit_date = $next_audit_date; $asset->next_audit_date = $next_audit_date;
if ($request->filled('purchase_date') && !$request->filled('asset_eol_date') && ($asset->model->eol > 0)) { if ($purchase_date && !$asset_eol_date && ($asset->model->eol > 0)) {
$asset->purchase_date = $request->input('purchase_date', null); $asset->purchase_date = $purchase_date;
$asset->asset_eol_date = Carbon::parse($request->input('purchase_date'))->addMonths($asset->model->eol)->format('Y-m-d'); $asset->asset_eol_date = Carbon::parse($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 ($asset_eol_date) {
$asset->asset_eol_date = $request->input('asset_eol_date', null); $asset->asset_eol_date = $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) {
@ -58,15 +63,15 @@ class UpdateAssetAction
} else { } else {
$asset->eol_explicit = true; $asset->eol_explicit = true;
} }
} elseif (!$request->filled('asset_eol_date') && (($asset->model->eol) == 0)) { } elseif (!$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 = $supplier_id;
$asset->expected_checkin = $request->input('expected_checkin', null); $asset->expected_checkin = $expected_checkin;
$asset->requestable = $request->input('requestable', 0); $asset->requestable = $requestable;
$asset->rtd_location_id = $request->input('rtd_location_id', null); $asset->rtd_location_id = $rtd_location_id;
$asset->byod = $request->input('byod', 0); $asset->byod = $byod;
$status = Statuslabel::find($status_id); $status = Statuslabel::find($status_id);
@ -82,11 +87,11 @@ class UpdateAssetAction
} }
if ($asset->assigned_to == '') { if ($asset->assigned_to == '') {
$asset->location_id = $request->input('rtd_location_id', null); $asset->location_id = $rtd_location_id;
} }
if ($request->filled('image_delete')) { if ($image_delete) {
try { try {
unlink(public_path().'/uploads/assets/'.$asset->image); unlink(public_path().'/uploads/assets/'.$asset->image);
$asset->image = ''; $asset->image = '';
@ -95,29 +100,32 @@ class UpdateAssetAction
} }
} }
// Update the asset data // 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;
$serial = $request->input('serials'); if (is_array($serials)) {
$asset->serial = $request->input('serials');
if (is_array($request->input('serials'))) {
$asset->serial = $serial[1]; $asset->serial = $serial[1];
} }
$asset->name = $request->input('name'); $asset->name = $name;
$asset->company_id = Company::getIdForCurrentUser($request->input('company_id')); $asset->company_id = Company::getIdForCurrentUser($company_id);
$asset->model_id = $request->input('model_id'); $asset->model_id = $model_id;
$asset->order_number = $request->input('order_number'); $asset->order_number = $order_number;
$asset_tags = $request->input('asset_tags'); // same thing as serials above
$asset->asset_tag = $request->input('asset_tags'); $asset_tags = $asset_tags;
$asset->asset_tag = $asset_tags;
if (is_array($request->input('asset_tags'))) { if (is_array($asset_tags)) {
$asset->asset_tag = $asset_tags[1]; $asset->asset_tag = $asset_tags[1];
} }
$asset->notes = $request->input('notes'); $asset->notes = $notes;
// andddddddd the handleImages issue reappears - i guess we'll be passing the request through here as well
$asset = $request->handleImages($asset); $asset = $request->handleImages($asset);
// Update custom fields in the database. // Update custom fields in the database.

View file

@ -232,16 +232,10 @@ class AssetsController extends Controller
*/ */
public function update(UpdateAssetRequest $request, Asset $asset): RedirectResponse public function update(UpdateAssetRequest $request, Asset $asset): RedirectResponse
{ {
// 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);
try { try {
$asset = UpdateAssetAction::run( $asset = UpdateAssetAction::run(
$asset, asset: $asset,
request: $request,
status_id: $request->validated('status_id'), status_id: $request->validated('status_id'),
warranty_months: $request->validated('warranty_months'), warranty_months: $request->validated('warranty_months'),
purchase_cost: $request->validated('purchase_cost'), purchase_cost: $request->validated('purchase_cost'),
@ -252,7 +246,15 @@ class AssetsController extends Controller
expected_checkin: $request->validated('expected_checkin'), expected_checkin: $request->validated('expected_checkin'),
requestable: $request->validated('requestable'), requestable: $request->validated('requestable'),
rtd_location_id: $request->validated('rtd_location_id'), rtd_location_id: $request->validated('rtd_location_id'),
byod: $request->validated('byod') byod: $request->validated('byod'),
image_delete: $request->validated('image_delete'),
serials: null, // this needs to be set up in request somehow
name: $request->validated('name'),
company_id: $request->validated('company_id'),
model_id: $request->validated('model_id'),
order_number: $request->validated('order_number'),
asset_tags: null, // same as serials
notes: $request->validated('notes'),
); );
return redirect()->back()->with('success', trans('admin/hardware/message.update.success')); return redirect()->back()->with('success', trans('admin/hardware/message.update.success'));
} catch (\Exception $e) { } catch (\Exception $e) {
@ -260,66 +262,7 @@ class AssetsController extends Controller
return redirect()->back()->with('error', trans('admin/hardware/message.update.error')); return redirect()->back()->with('error', trans('admin/hardware/message.update.error'));
} }
//$asset->status_id = $request->input('status_id', null); // serials????
//$asset->warranty_months = $request->input('warranty_months', null);
//$asset->purchase_cost = $request->input('purchase_cost', null);
//$asset->purchase_date = $request->input('purchase_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)) {
// $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($request->input('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'); $serial = $request->input('serials');
$asset->serial = $request->input('serials'); $asset->serial = $request->input('serials');
@ -344,9 +287,6 @@ class AssetsController extends Controller
$asset = $request->handleImages($asset); $asset = $request->handleImages($asset);
// Update custom fields in the database. // 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')); $model = AssetModel::find($request->get('model_id'));
if (($model) && ($model->fieldset)) { if (($model) && ($model->fieldset)) {
foreach ($model->fieldset->fields as $field) { foreach ($model->fieldset->fields as $field) {

View file

@ -34,6 +34,7 @@ class UpdateAssetRequest extends ImageUploadRequest
(new Asset)->getRules(), (new Asset)->getRules(),
// this is to overwrite rulesets that include required, and rewrite unique_undeleted // this is to overwrite rulesets that include required, and rewrite unique_undeleted
[ [
'image_delete' => ['bool'],
'model_id' => ['integer', 'exists:models,id,deleted_at,NULL', 'not_array'], 'model_id' => ['integer', 'exists:models,id,deleted_at,NULL', 'not_array'],
'status_id' => ['integer', 'exists:status_labels,id'], 'status_id' => ['integer', 'exists:status_labels,id'],
'asset_tag' => [ 'asset_tag' => [