mirror of
https://github.com/snipe/snipe-it.git
synced 2025-02-21 03:15:45 -08:00
Added create/destroy methods for asset API
This commit is contained in:
parent
2c695cf7e5
commit
95f2d94e01
|
@ -38,6 +38,7 @@ use TCPDF;
|
|||
use Validator;
|
||||
use View;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
/**
|
||||
* This class controls all actions related to assets for
|
||||
|
@ -49,6 +50,14 @@ use App\Http\Controllers\Controller;
|
|||
class AssetsController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns JSON listing of all assets
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @param int $assetId
|
||||
* @since [v4.0]
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function index(Request $request, $status = null)
|
||||
{
|
||||
$this->authorize('index', 'App\Models\Asset');
|
||||
|
@ -174,8 +183,8 @@ class AssetsController extends Controller
|
|||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @param int $assetId
|
||||
* @since [v1.0]
|
||||
* @return View
|
||||
* @since [v4.0]
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function show($id = null)
|
||||
{
|
||||
|
@ -191,5 +200,97 @@ class AssetsController extends Controller
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Accepts a POST request to create a new asset
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @param int $assetId
|
||||
* @since [v4.0]
|
||||
* @return Redirect
|
||||
*/
|
||||
public function store(AssetRequest $request)
|
||||
{
|
||||
$this->authorize('create', Asset::class);
|
||||
|
||||
$asset = new Asset();
|
||||
$asset->model()->associate(AssetModel::find(e($request->get('model_id'))));
|
||||
|
||||
$asset->name = $request->get('name');
|
||||
$asset->serial = $request->get('serial');
|
||||
$asset->company_id = Company::getIdForCurrentUser($request->get('company_id'));
|
||||
$asset->model_id = $request->get('model_id');
|
||||
$asset->order_number = $request->get('order_number');
|
||||
$asset->notes = $request->get('notes');
|
||||
$asset->asset_tag = $request->get('asset_tag');
|
||||
$asset->user_id = Auth::id();
|
||||
$asset->archived = '0';
|
||||
$asset->physical = '1';
|
||||
$asset->depreciate = '0';
|
||||
$asset->status_id = $request->get('status_id', 0);
|
||||
$asset->warranty_months = $request->get('warranty_months', null);
|
||||
$asset->purchase_cost = Helper::ParseFloat($request->get('purchase_cost'));
|
||||
$asset->purchase_date = $request->get('purchase_date', null);
|
||||
$asset->assigned_to = $request->get('assigned_to', null);
|
||||
$asset->supplier_id = $request->get('supplier_id', 0);
|
||||
$asset->requestable = $request->get('requestable', 0);
|
||||
$asset->rtd_location_id = $request->get('rtd_location_id', null);
|
||||
|
||||
// Update custom fields in the database.
|
||||
// Validation for these fields is handled through the AssetRequest form request
|
||||
// Need to investigate and fix. Using static method for now.
|
||||
$model = AssetModel::find($request->get('model_id'));
|
||||
if ($model->fieldset) {
|
||||
foreach ($model->fieldset->fields as $field) {
|
||||
$asset->{CustomField::name_to_db_name($field->name)} = e($request->input(CustomField::name_to_db_name($field->name)));
|
||||
}
|
||||
}
|
||||
|
||||
if ($asset->save()) {
|
||||
$asset->logCreate();
|
||||
if(request('assigned_user')) {
|
||||
$target = User::find(request('assigned_user'));
|
||||
} elseif(request('assigned_asset')) {
|
||||
$target = Asset::find(request('assigned_asset'));
|
||||
} elseif(request('assigned_location')) {
|
||||
$target = Location::find(request('assigned_location'));
|
||||
}
|
||||
if ($target) {
|
||||
$asset->checkOut($target, Auth::user(), date('Y-m-d H:i:s'), '', 'Checked out on asset creation', e($request->get('name')));
|
||||
}
|
||||
return response()->json(['success' => trans('admin/hardware/message.create.success')]);
|
||||
|
||||
}
|
||||
return response()->json(['errors' => $asset->getErrors()], 500);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a given asset (mark as deleted).
|
||||
*
|
||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||
* @param int $assetId
|
||||
* @since [v4.0]
|
||||
* @return Redirect
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
|
||||
if ($asset = Asset::find($id)) {
|
||||
$this->authorize('delete', $asset);
|
||||
|
||||
DB::table('assets')
|
||||
->where('id', $asset->id)
|
||||
->update(array('assigned_to' => null));
|
||||
|
||||
$asset->delete();
|
||||
return response()->json(['success' => trans('admin/hardware/message.delete.success')]);
|
||||
|
||||
}
|
||||
return response()->json(['error' => trans('admin/hardware/message.does_not_exist')], 404);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -579,6 +579,9 @@ class AssetsController extends Controller
|
|||
$asset = Asset::withTrashed()->find($assetId);
|
||||
$settings = Setting::getSettings();
|
||||
$this->authorize('view', $asset);
|
||||
|
||||
if (isset($asset->id)) {
|
||||
|
||||
if ($asset->userloc) {
|
||||
$use_currency = $asset->userloc->currency;
|
||||
} elseif ($asset->assetloc) {
|
||||
|
@ -592,8 +595,6 @@ class AssetsController extends Controller
|
|||
}
|
||||
}
|
||||
|
||||
if (isset($asset->id)) {
|
||||
|
||||
$qr_code = (object) array(
|
||||
'display' => $settings->qr_code == '1',
|
||||
'url' => route('qr_code/hardware', $asset->id)
|
||||
|
@ -601,11 +602,8 @@ class AssetsController extends Controller
|
|||
|
||||
return View::make('hardware/view', compact('asset', 'qr_code', 'settings'))->with('use_currency', $use_currency);
|
||||
}
|
||||
// Prepare the error message
|
||||
$error = trans('admin/hardware/message.does_not_exist', compact('id'));
|
||||
|
||||
// Redirect to the user management page
|
||||
return redirect()->route('hardware')->with('error', $error);
|
||||
return redirect()->route('hardware')->with('error', trans('admin/hardware/message.does_not_exist', compact('id')));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -728,7 +726,6 @@ class AssetsController extends Controller
|
|||
|
||||
if (!Company::isCurrentUserAuthorized()) {
|
||||
return redirect()->route('hardware.index')->with('error', trans('general.insufficient_permissions'));
|
||||
|
||||
} elseif (!config('app.lock_passwords')) {
|
||||
|
||||
$files = Input::file('files');
|
||||
|
|
|
@ -26,7 +26,8 @@ Route::group(['prefix' => 'v1'], function () {
|
|||
['names' =>
|
||||
[
|
||||
'index' => 'api.assets.index',
|
||||
'create' => 'api.assets.create'
|
||||
'create' => 'api.assets.create',
|
||||
'destroy' => 'api.assets.destroy'
|
||||
],
|
||||
'parameters' =>
|
||||
['asset' => 'asset_id']
|
||||
|
|
Loading…
Reference in a new issue