2017-01-11 18:14:06 -08:00
< ? php
namespace App\Http\Controllers\Api ;
use App\Helpers\Helper ;
2017-01-25 21:29:23 -08:00
use App\Http\Controllers\Controller ;
2017-01-11 18:14:06 -08:00
use App\Http\Requests\AssetRequest ;
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 ;
2017-01-11 18:14:06 -08:00
use App\Models\Asset ;
use App\Models\AssetModel ;
use App\Models\Company ;
use App\Models\CustomField ;
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 ;
2017-01-25 21:29:23 -08:00
use Artisan ;
use Auth ;
use Carbon\Carbon ;
use Config ;
use DB ;
use Gate ;
2017-01-11 18:14:06 -08:00
use Illuminate\Http\Request ;
2017-01-25 21:29:23 -08:00
use Input ;
use Lang ;
use Log ;
use Mail ;
use Paginator ;
use Response ;
use Slack ;
use Str ;
use TCPDF ;
use Validator ;
use View ;
2017-10-26 21:50:01 -07:00
use App\Http\Transformers\SelectlistTransformer ;
2017-01-11 18:14:06 -08:00
2017-08-25 10:04:19 -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 ]
* @ return JsonResponse
*/
2017-02-08 03:37:44 -08:00
public function index ( Request $request )
2017-01-11 18:14:06 -08:00
{
2018-01-11 15:17:34 -08:00
2017-01-13 11:41:00 -08:00
$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' ,
2017-01-13 11:41:00 -08:00
];
2017-03-11 04:26:01 -08:00
$filter = array ();
2018-01-11 15:17:34 -08:00
2017-03-11 04:26:01 -08:00
if ( $request -> has ( 'filter' )) {
$filter = json_decode ( $request -> input ( 'filter' ));
}
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 ) {
$allowed_columns [] = $field -> db_column_name ();
}
2018-01-11 15:17:34 -08:00
$assets = Company :: scopeCompanyables ( Asset :: select ( 'assets.*' ), " company_id " , " assets " )
-> with ( 'location' , 'assetstatus' , 'assetlog' , 'company' , 'defaultLoc' , 'assignedTo' ,
2017-09-05 17:54:58 -07:00
'model.category' , 'model.manufacturer' , 'model.fieldset' , 'supplier' );
2017-10-18 10:07:35 -07:00
2017-01-11 19:00:34 -08:00
2017-10-18 10:07:35 -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.
2017-02-03 20:34:09 -08:00
if ( $request -> has ( 'status_id' )) {
2017-10-20 17:37:46 -07:00
$assets -> where ( 'assets.status_id' , '=' , $request -> input ( 'status_id' ));
2017-02-03 20:34:09 -08:00
}
2017-02-03 19:34:24 -08:00
if ( $request -> has ( 'model_id' )) {
$assets -> InModelList ([ $request -> input ( 'model_id' )]);
}
if ( $request -> has ( 'category_id' )) {
$assets -> InCategory ( $request -> input ( 'category_id' ));
}
2017-02-08 03:31:42 -08:00
if ( $request -> has ( 'location_id' )) {
2017-11-03 20:10:36 -07:00
$assets -> where ( 'assets.location_id' , '=' , $request -> input ( 'location_id' ));
2018-01-11 15:17:34 -08:00
// dd($assets->toSql());
2017-02-08 03:31:42 -08:00
}
2017-05-15 20:55:39 -07:00
if ( $request -> has ( 'supplier_id' )) {
$assets -> where ( 'assets.supplier_id' , '=' , $request -> input ( 'supplier_id' ));
}
2018-01-10 05:44:11 -08:00
if (( $request -> has ( 'assigned_to' )) && ( $request -> has ( 'assigned_type' ))) {
$assets -> where ( 'assets.assigned_to' , '=' , $request -> input ( 'assigned_to' ))
-> where ( 'assets.assigned_type' , '=' , $request -> input ( 'assigned_type' ));
}
2017-02-08 03:31:42 -08:00
if ( $request -> has ( 'company_id' )) {
2017-03-14 08:37:39 -07:00
$assets -> where ( 'assets.company_id' , '=' , $request -> input ( 'company_id' ));
2017-02-08 03:31:42 -08:00
}
2017-02-03 19:52:00 -08:00
if ( $request -> has ( 'manufacturer_id' )) {
$assets -> ByManufacturer ( $request -> input ( 'manufacturer_id' ));
}
2017-10-17 11:20:05 -07:00
if ( $request -> has ( 'depreciation_id' )) {
$assets -> ByDepreciationId ( $request -> input ( 'depreciation_id' ));
}
2017-10-11 12:29:08 -07:00
$request -> has ( 'order_number' ) ? $assets = $assets -> where ( 'assets.order_number' , '=' , e ( $request -> get ( 'order_number' ))) : '' ;
2017-01-11 18:14:06 -08:00
2017-01-13 11:41:00 -08:00
$offset = request ( 'offset' , 0 );
$limit = $request -> input ( 'limit' , 50 );
$order = $request -> input ( 'order' ) === 'asc' ? 'asc' : 'desc' ;
2017-01-11 18:14:06 -08:00
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' :
$assets -> withTrashed () -> Deleted ();
break ;
case 'Pending' :
2017-10-18 01:21:08 -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 )
-> 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' )
-> 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 )
-> where ( 'status_alias.archived' , '=' , 0 );
2017-10-17 12:48:18 -07:00
});
2017-01-11 18:14:06 -08:00
break ;
case 'Undeployable' :
$assets -> Undeployable ();
break ;
case 'Archived' :
2017-10-18 01:21:08 -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 )
-> 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 )
2017-10-18 01:21:08 -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 )
-> 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
$assets -> where ( 'assets.assigned_to' , '>' , '0' );
2017-01-11 18:14:06 -08:00
break ;
2017-10-17 11:20:05 -07:00
default :
2018-01-17 19:18:48 -08:00
2018-02-01 14:31:32 -08:00
if (( ! $request -> has ( '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
$assets -> join ( 'status_labels AS status_alias' , function ( $join ) {
$join -> on ( 'status_alias.id' , " = " , " assets.status_id " )
-> where ( 'status_alias.archived' , '=' , 0 );
});
2018-02-01 14:31:32 -08:00
// If there is a status ID, don't take show_archived_in_list into consideration
} else {
$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
}
2017-01-11 18:14:06 -08:00
}
2018-01-11 15:17:34 -08:00
if ( count ( $filter ) > 0 ) {
$assets -> ByFilter ( $filter );
} elseif ( $request -> has ( 'search' )) {
$assets -> TextSearch ( $request -> input ( 'search' ));
}
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.
$sort_override = str_replace ( 'custom_fields.' , '' , $request -> input ( 'sort' )) ;
// 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' ;
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
2017-01-13 11:41:00 -08:00
$total = $assets -> count ();
$assets = $assets -> skip ( $offset ) -> take ( $limit ) -> get ();
return ( new AssetsTransformer ) -> transformAssets ( $assets , $total );
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 .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param string $tag
* @ since [ v4 . 2.1 ]
* @ return JsonResponse
*/
public function showByTag ( $tag )
{
if ( $asset = Asset :: with ( 'assetstatus' ) -> with ( 'assignedTo' ) -> withTrashed () -> where ( 'asset_tag' , $tag ) -> first ()) {
$this -> authorize ( 'view' , $asset );
return ( new AssetsTransformer ) -> transformAsset ( $asset );
}
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , 'Asset not found' ), 404 );
}
/**
* 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 ]
* @ return JsonResponse
*/
public function showBySerial ( $serial )
{
if ( $assets = Asset :: with ( 'assetstatus' ) -> with ( 'assignedTo' )
-> withTrashed () -> where ( 'serial' , $serial ) -> get ()) {
$this -> authorize ( 'view' , $assets );
return ( new AssetsTransformer ) -> transformAssets ( $assets , $assets -> count ());
}
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , 'Asset not found' ), 404 );
}
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 ]
* @ return JsonResponse
2017-01-11 19:00:34 -08:00
*/
2017-01-12 02:19:55 -08:00
public function show ( $id )
2017-01-11 19:00:34 -08:00
{
2017-10-28 15:17:36 -07:00
if ( $asset = Asset :: with ( 'assetstatus' ) -> with ( 'assignedTo' ) -> withTrashed () -> findOrFail ( $id )) {
2017-01-11 19:00:34 -08:00
$this -> authorize ( 'view' , $asset );
2017-01-24 21:04:38 -08:00
return ( new AssetsTransformer ) -> transformAsset ( $asset );
2017-01-11 19:00:34 -08:00
}
2018-03-23 14:50:11 -07:00
2017-01-11 19:00:34 -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
2017-10-26 02:28:17 -07:00
*
*/
public function selectlist ( Request $request )
{
$assets = Company :: scopeCompanyables ( Asset :: select ([
'assets.id' ,
'assets.name' ,
'assets.asset_tag' ,
'assets.model_id' ,
2017-11-16 16:49:16 -08:00
'assets.assigned_to' ,
'assets.assigned_type' ,
2017-11-03 11:33:36 -07:00
'assets.status_id'
2017-11-16 16:49:16 -08:00
]) -> with ( 'model' , 'assetstatus' , 'assignedTo' ) -> NotArchived ());
2017-10-26 02:28:17 -07:00
if ( $request -> has ( '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 ();
}
2017-11-03 11:33:36 -07:00
if ( $asset -> assetstatus -> getStatuslabelType () == 'pending' ) {
2017-11-16 16:49:16 -08:00
$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 > ]
2017-01-12 02:19:55 -08:00
* @ param Request $request
2017-01-11 23:40:56 -08:00
* @ since [ v4 . 0 ]
2017-01-12 02:19:55 -08:00
* @ return JsonResponse
2017-01-11 23:40:56 -08:00
*/
public function store ( AssetRequest $request )
{
2017-08-25 03:26:50 -07:00
$this -> authorize ( 'create' , Asset :: class );
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
$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' );
2018-01-03 17:24:32 -08:00
$asset -> asset_tag = $request -> get ( 'asset_tag' , Asset :: autoincrement_asset ());
2017-01-11 23:40:56 -08:00
$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
$model = AssetModel :: find ( $request -> get ( 'model_id' ));
if ( $model -> fieldset ) {
foreach ( $model -> fieldset -> fields as $field ) {
2017-10-31 05:22:21 -07:00
$asset -> { $field -> convertUnicodeDbSlug ()} = e ( $request -> input ( $field -> convertUnicodeDbSlug (), null ));
2017-01-11 23:40:56 -08:00
}
}
if ( $asset -> save ()) {
2017-10-31 05:38:52 -07:00
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' )));
}
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 > ]
* @ param Request $request
* @ since [ v4 . 0 ]
* @ return JsonResponse
*/
public function update ( Request $request , $id )
{
2018-03-05 21:27:17 -08:00
$this -> authorize ( 'edit' , Asset :: class );
2017-01-12 03:48:18 -08:00
if ( $asset = Asset :: find ( $id )) {
( $request -> has ( 'model_id' )) ?
$asset -> model () -> associate ( AssetModel :: find ( $request -> get ( 'model_id' ))) : '' ;
2017-03-31 13:48:11 -07:00
( $request -> has ( 'name' )) ?
$asset -> name = $request -> get ( 'name' ) : '' ;
( $request -> has ( 'serial' )) ?
$asset -> serial = $request -> get ( 'serial' ) : '' ;
( $request -> has ( 'model_id' )) ?
$asset -> model_id = $request -> get ( 'model_id' ) : '' ;
( $request -> has ( 'order_number' )) ?
$asset -> order_number = $request -> get ( 'order_number' ) : '' ;
( $request -> has ( 'notes' )) ?
$asset -> notes = $request -> get ( 'notes' ) : '' ;
( $request -> has ( 'asset_tag' )) ?
$asset -> asset_tag = $request -> get ( 'asset_tag' ) : '' ;
( $request -> has ( 'archived' )) ?
$asset -> archived = $request -> get ( 'archived' ) : '' ;
( $request -> has ( 'status_id' )) ?
$asset -> status_id = $request -> get ( 'status_id' ) : '' ;
( $request -> has ( 'warranty_months' )) ?
$asset -> warranty_months = $request -> get ( 'warranty_months' ) : '' ;
2017-01-12 03:48:18 -08:00
( $request -> has ( 'purchase_cost' )) ?
$asset -> purchase_cost = Helper :: ParseFloat ( $request -> get ( 'purchase_cost' )) : '' ;
2017-03-31 13:48:11 -07:00
( $request -> has ( 'purchase_date' )) ?
$asset -> purchase_date = $request -> get ( 'purchase_date' ) : '' ;
( $request -> has ( 'assigned_to' )) ?
$asset -> assigned_to = $request -> get ( 'assigned_to' ) : '' ;
( $request -> has ( 'supplier_id' )) ?
$asset -> supplier_id = $request -> get ( 'supplier_id' ) : '' ;
( $request -> has ( 'requestable' )) ?
$asset -> requestable = $request -> get ( 'requestable' ) : '' ;
( $request -> has ( 'rtd_location_id' )) ?
$asset -> rtd_location_id = $request -> get ( 'rtd_location_id' ) : '' ;
2017-11-03 19:42:45 -07:00
( $request -> has ( 'rtd_location_id' )) ?
$asset -> location_id = $request -> get ( 'rtd_location_id' ) : '' ;
2017-01-12 03:48:18 -08:00
( $request -> has ( 'company_id' )) ?
$asset -> company_id = Company :: getIdForCurrentUser ( $request -> get ( 'company_id' )) : '' ;
2018-01-17 13:12:32 -08:00
// Update custom fields
if (( $model = AssetModel :: find ( $asset -> model_id )) && ( isset ( $model -> fieldset ))) {
foreach ( $model -> fieldset -> fields as $field ) {
if ( $request -> has ( $field -> convertUnicodeDbSlug ())) {
$asset -> { $field -> convertUnicodeDbSlug ()} = e ( $request -> input ( $field -> convertUnicodeDbSlug ()));
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 ()) {
2017-06-15 20:54:14 -07:00
2018-01-17 13:12:32 -08:00
if (( $request -> has ( 'assigned_user' )) && ( $target = User :: find ( $request -> get ( 'assigned_user' )))) {
$location = $target -> location_id ;
} elseif (( $request -> has ( 'assigned_asset' )) && ( $target = Asset :: find ( $request -> get ( 'assigned_asset' )))) {
$location = $target -> location_id ;
} elseif (( $request -> has ( '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
}
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
}
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
}
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 ]
2017-01-12 02:19:55 -08:00
* @ return 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 )) {
2017-08-03 19:49:41 -07:00
2017-01-11 23:40:56 -08:00
$this -> authorize ( 'delete' , $asset );
DB :: table ( 'assets' )
-> where ( 'id' , $asset -> id )
-> update ( array ( 'assigned_to' => null ));
$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 );
2017-01-11 23:40:56 -08:00
}
2017-03-11 14:04:52 -08:00
/**
* Checkout an asset
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v4 . 0 ]
* @ return JsonResponse
*/
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 );
if ( ! $asset -> availableForCheckout ()) {
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
if ( request ( 'checkout_to_type' ) == 'location' ) {
$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' ;
} elseif ( request ( 'checkout_to_type' ) == 'asset' ) {
2018-02-12 19:13:33 -08:00
$target = Asset :: where ( 'id' , '!=' , $asset_id ) -> find ( request ( 'assigned_asset' ));
2017-11-27 21:17:58 -08:00
$asset -> location_id = $target -> rtd_location_id ;
// Override with the asset's location_id if it has one
if ( $target -> location_id != '' ) {
$asset -> location_id = ( $target ) ? $target -> location_id : '' ;
}
$error_payload [ 'target_id' ] = $request -> input ( 'assigned_asset' );
$error_payload [ 'target_type' ] = 'asset' ;
} elseif ( request ( 'checkout_to_type' ) == 'user' ) {
// Fetch the target and set the asset's new location_id
$target = User :: find ( request ( 'assigned_user' ));
$asset -> location_id = ( $target ) ? $target -> location_id : '' ;
$error_payload [ 'target_id' ] = $request -> input ( 'assigned_user' );
$error_payload [ 'target_type' ] = 'user' ;
2017-03-11 14:04:52 -08:00
}
2017-11-27 21:17:58 -08:00
2017-03-11 14:04:52 -08: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
2017-03-11 14:04:52 -08:00
$checkout_at = request ( 'checkout_at' , date ( " Y-m-d H:i:s " ));
$expected_checkin = request ( 'expected_checkin' , null );
$note = request ( 'note' , null );
$asset_name = request ( 'name' , null );
2017-10-28 07:38:36 -07:00
// Set the location ID to the RTD location id if there is one
if ( $asset -> rtd_location_id != '' ) {
$asset -> location_id = $target -> rtd_location_id ;
}
2017-11-27 21:17:58 -08:00
2017-11-03 19:42:45 -07:00
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' )));
}
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , [ 'asset' => e ( $asset -> asset_tag )], trans ( 'admin/hardware/message.checkout.error' ))) -> withErrors ( $asset -> getErrors ());
}
/**
* Checkin an asset
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v4 . 0 ]
* @ return JsonResponse
*/
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 );
$user = $asset -> assignedUser ;
if ( is_null ( $target = $asset -> assignedTo )) {
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 ;
$asset -> assigned_to = null ;
$asset -> assignedTo () -> disassociate ( $asset );
$asset -> accepted = null ;
2018-03-25 13:46:57 -07:00
$asset -> name = Input :: get ( 'name' );
2017-11-03 19:42:45 -07:00
$asset -> location_id = $asset -> rtd_location_id ;
if ( $request -> has ( 'location_id' )) {
$asset -> location_id = $request -> input ( 'location_id' );
}
2017-03-11 14:04:52 -08:00
if ( Input :: has ( 'status_id' )) {
2018-03-25 13:46:57 -07:00
$asset -> status_id = Input :: get ( 'status_id' );
2017-03-11 14:04:52 -08:00
}
if ( $asset -> save ()) {
2018-03-25 13:46:57 -07:00
$asset -> logCheckin ( $target , e ( request ( 'note' )));
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' )));
}
return response () -> json ( Helper :: formatStandardApiResponse ( 'success' , [ 'asset' => e ( $asset -> asset_tag )], trans ( 'admin/hardware/message.checkin.error' )));
}
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 ]
* @ return JsonResponse
*/
2017-08-29 16:00:22 -07:00
public function audit ( Request $request ) {
2017-08-25 10:04:19 -07:00
2017-08-29 16:00:22 -07:00
$this -> authorize ( 'audit' , Asset :: class );
2017-08-25 10:04:19 -07:00
$rules = array (
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' ,
'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
2017-08-29 16:00:22 -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 ();
2017-08-29 16:00:22 -07:00
$asset -> next_audit_date = $request -> input ( 'next_audit_date' );
2017-12-12 03:03:43 -08:00
$asset -> last_audit_date = date ( 'Y-m-d h:i:s' );
2017-08-29 16:00:22 -07:00
if ( $asset -> save ()) {
$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' )),
'next_audit_date' => Helper :: getFormattedDateObject ( $log -> calcNextAuditDate ())
], trans ( 'admin/hardware/message.audit.success' )));
2017-08-29 16:00:22 -07:00
}
2017-08-25 10:04:19 -07:00
}
2017-08-29 16:00:22 -07:00
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , [ 'asset_tag' => e ( $request -> input ( 'asset_tag' ))], 'Asset with tag ' . $request -> input ( 'asset_tag' ) . ' not found' ));
2017-08-25 18:40:20 -07:00
2017-08-25 10:04:19 -07:00
}
2017-01-11 18:14:06 -08:00
}