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;
use App\Exceptions\CheckoutNotAllowed;
use App\Http\Requests\ImageUploadRequest;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company;
@ -23,6 +24,7 @@ class StoreAssetAction
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...
$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,
$serial = null,
$company_id = null,
@ -45,7 +47,7 @@ class StoreAssetAction
$assigned_asset = null,
$assigned_location = 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,
)
{

View file

@ -4,6 +4,8 @@ namespace App\Actions\Assets;
use App\Events\CheckoutableCheckedIn;
use App\Helpers\Helper;
use App\Http\Requests\ImageUploadRequest;
use App\Http\Requests\Request;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company;
@ -17,6 +19,7 @@ class UpdateAssetAction
{
public static function run(
Asset $asset,
ImageUploadRequest $request,
$status_id = null,
$warranty_months = null,
$purchase_cost = null,
@ -25,29 +28,31 @@ class UpdateAssetAction
$asset_eol_date = null,
$supplier_id = null,
$expected_checkin = null,
$requestable = null,
$requestable = false,
$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->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');
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');
$asset->eol_explicit = false;
} elseif ($request->filled('asset_eol_date')) {
$asset->asset_eol_date = $request->input('asset_eol_date', null);
} elseif ($asset_eol_date) {
$asset->asset_eol_date = $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) {
@ -58,15 +63,15 @@ class UpdateAssetAction
} else {
$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->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);
$asset->supplier_id = $supplier_id;
$asset->expected_checkin = $expected_checkin;
$asset->requestable = $requestable;
$asset->rtd_location_id = $rtd_location_id;
$asset->byod = $byod;
$status = Statuslabel::find($status_id);
@ -82,11 +87,11 @@ class UpdateAssetAction
}
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 {
unlink(public_path().'/uploads/assets/'.$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');
$asset->serial = $request->input('serials');
if (is_array($request->input('serials'))) {
if (is_array($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->name = $name;
$asset->company_id = Company::getIdForCurrentUser($company_id);
$asset->model_id = $model_id;
$asset->order_number = $order_number;
$asset_tags = $request->input('asset_tags');
$asset->asset_tag = $request->input('asset_tags');
// same thing as serials above
$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->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);
// Update custom fields in the database.

View file

@ -232,16 +232,10 @@ class AssetsController extends Controller
*/
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 {
$asset = UpdateAssetAction::run(
$asset,
asset: $asset,
request: $request,
status_id: $request->validated('status_id'),
warranty_months: $request->validated('warranty_months'),
purchase_cost: $request->validated('purchase_cost'),
@ -252,7 +246,15 @@ class AssetsController extends Controller
expected_checkin: $request->validated('expected_checkin'),
requestable: $request->validated('requestable'),
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'));
} catch (\Exception $e) {
@ -260,66 +262,7 @@ class AssetsController extends Controller
return redirect()->back()->with('error', trans('admin/hardware/message.update.error'));
}
//$asset->status_id = $request->input('status_id', null);
//$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
// serials????
$serial = $request->input('serials');
$asset->serial = $request->input('serials');
@ -344,9 +287,6 @@ class AssetsController extends Controller
$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) {

View file

@ -34,6 +34,7 @@ class UpdateAssetRequest extends ImageUploadRequest
(new Asset)->getRules(),
// 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'],
'status_id' => ['integer', 'exists:status_labels,id'],
'asset_tag' => [