2017-01-11 18:14:06 -08:00
< ? php
2021-06-10 13:15:52 -07:00
2017-01-11 18:14:06 -08:00
namespace App\Http\Controllers\Api ;
2021-06-08 11:16:13 -07:00
use App\Events\CheckoutableCheckedIn ;
2023-11-28 13:17:46 -08:00
use App\Http\Requests\StoreAssetRequest ;
2023-11-28 19:46:03 -08:00
use Illuminate\Http\JsonResponse ;
2023-11-28 13:17:46 -08:00
use Illuminate\Support\Facades\Crypt ;
2019-05-15 15:47:40 -07:00
use Illuminate\Support\Facades\Gate ;
2017-01-11 18:14:06 -08:00
use App\Helpers\Helper ;
2017-01-25 21:29:23 -08:00
use App\Http\Controllers\Controller ;
2017-11-27 21:17:16 -08:00
use App\Http\Requests\AssetCheckoutRequest ;
2017-01-25 21:29:23 -08:00
use App\Http\Transformers\AssetsTransformer ;
2019-01-22 14:47:40 -08:00
use App\Http\Transformers\LicensesTransformer ;
2019-03-13 20:12:03 -07:00
use App\Http\Transformers\SelectlistTransformer ;
2017-01-11 18:14:06 -08:00
use App\Models\Asset ;
use App\Models\AssetModel ;
use App\Models\Company ;
use App\Models\CustomField ;
2019-01-22 14:47:40 -08:00
use App\Models\License ;
2017-01-11 18:14:06 -08:00
use App\Models\Location ;
2017-01-25 21:29:23 -08:00
use App\Models\Setting ;
2017-01-11 18:14:06 -08:00
use App\Models\User ;
2023-11-28 13:17:46 -08:00
use \Illuminate\Support\Facades\Auth ;
2017-01-25 21:29:23 -08:00
use Carbon\Carbon ;
use DB ;
2017-01-11 18:14:06 -08:00
use Illuminate\Http\Request ;
2021-06-29 02:26:24 -07:00
use App\Http\Requests\ImageUploadRequest ;
2023-11-28 13:17:46 -08:00
use Illuminate\Support\Facades\Log ;
2017-01-25 21:29:23 -08:00
use Input ;
use Paginator ;
use Slack ;
use Str ;
use TCPDF ;
use Validator ;
2021-09-01 17:33:39 -07:00
use Route ;
2017-01-11 18:14:06 -08:00
2023-10-31 19:06:44 -07:00
2017-01-11 18:14:06 -08:00
/**
* This class controls all actions related to assets for
* the Snipe - IT Asset Management application .
*
* @ version v1 . 0
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
*/
class AssetsController extends Controller
{
2017-01-11 23:40:56 -08:00
/**
* Returns JSON listing of all assets
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v4 . 0 ]
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2017-01-11 23:40:56 -08:00
*/
2021-09-01 17:33:39 -07:00
public function index ( Request $request , $audit = null )
2017-01-11 18:14:06 -08:00
{
2018-01-11 15:17:34 -08:00
2022-01-15 12:01:19 -08:00
$filter_non_deprecable_assets = false ;
2021-09-01 17:33:39 -07:00
/**
* This looks MAD janky ( and it is ), but the AssetsController @ index does a LOT of heavy lifting throughout the
* app . This bit here just makes sure that someone without permission to view assets doesn ' t
* end up with priv escalations because they asked for a different endpoint .
*
* Since we never gave the specification for which transformer to use before , it should default
* gracefully to just use the AssetTransformer by default , which shouldn ' t break anything .
*
* It was either this mess , or repeating ALL of the searching and sorting and filtering code ,
* which would have been far worse of a mess . * sad face * - snipe ( Sept 1 , 2021 )
*/
if ( Route :: currentRouteName () == 'api.depreciation-report.index' ) {
2022-01-15 12:01:19 -08:00
$filter_non_deprecable_assets = true ;
2021-09-01 17:33:39 -07:00
$transformer = 'App\Http\Transformers\DepreciationReportTransformer' ;
$this -> authorize ( 'reports.view' );
} else {
$transformer = 'App\Http\Transformers\AssetsTransformer' ;
$this -> authorize ( 'index' , Asset :: class );
}
2018-01-17 19:18:48 -08:00
$settings = Setting :: getSettings ();
2017-01-13 11:41:00 -08:00
$allowed_columns = [
'id' ,
'name' ,
'asset_tag' ,
'serial' ,
'model_number' ,
'last_checkout' ,
'notes' ,
'expected_checkin' ,
'order_number' ,
'image' ,
'assigned_to' ,
'created_at' ,
2017-02-23 16:23:02 -08:00
'updated_at' ,
2017-01-13 11:41:00 -08:00
'purchase_date' ,
2017-10-31 07:05:15 -07:00
'purchase_cost' ,
2017-12-12 03:03:43 -08:00
'last_audit_date' ,
'next_audit_date' ,
2017-10-31 07:05:15 -07:00
'warranty_months' ,
2018-05-16 19:20:43 -07:00
'checkout_counter' ,
'checkin_counter' ,
'requests_counter' ,
2023-01-18 13:03:31 -08:00
'byod' ,
2023-01-22 00:56:44 -08:00
'asset_eol_date' ,
2017-01-13 11:41:00 -08:00
];
2021-06-10 13:15:52 -07:00
$filter = [];
2018-01-11 15:17:34 -08:00
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'filter' )) {
2018-04-25 20:25:03 -07:00
$filter = json_decode ( $request -> input ( 'filter' ), true );
2017-03-11 04:26:01 -08:00
}
2017-01-13 11:41:00 -08:00
$all_custom_fields = CustomField :: all (); //used as a 'cache' of custom fields throughout this page load
foreach ( $all_custom_fields as $field ) {
2021-06-10 13:15:52 -07:00
$allowed_columns [] = $field -> db_column_name ();
2017-01-13 11:41:00 -08:00
}
2023-06-21 16:29:44 -07:00
$assets = Asset :: select ( 'assets.*' )
2021-12-10 18:50:34 -08:00
-> with ( 'location' , 'assetstatus' , 'company' , 'defaultLoc' , 'assignedTo' ,
2021-12-10 18:42:56 -08:00
'model.category' , 'model.manufacturer' , 'model.fieldset' , 'supplier' ); //it might be tempting to add 'assetlog' here, but don't. It blows up update-heavy users.
2017-10-18 10:07:35 -07:00
2022-01-28 09:08:48 -08:00
if ( $filter_non_deprecable_assets ) {
2022-01-15 12:01:19 -08:00
$non_deprecable_models = AssetModel :: select ( 'id' ) -> whereNotNull ( 'depreciation_id' ) -> get ();
$assets -> InModelList ( $non_deprecable_models -> toArray ());
}
2023-06-29 05:35:41 -07:00
2018-01-11 15:17:34 -08:00
// These are used by the API to query against specific ID numbers.
// They are also used by the individual searches on detail pages like
// locations, etc.
2022-01-28 09:08:48 -08:00
// Search custom fields by column name
foreach ( $all_custom_fields as $field ) {
if ( $request -> filled ( $field -> db_column_name ())) {
$assets -> where ( $field -> db_column_name (), '=' , $request -> input ( $field -> db_column_name ()));
}
}
2023-06-29 05:35:41 -07:00
if (( ! is_null ( $filter )) && ( count ( $filter )) > 0 ) {
$assets -> ByFilter ( $filter );
} elseif ( $request -> filled ( 'search' )) {
$assets -> TextSearch ( $request -> input ( 'search' ));
}
2017-01-11 18:14:06 -08:00
2019-05-05 19:32:52 -07:00
// This is used by the audit reporting routes
if ( Gate :: allows ( 'audit' , Asset :: class )) {
switch ( $audit ) {
case 'due' :
$assets -> DueOrOverdueForAudit ( $settings );
break ;
case 'overdue' :
$assets -> overdueForAudit ( $settings );
break ;
}
}
2017-05-15 20:55:39 -07:00
// This is used by the sidenav, mostly
2017-10-17 12:48:18 -07:00
// We switched from using query scopes here because of a Laravel bug
// related to fulltext searches on complex queries.
// I am sad. :(
2017-02-08 03:37:44 -08:00
switch ( $request -> input ( 'status' )) {
2017-01-11 18:14:06 -08:00
case 'Deleted' :
2019-01-24 14:47:44 -08:00
$assets -> onlyTrashed ();
2017-01-11 18:14:06 -08:00
break ;
case 'Pending' :
2021-06-10 13:15:52 -07:00
$assets -> join ( 'status_labels AS status_alias' , function ( $join ) {
$join -> on ( 'status_alias.id' , '=' , 'assets.status_id' )
-> where ( 'status_alias.deployable' , '=' , 0 )
-> where ( 'status_alias.pending' , '=' , 1 )
2017-10-18 01:21:08 -07:00
-> where ( 'status_alias.archived' , '=' , 0 );
2017-10-17 12:48:18 -07:00
});
2017-01-11 18:14:06 -08:00
break ;
case 'RTD' :
2017-10-18 01:21:08 -07:00
$assets -> whereNull ( 'assets.assigned_to' )
2021-06-10 13:15:52 -07:00
-> join ( 'status_labels AS status_alias' , function ( $join ) {
$join -> on ( 'status_alias.id' , '=' , 'assets.status_id' )
-> where ( 'status_alias.deployable' , '=' , 1 )
-> where ( 'status_alias.pending' , '=' , 0 )
2019-02-13 04:45:21 -08:00
-> where ( 'status_alias.archived' , '=' , 0 );
});
2017-01-11 18:14:06 -08:00
break ;
case 'Undeployable' :
$assets -> Undeployable ();
break ;
case 'Archived' :
2021-06-10 13:15:52 -07:00
$assets -> join ( 'status_labels AS status_alias' , function ( $join ) {
$join -> on ( 'status_alias.id' , '=' , 'assets.status_id' )
-> where ( 'status_alias.deployable' , '=' , 0 )
-> where ( 'status_alias.pending' , '=' , 0 )
2017-10-18 01:21:08 -07:00
-> where ( 'status_alias.archived' , '=' , 1 );
2017-10-17 12:48:18 -07:00
});
2017-01-11 18:14:06 -08:00
break ;
case 'Requestable' :
2017-10-17 12:48:18 -07:00
$assets -> where ( 'assets.requestable' , '=' , 1 )
2021-06-10 13:15:52 -07:00
-> join ( 'status_labels AS status_alias' , function ( $join ) {
$join -> on ( 'status_alias.id' , '=' , 'assets.status_id' )
-> where ( 'status_alias.deployable' , '=' , 1 )
-> where ( 'status_alias.pending' , '=' , 0 )
2019-02-13 04:45:21 -08:00
-> where ( 'status_alias.archived' , '=' , 0 );
});
2017-10-17 12:48:18 -07:00
2017-01-11 18:14:06 -08:00
break ;
case 'Deployed' :
2017-10-17 12:48:18 -07:00
// more sad, horrible workarounds for laravel bugs when doing full text searches
2023-06-29 05:35:41 -07:00
$assets -> whereNotNull ( 'assets.assigned_to' );
2017-01-11 18:14:06 -08:00
break ;
2023-01-18 13:25:46 -08:00
case 'byod' :
// This is kind of redundant, since we already check for byod=1 above, but this keeps the
// sidebar nav links a little less chaotic
$assets -> where ( 'assets.byod' , '=' , '1' );
break ;
2017-10-17 11:20:05 -07:00
default :
2018-01-17 19:18:48 -08:00
2021-06-10 13:15:52 -07:00
if (( ! $request -> filled ( 'status_id' )) && ( $settings -> show_archived_in_list != '1' )) {
2018-01-17 19:18:48 -08:00
// terrible workaround for complex-query Laravel bug in fulltext
2021-06-10 13:15:52 -07:00
$assets -> join ( 'status_labels AS status_alias' , function ( $join ) {
$join -> on ( 'status_alias.id' , '=' , 'assets.status_id' )
2018-01-17 19:18:48 -08:00
-> where ( 'status_alias.archived' , '=' , 0 );
});
2019-02-13 04:45:21 -08:00
// If there is a status ID, don't take show_archived_in_list into consideration
2018-02-01 14:31:32 -08:00
} else {
2021-06-10 13:15:52 -07:00
$assets -> join ( 'status_labels AS status_alias' , function ( $join ) {
$join -> on ( 'status_alias.id' , '=' , 'assets.status_id' );
2018-02-01 14:31:32 -08:00
});
2018-01-17 19:18:48 -08:00
}
2017-01-11 18:14:06 -08:00
}
2018-04-25 20:25:03 -07:00
2023-06-21 01:26:54 -07:00
// Leave these under the TextSearch scope, else the fuzziness will override the specific ID (status ID, etc) requested
2023-06-21 01:24:17 -07:00
if ( $request -> filled ( 'status_id' )) {
$assets -> where ( 'assets.status_id' , '=' , $request -> input ( 'status_id' ));
}
if ( $request -> filled ( 'asset_tag' )) {
$assets -> where ( 'assets.asset_tag' , '=' , $request -> input ( 'asset_tag' ));
}
if ( $request -> filled ( 'serial' )) {
$assets -> where ( 'assets.serial' , '=' , $request -> input ( 'serial' ));
}
if ( $request -> input ( 'requestable' ) == 'true' ) {
$assets -> where ( 'assets.requestable' , '=' , '1' );
}
if ( $request -> filled ( 'model_id' )) {
$assets -> InModelList ([ $request -> input ( 'model_id' )]);
}
if ( $request -> filled ( 'category_id' )) {
$assets -> InCategory ( $request -> input ( 'category_id' ));
}
if ( $request -> filled ( 'location_id' )) {
$assets -> where ( 'assets.location_id' , '=' , $request -> input ( 'location_id' ));
}
if ( $request -> filled ( 'rtd_location_id' )) {
$assets -> where ( 'assets.rtd_location_id' , '=' , $request -> input ( 'rtd_location_id' ));
}
if ( $request -> filled ( 'supplier_id' )) {
$assets -> where ( 'assets.supplier_id' , '=' , $request -> input ( 'supplier_id' ));
}
if ( $request -> filled ( 'asset_eol_date' )) {
$assets -> where ( 'assets.asset_eol_date' , '=' , $request -> input ( 'asset_eol_date' ));
}
if (( $request -> filled ( 'assigned_to' )) && ( $request -> filled ( 'assigned_type' ))) {
$assets -> where ( 'assets.assigned_to' , '=' , $request -> input ( 'assigned_to' ))
-> where ( 'assets.assigned_type' , '=' , $request -> input ( 'assigned_type' ));
}
if ( $request -> filled ( 'company_id' )) {
$assets -> where ( 'assets.company_id' , '=' , $request -> input ( 'company_id' ));
}
if ( $request -> filled ( 'manufacturer_id' )) {
$assets -> ByManufacturer ( $request -> input ( 'manufacturer_id' ));
}
if ( $request -> filled ( 'depreciation_id' )) {
$assets -> ByDepreciationId ( $request -> input ( 'depreciation_id' ));
}
if ( $request -> filled ( 'byod' )) {
$assets -> where ( 'assets.byod' , '=' , $request -> input ( 'byod' ));
}
2017-01-11 18:14:06 -08:00
2023-06-22 13:16:24 -07:00
if ( $request -> filled ( 'order_number' )) {
2023-10-18 06:36:16 -07:00
$assets -> where ( 'assets.order_number' , '=' , strval ( $request -> get ( 'order_number' )));
2023-06-22 13:16:24 -07:00
}
2017-01-11 18:14:06 -08:00
2017-10-18 09:27:34 -07:00
// This is kinda gross, but we need to do this because the Bootstrap Tables
// API passes custom field ordering as custom_fields.fieldname, and we have to strip
// that out to let the default sorter below order them correctly on the assets table.
2021-06-10 13:15:52 -07:00
$sort_override = str_replace ( 'custom_fields.' , '' , $request -> input ( 'sort' ));
2017-10-18 09:27:34 -07:00
// This handles all of the pivot sorting (versus the assets.* fields
// in the allowed_columns array)
$column_sort = in_array ( $sort_override , $allowed_columns ) ? $sort_override : 'assets.created_at' ;
2019-02-13 04:45:21 -08:00
2023-06-29 05:35:41 -07:00
$order = $request -> input ( 'order' ) === 'asc' ? 'asc' : 'desc' ;
2017-10-18 09:27:34 -07:00
switch ( $sort_override ) {
2017-01-11 18:14:06 -08:00
case 'model' :
2017-01-13 11:41:00 -08:00
$assets -> OrderModels ( $order );
2017-01-11 18:14:06 -08:00
break ;
case 'model_number' :
2017-01-13 11:41:00 -08:00
$assets -> OrderModelNumber ( $order );
2017-01-11 18:14:06 -08:00
break ;
case 'category' :
2017-01-13 11:41:00 -08:00
$assets -> OrderCategory ( $order );
2017-01-11 18:14:06 -08:00
break ;
case 'manufacturer' :
2017-01-13 11:41:00 -08:00
$assets -> OrderManufacturer ( $order );
2017-01-11 18:14:06 -08:00
break ;
2017-01-13 11:41:00 -08:00
case 'company' :
$assets -> OrderCompany ( $order );
2017-01-11 18:14:06 -08:00
break ;
case 'location' :
2017-01-13 11:41:00 -08:00
$assets -> OrderLocation ( $order );
2018-01-24 14:27:12 -08:00
case 'rtd_location' :
$assets -> OrderRtdLocation ( $order );
2017-01-11 18:14:06 -08:00
break ;
case 'status_label' :
2017-01-13 11:41:00 -08:00
$assets -> OrderStatus ( $order );
2017-01-11 18:14:06 -08:00
break ;
2017-05-15 20:55:39 -07:00
case 'supplier' :
$assets -> OrderSupplier ( $order );
break ;
2017-01-11 18:14:06 -08:00
case 'assigned_to' :
2017-01-13 11:41:00 -08:00
$assets -> OrderAssigned ( $order );
2017-01-11 18:14:06 -08:00
break ;
default :
2017-05-15 20:55:39 -07:00
$assets -> orderBy ( $column_sort , $order );
2017-01-11 18:14:06 -08:00
break ;
}
2018-01-10 05:44:11 -08:00
2019-02-13 04:44:19 -08:00
2023-06-29 05:35:41 -07:00
// Make sure the offset and limit are actually integers and do not exceed system limits
2023-10-14 12:39:52 -07:00
$offset = ( $request -> input ( 'offset' ) > $assets -> count ()) ? $assets -> count () : app ( 'api_offset_value' );
2023-06-29 05:35:41 -07:00
$limit = app ( 'api_limit_value' );
2017-01-13 11:41:00 -08:00
$total = $assets -> count ();
$assets = $assets -> skip ( $offset ) -> take ( $limit ) -> get ();
2021-09-23 17:23:53 -07:00
2021-10-20 17:26:41 -07:00
2021-09-23 17:23:53 -07:00
/**
* Include additional associated relationships
*/
if ( $request -> input ( 'components' )) {
2021-09-23 17:31:19 -07:00
$assets -> loadMissing ([ 'components' => function ( $query ) {
2021-09-23 17:23:53 -07:00
$query -> orderBy ( 'created_at' , 'desc' );
}]);
}
2021-09-01 17:33:39 -07:00
/**
* Here we ' re just determining which Transformer ( via $transformer ) to use based on the
* variables we set earlier on in this method - we default to AssetsTransformer .
*/
2021-09-23 17:23:53 -07:00
return ( new $transformer ) -> transformAssets ( $assets , $total , $request );
2017-01-11 18:14:06 -08:00
}
2018-03-23 14:50:11 -07:00
/**
* Returns JSON with information about an asset ( by tag ) for detail view .
*
* @ param string $tag
* @ since [ v4 . 2.1 ]
2022-11-15 09:33:56 -08:00
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ return \Illuminate\Http\JsonResponse
2018-03-23 14:50:11 -07:00
*/
2021-09-23 17:23:53 -07:00
public function showByTag ( Request $request , $tag )
2018-03-23 14:50:11 -07:00
{
2022-11-15 09:18:09 -08:00
$this -> authorize ( 'index' , Asset :: class );
2022-11-15 09:33:56 -08:00
$assets = Asset :: where ( 'asset_tag' , $tag ) -> with ( 'assetstatus' ) -> with ( 'assignedTo' );
2018-03-23 14:50:11 -07:00
2022-11-15 09:33:56 -08:00
// Check if they've passed ?deleted=true
if ( $request -> input ( 'deleted' , 'false' ) == 'true' ) {
2022-11-15 09:18:09 -08:00
$assets = $assets -> withTrashed ();
}
2023-01-09 20:23:05 -08:00
if (( $assets = $assets -> get ()) && ( $assets -> count ()) > 0 ) {
2023-01-09 20:40:12 -08:00
// If there is exactly one result and the deleted parameter is not passed, we should pull the first (and only)
// asset from the returned collection, since transformAsset() expects an Asset object, NOT a collection
if (( $assets -> count () == 1 ) && ( $request -> input ( 'deleted' ) != 'true' )) {
2023-01-09 20:23:05 -08:00
return ( new AssetsTransformer ) -> transformAsset ( $assets -> first ());
2022-11-15 09:18:09 -08:00
2023-01-09 20:40:12 -08:00
// If there is more than one result OR if the endpoint is requesting deleted items (even if there is only one
// match, return the normal collection transformed.
} else {
2023-01-09 20:23:05 -08:00
return ( new AssetsTransformer ) -> transformAssets ( $assets , $assets -> count ());
}
2023-01-09 19:54:21 -08:00
2022-11-15 09:18:09 -08:00
}
2023-01-09 20:43:57 -08:00
// If there are 0 results, return the "no such asset" response
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'admin/hardware/message.does_not_exist' )), 200 );
2018-03-23 14:50:11 -07:00
}
/**
* Returns JSON with information about an asset ( by serial ) for detail view .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param string $serial
* @ since [ v4 . 2.1 ]
2022-11-15 09:47:21 -08:00
* @ return \Illuminate\Http\JsonResponse
2018-03-23 14:50:11 -07:00
*/
2021-09-23 17:23:53 -07:00
public function showBySerial ( Request $request , $serial )
2018-03-23 14:50:11 -07:00
{
2019-02-13 04:46:19 -08:00
$this -> authorize ( 'index' , Asset :: class );
2022-11-15 09:33:56 -08:00
$assets = Asset :: where ( 'serial' , $serial ) -> with ( 'assetstatus' ) -> with ( 'assignedTo' );
2018-03-23 14:50:11 -07:00
2022-11-15 09:33:56 -08:00
// Check if they've passed ?deleted=true
if ( $request -> input ( 'deleted' , 'false' ) == 'true' ) {
2021-09-28 04:53:59 -07:00
$assets = $assets -> withTrashed ();
2022-11-15 09:18:09 -08:00
}
2023-01-09 21:05:51 -08:00
2023-01-09 20:23:05 -08:00
if (( $assets = $assets -> get ()) && ( $assets -> count ()) > 0 ) {
2023-01-09 21:05:51 -08:00
return ( new AssetsTransformer ) -> transformAssets ( $assets , $assets -> count ());
2021-09-28 04:53:59 -07:00
}
2023-01-09 19:57:47 -08:00
// If there are 0 results, return the "no such asset" response
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'admin/hardware/message.does_not_exist' )), 200 );
2018-03-23 14:50:11 -07:00
}
2019-02-13 04:45:21 -08:00
/**
2017-01-11 19:00:34 -08:00
* Returns JSON with information about an asset for detail view .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
2017-01-11 23:40:56 -08:00
* @ since [ v4 . 0 ]
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2017-01-11 19:00:34 -08:00
*/
2021-09-23 17:23:53 -07:00
public function show ( Request $request , $id )
2017-01-11 19:00:34 -08:00
{
2018-07-24 22:40:05 -07:00
if ( $asset = Asset :: with ( 'assetstatus' ) -> with ( 'assignedTo' ) -> withTrashed ()
-> withCount ( 'checkins as checkins_count' , 'checkouts as checkouts_count' , 'userRequests as user_requests_count' ) -> findOrFail ( $id )) {
2017-01-11 19:00:34 -08:00
$this -> authorize ( 'view' , $asset );
2021-06-10 13:15:52 -07:00
2021-09-23 17:23:53 -07:00
return ( new AssetsTransformer ) -> transformAsset ( $asset , $request -> input ( 'components' ) );
2017-01-11 19:00:34 -08:00
}
2018-03-23 14:50:11 -07:00
2017-01-11 19:00:34 -08:00
}
2021-06-10 13:15:52 -07:00
2021-09-23 17:23:53 -07:00
public function licenses ( Request $request , $id )
2019-01-22 14:47:40 -08:00
{
$this -> authorize ( 'view' , Asset :: class );
$this -> authorize ( 'view' , License :: class );
2023-03-29 18:11:28 -07:00
$asset = Asset :: where ( 'id' , $id ) -> withTrashed () -> firstorfail ();
2019-01-22 14:47:40 -08:00
$licenses = $asset -> licenses () -> get ();
2019-02-13 04:44:19 -08:00
2019-01-22 14:47:40 -08:00
return ( new LicensesTransformer ()) -> transformLicenses ( $licenses , $licenses -> count ());
}
2019-02-13 04:44:19 -08:00
2017-10-26 21:50:01 -07:00
2017-10-26 02:28:17 -07:00
/**
2017-10-26 21:50:01 -07:00
* Gets a paginated collection for the select2 menus
2017-10-26 02:28:17 -07:00
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
2017-10-26 21:50:01 -07:00
* @ since [ v4 . 0.16 ]
* @ see \App\Http\Transformers\SelectlistTransformer
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2017-10-26 02:28:17 -07:00
*/
public function selectlist ( Request $request )
{
2023-06-21 17:21:36 -07:00
$assets = Asset :: select ([
2017-10-26 02:28:17 -07:00
'assets.id' ,
'assets.name' ,
'assets.asset_tag' ,
'assets.model_id' ,
2017-11-16 16:49:16 -08:00
'assets.assigned_to' ,
'assets.assigned_type' ,
2021-06-10 13:15:52 -07:00
'assets.status_id' ,
2023-06-21 17:21:36 -07:00
]) -> with ( 'model' , 'assetstatus' , 'assignedTo' ) -> NotArchived ();
2017-10-26 02:28:17 -07:00
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'assetStatusType' ) && $request -> input ( 'assetStatusType' ) === 'RTD' ) {
2018-04-25 02:39:23 -07:00
$assets = $assets -> RTD ();
}
2017-10-26 02:28:17 -07:00
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'search' )) {
2017-11-16 16:49:16 -08:00
$assets = $assets -> AssignedSearch ( $request -> input ( 'search' ));
2017-10-26 02:28:17 -07:00
}
2017-11-16 16:49:16 -08:00
2017-10-26 02:28:17 -07:00
$assets = $assets -> paginate ( 50 );
2017-10-26 21:50:01 -07:00
// Loop through and set some custom properties for the transformer to use.
// This lets us have more flexibility in special cases like assets, where
// they may not have a ->name value but we want to display something anyway
2017-10-26 02:28:17 -07:00
foreach ( $assets as $asset ) {
2017-11-16 16:49:16 -08:00
2017-10-26 21:50:01 -07:00
$asset -> use_text = $asset -> present () -> fullName ;
2017-11-16 16:49:16 -08:00
2017-11-22 15:07:34 -08:00
if (( $asset -> checkedOutToUser ()) && ( $asset -> assigned )) {
2017-11-16 16:49:16 -08:00
$asset -> use_text .= ' → ' . $asset -> assigned -> getFullNameAttribute ();
}
2019-02-13 04:45:21 -08:00
2021-06-10 13:15:52 -07:00
if ( $asset -> assetstatus -> getStatuslabelType () == 'pending' ) {
$asset -> use_text .= '(' . $asset -> assetstatus -> getStatuslabelType () . ')' ;
2017-11-03 11:33:36 -07:00
}
2017-10-26 21:50:01 -07:00
$asset -> use_image = ( $asset -> getImageUrl ()) ? $asset -> getImageUrl () : null ;
2017-10-26 02:28:17 -07:00
}
2017-10-26 03:43:28 -07:00
2017-10-26 21:50:01 -07:00
return ( new SelectlistTransformer ) -> transformSelectlist ( $assets );
2017-10-26 02:28:17 -07:00
}
2017-01-11 19:00:34 -08:00
2017-01-11 23:40:56 -08:00
/**
* Accepts a POST request to create a new asset
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
2021-06-29 02:26:24 -07:00
* @ param \App\Http\Requests\ImageUploadRequest $request
2017-01-11 23:40:56 -08:00
* @ since [ v4 . 0 ]
*/
2023-11-28 19:46:03 -08:00
public function store ( StoreAssetRequest $request ) : JsonResponse
2017-01-11 23:40:56 -08:00
{
$asset = new Asset ();
2017-03-14 08:37:39 -07:00
$asset -> model () -> associate ( AssetModel :: find (( int ) $request -> get ( 'model_id' )));
2017-01-11 23:40:56 -08:00
2023-11-28 13:17:46 -08:00
$asset -> fill ( $request -> validated ());
2023-11-28 19:46:03 -08:00
$asset -> user_id = Auth :: id ();
2023-11-29 11:15:41 -08:00
// @todo: verify eol is working as expected - move to request? or do I want this at all?
// might just remove this and then use the method from here https://github.com/snipe/snipe-it/pull/13846
2023-11-29 13:04:52 -08:00
$asset -> asset_eol_date = $request -> validated ()[ 'asset_eol_date' ] ? ? $asset -> present () -> eol_date (); // so this isn't really necessary, because it's happening on the observer - but that might change?
2023-11-30 09:53:26 -08:00
$asset -> archived = false ;
2023-11-29 11:15:41 -08:00
$asset -> physical = '1' ;
$asset -> depreciate = '0' ;
2023-04-25 23:39:32 -07:00
2021-07-14 03:09:50 -07:00
/**
* this is here just legacy reasons . Api\AssetController
* used image_source once to allow encoded image uploads .
*/
if ( $request -> has ( 'image_source' )) {
$request -> offsetSet ( 'image' , $request -> offsetGet ( 'image_source' ));
}
2021-06-29 02:26:24 -07:00
$asset = $request -> handleImages ( $asset );
2017-01-11 23:40:56 -08:00
// Update custom fields in the database.
// Validation for these fields is handled through the AssetRequest form request
$model = AssetModel :: find ( $request -> get ( 'model_id' ));
2023-05-01 15:05:03 -07:00
2019-05-24 15:44:54 -07:00
if (( $model ) && ( $model -> fieldset )) {
2017-01-11 23:40:56 -08:00
foreach ( $model -> fieldset -> fields as $field ) {
2021-04-05 20:16:06 -07:00
// Set the field value based on what was sent in the request
2022-06-27 14:17:07 -07:00
$field_val = $request -> input ( $field -> db_column , null );
2021-04-05 20:16:06 -07:00
// If input value is null, use custom field's default value
if ( $field_val == null ) {
2023-11-28 13:17:46 -08:00
Log :: debug ( 'Field value for ' . $field -> db_column . ' is null' );
2021-04-05 20:16:06 -07:00
$field_val = $field -> defaultValue ( $request -> get ( 'model_id' ));
2023-11-28 13:17:46 -08:00
Log :: debug ( 'Use the default fieldset value of ' . $field -> defaultValue ( $request -> get ( 'model_id' )));
2021-04-05 20:16:06 -07:00
}
// if the field is set to encrypted, make sure we encrypt the value
if ( $field -> field_encrypted == '1' ) {
2023-11-28 13:17:46 -08:00
Log :: debug ( 'This model field is encrypted in this fieldset.' );
2021-04-05 20:16:06 -07:00
2019-05-15 19:33:30 -07:00
if ( Gate :: allows ( 'admin' )) {
2021-04-05 20:16:06 -07:00
// If input value is null, use custom field's default value
2021-06-10 13:15:52 -07:00
if (( $field_val == null ) && ( $request -> has ( 'model_id' ) != '' )) {
2023-11-28 13:17:46 -08:00
$field_val = Crypt :: encrypt ( $field -> defaultValue ( $request -> get ( 'model_id' )));
2021-04-05 20:16:06 -07:00
} else {
2023-11-28 13:17:46 -08:00
$field_val = Crypt :: encrypt ( $request -> input ( $field -> db_column ));
2021-04-05 20:16:06 -07:00
}
2019-05-15 19:33:30 -07:00
}
}
2021-04-05 20:16:06 -07:00
2022-06-27 14:17:07 -07:00
$asset -> { $field -> db_column } = $field_val ;
2017-01-11 23:40:56 -08:00
}
}
if ( $asset -> save ()) {
2017-03-14 08:37:39 -07:00
if ( $request -> get ( 'assigned_user' )) {
2017-01-11 23:40:56 -08:00
$target = User :: find ( request ( 'assigned_user' ));
2017-03-14 08:37:39 -07:00
} elseif ( $request -> get ( 'assigned_asset' )) {
2017-01-11 23:40:56 -08:00
$target = Asset :: find ( request ( 'assigned_asset' ));
2017-03-14 08:37:39 -07:00
} elseif ( $request -> get ( 'assigned_location' )) {
2017-01-11 23:40:56 -08:00
$target = Location :: find ( request ( 'assigned_location' ));
}
2017-01-12 02:19:55 -08:00
if ( isset ( $target )) {
2017-01-11 23:40:56 -08:00
$asset -> checkOut ( $target , Auth :: user (), date ( 'Y-m-d H:i:s' ), '' , 'Checked out on asset creation' , e ( $request -> get ( 'name' )));
}
2019-03-13 21:00:40 -07:00
if ( $asset -> image ) {
$asset -> image = $asset -> getImageUrl ();
}
2017-08-01 20:01:11 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , $asset , trans ( 'admin/hardware/message.create.success' )));
2017-01-11 23:40:56 -08:00
}
2017-08-25 03:26:50 -07:00
2017-03-14 08:37:39 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , $asset -> getErrors ()), 200 );
2017-01-11 23:40:56 -08:00
}
2017-01-12 03:48:18 -08:00
/**
* Accepts a POST request to update an asset
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
2021-06-29 02:26:24 -07:00
* @ param \App\Http\Requests\ImageUploadRequest $request
2017-01-12 03:48:18 -08:00
* @ since [ v4 . 0 ]
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2017-01-12 03:48:18 -08:00
*/
2021-06-29 02:26:24 -07:00
public function update ( ImageUploadRequest $request , $id )
2017-01-12 03:48:18 -08:00
{
2018-07-12 18:28:02 -07:00
$this -> authorize ( 'update' , Asset :: class );
2017-01-12 03:48:18 -08:00
2019-02-13 04:44:19 -08:00
if ( $asset = Asset :: find ( $id )) {
$asset -> fill ( $request -> all ());
2019-02-12 22:08:38 -08:00
2019-05-23 17:39:50 -07:00
( $request -> filled ( 'model_id' )) ?
2019-02-12 22:08:38 -08:00
$asset -> model () -> associate ( AssetModel :: find ( $request -> get ( 'model_id' ))) : null ;
2018-07-24 22:51:31 -07:00
( $request -> filled ( 'rtd_location_id' )) ?
2017-11-03 19:42:45 -07:00
$asset -> location_id = $request -> get ( 'rtd_location_id' ) : '' ;
2019-05-23 17:39:50 -07:00
( $request -> filled ( 'company_id' )) ?
2017-01-12 03:48:18 -08:00
$asset -> company_id = Company :: getIdForCurrentUser ( $request -> get ( 'company_id' )) : '' ;
2019-05-23 17:39:50 -07:00
( $request -> filled ( 'rtd_location_id' )) ?
2019-02-13 04:44:19 -08:00
$asset -> location_id = $request -> get ( 'rtd_location_id' ) : null ;
2018-01-17 13:12:32 -08:00
2021-07-06 23:33:48 -07:00
/**
* this is here just legacy reasons . Api\AssetController
* used image_source once to allow encoded image uploads .
*/
if ( $request -> has ( 'image_source' )) {
$request -> offsetSet ( 'image' , $request -> offsetGet ( 'image_source' ));
}
2023-11-03 07:06:27 -07:00
$asset = $request -> handleImages ( $asset );
$model = AssetModel :: find ( $asset -> model_id );
2021-06-29 02:26:24 -07:00
2018-01-17 13:12:32 -08:00
// Update custom fields
2023-11-03 07:06:27 -07:00
if (( $model ) && ( isset ( $model -> fieldset ))) {
2018-01-17 13:12:32 -08:00
foreach ( $model -> fieldset -> fields as $field ) {
2022-06-27 14:17:07 -07:00
if ( $request -> has ( $field -> db_column )) {
2021-06-10 13:15:52 -07:00
if ( $field -> field_encrypted == '1' ) {
2019-05-20 11:49:18 -07:00
if ( Gate :: allows ( 'admin' )) {
2022-06-27 14:17:07 -07:00
$asset -> { $field -> db_column } = \Crypt :: encrypt ( $request -> input ( $field -> db_column ));
2019-05-20 11:49:18 -07:00
}
} else {
2022-06-27 14:17:07 -07:00
$asset -> { $field -> db_column } = $request -> input ( $field -> db_column );
2019-05-15 19:33:30 -07:00
}
2017-01-12 03:48:18 -08:00
}
}
}
2018-01-17 13:12:32 -08:00
2017-01-12 03:48:18 -08:00
if ( $asset -> save ()) {
2019-05-23 17:39:50 -07:00
if (( $request -> filled ( 'assigned_user' )) && ( $target = User :: find ( $request -> get ( 'assigned_user' )))) {
2018-01-17 13:12:32 -08:00
$location = $target -> location_id ;
2019-05-23 17:39:50 -07:00
} elseif (( $request -> filled ( 'assigned_asset' )) && ( $target = Asset :: find ( $request -> get ( 'assigned_asset' )))) {
2019-02-13 04:45:21 -08:00
$location = $target -> location_id ;
2019-12-04 16:19:25 -08:00
2021-06-10 13:16:56 -07:00
Asset :: where ( 'assigned_type' , \App\Models\Asset :: class ) -> where ( 'assigned_to' , $id )
2019-12-04 16:19:25 -08:00
-> update ([ 'location_id' => $target -> location_id ]);
2019-05-23 17:39:50 -07:00
} elseif (( $request -> filled ( 'assigned_location' )) && ( $target = Location :: find ( $request -> get ( 'assigned_location' )))) {
2017-11-03 19:42:45 -07:00
$location = $target -> id ;
2017-01-12 03:48:18 -08:00
}
if ( isset ( $target )) {
2017-11-03 19:42:45 -07:00
$asset -> checkOut ( $target , Auth :: user (), date ( 'Y-m-d H:i:s' ), '' , 'Checked out on asset update' , e ( $request -> get ( 'name' )), $location );
2017-01-12 03:48:18 -08:00
}
2019-03-13 21:00:40 -07:00
if ( $asset -> image ) {
$asset -> image = $asset -> getImageUrl ();
}
2017-03-14 08:37:39 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , $asset , trans ( 'admin/hardware/message.update.success' )));
2017-01-12 03:48:18 -08:00
}
2021-06-10 13:15:52 -07:00
2017-03-14 08:37:39 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , $asset -> getErrors ()), 200 );
2017-01-12 03:48:18 -08:00
}
2021-06-10 13:15:52 -07:00
2017-03-14 08:37:39 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'admin/hardware/message.does_not_exist' )), 200 );
2017-01-12 03:48:18 -08:00
}
2017-01-11 23:40:56 -08:00
/**
* Delete a given asset ( mark as deleted ) .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v4 . 0 ]
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2017-01-11 23:40:56 -08:00
*/
public function destroy ( $id )
{
2017-08-03 19:49:41 -07:00
$this -> authorize ( 'delete' , Asset :: class );
2017-01-11 23:40:56 -08:00
if ( $asset = Asset :: find ( $id )) {
$this -> authorize ( 'delete' , $asset );
DB :: table ( 'assets' )
-> where ( 'id' , $asset -> id )
2021-06-10 13:15:52 -07:00
-> update ([ 'assigned_to' => null ]);
2017-01-11 23:40:56 -08:00
$asset -> delete ();
2017-03-14 08:37:39 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , null , trans ( 'admin/hardware/message.delete.success' )));
2017-01-11 23:40:56 -08:00
}
2017-03-14 08:37:39 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'admin/hardware/message.does_not_exist' )), 200 );
2021-08-14 14:06:15 -07:00
}
/**
2021-08-14 14:07:04 -07:00
* Restore a soft - deleted asset .
2021-08-14 14:06:15 -07:00
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v5 . 1.18 ]
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2021-08-14 14:06:15 -07:00
*/
2022-06-09 17:23:50 -07:00
public function restore ( Request $request , $assetId = null )
2021-08-14 14:06:15 -07:00
{
2022-06-09 17:23:50 -07:00
2023-11-22 10:04:24 -08:00
if ( $asset = Asset :: withTrashed () -> find ( $assetId )) {
$this -> authorize ( 'delete' , $asset );
2022-06-09 17:23:50 -07:00
2023-11-22 10:04:24 -08:00
if ( $asset -> deleted_at == '' ) {
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , trans ( 'general.not_deleted' , [ 'item_type' => trans ( 'general.asset' )])), 200 );
}
2022-06-09 17:23:50 -07:00
2023-11-22 10:04:24 -08:00
if ( $asset -> restore ()) {
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , trans ( 'admin/hardware/message.restore.success' )), 200 );
}
2021-08-14 14:06:15 -07:00
2023-11-22 10:04:24 -08:00
// Check validation to make sure we're not restoring an asset with the same asset tag (or unique attribute) as an existing asset
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , trans ( 'general.could_not_restore' , [ 'item_type' => trans ( 'general.asset' ), 'error' => $asset -> getErrors () -> first ()])), 200 );
2021-08-14 14:06:15 -07:00
}
2023-11-22 10:04:24 -08:00
2021-08-14 14:06:15 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , trans ( 'admin/hardware/message.does_not_exist' )), 200 );
2023-11-22 10:04:24 -08:00
2017-01-11 23:40:56 -08:00
}
2022-06-28 23:11:57 -07:00
/**
* Checkout an asset by its tag .
*
* @ author [ N . Butler ]
* @ param string $tag
* @ since [ v6 . 0.5 ]
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2022-06-28 23:11:57 -07:00
*/
public function checkoutByTag ( AssetCheckoutRequest $request , $tag )
{
2022-06-29 00:49:50 -07:00
if ( $asset = Asset :: where ( 'asset_tag' , $tag ) -> first ()) {
2022-06-28 23:11:57 -07:00
return $this -> checkout ( $request , $asset -> id );
}
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , 'Asset not found' ), 200 );
}
2017-03-11 14:04:52 -08:00
/**
* Checkout an asset
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v4 . 0 ]
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2017-03-11 14:04:52 -08:00
*/
2017-11-27 21:17:16 -08:00
public function checkout ( AssetCheckoutRequest $request , $asset_id )
2017-03-14 08:37:39 -07:00
{
2017-03-11 14:04:52 -08:00
$this -> authorize ( 'checkout' , Asset :: class );
$asset = Asset :: findOrFail ( $asset_id );
2021-06-10 13:15:52 -07:00
if ( ! $asset -> availableForCheckout ()) {
2017-03-11 14:04:52 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , [ 'asset' => e ( $asset -> asset_tag )], trans ( 'admin/hardware/message.checkout.not_available' )));
}
$this -> authorize ( 'checkout' , $asset );
2017-10-19 15:51:55 -07:00
$error_payload = [];
$error_payload [ 'asset' ] = [
'id' => $asset -> id ,
'asset_tag' => $asset -> asset_tag ,
];
2017-11-27 21:17:58 -08:00
// This item is checked out to a location
2021-06-10 13:15:52 -07:00
if ( request ( 'checkout_to_type' ) == 'location' ) {
2017-11-27 21:17:58 -08:00
$target = Location :: find ( request ( 'assigned_location' ));
$asset -> location_id = ( $target ) ? $target -> id : '' ;
$error_payload [ 'target_id' ] = $request -> input ( 'assigned_location' );
$error_payload [ 'target_type' ] = 'location' ;
2021-06-10 13:15:52 -07:00
} elseif ( request ( 'checkout_to_type' ) == 'asset' ) {
$target = Asset :: where ( 'id' , '!=' , $asset_id ) -> find ( request ( 'assigned_asset' ));
2017-11-27 21:17:58 -08:00
// Override with the asset's location_id if it has one
2020-04-06 15:54:40 -07:00
$asset -> location_id = (( $target ) && ( isset ( $target -> location_id ))) ? $target -> location_id : '' ;
2017-11-27 21:17:58 -08:00
$error_payload [ 'target_id' ] = $request -> input ( 'assigned_asset' );
$error_payload [ 'target_type' ] = 'asset' ;
2021-06-10 13:15:52 -07:00
} elseif ( request ( 'checkout_to_type' ) == 'user' ) {
2017-11-27 21:17:58 -08:00
// Fetch the target and set the asset's new location_id
$target = User :: find ( request ( 'assigned_user' ));
2020-04-06 15:54:40 -07:00
$asset -> location_id = (( $target ) && ( isset ( $target -> location_id ))) ? $target -> location_id : '' ;
2017-11-27 21:17:58 -08:00
$error_payload [ 'target_id' ] = $request -> input ( 'assigned_user' );
$error_payload [ 'target_type' ] = 'user' ;
2017-03-11 14:04:52 -08:00
}
2022-04-15 05:06:35 -07:00
if ( $request -> filled ( 'status_id' )) {
$asset -> status_id = $request -> get ( 'status_id' );
}
2017-11-27 21:17:58 -08:00
2021-06-10 13:15:52 -07:00
if ( ! isset ( $target )) {
2017-11-27 21:17:58 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , $error_payload , 'Checkout target for asset ' . e ( $asset -> asset_tag ) . ' is invalid - ' . $error_payload [ 'target_type' ] . ' does not exist.' ));
2017-03-11 14:04:52 -08:00
}
2017-10-28 07:38:36 -07:00
2021-06-10 13:15:52 -07:00
$checkout_at = request ( 'checkout_at' , date ( 'Y-m-d H:i:s' ));
2017-03-11 14:04:52 -08:00
$expected_checkin = request ( 'expected_checkin' , null );
$note = request ( 'note' , null );
2023-02-14 12:19:16 -08:00
// Using `->has` preserves the asset name if the name parameter was not included in request.
2023-02-14 12:08:20 -08:00
$asset_name = request () -> has ( 'name' ) ? request ( 'name' ) : $asset -> name ;
2019-02-13 04:45:21 -08:00
2017-10-28 07:38:36 -07:00
// Set the location ID to the RTD location id if there is one
2020-04-09 14:17:39 -07:00
// Wait, why are we doing this? This overrides the stuff we set further up, which makes no sense.
// TODO: Follow up here. WTF. Commented out for now.
2017-11-27 21:17:58 -08:00
2020-04-09 14:17:39 -07:00
// if ((isset($target->rtd_location_id)) && ($asset->rtd_location_id!='')) {
// $asset->location_id = $target->rtd_location_id;
// }
2017-10-28 07:38:36 -07:00
2017-03-11 14:04:52 -08:00
2017-11-27 21:17:58 -08:00
if ( $asset -> checkOut ( $target , Auth :: user (), $checkout_at , $expected_checkin , $note , $asset_name , $asset -> location_id )) {
2017-03-11 14:04:52 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , [ 'asset' => e ( $asset -> asset_tag )], trans ( 'admin/hardware/message.checkout.success' )));
}
2019-08-22 21:36:47 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , [ 'asset' => e ( $asset -> asset_tag )], trans ( 'admin/hardware/message.checkout.error' )));
2017-03-11 14:04:52 -08:00
}
/**
* Checkin an asset
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v4 . 0 ]
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2017-03-11 14:04:52 -08:00
*/
2017-11-03 19:42:45 -07:00
public function checkin ( Request $request , $asset_id )
2017-03-14 08:37:39 -07:00
{
2017-03-11 14:04:52 -08:00
$this -> authorize ( 'checkin' , Asset :: class );
$asset = Asset :: findOrFail ( $asset_id );
$this -> authorize ( 'checkin' , $asset );
2021-12-19 15:39:57 -08:00
$target = $asset -> assignedTo ;
if ( is_null ( $target )) {
2017-03-11 14:04:52 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , [ 'asset' => e ( $asset -> asset_tag )], trans ( 'admin/hardware/message.checkin.already_checked_in' )));
}
$asset -> expected_checkin = null ;
$asset -> last_checkout = null ;
2023-08-21 11:57:33 -07:00
$asset -> last_checkin = now ();
2017-03-11 14:04:52 -08:00
$asset -> assigned_to = null ;
$asset -> assignedTo () -> disassociate ( $asset );
$asset -> accepted = null ;
2019-07-26 12:37:38 -07:00
2022-06-21 10:57:39 -07:00
if ( $request -> has ( 'name' )) {
2019-07-26 12:37:38 -07:00
$asset -> name = $request -> input ( 'name' );
}
2021-12-19 13:53:31 -08:00
2021-06-10 13:15:52 -07:00
$asset -> location_id = $asset -> rtd_location_id ;
2017-11-03 19:42:45 -07:00
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'location_id' )) {
2021-06-10 13:15:52 -07:00
$asset -> location_id = $request -> input ( 'location_id' );
2017-11-03 19:42:45 -07:00
}
2019-12-11 11:09:54 -08:00
if ( $request -> has ( 'status_id' )) {
2021-06-10 13:15:52 -07:00
$asset -> status_id = $request -> input ( 'status_id' );
2017-03-11 14:04:52 -08:00
}
2022-07-21 21:45:25 -07:00
$checkin_at = $request -> filled ( 'checkin_at' ) ? $request -> input ( 'checkin_at' ) . ' ' . date ( 'H:i:s' ) : date ( 'Y-m-d H:i:s' );
2023-08-31 20:12:07 -07:00
$originalValues = $asset -> getRawOriginal ();
2017-03-11 14:04:52 -08:00
2023-08-31 20:12:07 -07:00
if (( $request -> filled ( 'checkin_at' )) && ( $request -> get ( 'checkin_at' ) != date ( 'Y-m-d' ))) {
$originalValues [ 'action_date' ] = $checkin_at ;
}
2022-02-04 22:52:30 -08:00
2017-03-11 14:04:52 -08:00
if ( $asset -> save ()) {
2023-08-31 20:12:07 -07:00
event ( new CheckoutableCheckedIn ( $asset , $target , Auth :: user (), $request -> input ( 'note' ), $checkin_at , $originalValues ));
2021-06-08 11:16:13 -07:00
2017-03-11 14:04:52 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , [ 'asset' => e ( $asset -> asset_tag )], trans ( 'admin/hardware/message.checkin.success' )));
}
2021-12-19 15:39:57 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , [ 'asset' => e ( $asset -> asset_tag )], trans ( 'admin/hardware/message.checkin.error' )));
2017-03-11 14:04:52 -08:00
}
2017-08-25 10:04:19 -07:00
2021-12-19 13:53:31 -08:00
/**
2021-12-19 16:11:24 -08:00
* Checkin an asset by asset tag
2021-12-19 13:53:31 -08:00
*
2021-12-19 15:39:57 -08:00
* @ author [ A . Janes ] [ < ajanes @ adagiohealth . org > ]
2021-12-19 16:11:24 -08:00
* @ since [ v6 . 0 ]
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2021-12-19 13:53:31 -08:00
*/
2022-06-29 16:23:52 -07:00
public function checkinByTag ( Request $request , $tag = null )
2021-12-19 13:53:31 -08:00
{
$this -> authorize ( 'checkin' , Asset :: class );
2022-06-29 16:23:52 -07:00
if ( null == $tag && null !== ( $request -> input ( 'asset_tag' ))) {
$tag = $request -> input ( 'asset_tag' );
}
$asset = Asset :: where ( 'asset_tag' , $tag ) -> first ();
2021-12-19 13:53:31 -08:00
2022-07-21 21:45:25 -07:00
if ( $asset ) {
2021-12-19 15:39:57 -08:00
return $this -> checkin ( $request , $asset -> id );
2021-12-19 13:53:31 -08:00
}
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , [
2022-06-29 16:23:52 -07:00
'asset' => e ( $tag )
], 'Asset with tag ' . e ( $tag ) . ' not found' ));
2017-03-11 14:04:52 -08:00
}
2017-08-25 10:04:19 -07:00
/**
* Mark an asset as audited
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $id
* @ since [ v4 . 0 ]
2023-10-25 08:40:49 -07:00
* @ return \Illuminate\Http\JsonResponse
2017-08-25 10:04:19 -07:00
*/
2021-06-10 13:15:52 -07:00
public function audit ( Request $request )
2017-08-25 10:04:19 -07:00
2021-06-10 13:15:52 -07:00
{
2017-08-29 16:00:22 -07:00
$this -> authorize ( 'audit' , Asset :: class );
2021-06-10 13:15:52 -07:00
$rules = [
2017-08-29 16:00:22 -07:00
'asset_tag' => 'required' ,
2017-08-25 18:40:20 -07:00
'location_id' => 'exists:locations,id|nullable|numeric' ,
2021-06-10 13:15:52 -07:00
'next_audit_date' => 'date|nullable' ,
];
2017-08-25 10:04:19 -07:00
2017-08-29 16:00:22 -07:00
$validator = Validator :: make ( $request -> all (), $rules );
2017-08-25 18:40:20 -07:00
if ( $validator -> fails ()) {
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , $validator -> errors () -> all ()));
}
2017-08-25 10:04:19 -07:00
2018-08-01 03:04:29 -07:00
$settings = Setting :: getSettings ();
$dt = Carbon :: now () -> addMonths ( $settings -> audit_interval ) -> toDateString ();
2021-06-10 13:15:52 -07:00
$asset = Asset :: where ( 'asset_tag' , '=' , $request -> input ( 'asset_tag' )) -> first ();
2017-08-25 10:04:19 -07:00
2017-08-29 16:00:22 -07:00
if ( $asset ) {
2017-12-12 03:03:43 -08:00
// We don't want to log this as a normal update, so let's bypass that
$asset -> unsetEventDispatcher ();
2018-08-01 03:04:29 -07:00
$asset -> next_audit_date = $dt ;
if ( $request -> filled ( 'next_audit_date' )) {
$asset -> next_audit_date = $request -> input ( 'next_audit_date' );
}
2018-09-07 05:39:41 -07:00
// Check to see if they checked the box to update the physical location,
// not just note it in the audit notes
2021-06-10 13:15:52 -07:00
if ( $request -> input ( 'update_location' ) == '1' ) {
2018-09-07 05:39:41 -07:00
$asset -> location_id = $request -> input ( 'location_id' );
}
2021-01-26 12:22:59 -08:00
$asset -> last_audit_date = date ( 'Y-m-d H:i:s' );
2017-12-12 03:03:43 -08:00
2017-08-29 16:00:22 -07:00
if ( $asset -> save ()) {
2021-06-10 13:15:52 -07:00
$log = $asset -> logAudit ( request ( 'note' ), request ( 'location_id' ));
2017-08-31 21:30:38 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , [
'asset_tag' => e ( $asset -> asset_tag ),
'note' => e ( $request -> input ( 'note' )),
2021-06-10 13:15:52 -07:00
'next_audit_date' => Helper :: getFormattedDateObject ( $asset -> next_audit_date ),
2017-08-31 21:30:38 -07:00
], trans ( 'admin/hardware/message.audit.success' )));
2017-08-29 16:00:22 -07:00
}
2017-08-25 10:04:19 -07:00
}
2021-11-09 19:39:32 -08:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , [ 'asset_tag' => e ( $request -> input ( 'asset_tag' ))], 'Asset with tag ' . e ( $request -> input ( 'asset_tag' )) . ' not found' ));
2018-04-04 17:33:02 -07:00
}
/**
* Returns JSON listing of all requestable assets
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v4 . 0 ]
2023-10-25 09:26:51 -07:00
* @ return \Illuminate\Http\JsonResponse
2018-04-04 17:33:02 -07:00
*/
public function requestable ( Request $request )
{
2018-07-12 18:28:02 -07:00
$this -> authorize ( 'viewRequestable' , Asset :: class );
2023-10-25 09:26:51 -07:00
$allowed_columns = [
'name' ,
'asset_tag' ,
'serial' ,
'model_number' ,
'image' ,
'purchase_cost' ,
'expected_checkin' ,
];
$all_custom_fields = CustomField :: all (); //used as a 'cache' of custom fields throughout this page load
foreach ( $all_custom_fields as $field ) {
$allowed_columns [] = $field -> db_column_name ();
}
2023-06-22 12:36:43 -07:00
$assets = Asset :: select ( 'assets.*' )
2023-10-25 09:26:51 -07:00
-> with ( 'location' , 'assetstatus' , 'assetlog' , 'company' , 'assignedTo' ,
'model.category' , 'model.manufacturer' , 'model.fieldset' , 'supplier' , 'requests' )
2023-06-22 12:36:43 -07:00
-> requestableAssets ();
2018-04-04 17:33:02 -07:00
2023-10-25 09:26:51 -07:00
2022-01-13 16:32:40 -08:00
if ( $request -> filled ( 'search' )) {
$assets -> TextSearch ( $request -> input ( 'search' ));
}
2018-04-04 17:33:02 -07:00
2023-10-25 09:26:51 -07:00
// Search custom fields by column name
foreach ( $all_custom_fields as $field ) {
if ( $request -> filled ( $field -> db_column_name ())) {
$assets -> where ( $field -> db_column_name (), '=' , $request -> input ( $field -> db_column_name ()));
}
}
$order = $request -> input ( 'order' ) === 'asc' ? 'asc' : 'desc' ;
$sort_override = str_replace ( 'custom_fields.' , '' , $request -> input ( 'sort' ));
// This handles all the pivot sorting (versus the assets.* fields
// in the allowed_columns array)
$column_sort = in_array ( $sort_override , $allowed_columns ) ? $sort_override : 'assets.created_at' ;
2018-04-04 17:33:02 -07:00
switch ( $request -> input ( 'sort' )) {
case 'model' :
$assets -> OrderModels ( $order );
break ;
case 'model_number' :
$assets -> OrderModelNumber ( $order );
break ;
2023-10-25 09:26:51 -07:00
case 'location' :
$assets -> OrderLocation ( $order );
2023-10-26 01:21:27 -07:00
break ;
2018-04-04 17:33:02 -07:00
default :
2023-10-25 09:26:51 -07:00
$assets -> orderBy ( $column_sort , $order );
2018-04-04 17:33:02 -07:00
break ;
}
2023-10-25 09:26:51 -07:00
// Make sure the offset and limit are actually integers and do not exceed system limits
$offset = ( $request -> input ( 'offset' ) > $assets -> count ()) ? $assets -> count () : app ( 'api_offset_value' );
$limit = app ( 'api_limit_value' );
2018-04-04 17:33:02 -07:00
$total = $assets -> count ();
$assets = $assets -> skip ( $offset ) -> take ( $limit ) -> get ();
2021-06-10 13:15:52 -07:00
2018-04-04 17:33:02 -07:00
return ( new AssetsTransformer ) -> transformRequestedAssets ( $assets , $total );
2017-08-25 10:04:19 -07:00
}
2017-01-11 18:14:06 -08:00
}