2016-03-25 01:18:05 -07:00
< ? php
namespace App\Http\Controllers ;
use App\Helpers\Helper ;
use App\Http\Requests\AssetCheckinRequest ;
use App\Http\Requests\AssetCheckoutRequest ;
2016-12-19 11:04:28 -08:00
use App\Http\Requests\AssetFileRequest ;
use App\Http\Requests\AssetRequest ;
2016-12-26 15:16:42 -08:00
use App\Http\Requests\ItemImportRequest ;
2016-03-25 01:18:05 -07:00
use App\Models\Actionlog ;
use App\Models\Asset ;
use App\Models\AssetMaintenance ;
use App\Models\AssetModel ;
use App\Models\Company ;
use App\Models\CustomField ;
use App\Models\Depreciation ;
use App\Models\Location ;
2016-12-19 11:04:28 -08:00
use App\Models\Manufacturer ;
2016-03-25 01:18:05 -07:00
use App\Models\Setting ;
use App\Models\Statuslabel ;
use App\Models\Supplier ;
use App\Models\User ;
use Artisan ;
use Auth ;
2016-12-19 11:04:28 -08:00
use Carbon\Carbon ;
2016-03-25 01:18:05 -07:00
use Config ;
use DB ;
2016-12-19 11:04:28 -08:00
use Gate ;
use Illuminate\Database\Eloquent\ModelNotFoundException ;
use Illuminate\Http\Request ;
2016-03-25 01:18:05 -07:00
use Image ;
use Input ;
use Lang ;
2016-12-19 11:04:28 -08:00
use League\Csv\Reader ;
2016-03-25 01:18:05 -07:00
use Log ;
use Mail ;
use Paginator ;
use Redirect ;
use Response ;
use Slack ;
use Str ;
use Symfony\Component\Console\Output\BufferedOutput ;
2016-12-19 22:00:50 -08:00
use Symfony\Component\HttpFoundation\File\Exception\FileException ;
2016-03-25 01:18:05 -07:00
use Symfony\Component\HttpFoundation\JsonResponse ;
use TCPDF ;
2016-12-19 11:04:28 -08:00
use Validator ;
2016-03-25 01:18:05 -07:00
use View ;
/**
2016-04-07 13:21:09 -07: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 > ]
2016-03-25 01:18:05 -07:00
*/
class AssetsController extends Controller
{
protected $qrCodeDimensions = array ( 'height' => 3.5 , 'width' => 3.5 );
protected $barCodeDimensions = array ( 'height' => 2 , 'width' => 22 );
public function __construct ()
{
$this -> middleware ( 'auth' );
parent :: __construct ();
}
/**
* Returns a view that invokes the ajax tables which actually contains
* the content for the assets listing , which is generated in getDatatable .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ see AssetController :: getDatatable () method that generates the JSON response
* @ since [ v1 . 0 ]
* @ return View
*/
2016-12-15 04:09:40 -08:00
public function index ()
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'index' , Asset :: class );
2016-03-25 01:18:05 -07:00
return View :: make ( 'hardware/index' );
}
2016-05-20 17:02:28 -07:00
/**
* Searches the assets table by asset tag , and redirects if it finds one
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 0 ]
* @ return Redirect
*/
2016-06-22 12:27:41 -07:00
public function getAssetByTag ()
{
2016-12-19 22:00:50 -08:00
$topsearch = ( Input :: get ( 'topsearch' ) == " true " );
2016-12-19 11:04:28 -08:00
if ( ! $asset = Asset :: where ( 'asset_tag' , '=' , Input :: get ( 'assetTag' )) -> first ()) {
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-05-20 17:02:28 -07:00
}
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'view' , $asset );
return redirect () -> route ( 'hardware.show' , $asset -> id ) -> with ( 'topsearch' , $topsearch );
2016-05-20 17:02:28 -07:00
}
2016-03-25 01:18:05 -07:00
/**
2016-12-19 22:00:50 -08:00
* Returns a view that presents a form to create a new asset .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
2016-12-29 08:10:52 -08:00
* @ param Request $request
2016-12-19 22:00:50 -08:00
* @ return View
2016-12-29 08:10:52 -08:00
* @ internal param int $model_id
2016-12-19 22:00:50 -08:00
*/
2016-12-22 17:08:42 -08:00
public function create ( Request $request )
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'create' , Asset :: class );
2016-12-29 08:10:52 -08:00
$view = View :: make ( 'hardware/edit' )
-> with ( 'supplier_list' , Helper :: suppliersList ())
-> with ( 'company_list' , Helper :: companyList ())
-> with ( 'model_list' , Helper :: modelList ())
-> with ( 'statuslabel_list' , Helper :: statusLabelList ())
-> with ( 'location_list' , Helper :: locationsList ())
-> with ( 'item' , new Asset )
-> with ( 'manufacturer' , Helper :: manufacturerList ())
-> with ( 'category' , Helper :: categoryList ( 'asset' ))
-> with ( 'statuslabel_types' , Helper :: statusTypeList ())
-> with ( 'users_list' , Helper :: usersList ())
-> with ( 'assets_list' , Helper :: assetsList ())
-> with ( 'locations_list' , Helper :: locationsList ());
2016-03-25 01:18:05 -07:00
2016-12-22 17:08:42 -08:00
if ( $request -> has ( 'model_id' )) {
$selected_model = AssetModel :: find ( $request -> input ( 'model_id' ));
2016-03-25 01:18:05 -07:00
$view -> with ( 'selected_model' , $selected_model );
2016-12-22 17:08:42 -08:00
} else {
2016-03-25 01:18:05 -07:00
}
return $view ;
}
/**
* Validate and process new asset form data .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ return Redirect
*/
2016-12-15 04:09:40 -08:00
public function store ( AssetRequest $request )
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( Asset :: class );
2016-03-25 01:18:05 -07:00
// create a new model instance
$asset = new Asset ();
$asset -> model () -> associate ( AssetModel :: find ( e ( Input :: get ( 'model_id' ))));
2016-12-19 22:00:50 -08:00
$asset -> name = Input :: get ( 'name' );
$asset -> serial = Input :: get ( 'serial' );
$asset -> company_id = Company :: getIdForCurrentUser ( Input :: get ( 'company_id' ));
$asset -> model_id = Input :: get ( 'model_id' );
$asset -> order_number = Input :: get ( 'order_number' );
$asset -> notes = Input :: get ( 'notes' );
$asset -> asset_tag = Input :: get ( 'asset_tag' );
$asset -> user_id = Auth :: id ();
2016-12-26 15:17:46 -08:00
$asset -> archived = '0' ;
$asset -> physical = '1' ;
$asset -> depreciate = '0' ;
2016-12-29 14:02:18 -08:00
$asset -> status_id = request ( 'status_id' , 0 );
2016-12-26 15:17:46 -08:00
$asset -> warranty_months = request ( 'warranty_months' , null );
$asset -> purchase_cost = Helper :: ParseFloat ( Input :: get ( 'purchase_cost' ));
$asset -> purchase_date = request ( 'purchase_date' , null );
$asset -> assigned_to = request ( 'assigned_to' , null );
$asset -> supplier_id = request ( 'supplier_id' , 0 );
$asset -> requestable = request ( 'requestable' , 0 );
$asset -> rtd_location_id = request ( 'rtd_location_id' , null );
2016-03-25 01:18:05 -07:00
// Create the image (if one was chosen.)
2016-05-20 17:23:34 -07:00
if ( Input :: has ( 'image' )) {
2016-07-10 18:43:00 -07:00
2016-05-20 17:23:34 -07:00
$image = Input :: get ( 'image' );
2016-07-10 18:43:00 -07:00
// After modification, the image is prefixed by mime info like the following:
// data:image/jpeg;base64,; This causes the image library to be unhappy, so we need to remove it.
2016-05-20 17:23:34 -07:00
$header = explode ( ';' , $image , 2 )[ 0 ];
2016-07-10 18:43:00 -07:00
// Grab the image type from the header while we're at it.
2016-06-22 12:27:41 -07:00
$extension = substr ( $header , strpos ( $header , '/' ) + 1 );
2016-07-10 18:43:00 -07:00
// Start reading the image after the first comma, postceding the base64.
2016-06-22 12:27:41 -07:00
$image = substr ( $image , strpos ( $image , ',' ) + 1 );
2016-05-20 17:23:34 -07:00
$file_name = str_random ( 25 ) . " . " . $extension ;
2016-07-10 18:43:00 -07:00
$directory = public_path ( 'uploads/assets/' );
// Check if the uploads directory exists. If not, try to create it.
if ( ! file_exists ( $directory )) {
2016-09-21 12:05:06 -07:00
mkdir ( $directory , 0755 , true );
2016-07-10 18:43:00 -07:00
}
$path = public_path ( 'uploads/assets/' . $file_name );
try {
Image :: make ( $image ) -> resize ( 500 , 500 , function ( $constraint ) {
$constraint -> aspectRatio ();
$constraint -> upsize ();
}) -> save ( $path );
$asset -> image = $file_name ;
2016-08-11 19:13:49 -07:00
} catch ( \Exception $e ) {
2016-07-10 18:43:00 -07:00
\Input :: flash ();
$messageBag = new \Illuminate\Support\MessageBag ();
$messageBag -> add ( 'image' , $e -> getMessage ());
\Session () -> flash ( 'errors' , \Session :: get ( 'errors' , new \Illuminate\Support\ViewErrorBag )
-> put ( 'default' , $messageBag ));
return response () -> json ([ 'image' => $e -> getMessage ()], 422 );
}
2016-03-25 01:18:05 -07:00
}
2016-06-10 16:36:46 -07:00
// 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' ));
2016-06-22 12:27:41 -07:00
if ( $model -> fieldset ) {
foreach ( $model -> fieldset -> fields as $field ) {
2016-12-19 22:00:50 -08:00
$asset -> { CustomField :: name_to_db_name ( $field -> name )} = e ( $request -> input ( CustomField :: name_to_db_name ( $field -> name )));
2016-06-10 16:36:46 -07:00
}
}
2016-03-25 01:18:05 -07:00
// Was the asset created?
if ( $asset -> save ()) {
2016-10-12 18:45:32 -07:00
$asset -> logCreate ();
2016-12-29 08:10:52 -08:00
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 ( Input :: get ( 'name' )));
2016-03-25 01:18:05 -07:00
}
// Redirect to the asset listing page
2016-05-20 17:23:34 -07:00
\Session :: flash ( 'success' , trans ( 'admin/hardware/message.create.success' ));
2016-12-15 15:15:11 -08:00
return response () -> json ([ 'redirect_url' => route ( 'hardware.index' )]);
2016-03-25 01:18:05 -07:00
}
2016-06-22 12:20:49 -07:00
\Input :: flash ();
2016-06-22 07:02:29 -07:00
\Session :: flash ( 'errors' , $asset -> getErrors ());
return response () -> json ([ 'errors' => $asset -> getErrors ()], 500 );
2016-03-25 01:18:05 -07:00
}
/**
* Returns a view that presents a form to edit an existing asset .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return View
*/
2016-12-15 04:09:40 -08:00
public function edit ( $assetId = null )
2016-03-25 01:18:05 -07:00
{
Partialize forms (#2884)
* Consolidate edit form elements into reusable partials.
This is a large code change that doesn't do much immediately. It
refactors all of the various edit.blade.php files to reference
standardized partials, so that they all reference the same base html
layout. This has the side effect of moving everything to the new fancy
"required" indicators, and making things look consistent.
In addition, I've gone ahead and renamed a few database fields. We had
Assetmodel::modelno and Consumable::model_no, I've renamed both to
model_number. We had items using ::note and ::notes, I've standardized
on ::notes. Component used total_qty where consumables and accessories
used qty, so I've moved everything to qty (And fixed a few bugs in the
helper file in the process.
TODO includes looking at how/where to place the modal javascripts to
allow for on the fly creation from all places, rather than just the
asset page.
Rename assetmodel::modelno to model_number for clarity and consistency
Rename consumable::model_no to model_number for clarity and consistency
Rename assetmodel::note to notes for clarity and consistency
Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying.
* Share a settings variable with all views.
* Allow editing the per_page setting. We showed the value, but we never showed it on the edit page..
* use snipeSettings in all views instead of the long ugly path.
* War on partials. Centralize all bootstrap table javascript
* Use model_number instead of modelno in importer
* Codacy fix.
* More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read.
* Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs
* Fix DB seeder.
* Base sql dump and csv's to import data from for tests.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name
* Use a .env.tests file for testing functional and unit to allow a separate database.
* Add functional tests for compoents, groups, and licenses.
* Now that the config is in the functional.yml, this just confuses things.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* update db dump
* Update tests to new reality
* env for the test setup
* only load the database at beginning of tests, not between each Functional test.
* Fix a miss from renaming note to notes.
* Set Termination date when creating an asset. It was only set on edit.
* Rename serial_number to serial in components for consistency.
* Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB.
* Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
if ( ! $item = Asset :: find ( $assetId )) {
2016-12-19 11:04:28 -08:00
// Redirect to the asset management page with error
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 11:04:28 -08:00
//Handles company checks and permissions.
$this -> authorize ( $item );
2016-03-25 01:18:05 -07:00
Partialize forms (#2884)
* Consolidate edit form elements into reusable partials.
This is a large code change that doesn't do much immediately. It
refactors all of the various edit.blade.php files to reference
standardized partials, so that they all reference the same base html
layout. This has the side effect of moving everything to the new fancy
"required" indicators, and making things look consistent.
In addition, I've gone ahead and renamed a few database fields. We had
Assetmodel::modelno and Consumable::model_no, I've renamed both to
model_number. We had items using ::note and ::notes, I've standardized
on ::notes. Component used total_qty where consumables and accessories
used qty, so I've moved everything to qty (And fixed a few bugs in the
helper file in the process.
TODO includes looking at how/where to place the modal javascripts to
allow for on the fly creation from all places, rather than just the
asset page.
Rename assetmodel::modelno to model_number for clarity and consistency
Rename consumable::model_no to model_number for clarity and consistency
Rename assetmodel::note to notes for clarity and consistency
Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying.
* Share a settings variable with all views.
* Allow editing the per_page setting. We showed the value, but we never showed it on the edit page..
* use snipeSettings in all views instead of the long ugly path.
* War on partials. Centralize all bootstrap table javascript
* Use model_number instead of modelno in importer
* Codacy fix.
* More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read.
* Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs
* Fix DB seeder.
* Base sql dump and csv's to import data from for tests.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name
* Use a .env.tests file for testing functional and unit to allow a separate database.
* Add functional tests for compoents, groups, and licenses.
* Now that the config is in the functional.yml, this just confuses things.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* update db dump
* Update tests to new reality
* env for the test setup
* only load the database at beginning of tests, not between each Functional test.
* Fix a miss from renaming note to notes.
* Set Termination date when creating an asset. It was only set on edit.
* Rename serial_number to serial in components for consistency.
* Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB.
* Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
return View :: make ( 'hardware/edit' , compact ( 'item' ))
2016-12-19 22:00:50 -08:00
-> with ( 'model_list' , Helper :: modelList ())
-> with ( 'supplier_list' , Helper :: suppliersList ())
-> with ( 'company_list' , Helper :: companyList ())
-> with ( 'location_list' , Helper :: locationsList ())
-> with ( 'statuslabel_list' , Helper :: statusLabelList ())
-> with ( 'assigned_to' , Helper :: usersList ())
-> with ( 'manufacturer' , Helper :: manufacturerList ())
-> with ( 'statuslabel_types' , Helper :: statusTypeList ())
-> with ( 'category' , Helper :: categoryList ( 'asset' ));
2016-03-25 01:18:05 -07:00
}
/**
* Validate and process asset edit form .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return Redirect
*/
2016-06-10 05:35:30 -07:00
2016-12-19 11:04:28 -08:00
public function update ( AssetRequest $request , $assetId = null )
2016-03-25 01:18:05 -07:00
{
2016-06-10 16:36:46 -07:00
2016-03-25 01:18:05 -07:00
// Check if the asset exists
2016-06-10 05:16:27 -07:00
if ( ! $asset = Asset :: find ( $assetId )) {
2016-03-25 01:18:05 -07:00
// Redirect to the asset management page with error
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 11:04:28 -08:00
$this -> authorize ( $asset );
2016-03-25 01:18:05 -07:00
2016-12-26 15:17:46 -08:00
$asset -> status_id = $request -> input ( 'status_id' , null );
$asset -> warranty_months = $request -> input ( 'warranty_months' , null );
$asset -> purchase_cost = Helper :: ParseFloat ( $request -> input ( 'purchase_cost' , null ));
$asset -> purchase_date = $request -> input ( 'purchase_date' , null );
$asset -> supplier_id = $request -> input ( 'supplier_id' , null );
2016-03-25 01:18:05 -07:00
2016-06-27 20:16:03 -07:00
// If the box isn't checked, it's not in the request at all.
$asset -> requestable = $request -> has ( 'requestable' );
2016-12-26 15:17:46 -08:00
$asset -> rtd_location_id = $request -> input ( 'rtd_location_id' , null );
2016-03-25 01:18:05 -07:00
2016-06-10 05:16:27 -07:00
if ( $request -> has ( 'image_delete' )) {
2016-03-25 01:18:05 -07:00
unlink ( public_path () . '/uploads/assets/' . $asset -> image );
$asset -> image = '' ;
}
// Update the asset data
2016-12-19 22:00:50 -08:00
$asset -> name = $request -> input ( 'name' );
$asset -> serial = $request -> input ( 'serial' );
$asset -> company_id = Company :: getIdForCurrentUser ( $request -> input ( 'company_id' ));
$asset -> model_id = $request -> input ( 'model_id' );
$asset -> order_number = $request -> input ( 'order_number' );
$asset -> asset_tag = $request -> input ( 'asset_tag' );
$asset -> notes = $request -> input ( 'notes' );
2016-03-25 01:18:05 -07:00
$asset -> physical = '1' ;
2016-06-10 16:36:46 -07:00
// Update the image
if ( Input :: has ( 'image' )) {
2016-06-10 05:16:27 -07:00
$image = $request -> input ( 'image' );
2016-07-10 18:43:00 -07:00
// See postCreate for more explaination of the following.
2016-05-22 13:15:20 -07:00
$header = explode ( ';' , $image , 2 )[ 0 ];
2016-06-22 12:27:41 -07:00
$extension = substr ( $header , strpos ( $header , '/' ) + 1 );
$image = substr ( $image , strpos ( $image , ',' ) + 1 );
2016-05-22 13:15:20 -07:00
2016-07-10 18:43:00 -07:00
$directory = public_path ( 'uploads/assets/' );
// Check if the uploads directory exists. If not, try to create it.
if ( ! file_exists ( $directory )) {
2016-09-21 12:05:06 -07:00
mkdir ( $directory , 0755 , true );
2016-07-10 18:43:00 -07:00
}
2016-05-22 13:15:20 -07:00
$file_name = str_random ( 25 ) . " . " . $extension ;
2016-03-25 01:18:05 -07:00
$path = public_path ( 'uploads/assets/' . $file_name );
2016-07-10 18:43:00 -07:00
try {
Image :: make ( $image ) -> resize ( 500 , 500 , function ( $constraint ) {
$constraint -> aspectRatio ();
$constraint -> upsize ();
}) -> save ( $path );
$asset -> image = $file_name ;
2016-08-11 19:13:49 -07:00
} catch ( \Exception $e ) {
2016-07-10 18:43:00 -07:00
\Input :: flash ();
$messageBag = new \Illuminate\Support\MessageBag ();
$messageBag -> add ( 'image' , $e -> getMessage ());
\Session () -> flash ( 'errors' , \Session :: get ( 'errors' , new \Illuminate\Support\ViewErrorBag )
-> put ( 'default' , $messageBag ));
return response () -> json ([ 'image' => $e -> getMessage ()], 422 );
}
2016-03-25 01:18:05 -07:00
$asset -> image = $file_name ;
}
2016-06-10 16:36:46 -07:00
// 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' ));
2016-06-22 12:27:41 -07:00
if ( $model -> fieldset ) {
foreach ( $model -> fieldset -> fields as $field ) {
2016-08-25 20:59:54 -07:00
if ( $field -> field_encrypted == '1' ) {
if ( Gate :: allows ( 'admin' )) {
2016-12-19 22:00:50 -08:00
$asset -> { CustomField :: name_to_db_name ( $field -> name )} = \Crypt :: encrypt ( e ( $request -> input ( CustomField :: name_to_db_name ( $field -> name ))));
2016-08-25 20:59:54 -07:00
}
} else {
2016-12-19 22:00:50 -08:00
$asset -> { CustomField :: name_to_db_name ( $field -> name )} = e ( $request -> input ( CustomField :: name_to_db_name ( $field -> name )));
2016-08-25 20:59:54 -07:00
}
2016-06-10 16:36:46 -07:00
}
}
2016-03-25 01:18:05 -07:00
2016-08-25 20:59:54 -07:00
2016-03-25 01:18:05 -07:00
if ( $asset -> save ()) {
// Redirect to the new asset page
2016-06-10 05:33:33 -07:00
\Session :: flash ( 'success' , trans ( 'admin/hardware/message.update.success' ));
2016-12-15 18:17:20 -08:00
return response () -> json ([ 'redirect_url' => route ( " hardware.show " , $assetId )]);
2016-03-25 01:18:05 -07:00
}
2016-06-22 12:20:49 -07:00
\Input :: flash ();
2016-06-22 09:53:27 -07:00
\Session :: flash ( 'errors' , $asset -> getErrors ());
return response () -> json ([ 'errors' => $asset -> getErrors ()], 500 );
2016-03-25 01:18:05 -07:00
}
/**
* Delete a given asset ( mark as deleted ) .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return Redirect
*/
2016-12-19 10:42:33 -08:00
public function destroy ( $assetId )
2016-03-25 01:18:05 -07:00
{
// Check if the asset exists
if ( is_null ( $asset = Asset :: find ( $assetId ))) {
// Redirect to the asset management page with error
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'delete' , $asset );
2016-03-25 01:18:05 -07:00
DB :: table ( 'assets' )
-> where ( 'id' , $asset -> id )
-> update ( array ( 'assigned_to' => null ));
$asset -> delete ();
// Redirect to the asset management page
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'success' , trans ( 'admin/hardware/message.delete.success' ));
2016-03-25 01:18:05 -07:00
}
/**
* Returns a view that presents a form to check an asset out to a
* user .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return View
*/
public function getCheckout ( $assetId )
{
// Check if the asset exists
2016-03-25 15:24:12 -07:00
if ( is_null ( $asset = Asset :: find ( e ( $assetId )))) {
2016-03-25 01:18:05 -07:00
// Redirect to the asset management page with error
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'checkout' , $asset );
2016-03-25 01:18:05 -07:00
// Get the dropdown of users and then pass it to the checkout view
2016-12-27 16:24:41 -08:00
return View :: make ( 'hardware/checkout' , compact ( 'asset' ))
-> with ( 'users_list' , Helper :: usersList ())
-> with ( 'assets_list' , Helper :: assetsList ())
-> with ( 'locations_list' , Helper :: locationsList ());
2016-03-25 01:18:05 -07:00
}
/**
2016-12-26 15:19:04 -08:00
* Validate and process the form data to check out an asset to a user .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param AssetCheckoutRequest $request
* @ param int $assetId
* @ return Redirect
* @ since [ v1 . 0 ]
*/
2016-03-25 01:18:05 -07:00
public function postCheckout ( AssetCheckoutRequest $request , $assetId )
{
// Check if the asset exists
if ( ! $asset = Asset :: find ( $assetId )) {
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-08-16 13:02:42 -07:00
} elseif ( ! $asset -> availableForCheckout ()) {
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.checkout.not_available' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'checkout' , $asset );
2016-03-25 01:18:05 -07:00
2016-12-27 16:24:41 -08:00
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' ));
}
// $user = User::find(Input::get('assigned_to'));
2016-03-25 01:18:05 -07:00
$admin = Auth :: user ();
2016-04-23 02:34:29 -07:00
if (( Input :: has ( 'checkout_at' )) && ( Input :: get ( 'checkout_at' ) != date ( " Y-m-d " ))) {
$checkout_at = e ( Input :: get ( 'checkout_at' ));
2016-03-25 01:18:05 -07:00
} else {
$checkout_at = date ( " Y-m-d H:i:s " );
}
if ( Input :: has ( 'expected_checkin' )) {
$expected_checkin = e ( Input :: get ( 'expected_checkin' ));
} else {
$expected_checkin = '' ;
}
2016-12-27 16:24:41 -08:00
if ( $asset -> checkOut ( $target , $admin , $checkout_at , $expected_checkin , e ( Input :: get ( 'note' )), e ( Input :: get ( 'name' )))) {
2016-12-26 15:19:04 -08:00
// Redirect to the new asset page
2016-04-28 21:06:41 -07:00
return redirect () -> to ( " hardware " ) -> with ( 'success' , trans ( 'admin/hardware/message.checkout.success' ));
2016-03-25 01:18:05 -07:00
}
// Redirect to the asset management page with error
2016-04-28 21:06:41 -07:00
return redirect () -> to ( " hardware/ $assetId /checkout " ) -> with ( 'error' , trans ( 'admin/hardware/message.checkout.error' )) -> withErrors ( $asset -> getErrors ());
2016-03-25 01:18:05 -07:00
}
/**
* Returns a view that presents a form to check an asset back into inventory .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ param string $backto
* @ since [ v1 . 0 ]
* @ return View
*/
public function getCheckin ( $assetId , $backto = null )
{
// Check if the asset exists
if ( is_null ( $asset = Asset :: find ( $assetId ))) {
// Redirect to the asset management page with error
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'checkin' , $asset );
2016-12-19 22:00:50 -08:00
return View :: make ( 'hardware/checkin' , compact ( 'asset' )) -> with ( 'statusLabel_list' , Helper :: statusLabelList ()) -> with ( 'backto' , $backto );
2016-03-25 01:18:05 -07:00
}
/**
2016-12-26 15:19:04 -08:00
* Validate and process the form data to check an asset back into inventory .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param AssetCheckinRequest $request
* @ param int $assetId
* @ param null $backto
* @ return Redirect
* @ since [ v1 . 0 ]
*/
2016-03-25 01:18:05 -07:00
public function postCheckin ( AssetCheckinRequest $request , $assetId = null , $backto = null )
{
// Check if the asset exists
if ( is_null ( $asset = Asset :: find ( $assetId ))) {
// Redirect to the asset management page with error
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'checkin' , $asset );
2016-04-23 02:08:38 -07:00
$admin = Auth :: user ();
2016-12-29 22:23:36 -08:00
$user = $asset -> assignedUser ;
2016-12-27 19:04:11 -08:00
if ( is_null ( $target = $asset -> assignedTo )) {
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.checkin.already_checked_in' ));
2016-03-25 01:18:05 -07:00
}
// This is just used for the redirect
$return_to = $asset -> assigned_to ;
$asset -> expected_checkin = null ;
$asset -> last_checkout = null ;
2016-04-23 02:08:38 -07:00
$asset -> assigned_to = null ;
2016-12-27 16:24:41 -08:00
$asset -> assignedTo () -> disassociate ( $asset );
2016-04-23 02:08:38 -07:00
$asset -> accepted = null ;
2016-05-12 12:34:44 -07:00
$asset -> name = e ( Input :: get ( 'name' ));
2016-04-23 02:08:38 -07:00
2016-03-25 01:18:05 -07:00
if ( Input :: has ( 'status_id' )) {
$asset -> status_id = e ( Input :: get ( 'status_id' ));
}
// Was the asset updated?
if ( $asset -> save ()) {
2016-12-27 19:04:11 -08:00
$logaction = $asset -> logCheckin ( $target , e ( request ( 'note' )));
2016-03-25 01:18:05 -07:00
$data [ 'log_id' ] = $logaction -> id ;
2016-12-27 19:04:11 -08:00
$data [ 'first_name' ] = get_class ( $target ) == User :: class ? $target -> first_name : '' ;
2016-12-23 17:52:00 -08:00
$data [ 'item_name' ] = $asset -> present () -> name ();
2016-03-25 01:18:05 -07:00
$data [ 'checkin_date' ] = $logaction -> created_at ;
$data [ 'item_tag' ] = $asset -> asset_tag ;
$data [ 'item_serial' ] = $asset -> serial ;
$data [ 'note' ] = $logaction -> note ;
2016-12-29 18:19:08 -08:00
if ((( $asset -> checkin_email () == '1' )) && ( isset ( $user )) && ( ! config ( 'app.lock_passwords' ))) {
2016-03-25 01:18:05 -07:00
Mail :: send ( 'emails.checkin-asset' , $data , function ( $m ) use ( $user ) {
$m -> to ( $user -> email , $user -> first_name . ' ' . $user -> last_name );
2016-09-20 07:20:40 -07:00
$m -> replyTo ( config ( 'mail.reply_to.address' ), config ( 'mail.reply_to.name' ));
2016-09-26 14:13:07 -07:00
$m -> subject ( trans ( 'mail.Confirm_Asset_Checkin' ));
2016-03-25 01:18:05 -07:00
});
}
if ( $backto == 'user' ) {
2016-04-28 21:06:41 -07:00
return redirect () -> to ( " admin/users/ " . $return_to . '/view' ) -> with ( 'success' , trans ( 'admin/hardware/message.checkin.success' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 22:00:50 -08:00
return redirect () -> to ( " hardware " ) -> with ( 'success' , trans ( 'admin/hardware/message.checkin.success' ));
2016-03-25 01:18:05 -07:00
}
// Redirect to the asset management page with error
2016-04-28 21:06:41 -07:00
return redirect () -> to ( " hardware " ) -> with ( 'error' , trans ( 'admin/hardware/message.checkin.error' ));
2016-03-25 01:18:05 -07:00
}
/**
* Returns a view that presents information about an asset for detail view .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return View
*/
2016-12-15 04:09:40 -08:00
public function show ( $assetId = null )
2016-03-25 01:18:05 -07:00
{
$asset = Asset :: withTrashed () -> find ( $assetId );
$settings = Setting :: getSettings ();
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'view' , $asset );
if ( $asset -> userloc ) {
2016-03-25 01:18:05 -07:00
$use_currency = $asset -> userloc -> currency ;
} elseif ( $asset -> assetloc ) {
$use_currency = $asset -> assetloc -> currency ;
} else {
if ( $settings -> default_currency != '' ) {
$use_currency = $settings -> default_currency ;
} else {
2016-04-07 13:39:35 -07:00
$use_currency = trans ( 'general.currency' );
2016-03-25 01:18:05 -07:00
}
}
if ( isset ( $asset -> id )) {
$qr_code = ( object ) array (
'display' => $settings -> qr_code == '1' ,
'url' => route ( 'qr_code/hardware' , $asset -> id )
);
return View :: make ( 'hardware/view' , compact ( 'asset' , 'qr_code' , 'settings' )) -> with ( 'use_currency' , $use_currency );
}
2016-12-19 22:00:50 -08:00
// Prepare the error message
$error = trans ( 'admin/hardware/message.does_not_exist' , compact ( 'id' ));
2016-03-25 01:18:05 -07:00
2016-12-19 22:00:50 -08:00
// Redirect to the user management page
return redirect () -> route ( 'hardware' ) -> with ( 'error' , $error );
2016-03-25 01:18:05 -07:00
}
/**
* Return a QR code for the asset
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return Response
*/
public function getQrCode ( $assetId = null )
{
$settings = Setting :: getSettings ();
if ( $settings -> qr_code == '1' ) {
$asset = Asset :: find ( $assetId );
$size = Helper :: barcodeDimensions ( $settings -> barcode_type );
2016-08-01 22:56:28 -07:00
$qr_file = public_path () . '/uploads/barcodes/qr-' . str_slug ( $asset -> asset_tag ) . '.png' ;
2016-03-25 01:18:05 -07:00
if ( isset ( $asset -> id , $asset -> asset_tag )) {
2016-08-01 22:56:28 -07:00
if ( file_exists ( $qr_file )) {
$header = [ 'Content-type' => 'image/png' ];
return response () -> file ( $qr_file , $header );
} else {
$barcode = new \Com\Tecnick\Barcode\Barcode ();
2016-12-15 18:17:20 -08:00
$barcode_obj = $barcode -> getBarcodeObj ( $settings -> barcode_type , route ( 'hardware.show' , $asset -> id ), $size [ 'height' ], $size [ 'width' ], 'black' , array ( - 2 , - 2 , - 2 , - 2 ));
2016-08-11 19:13:49 -07:00
file_put_contents ( $qr_file , $barcode_obj -> getPngData ());
2016-08-01 22:56:28 -07:00
return response ( $barcode_obj -> getPngData ()) -> header ( 'Content-type' , 'image/png' );
}
2016-03-25 01:18:05 -07:00
}
}
}
2016-05-19 19:12:42 -07:00
/**
* Return a 2 D barcode for the asset
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return Response
*/
public function getBarCode ( $assetId = null )
{
$settings = Setting :: getSettings ();
$asset = Asset :: find ( $assetId );
2016-08-01 22:56:28 -07:00
$barcode_file = public_path () . '/uploads/barcodes/' . str_slug ( $settings -> alt_barcode ) . '-' . str_slug ( $asset -> asset_tag ) . '.png' ;
2016-05-19 19:12:42 -07:00
if ( isset ( $asset -> id , $asset -> asset_tag )) {
2016-08-01 22:56:28 -07:00
if ( file_exists ( $barcode_file )) {
$header = [ 'Content-type' => 'image/png' ];
return response () -> file ( $barcode_file , $header );
} else {
$barcode = new \Com\Tecnick\Barcode\Barcode ();
$barcode_obj = $barcode -> getBarcodeObj ( $settings -> alt_barcode , $asset -> asset_tag , 250 , 20 );
2016-08-11 19:13:49 -07:00
file_put_contents ( $barcode_file , $barcode_obj -> getPngData ());
2016-08-01 22:56:28 -07:00
return response ( $barcode_obj -> getPngData ()) -> header ( 'Content-type' , 'image/png' );
}
2016-05-19 19:12:42 -07:00
}
}
2016-03-25 01:18:05 -07:00
/**
* Get the Asset import upload page .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v2 . 0 ]
* @ return View
*/
public function getImportUpload ()
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'create' , Asset :: class );
2016-03-25 15:50:08 -07:00
$path = config ( 'app.private_uploads' ) . '/imports/assets' ;
2016-03-25 01:18:05 -07:00
$files = array ();
if ( ! Company :: isCurrentUserAuthorized ()) {
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'general.insufficient_permissions' ));
2016-03-25 01:18:05 -07:00
}
2016-05-26 06:00:00 -07:00
// Check if the uploads directory exists. If not, try to create it.
2016-06-22 12:27:41 -07:00
if ( ! file_exists ( $path )) {
2016-09-21 12:05:06 -07:00
mkdir ( $path , 0755 , true );
2016-06-22 12:27:41 -07:00
}
2016-03-25 01:18:05 -07:00
if ( $handle = opendir ( $path )) {
/* This is the correct way to loop over the directory. */
while ( false !== ( $entry = readdir ( $handle ))) {
clearstatcache ();
if ( substr ( strrchr ( $entry , '.' ), 1 ) == 'csv' ) {
$files [] = array (
'filename' => $entry ,
'filesize' => Setting :: fileSizeConvert ( filesize ( $path . '/' . $entry )),
'modified' => filemtime ( $path . '/' . $entry )
);
}
}
closedir ( $handle );
$files = array_reverse ( $files );
}
return View :: make ( 'hardware/import' ) -> with ( 'files' , $files );
}
/**
2016-12-19 22:00:50 -08:00
* Upload the import file via AJAX
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v2 . 0 ]
* @ param AssetFileRequest $request
* @ return array
*/
2016-03-25 01:18:05 -07:00
public function postAPIImportUpload ( AssetFileRequest $request )
{
if ( ! Company :: isCurrentUserAuthorized ()) {
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'general.insufficient_permissions' ));
2016-03-25 01:18:05 -07:00
} elseif ( ! config ( 'app.lock_passwords' )) {
$files = Input :: file ( 'files' );
2016-03-25 15:50:08 -07:00
$path = config ( 'app.private_uploads' ) . '/imports/assets' ;
2016-03-25 01:18:05 -07:00
$results = array ();
foreach ( $files as $file ) {
if ( ! in_array ( $file -> getMimeType (), array (
'application/vnd.ms-excel' ,
'text/csv' ,
'text/plain' ,
'text/comma-separated-values' ,
'text/tsv' ))) {
$results [ 'error' ] = 'File type must be CSV' ;
return $results ;
}
$date = date ( 'Y-m-d-his' );
$fixed_filename = str_replace ( ' ' , '-' , $file -> getClientOriginalName ());
2016-06-27 20:37:15 -07:00
try {
$file -> move ( $path , $date . '-' . $fixed_filename );
2016-12-19 22:00:50 -08:00
} catch ( FileException $exception ) {
2016-08-11 19:13:49 -07:00
$results [ 'error' ] = trans ( 'admin/hardware/message.upload.error' );
if ( config ( 'app.debug' )) {
$results [ 'error' ] .= ' ' . $exception -> getMessage ();
}
return $results ;
2016-06-27 20:37:15 -07:00
}
2016-03-25 01:18:05 -07:00
$name = date ( 'Y-m-d-his' ) . '-' . $fixed_filename ;
2016-03-25 19:26:22 -07:00
$filesize = Setting :: fileSizeConvert ( filesize ( $path . '/' . $name ));
2016-03-25 01:18:05 -07:00
$results [] = compact ( 'name' , 'filesize' );
}
2016-12-19 22:00:50 -08:00
return [
2016-03-25 01:18:05 -07:00
'files' => $results
2016-12-19 22:00:50 -08:00
];
2016-03-25 01:18:05 -07:00
}
2016-12-19 22:00:50 -08:00
$results [ 'error' ] = trans ( 'general.feature_disabled' );
return $results ;
2016-03-25 01:18:05 -07:00
}
2016-08-29 15:49:32 -07:00
public function getDeleteImportFile ( $filename )
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'create' , Asset :: class );
2016-08-29 15:49:32 -07:00
if ( unlink ( config ( 'app.private_uploads' ) . '/imports/assets/' . $filename )) {
return redirect () -> back () -> with ( 'success' , trans ( 'admin/hardware/message.import.file_delete_success' ));
}
return redirect () -> back () -> with ( 'error' , trans ( 'admin/hardware/message.import.file_delete_error' ));
}
2016-03-25 01:18:05 -07:00
/**
* Process the uploaded file
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v2 . 0 ]
* @ return Redirect
*/
2016-12-26 15:16:42 -08:00
public function postProcessImportFile ( ItemImportRequest $request )
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'create' , Asset :: class );
2016-12-26 15:16:42 -08:00
$errors = $request -> import ();
2016-08-29 15:49:32 -07:00
// We use hardware instead of asset in the url
$redirectTo = " hardware " ;
2016-12-29 14:02:18 -08:00
switch ( request ( 'import-type' )) {
2016-08-29 15:49:32 -07:00
case " asset " :
2016-12-26 15:16:42 -08:00
$redirectTo = " hardware.index " ;
2016-08-29 15:49:32 -07:00
break ;
case " accessory " :
2016-12-26 15:16:42 -08:00
$redirectTo = " accessories.index " ;
2016-08-29 15:49:32 -07:00
break ;
case " consumable " :
2016-12-26 15:16:42 -08:00
$redirectTo = " consumables.index " ;
2016-08-29 15:49:32 -07:00
break ;
2017-01-05 15:45:12 -08:00
case " component " :
$redirectTo = " components.index " ;
break ;
2016-08-29 15:49:32 -07:00
}
2016-12-26 15:16:42 -08:00
if ( $errors ) { //Failure
return redirect () -> back () -> with ( 'import_errors' , json_decode ( json_encode ( $errors ))) -> with ( 'error' , trans ( 'admin/hardware/message.import.error' ));
2016-06-19 20:06:54 -07:00
}
2016-12-26 15:16:42 -08:00
return redirect () -> to ( route ( $redirectTo )) -> with ( 'success' , trans ( 'admin/hardware/message.import.success' ));
2016-03-25 01:18:05 -07:00
}
/**
* Returns a view that presents a form to clone an asset .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return View
*/
public function getClone ( $assetId = null )
{
// Check if the asset exists
if ( is_null ( $asset_to_clone = Asset :: find ( $assetId ))) {
// Redirect to the asset management page
2016-12-15 20:02:47 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-03-25 01:18:05 -07:00
}
2016-12-29 14:02:18 -08:00
$this -> authorize ( 'create' , $asset_to_clone );
2016-12-19 11:04:28 -08:00
2016-03-25 01:18:05 -07:00
$asset = clone $asset_to_clone ;
$asset -> id = null ;
$asset -> asset_tag = '' ;
$asset -> serial = '' ;
$asset -> assigned_to = '' ;
return View :: make ( 'hardware/edit' )
2016-12-19 22:00:50 -08:00
-> with ( 'supplier_list' , Helper :: suppliersList ())
-> with ( 'model_list' , Helper :: modelList ())
-> with ( 'statuslabel_list' , Helper :: statusLabelList ())
-> with ( 'statuslabel_types' , Helper :: statusTypeList ())
-> with ( 'assigned_to' , Helper :: usersList ())
Partialize forms (#2884)
* Consolidate edit form elements into reusable partials.
This is a large code change that doesn't do much immediately. It
refactors all of the various edit.blade.php files to reference
standardized partials, so that they all reference the same base html
layout. This has the side effect of moving everything to the new fancy
"required" indicators, and making things look consistent.
In addition, I've gone ahead and renamed a few database fields. We had
Assetmodel::modelno and Consumable::model_no, I've renamed both to
model_number. We had items using ::note and ::notes, I've standardized
on ::notes. Component used total_qty where consumables and accessories
used qty, so I've moved everything to qty (And fixed a few bugs in the
helper file in the process.
TODO includes looking at how/where to place the modal javascripts to
allow for on the fly creation from all places, rather than just the
asset page.
Rename assetmodel::modelno to model_number for clarity and consistency
Rename consumable::model_no to model_number for clarity and consistency
Rename assetmodel::note to notes for clarity and consistency
Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying.
* Share a settings variable with all views.
* Allow editing the per_page setting. We showed the value, but we never showed it on the edit page..
* use snipeSettings in all views instead of the long ugly path.
* War on partials. Centralize all bootstrap table javascript
* Use model_number instead of modelno in importer
* Codacy fix.
* More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read.
* Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs
* Fix DB seeder.
* Base sql dump and csv's to import data from for tests.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name
* Use a .env.tests file for testing functional and unit to allow a separate database.
* Add functional tests for compoents, groups, and licenses.
* Now that the config is in the functional.yml, this just confuses things.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* update db dump
* Update tests to new reality
* env for the test setup
* only load the database at beginning of tests, not between each Functional test.
* Fix a miss from renaming note to notes.
* Set Termination date when creating an asset. It was only set on edit.
* Rename serial_number to serial in components for consistency.
* Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB.
* Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
-> with ( 'item' , $asset )
2016-12-19 22:00:50 -08:00
-> with ( 'location_list' , Helper :: locationsList ())
-> with ( 'manufacturer' , Helper :: manufacturerList ())
-> with ( 'category' , Helper :: categoryList ( 'asset' ))
-> with ( 'company_list' , Helper :: companyList ());
2016-03-25 01:18:05 -07:00
}
2016-08-12 16:01:59 -07:00
/**
* Return history import view
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ return View
*/
public function getImportHistory ()
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'checkout' , Asset :: class );
2016-08-12 16:01:59 -07:00
return View :: make ( 'hardware/history' );
}
/**
* Import history
*
* This needs a LOT of love . It ' s done very inelegantly right now , and there are
* a ton of optimizations that could ( and should ) be done .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v3 . 3 ]
* @ return View
*/
public function postImportHistory ( Request $request )
{
if ( ! ini_get ( " auto_detect_line_endings " )) {
ini_set ( " auto_detect_line_endings " , '1' );
}
$csv = Reader :: createFromPath ( Input :: file ( 'user_import_csv' ));
$csv -> setNewline ( " \r \n " );
//get the first row, usually the CSV header
//$headers = $csv->fetchOne();
$results = $csv -> fetchAssoc ();
$item = array ();
2016-08-12 19:03:32 -07:00
$status = array ();
2016-09-20 09:22:49 -07:00
$status [ 'error' ] = array ();
$status [ 'success' ] = array ();
2016-08-12 16:01:59 -07:00
2016-12-29 14:02:18 -08:00
foreach ( $results as $row ) {
2016-08-12 16:01:59 -07:00
if ( is_array ( $row )) {
$row = array_change_key_case ( $row , CASE_LOWER );
$asset_tag = Helper :: array_smart_fetch ( $row , " asset tag " );
if ( ! array_key_exists ( $asset_tag , $item )) {
$item [ $asset_tag ] = array ();
}
$batch_counter = count ( $item [ $asset_tag ]);
2016-08-12 19:03:32 -07:00
$item [ $asset_tag ][ $batch_counter ][ 'checkout_date' ] = Carbon :: parse ( Helper :: array_smart_fetch ( $row , " date " )) -> format ( 'Y-m-d H:i:s' );
2016-08-12 16:01:59 -07:00
$item [ $asset_tag ][ $batch_counter ][ 'asset_tag' ] = Helper :: array_smart_fetch ( $row , " asset tag " );
$item [ $asset_tag ][ $batch_counter ][ 'name' ] = Helper :: array_smart_fetch ( $row , " name " );
$item [ $asset_tag ][ $batch_counter ][ 'email' ] = Helper :: array_smart_fetch ( $row , " email " );
2016-12-29 14:02:18 -08:00
if ( $asset = Asset :: where ( 'asset_tag' , '=' , $asset_tag ) -> first ()) {
2016-08-12 16:01:59 -07:00
2016-09-20 09:22:49 -07:00
$item [ $asset_tag ][ $batch_counter ][ 'asset_id' ] = $asset -> id ;
2016-08-12 16:01:59 -07:00
2016-12-29 14:02:18 -08:00
$base_username = User :: generateFormattedNameFromFullName ( Setting :: getSettings () -> username_format , $item [ $asset_tag ][ $batch_counter ][ 'name' ]);
$user = User :: where ( 'username' , '=' , $base_username [ 'username' ]);
2016-09-20 09:22:49 -07:00
$user_query = ' on username ' . $base_username [ 'username' ];
2016-08-12 16:01:59 -07:00
2016-09-20 09:22:49 -07:00
if ( $request -> input ( 'match_firstnamelastname' ) == '1' ) {
2016-12-29 14:02:18 -08:00
$firstnamedotlastname = User :: generateFormattedNameFromFullName ( 'firstname.lastname' , $item [ $asset_tag ][ $batch_counter ][ 'name' ]);
2016-09-20 09:22:49 -07:00
$item [ $asset_tag ][ $batch_counter ][ 'username' ][] = $firstnamedotlastname [ 'username' ];
2016-12-29 14:02:18 -08:00
$user -> orWhere ( 'username' , '=' , $firstnamedotlastname [ 'username' ]);
2016-09-20 09:22:49 -07:00
$user_query .= ', or on username ' . $firstnamedotlastname [ 'username' ];
2016-08-12 16:01:59 -07:00
}
2016-09-20 09:22:49 -07:00
if ( $request -> input ( 'match_flastname' ) == '1' ) {
2016-12-29 14:02:18 -08:00
$flastname = User :: generateFormattedNameFromFullName ( 'filastname' , $item [ $asset_tag ][ $batch_counter ][ 'name' ]);
2016-09-20 09:22:49 -07:00
$item [ $asset_tag ][ $batch_counter ][ 'username' ][] = $flastname [ 'username' ];
2016-12-29 14:02:18 -08:00
$user -> orWhere ( 'username' , '=' , $flastname [ 'username' ]);
2016-09-20 09:22:49 -07:00
$user_query .= ', or on username ' . $flastname [ 'username' ];
}
if ( $request -> input ( 'match_firstname' ) == '1' ) {
2016-12-29 14:02:18 -08:00
$firstname = User :: generateFormattedNameFromFullName ( 'firstname' , $item [ $asset_tag ][ $batch_counter ][ 'name' ]);
2016-09-20 09:22:49 -07:00
$item [ $asset_tag ][ $batch_counter ][ 'username' ][] = $firstname [ 'username' ];
2016-12-29 14:02:18 -08:00
$user -> orWhere ( 'username' , '=' , $firstname [ 'username' ]);
2016-09-20 09:22:49 -07:00
$user_query .= ', or on username ' . $firstname [ 'username' ];
}
if ( $request -> input ( 'match_email' ) == '1' ) {
if ( $item [ $asset_tag ][ $batch_counter ][ 'email' ] == '' ) {
$item [ $asset_tag ][ $batch_counter ][ 'username' ][] = $user_email = User :: generateEmailFromFullName ( $item [ $asset_tag ][ $batch_counter ][ 'name' ]);
2016-12-29 14:02:18 -08:00
$user -> orWhere ( 'username' , '=' , $user_email );
2016-09-20 09:22:49 -07:00
$user_query .= ', or on username ' . $user_email ;
}
}
2016-08-12 16:01:59 -07:00
2016-09-20 09:22:49 -07:00
// A matching user was found
if ( $user = $user -> first ()) {
$item [ $asset_tag ][ $batch_counter ][ 'checkedout_to' ] = $user -> id ;
2016-08-12 16:01:59 -07:00
$item [ $asset_tag ][ $batch_counter ][ 'user_id' ] = $user -> id ;
Actionlog :: firstOrCreate ( array (
2016-09-20 09:22:49 -07:00
'item_id' => $asset -> id ,
'item_type' => Asset :: class ,
'user_id' => Auth :: user () -> id ,
2016-12-23 17:52:00 -08:00
'note' => 'Checkout imported by ' . Auth :: user () -> present () -> fullName () . ' from history importer' ,
2016-09-20 09:22:49 -07:00
'target_id' => $item [ $asset_tag ][ $batch_counter ][ 'user_id' ],
'target_type' => User :: class ,
'created_at' => $item [ $asset_tag ][ $batch_counter ][ 'checkout_date' ],
2016-09-29 22:20:49 -07:00
'action_type' => 'checkout' ,
2016-12-29 14:02:18 -08:00
));
2016-08-12 16:01:59 -07:00
$asset -> assigned_to = $user -> id ;
2016-09-20 09:22:49 -07:00
if ( $asset -> save ()) {
2016-09-23 13:14:11 -07:00
$status [ 'success' ][][ 'asset' ][ $asset_tag ][ 'msg' ] = 'Asset successfully matched for ' . Helper :: array_smart_fetch ( $row , " name " ) . $user_query . ' on ' . $item [ $asset_tag ][ $batch_counter ][ 'checkout_date' ];
2016-09-20 09:22:49 -07:00
} else {
$status [ 'error' ][][ 'asset' ][ $asset_tag ][ 'msg' ] = 'Asset and user was matched but could not be saved.' ;
}
2016-08-12 16:01:59 -07:00
} else {
2016-09-20 09:22:49 -07:00
$item [ $asset_tag ][ $batch_counter ][ 'checkedout_to' ] = null ;
$status [ 'error' ][][ 'user' ][ Helper :: array_smart_fetch ( $row , " name " )][ 'msg' ] = 'User does not exist so no checkin log was created.' ;
2016-08-12 16:01:59 -07:00
}
} else {
2016-09-20 09:22:49 -07:00
$item [ $asset_tag ][ $batch_counter ][ 'asset_id' ] = null ;
$status [ 'error' ][][ 'asset' ][ $asset_tag ][ 'msg' ] = 'Asset does not exist so no match was attempted.' ;
2016-08-12 16:01:59 -07:00
}
}
}
// Loop through and backfill the checkins
foreach ( $item as $key => $asset_batch ) {
2016-08-12 19:03:32 -07:00
$total_in_batch = count ( $asset_batch );
2016-12-29 14:02:18 -08:00
for ( $x = 0 ; $x < $total_in_batch ; $x ++ ) {
2016-08-12 19:03:32 -07:00
$next = $x + 1 ;
2016-08-12 17:10:03 -07:00
// Only do this if a matching user was found
2016-12-29 14:02:18 -08:00
if (( array_key_exists ( 'checkedout_to' , $asset_batch [ $x ])) && ( $asset_batch [ $x ][ 'checkedout_to' ] != '' )) {
2016-08-12 16:01:59 -07:00
2016-12-29 14:02:18 -08:00
if (( $total_in_batch > 1 ) && ( $x < $total_in_batch ) && ( array_key_exists ( $next , $asset_batch ))) {
2016-08-12 19:03:32 -07:00
$checkin_date = Carbon :: parse ( $asset_batch [ $next ][ 'checkout_date' ]) -> subDay ( 1 ) -> format ( 'Y-m-d H:i:s' );
$asset_batch [ $x ][ 'real_checkin' ] = $checkin_date ;
2016-08-12 17:10:03 -07:00
Actionlog :: firstOrCreate ( array (
2016-09-06 19:39:42 -07:00
'item_id' => $asset_batch [ $x ][ 'asset_id' ],
'item_type' => Asset :: class ,
2016-08-12 19:03:32 -07:00
'user_id' => Auth :: user () -> id ,
2016-12-23 17:52:00 -08:00
'note' => 'Checkin imported by ' . Auth :: user () -> present () -> fullName () . ' from history importer' ,
2016-09-06 19:39:42 -07:00
'target_id' => null ,
2016-08-12 19:03:32 -07:00
'created_at' => $checkin_date ,
'action_type' => 'checkin'
2016-12-29 14:02:18 -08:00
));
2016-08-12 17:10:03 -07:00
}
2016-08-12 16:01:59 -07:00
}
2016-08-12 19:03:32 -07:00
}
2016-08-12 16:01:59 -07:00
}
2016-12-29 14:02:18 -08:00
return View :: make ( 'hardware/history' ) -> with ( 'status' , $status );
2016-08-12 16:01:59 -07:00
}
2016-03-25 01:18:05 -07:00
/**
* Retore a deleted asset .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return View
*/
public function getRestore ( $assetId = null )
{
2016-12-19 11:04:28 -08:00
// Get asset information
2016-03-25 01:18:05 -07:00
$asset = Asset :: withTrashed () -> find ( $assetId );
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'delete' , $asset );
if ( isset ( $asset -> id )) {
2016-04-21 21:01:57 -07:00
// Restore the asset
2016-06-22 12:27:41 -07:00
Asset :: withTrashed () -> where ( 'id' , $assetId ) -> restore ();
2016-04-28 21:06:41 -07:00
return redirect () -> route ( 'hardware' ) -> with ( 'success' , trans ( 'admin/hardware/message.restore.success' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 22:00:50 -08:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-03-25 01:18:05 -07:00
}
/**
2016-12-19 22:00:50 -08:00
* Upload a file to the server .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param AssetFileRequest $request
* @ param int $assetId
* @ return Redirect
* @ since [ v1 . 0 ]
*/
2016-03-25 01:18:05 -07:00
public function postUpload ( AssetFileRequest $request , $assetId = null )
{
if ( ! $asset = Asset :: find ( $assetId )) {
2016-04-28 21:06:41 -07:00
return redirect () -> route ( 'hardware' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , $asset );
2016-03-25 01:18:05 -07:00
2016-03-25 17:20:28 -07:00
$destinationPath = config ( 'app.private_uploads' ) . '/assets' ;
2016-03-25 01:18:05 -07:00
if ( Input :: hasFile ( 'assetfile' )) {
foreach ( Input :: file ( 'assetfile' ) as $file ) {
$extension = $file -> getClientOriginalExtension ();
$filename = 'hardware-' . $asset -> id . '-' . str_random ( 8 );
2016-03-25 03:32:35 -07:00
$filename .= '-' . str_slug ( $file -> getClientOriginalName ()) . '.' . $extension ;
2016-03-25 01:18:05 -07:00
$upload_success = $file -> move ( $destinationPath , $filename );
//Log the deletion of seats to the log
2016-09-06 19:39:42 -07:00
$asset -> logUpload ( $filename , e ( Input :: get ( 'notes' )));
2016-03-25 01:18:05 -07:00
}
} else {
2016-04-28 21:06:41 -07:00
return redirect () -> back () -> with ( 'error' , trans ( 'admin/hardware/message.upload.nofiles' ));
2016-03-25 01:18:05 -07:00
}
if ( $upload_success ) {
2016-04-28 21:06:41 -07:00
return redirect () -> back () -> with ( 'success' , trans ( 'admin/hardware/message.upload.success' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 22:00:50 -08:00
return redirect () -> back () -> with ( 'error' , trans ( 'admin/hardware/message.upload.error' ));
2016-03-25 01:18:05 -07:00
}
/**
* Delete the associated file
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ param int $fileId
* @ since [ v1 . 0 ]
* @ return View
*/
public function getDeleteFile ( $assetId = null , $fileId = null )
{
$asset = Asset :: find ( $assetId );
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , $asset );
2016-03-25 15:50:08 -07:00
$destinationPath = config ( 'app.private_uploads' ) . '/imports/assets' ;
2016-03-25 01:18:05 -07:00
// the asset is valid
if ( isset ( $asset -> id )) {
2016-12-19 22:00:50 -08:00
$this -> authorize ( 'update' , $asset );
2016-03-25 01:18:05 -07:00
$log = Actionlog :: find ( $fileId );
$full_filename = $destinationPath . '/' . $log -> filename ;
if ( file_exists ( $full_filename )) {
unlink ( $destinationPath . '/' . $log -> filename );
}
$log -> delete ();
2016-04-28 21:06:41 -07:00
return redirect () -> back () -> with ( 'success' , trans ( 'admin/hardware/message.deletefile.success' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 22:00:50 -08:00
// Prepare the error message
$error = trans ( 'admin/hardware/message.does_not_exist' , compact ( 'id' ));
2016-03-25 01:18:05 -07:00
2016-12-19 22:00:50 -08:00
// Redirect to the hardware management page
return redirect () -> route ( 'hardware' ) -> with ( 'error' , $error );
}
2016-03-25 01:18:05 -07:00
/**
* Check for permissions and display the file .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ param int $fileId
* @ since [ v1 . 0 ]
* @ return View
*/
public function displayFile ( $assetId = null , $fileId = null )
{
$asset = Asset :: find ( $assetId );
// the asset is valid
if ( isset ( $asset -> id )) {
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'view' , $asset );
2016-03-25 01:18:05 -07:00
$log = Actionlog :: find ( $fileId );
2016-03-25 20:38:27 -07:00
$file = $log -> get_src ( 'assets' );
$filetype = Helper :: checkUploadIsImage ( $file );
2016-03-25 01:18:05 -07:00
if ( $filetype ) {
$contents = file_get_contents ( $file );
2016-03-25 20:38:27 -07:00
return Response :: make ( $contents ) -> header ( 'Content-Type' , $filetype );
2016-03-25 01:18:05 -07:00
}
2016-12-19 22:00:50 -08:00
return Response :: download ( $file );
2016-03-25 01:18:05 -07:00
}
2016-12-19 22:00:50 -08:00
// Prepare the error message
$error = trans ( 'admin/hardware/message.does_not_exist' , compact ( 'id' ));
2016-03-25 01:18:05 -07:00
2016-12-19 22:00:50 -08:00
// Redirect to the hardware management page
return redirect () -> route ( 'hardware' ) -> with ( 'error' , $error );
}
2016-03-25 01:18:05 -07:00
/**
2016-12-19 22:00:50 -08:00
* Display the bulk edit page .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ return View
* @ internal param int $assetId
* @ since [ v2 . 0 ]
*/
public function postBulkEdit ()
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , Asset :: class );
2016-12-19 22:00:50 -08:00
if ( ! Input :: has ( 'edit_asset' )) {
return redirect () -> back () -> with ( 'error' , 'No assets selected' );
}
$asset_raw_array = Input :: get ( 'edit_asset' );
foreach ( $asset_raw_array as $asset_id => $value ) {
$asset_ids [] = $asset_id ;
2016-03-25 01:18:05 -07:00
}
if ( Input :: has ( 'bulk_actions' )) {
// Create labels
if ( Input :: get ( 'bulk_actions' ) == 'labels' ) {
2016-12-19 22:00:50 -08:00
$count = 0 ;
2016-12-29 14:02:18 -08:00
return View :: make ( 'hardware/labels' ) -> with ( 'assets' , Asset :: find ( $asset_ids )) -> with ( 'settings' , Setting :: getSettings ()) -> with ( 'count' , $count ) -> with (
'settings' ,
Setting :: getSettings ()
);
2016-03-25 01:18:05 -07:00
} elseif ( Input :: get ( 'bulk_actions' ) == 'delete' ) {
2016-12-29 06:31:16 -08:00
$assets = Asset :: with ( 'assignedTo' , 'assetloc' ) -> find ( $asset_ids );
2016-12-19 11:04:28 -08:00
$assets -> each ( function ( $asset ) {
$this -> authorize ( 'delete' , $asset );
});
2016-03-25 01:18:05 -07:00
return View :: make ( 'hardware/bulk-delete' ) -> with ( 'assets' , $assets );
// Bulk edit
} elseif ( Input :: get ( 'bulk_actions' ) == 'edit' ) {
return View :: make ( 'hardware/bulk' )
2016-12-19 22:00:50 -08:00
-> with ( 'assets' , request ( 'edit_asset' ))
-> with ( 'supplier_list' , Helper :: suppliersList ())
-> with ( 'statuslabel_list' , Helper :: statusLabelList ())
-> with ( 'location_list' , Helper :: locationsList ())
-> with ( 'models_list' , Helper :: modelList ())
2016-12-29 14:02:18 -08:00
-> with (
'companies_list' ,
array ( '' => '' ) + array ( 'clear' => trans ( 'general.remove_company' )) + Helper :: companyList ()
);
2016-03-25 01:18:05 -07:00
}
}
2016-12-19 22:00:50 -08:00
return redirect () -> back () -> with ( 'error' , 'No action selected' );
2016-03-25 01:18:05 -07:00
}
/**
2016-12-19 22:00:50 -08:00
* Save bulk edits
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ return Redirect
* @ internal param array $assets
* @ since [ v2 . 0 ]
*/
public function postBulkSave ()
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , Asset :: class );
if ( Input :: has ( 'bulk_edit' )) {
2016-03-25 01:18:05 -07:00
$assets = Input :: get ( 'bulk_edit' );
2016-12-19 22:00:50 -08:00
if (( Input :: has ( 'purchase_date' ))
|| ( Input :: has ( 'purchase_cost' ))
|| ( Input :: has ( 'supplier_id' ))
|| ( Input :: has ( 'order_number' ))
|| ( Input :: has ( 'warranty_months' ))
|| ( Input :: has ( 'rtd_location_id' ))
|| ( Input :: has ( 'requestable' ))
|| ( Input :: has ( 'company_id' ))
|| ( Input :: has ( 'status_id' ))
|| ( Input :: has ( 'model_id' ))
) {
2016-03-25 01:18:05 -07:00
foreach ( $assets as $key => $value ) {
$update_array = array ();
if ( Input :: has ( 'purchase_date' )) {
$update_array [ 'purchase_date' ] = e ( Input :: get ( 'purchase_date' ));
}
if ( Input :: has ( 'purchase_cost' )) {
2016-09-27 19:07:30 -07:00
$update_array [ 'purchase_cost' ] = Helper :: ParseFloat ( e ( Input :: get ( 'purchase_cost' )));
2016-03-25 01:18:05 -07:00
}
if ( Input :: has ( 'supplier_id' )) {
$update_array [ 'supplier_id' ] = e ( Input :: get ( 'supplier_id' ));
}
if ( Input :: has ( 'model_id' )) {
$update_array [ 'model_id' ] = e ( Input :: get ( 'model_id' ));
}
if ( Input :: has ( 'company_id' )) {
if ( Input :: get ( 'company_id' ) == " clear " ) {
$update_array [ 'company_id' ] = null ;
} else {
$update_array [ 'company_id' ] = e ( Input :: get ( 'company_id' ));
}
}
if ( Input :: has ( 'order_number' )) {
$update_array [ 'order_number' ] = e ( Input :: get ( 'order_number' ));
}
if ( Input :: has ( 'warranty_months' )) {
$update_array [ 'warranty_months' ] = e ( Input :: get ( 'warranty_months' ));
}
if ( Input :: has ( 'rtd_location_id' )) {
$update_array [ 'rtd_location_id' ] = e ( Input :: get ( 'rtd_location_id' ));
}
if ( Input :: has ( 'status_id' )) {
$update_array [ 'status_id' ] = e ( Input :: get ( 'status_id' ));
}
if ( Input :: has ( 'requestable' )) {
$update_array [ 'requestable' ] = e ( Input :: get ( 'requestable' ));
}
if ( DB :: table ( 'assets' )
-> where ( 'id' , $key )
-> update ( $update_array )) {
2016-12-19 22:00:50 -08:00
$logAction = new Actionlog ();
$logAction -> item_type = Asset :: class ;
$logAction -> item_id = $key ;
$logAction -> created_at = date ( " Y-m-d H:i:s " );
2016-03-25 01:18:05 -07:00
if ( Input :: has ( 'rtd_location_id' )) {
2016-12-19 22:00:50 -08:00
$logAction -> location_id = e ( Input :: get ( 'rtd_location_id' ));
2016-03-25 01:18:05 -07:00
}
2016-12-19 22:00:50 -08:00
$logAction -> user_id = Auth :: user () -> id ;
$logAction -> logaction ( 'update' );
2016-03-25 01:18:05 -07:00
}
} // endforeach
2016-04-28 21:06:41 -07:00
return redirect () -> to ( " hardware " ) -> with ( 'success' , trans ( 'admin/hardware/message.update.success' ));
2016-03-25 01:18:05 -07:00
// no values given, nothing to update
}
2016-12-19 22:00:50 -08:00
return redirect () -> to ( " hardware " ) -> with ( 'info' , trans ( 'admin/hardware/message.update.nothing_updated' ));
2016-03-25 01:18:05 -07:00
} // endif
2016-04-28 21:06:41 -07:00
return redirect () -> to ( " hardware " );
2016-03-25 01:18:05 -07:00
}
/**
2016-12-19 22:00:50 -08:00
* Save bulk deleted .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ return View
* @ internal param array $assets
* @ since [ v2 . 0 ]
*/
public function postBulkDelete ()
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'delete' , Asset :: class );
if ( Input :: has ( 'bulk_edit' )) {
2016-03-25 01:18:05 -07:00
$assets = Asset :: find ( Input :: get ( 'bulk_edit' ));
foreach ( $assets as $asset ) {
2016-09-26 22:35:51 -07:00
$update_array [ 'deleted_at' ] = date ( 'Y-m-d H:i:s' );
2016-03-25 01:18:05 -07:00
$update_array [ 'assigned_to' ] = null ;
if ( DB :: table ( 'assets' )
-> where ( 'id' , $asset -> id )
-> update ( $update_array )) {
2016-12-19 22:00:50 -08:00
$logAction = new Actionlog ();
$logAction -> item_type = Asset :: class ;
$logAction -> item_id = $asset -> id ;
$logAction -> created_at = date ( " Y-m-d H:i:s " );
$logAction -> user_id = Auth :: user () -> id ;
$logAction -> logaction ( 'deleted' );
2016-03-25 01:18:05 -07:00
}
} // endforeach
2016-12-19 22:00:50 -08:00
return redirect () -> to ( " hardware " ) -> with ( 'success' , trans ( 'admin/hardware/message.delete.success' ));
2016-03-25 01:18:05 -07:00
// no values given, nothing to update
}
2016-12-19 22:00:50 -08:00
return redirect () -> to ( " hardware " ) -> with ( 'info' , trans ( 'admin/hardware/message.delete.nothing_updated' ));
2016-03-25 01:18:05 -07:00
}
/**
2016-12-19 22:00:50 -08:00
* Generates the JSON used to display the asset listing .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param Request $request
* @ param string $status
* @ return String JSON
* @ since [ v2 . 0 ]
*/
2016-08-25 17:10:09 -07:00
public function getDatatable ( Request $request , $status = null )
2016-03-25 01:18:05 -07:00
{
2016-12-29 08:45:37 -08:00
$this -> authorize ( 'index' , 'App\Models\Asset' );
2016-12-29 08:10:52 -08:00
$assets = Company :: scopeCompanyables ( Asset :: select ( 'assets.*' )) -> with (
'assetLoc' , 'assetstatus' , 'defaultLoc' , 'assetlog' , 'company' ,
'model.category' , 'model.manufacturer' , 'model.fieldset' );
2016-03-25 01:18:05 -07:00
2016-08-25 17:11:52 -07:00
if ( $request -> has ( 'search' )) {
$assets = $assets -> TextSearch ( e ( $request -> get ( 'search' )));
2016-03-25 01:18:05 -07:00
}
2016-12-19 22:00:50 -08:00
$offset = request ( 'offset' , 0 );
$limit = request ( 'limit' , 50 );
2016-03-25 01:18:05 -07:00
2016-08-25 17:11:52 -07:00
if ( $request -> has ( 'order_number' )) {
$assets -> where ( 'order_number' , '=' , e ( $request -> get ( 'order_number' )));
2016-03-25 01:18:05 -07:00
}
switch ( $status ) {
case 'Deleted' :
$assets -> withTrashed () -> Deleted ();
break ;
case 'Pending' :
$assets -> Pending ();
break ;
case 'RTD' :
$assets -> RTD ();
break ;
case 'Undeployable' :
$assets -> Undeployable ();
break ;
case 'Archived' :
$assets -> Archived ();
break ;
case 'Requestable' :
$assets -> RequestableAssets ();
break ;
case 'Deployed' :
$assets -> Deployed ();
break ;
}
2016-08-23 15:51:59 -07:00
if ( $request -> has ( 'status_id' )) {
2016-12-29 14:02:18 -08:00
$assets -> where ( 'status_id' , '=' , e ( $request -> get ( 'status_id' )));
2016-08-23 15:51:59 -07:00
}
2016-03-25 01:18:05 -07:00
$allowed_columns = [
'id' ,
'name' ,
'asset_tag' ,
'serial' ,
'model' ,
2016-06-27 22:32:40 -07:00
'model_number' ,
2016-03-25 01:18:05 -07:00
'last_checkout' ,
'category' ,
2016-05-12 10:38:57 -07:00
'manufacturer' ,
2016-03-25 01:18:05 -07:00
'notes' ,
'expected_checkin' ,
'order_number' ,
'companyName' ,
'location' ,
'image' ,
'status_label' ,
2016-06-15 21:06:17 -07:00
'assigned_to' ,
2016-07-07 02:07:21 -07:00
'created_at' ,
'purchase_date' ,
'purchase_cost'
2016-03-25 01:18:05 -07:00
];
2016-07-07 02:07:21 -07:00
$all_custom_fields = CustomField :: all (); //used as a 'cache' of custom fields throughout this page load
2016-03-25 01:18:05 -07:00
foreach ( $all_custom_fields as $field ) {
$allowed_columns [] = $field -> db_column_name ();
}
2016-08-25 17:11:52 -07:00
$order = $request -> get ( 'order' ) === 'asc' ? 'asc' : 'desc' ;
$sort = in_array ( $request -> get ( 'sort' ), $allowed_columns ) ? $request -> get ( 'sort' ) : 'asset_tag' ;
2016-03-25 01:18:05 -07:00
switch ( $sort ) {
case 'model' :
$assets = $assets -> OrderModels ( $order );
break ;
2016-06-27 22:32:40 -07:00
case 'model_number' :
$assets = $assets -> OrderModelNumber ( $order );
break ;
2016-03-25 01:18:05 -07:00
case 'category' :
$assets = $assets -> OrderCategory ( $order );
break ;
2016-05-12 10:38:57 -07:00
case 'manufacturer' :
$assets = $assets -> OrderManufacturer ( $order );
break ;
2016-03-25 01:18:05 -07:00
case 'companyName' :
$assets = $assets -> OrderCompany ( $order );
break ;
case 'location' :
$assets = $assets -> OrderLocation ( $order );
break ;
case 'status_label' :
$assets = $assets -> OrderStatus ( $order );
break ;
case 'assigned_to' :
$assets = $assets -> OrderAssigned ( $order );
break ;
default :
$assets = $assets -> orderBy ( $sort , $order );
break ;
}
$assetCount = $assets -> count ();
$assets = $assets -> skip ( $offset ) -> take ( $limit ) -> get ();
$rows = array ();
foreach ( $assets as $asset ) {
2016-12-23 17:52:00 -08:00
$row = $asset -> present () -> forDataTable ( $all_custom_fields );
2016-09-27 14:56:05 -07:00
if (( $request -> has ( 'report' )) && ( $request -> get ( 'report' ) == 'true' )) {
$rows [] = Helper :: stripTagsFromJSON ( $row );
} else {
$rows [] = $row ;
}
2016-03-25 01:18:05 -07:00
}
$data = array ( 'total' => $assetCount , 'rows' => $rows );
return $data ;
}
2016-07-29 13:59:50 -07:00
2016-12-29 14:02:18 -08:00
public function getBulkCheckout ()
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'checkout' , Asset :: class );
2016-12-29 14:02:18 -08:00
// Filter out assets that are not deployable.
$assets_list = Company :: scopeCompanyables ( Asset :: RTD () -> get (), 'assets.company_id' ) -> pluck ( 'detailed_name' , 'id' ) -> toArray ();
return View :: make ( 'hardware/bulk-checkout' )
-> with ( 'users_list' , Helper :: usersList ())
-> with ( 'assets_list' , $assets_list );
}
public function postBulkCheckout ( Request $request )
{
$this -> validate ( $request , [
" assigned_to " => 'required'
]);
2016-07-29 13:59:50 -07:00
2016-12-29 14:02:18 -08:00
$user = User :: find ( e ( Input :: get ( 'assigned_to' )));
$admin = Auth :: user ();
$asset_ids = array_filter ( Input :: get ( 'selected_assets' ));
if (( Input :: has ( 'checkout_at' )) && ( Input :: get ( 'checkout_at' ) != date ( " Y-m-d " ))) {
$checkout_at = e ( Input :: get ( 'checkout_at' ));
} else {
$checkout_at = date ( " Y-m-d H:i:s " );
}
if ( Input :: has ( 'expected_checkin' )) {
$expected_checkin = e ( Input :: get ( 'expected_checkin' ));
} else {
$expected_checkin = '' ;
}
$errors = [];
DB :: transaction ( function () use ( $user , $admin , $checkout_at , $expected_checkin , $errors , $asset_ids ) {
foreach ( $asset_ids as $asset_id ) {
$asset = Asset :: find ( $asset_id );
$this -> authorize ( 'checkout' , $asset );
$error = $asset -> checkOutToUser ( $user , $admin , $checkout_at , $expected_checkin , e ( Input :: get ( 'note' )), null );
if ( $error ) {
array_merge_recursive ( $errors , $asset -> getErrors () -> toArray ());
}
}
});
if ( ! $errors ) {
// Redirect to the new asset page
return redirect () -> to ( " hardware " ) -> with ( 'success' , trans ( 'admin/hardware/message.checkout.success' ));
}
// Redirect to the asset management page with error
return redirect () -> to ( " hardware/bulk-checkout " ) -> with ( 'error' , trans ( 'admin/hardware/message.checkout.error' )) -> withErrors ( $errors );
}
2016-03-25 01:18:05 -07:00
}