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\AssetModel ;
use App\Models\Company ;
use App\Models\CustomField ;
Importer mapping - v1 (#3677)
* Move importer to an inline-template, allows for translations and easier passing of data from laravel to vue.
* Pull the modal out into a dedicated partial, move importer to views/importer.
* Add document of CSV->importer mappings. Reorganize some code.
Progress.
* Add header_row and first_row to imports table, and process upon uploading a file
* Use an expandable table row instead of a modal for import processing. This should allow for field mapping interaction easier.
* Fix import processing after moving method.
* Frontend importer mapping improvements.
Invert display so we show found columns and allow users to select an
importer field to map to. Also implement sample data based on first row
of csv.
* Update select2. Maintain selected items properly.
* Backend support for importing. Only works on the web importer currently. Definitely needs testing and polish.
* We no longer use vue-modal plugin.
* Add a column to track field mappings to the imports table.
* Cleanup/rename methods+refactor
* Save field mappings and import type when attempting an import, and repopulate these values when returning to the page.
* Update debugbar to fix a bug in the debugbar code.
* Fix asset tag detection.
Also rename findMatch to be a bit clearer as to what it does.
Remove logging to file of imports for http imports because
it eats an incredible amouint of memory.
This commit also moves imports out of the hardware namespace and into
their own webcontroller and route prefix, remove dead code from
AssetController as a result.
* Dynamically limit options for select2 based on import type selected, and group them by item type.
* Add user importer.
Still need to implement emailing of passwords to new users, and probably
test a bit more.
This also bumps the memory limit for web imports up as well, I need to
profile memory usage here before too long.
* Query the db to find user matches rather than search the array. Performance is much much better.
* Speed/memory improvements in importers.
Move to querying the db rather than maintaining an array for all
importers. Also only store the id of items when we import, rather than
the full model. It saves a decent amount of memory.
* Remove grouping of items in select2
With the values being set dynamically, the grouping is redundant. It
also caused a regression with automatically guessing/matching field
names. This is starting to get close.
* Remove debug line on every create.
* Switch migration to be text field instead of json field for compatibility with older mysql/mariadb
* Fix asset import regression matching email address.
* Rearrange travis order in attempt to fix null settings.
* Use auth::id instead of fetching it off the user. Fixes a null object reference during seeding.
2017-06-21 16:37:37 -07:00
use App\Models\Import ;
2016-03-25 01:18:05 -07:00
use App\Models\Location ;
use App\Models\Setting ;
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\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 ;
2016-12-19 22:00:50 -08:00
use Symfony\Component\HttpFoundation\File\Exception\FileException ;
2016-03-25 01:18:05 -07:00
use TCPDF ;
2016-12-19 11:04:28 -08:00
use Validator ;
2016-03-25 01:18:05 -07:00
use View ;
2017-11-02 04:21:57 -07:00
use App\Models\CheckoutRequest ;
2016-03-25 01:18:05 -07:00
/**
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
*/
2017-02-08 08:48:41 -08:00
public function index ( Request $request )
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'index' , Asset :: class );
2017-02-08 08:48:41 -08:00
if ( $request -> has ( 'company_id' )) {
$company = Company :: find ( $request -> input ( 'company_id' ));
} else {
$company = null ;
}
2017-10-01 12:57:04 -07:00
return view ( 'hardware/index' ) -> with ( 'company' , $company );
2016-03-25 01:18:05 -07:00
}
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 ( 'model_list' , Helper :: modelList ())
-> with ( 'statuslabel_list' , Helper :: statusLabelList ())
-> with ( 'item' , new Asset )
2017-08-24 22:24:02 -07:00
-> with ( 'manufacturer' , Helper :: manufacturerList ()) //handled in modal now?
-> with ( 'category' , Helper :: categoryList ( 'asset' )) //handled in modal now?
2017-10-26 21:50:27 -07:00
-> with ( 'statuslabel_types' , Helper :: statusTypeList ());
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 );
}
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 );
2017-10-26 21:50:27 -07:00
2016-03-25 01:18:05 -07:00
$asset = new Asset ();
2017-10-26 21:50:27 -07:00
$asset -> model () -> associate ( AssetModel :: find ( $request -> input ( 'model_id' )));
$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 -> notes = $request -> input ( 'notes' );
$asset -> asset_tag = $request -> input ( 'asset_tag' );
2016-12-19 22:00:50 -08:00
$asset -> user_id = Auth :: id ();
2016-12-26 15:17:46 -08:00
$asset -> archived = '0' ;
$asset -> physical = '1' ;
$asset -> depreciate = '0' ;
2017-03-11 14:04:52 -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
2017-11-03 19:41:26 -07:00
if ( $asset -> assigned_to == '' ) {
$asset -> location_id = $request -> input ( 'rtd_location_id' , null );
}
2016-03-25 01:18:05 -07:00
// Create the image (if one was chosen.)
2017-10-26 21:50:27 -07:00
if ( $request -> has ( 'image' )) {
$image = $request -> input ( '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
}
2017-01-25 18:38:20 -08:00
2016-06-10 16:36:46 -07:00
// Update custom fields in the database.
2017-01-25 18:38:20 -08:00
// Validation for these fields is handled through the AssetRequest form request
2016-06-10 16:36:46 -07:00
$model = AssetModel :: find ( $request -> get ( 'model_id' ));
2017-07-08 17:04:24 -07:00
2016-06-22 12:27:41 -07:00
if ( $model -> fieldset ) {
foreach ( $model -> fieldset -> fields as $field ) {
2017-07-08 17:04:24 -07:00
if ( $field -> field_encrypted == '1' ) {
if ( Gate :: allows ( 'admin' )) {
$asset -> { $field -> convertUnicodeDbSlug ()} = \Crypt :: encrypt ( $request -> input ( $field -> convertUnicodeDbSlug ()));
}
} else {
$asset -> { $field -> convertUnicodeDbSlug ()} = $request -> input ( $field -> convertUnicodeDbSlug ());
}
2016-06-10 16:36:46 -07:00
}
}
2016-03-25 01:18:05 -07:00
// Was the asset created?
if ( $asset -> save ()) {
2017-12-12 03:39:55 -08:00
2017-11-03 19:41:26 -07:00
2017-10-01 12:57:04 -07:00
if ( request ( 'assigned_user' )) {
2016-12-29 08:10:52 -08:00
$target = User :: find ( request ( 'assigned_user' ));
2017-11-03 19:41:26 -07:00
$location = $target -> location_id ;
2017-10-01 12:57:04 -07:00
} elseif ( request ( 'assigned_asset' )) {
2016-12-29 08:10:52 -08:00
$target = Asset :: find ( request ( 'assigned_asset' ));
2017-11-03 19:41:26 -07:00
$location = $target -> location_id ;
2017-10-01 12:57:04 -07:00
} elseif ( request ( 'assigned_location' )) {
2016-12-29 08:10:52 -08:00
$target = Location :: find ( request ( 'assigned_location' ));
2017-11-03 19:41:26 -07:00
$location = $target -> id ;
2016-12-29 08:10:52 -08:00
}
2017-11-03 19:41:26 -07:00
2017-03-11 05:17:02 -08:00
if ( isset ( $target )) {
2017-11-03 19:41:26 -07:00
$asset -> checkOut ( $target , Auth :: user (), date ( 'Y-m-d H:i:s' ), '' , 'Checked out on asset creation' , e ( Input :: get ( 'name' )), $location );
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
2017-06-09 16:31:25 -07:00
return view ( 'hardware/edit' , compact ( 'item' ))
2017-10-01 12:57:04 -07:00
-> with ( 'model_list' , Helper :: modelList ())
-> with ( 'statuslabel_list' , Helper :: statusLabelList ())
2017-10-28 11:17:52 -07:00
-> with ( 'statuslabel_types' , Helper :: statusTypeList ());
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
{
// 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
2017-11-03 17:36:18 -07:00
if ( $asset -> assigned_to == '' ) {
$asset -> location_id = $request -> input ( 'rtd_location_id' , null );
}
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' )) {
2017-01-25 18:38:20 -08:00
$asset -> { $field -> convertUnicodeDbSlug ()} = \Crypt :: encrypt ( e ( $request -> input ( $field -> convertUnicodeDbSlug ())));
2016-08-25 20:59:54 -07:00
}
} else {
2017-07-08 17:04:24 -07:00
$asset -> { $field -> convertUnicodeDbSlug ()} = $request -> input ( $field -> convertUnicodeDbSlug ());
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' ));
2017-09-28 16:02:50 -07: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' )
2017-10-01 12:57:04 -07:00
-> where ( 'id' , $asset -> id )
-> update ( array ( 'assigned_to' => null ));
2016-03-25 01:18:05 -07:00
$asset -> delete ();
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
2017-11-27 21:18:29 -08:00
if ( $asset -> availableForCheckout ()) {
return view ( 'hardware/checkout' , compact ( 'asset' ));
}
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.checkout.not_available' ));
2016-03-25 01:18:05 -07:00
// Get the dropdown of users and then pass it to the checkout view
2017-11-27 21:18:29 -08:00
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 );
2017-11-21 15:58:31 -08:00
$admin = Auth :: user ();
2017-11-27 21:19:50 -08:00
2017-11-21 15:58:31 -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 : '' ;
2017-11-27 21:19:50 -08:00
2017-11-21 15:58:31 -08:00
} elseif ( request ( 'checkout_to_type' ) == 'asset' ) {
2017-10-19 15:51:55 -07:00
$target = Asset :: where ( 'id' , '!=' , $assetId ) -> find ( request ( 'assigned_asset' ));
2017-11-07 17:37:08 -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 : '' ;
}
2017-11-27 21:19:50 -08:00
} elseif ( request ( 'checkout_to_type' ) == 'user' ) {
2017-11-21 15:58:31 -08:00
// Fetch the target and set the asset's new location_id
$target = User :: find ( request ( 'assigned_user' ));
$asset -> location_id = ( $target ) ? $target -> location_id : '' ;
2016-12-27 16:24:41 -08:00
}
2017-11-07 17:37:08 -08:00
// No valid target was found - error out
if ( ! $target ) {
2017-11-27 21:19:50 -08:00
return redirect () -> back () -> with ( 'error' , trans ( 'admin/hardware/message.checkout.error' )) -> withErrors ( $asset -> getErrors ());
2017-11-07 17:37:08 -08:00
}
2016-03-25 01:18:05 -07:00
2016-04-23 02:34:29 -07:00
if (( Input :: has ( 'checkout_at' )) && ( Input :: get ( 'checkout_at' ) != date ( " Y-m-d " ))) {
2017-07-08 17:04:24 -07:00
$checkout_at = 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' )) {
2017-07-08 17:04:24 -07:00
$expected_checkin = Input :: get ( 'expected_checkin' );
2016-03-25 01:18:05 -07:00
} else {
$expected_checkin = '' ;
}
2017-10-28 07:38:36 -07:00
2017-07-08 17:04:24 -07:00
if ( $asset -> checkOut ( $target , $admin , $checkout_at , $expected_checkin , e ( Input :: get ( 'note' )), Input :: get ( 'name' ))) {
2017-07-17 16:42:39 -07:00
return redirect () -> route ( " hardware.index " ) -> with ( 'success' , trans ( 'admin/hardware/message.checkout.success' ));
2016-03-25 01:18:05 -07:00
}
2017-11-07 17:37:08 -08: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 );
2017-06-09 16:31:25 -07:00
return view ( '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 ();
2017-10-01 12:57:04 -07:00
if ( $asset -> assignedType () == Asset :: USER ) {
2017-09-05 17:54:58 -07:00
$user = $asset -> assignedTo ;
}
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
}
$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 );
2017-09-05 17:54:58 -07:00
$asset -> assigned_type = null ;
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' ));
}
2017-10-28 07:28:35 -07:00
2017-11-03 19:41:26 -07:00
$asset -> location_id = $asset -> rtd_location_id ;
2017-10-28 07:28:35 -07:00
if ( Input :: has ( 'location_id' )) {
$asset -> location_id = e ( Input :: get ( 'location_id' ));
}
2016-03-25 01:18:05 -07:00
// 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 : '' ;
2018-01-24 10:59:34 -08:00
$data [ 'last_name' ] = get_class ( $target ) == User :: class ? $target -> last_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 ;
2017-10-17 13:30:32 -07:00
$data [ 'manufacturer_name' ] = $asset -> model -> manufacturer -> name ;
$data [ 'model_name' ] = $asset -> model -> name ;
$data [ 'model_number' ] = $asset -> model -> model_number ;
2016-03-25 01:18:05 -07:00
2017-08-31 11:14:21 -07:00
if ((( $asset -> checkin_email () == '1' )) && ( isset ( $user )) && ( ! empty ( $user -> email )) && ( ! 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' ) {
2017-10-01 12:57:04 -07:00
return redirect () -> route ( " users.show " , $user -> id ) -> with ( 'success' , trans ( 'admin/hardware/message.checkin.success' ));
2016-03-25 01:18:05 -07:00
}
2017-07-17 16:42:39 -07:00
return redirect () -> route ( " hardware.index " ) -> with ( 'success' , trans ( 'admin/hardware/message.checkin.success' ));
2016-03-25 01:18:05 -07:00
}
// Redirect to the asset management page with error
2017-07-17 16:42:39 -07:00
return redirect () -> route ( " hardware.index " ) -> 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
{
2017-08-25 10:04:19 -07:00
2016-03-25 01:18:05 -07:00
$asset = Asset :: withTrashed () -> find ( $assetId );
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'view' , $asset );
2016-03-25 01:18:05 -07:00
$settings = Setting :: getSettings ();
2017-10-31 05:41:06 -07:00
2016-03-25 01:18:05 -07:00
2017-07-08 03:38:39 -07:00
if ( isset ( $asset )) {
2017-10-31 05:41:06 -07:00
$audit_log = Actionlog :: where ( 'action_type' , '=' , 'audit' )
-> where ( 'item_id' , '=' , $assetId )
-> where ( 'item_type' , '=' , Asset :: class )
-> orderBy ( 'created_at' , 'DESC' ) -> first ();
2017-10-28 02:37:59 -07:00
if ( $asset -> location ) {
$use_currency = $asset -> location -> currency ;
2016-03-25 01:18:05 -07:00
} else {
2017-01-11 23:40:56 -08:00
if ( $settings -> default_currency != '' ) {
$use_currency = $settings -> default_currency ;
} else {
$use_currency = trans ( 'general.currency' );
}
}
2016-03-25 01:18:05 -07:00
$qr_code = ( object ) array (
'display' => $settings -> qr_code == '1' ,
'url' => route ( 'qr_code/hardware' , $asset -> id )
);
2017-08-25 10:04:19 -07:00
return view ( 'hardware/view' , compact ( 'asset' , 'qr_code' , 'settings' ))
2017-10-01 12:57:04 -07:00
-> with ( 'use_currency' , $use_currency ) -> with ( 'audit_log' , $audit_log );
2016-03-25 01:18:05 -07:00
}
2017-10-27 17:38:11 -07:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
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' ) {
2017-10-17 16:21:50 -07:00
$asset = Asset :: withTrashed () -> find ( $assetId );
2017-11-08 01:04:14 -08:00
if ( $asset ) {
$size = Helper :: barcodeDimensions ( $settings -> barcode_type );
$qr_file = public_path () . '/uploads/barcodes/qr-' . str_slug ( $asset -> asset_tag ) . '-' . str_slug ( $asset -> id ) . '.png' ;
if ( isset ( $asset -> id , $asset -> asset_tag )) {
if ( file_exists ( $qr_file )) {
$header = [ 'Content-type' => 'image/png' ];
return response () -> file ( $qr_file , $header );
} else {
$barcode = new \Com\Tecnick\Barcode\Barcode ();
$barcode_obj = $barcode -> getBarcodeObj ( $settings -> barcode_type , route ( 'hardware.show' , $asset -> id ), $size [ 'height' ], $size [ 'width' ], 'black' , array ( - 2 , - 2 , - 2 , - 2 ));
file_put_contents ( $qr_file , $barcode_obj -> getPngData ());
return response ( $barcode_obj -> getPngData ()) -> header ( 'Content-type' , 'image/png' );
}
2016-08-01 22:56:28 -07:00
}
2016-03-25 01:18:05 -07:00
}
2017-11-08 01:04:14 -08:00
return 'That asset is invalid' ;
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
2017-10-01 12:57:04 -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 {
2017-10-19 06:08:01 -07:00
// Calculate barcode width in pixel based on label width (inch)
$barcode_width = ( $settings -> labels_width - $settings -> labels_display_sgutter ) * 96.000000000001 ;
2016-08-01 22:56:28 -07:00
$barcode = new \Com\Tecnick\Barcode\Barcode ();
2017-10-19 06:08:01 -07:00
$barcode_obj = $barcode -> getBarcodeObj ( $settings -> alt_barcode , $asset -> asset_tag ,( $barcode_width < 300 ? $barcode_width : 300 ), 50 );
2017-10-07 07:15:28 -07:00
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
/**
* 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-03-25 01:18:05 -07:00
$asset = clone $asset_to_clone ;
$asset -> id = null ;
$asset -> asset_tag = '' ;
$asset -> serial = '' ;
$asset -> assigned_to = '' ;
2017-06-09 16:31:25 -07:00
return view ( 'hardware/edit' )
2016-12-19 22:00:50 -08:00
-> with ( 'statuslabel_list' , Helper :: statusLabelList ())
-> with ( 'statuslabel_types' , Helper :: statusTypeList ())
2017-10-28 11:17:52 -07:00
-> with ( 'item' , $asset );
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 );
2017-06-09 16:31:25 -07:00
return view ( 'hardware/history' );
2016-08-12 16:01:59 -07:00
}
/**
* 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-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' ] != '' )) {
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
}
2017-06-09 16:31:25 -07:00
return view ( '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 ();
2017-12-12 02:32:45 -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 ( 'restored' );
2017-07-08 17:04:24 -07:00
return redirect () -> route ( 'hardware.index' ) -> 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 )) {
2017-07-08 17:04:24 -07:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-03-25 01:18:05 -07:00
}
2017-10-27 18:01:42 -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
2017-11-06 12:38:31 -08:00
if ( $request -> hasFile ( 'assetfile' )) {
foreach ( $request -> file ( 'assetfile' ) as $file ) {
2016-03-25 01:18:05 -07:00
$extension = $file -> getClientOriginalExtension ();
$filename = 'hardware-' . $asset -> id . '-' . str_random ( 8 );
2016-03-25 03:32:35 -07:00
$filename .= '-' . str_slug ( $file -> getClientOriginalName ()) . '.' . $extension ;
2017-10-27 17:38:11 -07:00
$file -> move ( $destinationPath , $filename );
2016-09-06 19:39:42 -07:00
$asset -> logUpload ( $filename , e ( Input :: get ( 'notes' )));
2016-03-25 01:18:05 -07:00
}
2017-10-27 17:38:11 -07:00
return redirect () -> back () -> with ( 'success' , trans ( 'admin/hardware/message.upload.success' ));
2016-03-25 01:18:05 -07:00
}
2017-10-27 18:01:42 -07:00
return redirect () -> back () -> with ( 'error' , trans ( 'admin/hardware/message.upload.nofiles' ));
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
*/
2017-09-05 17:54:58 -07:00
public function deleteFile ( $assetId = null , $fileId = null )
2016-03-25 01:18:05 -07:00
{
$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
// Redirect to the hardware management page
2017-10-27 17:38:11 -07:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
2016-12-19 22:00:50 -08:00
}
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
2017-07-08 17:04:24 -07:00
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , $error );
2016-12-19 22:00:50 -08:00
}
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 ]
*/
2017-01-18 19:38:42 -08:00
public function postBulkEdit ( Request $request )
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , Asset :: class );
2016-03-25 01:18:05 -07:00
2017-01-18 19:38:42 -08:00
if ( ! $request -> has ( 'ids' )) {
2016-12-19 22:00:50 -08:00
return redirect () -> back () -> with ( 'error' , 'No assets selected' );
2016-03-25 01:18:05 -07:00
}
2017-01-18 19:38:42 -08:00
$asset_raw_array = $request -> input ( 'ids' );
2016-12-19 22:00:50 -08:00
foreach ( $asset_raw_array as $asset_id => $value ) {
$asset_ids [] = $asset_id ;
2016-03-25 01:18:05 -07:00
}
2016-09-21 12:05:06 -07:00
2017-01-18 19:38:42 -08:00
if ( $request -> has ( 'bulk_actions' )) {
if ( $request -> input ( 'bulk_actions' ) == 'labels' ) {
2016-12-19 22:00:50 -08:00
$count = 0 ;
2017-06-09 16:31:25 -07:00
return view ( 'hardware/labels' )
2017-01-18 19:38:42 -08:00
-> with ( 'assets' , Asset :: find ( $asset_ids ))
-> with ( 'settings' , Setting :: getSettings ())
2017-10-28 11:17:52 -07:00
-> with ( 'count' , $count );
2017-01-18 19:38:42 -08:00
} elseif ( $request -> input ( 'bulk_actions' ) == 'delete' ) {
2017-10-28 02:58:38 -07:00
$assets = Asset :: with ( 'assignedTo' , 'location' ) -> find ( $asset_ids );
2017-10-01 12:57:04 -07:00
$assets -> each ( function ( $asset ) {
$this -> authorize ( 'delete' , $asset );
2016-12-19 11:04:28 -08:00
});
2017-06-09 16:31:25 -07:00
return view ( 'hardware/bulk-delete' ) -> with ( 'assets' , $assets );
2016-03-25 01:18:05 -07:00
// Bulk edit
2017-01-18 19:38:42 -08:00
} elseif ( $request -> input ( 'bulk_actions' ) == 'edit' ) {
2017-06-09 16:31:25 -07:00
return view ( 'hardware/bulk' )
2017-01-18 19:38:42 -08:00
-> with ( 'assets' , request ( 'ids' ))
2016-12-19 22:00:50 -08:00
-> with ( 'statuslabel_list' , Helper :: statusLabelList ())
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 ]
*/
2017-11-30 13:32:00 -08:00
public function postBulkSave ( Request $request )
2016-03-25 01:18:05 -07:00
{
2016-12-19 11:04:28 -08:00
$this -> authorize ( 'update' , Asset :: class );
2017-11-30 13:32:00 -08:00
\Log :: debug ( $request -> input ( 'ids' ));
2018-01-24 04:24:45 -08:00
if (( $request -> has ( 'ids' )) && ( count ( $request -> input ( 'ids' )) > 0 )) {
2017-11-30 13:32:00 -08:00
$assets = $request -> input ( 'ids' );
if (( $request -> has ( 'purchase_date' ))
|| ( $request -> has ( 'purchase_cost' ))
|| ( $request -> has ( 'supplier_id' ))
|| ( $request -> has ( 'order_number' ))
|| ( $request -> has ( 'warranty_months' ))
|| ( $request -> has ( 'rtd_location_id' ))
|| ( $request -> has ( 'requestable' ))
|| ( $request -> has ( 'company_id' ))
|| ( $request -> has ( 'status_id' ))
|| ( $request -> has ( 'model_id' ))
2016-12-19 22:00:50 -08:00
) {
2016-03-25 01:18:05 -07:00
foreach ( $assets as $key => $value ) {
$update_array = array ();
2017-11-30 13:32:00 -08:00
if ( $request -> has ( 'purchase_date' )) {
$update_array [ 'purchase_date' ] = $request -> input ( 'purchase_date' );
2016-03-25 01:18:05 -07:00
}
2017-11-30 13:32:00 -08:00
if ( $request -> has ( 'purchase_cost' )) {
2017-11-30 13:40:26 -08:00
$update_array [ 'purchase_cost' ] = Helper :: ParseFloat ( $request -> input ( 'purchase_cost' ));
2016-03-25 01:18:05 -07:00
}
2017-11-30 13:32:00 -08:00
if ( $request -> has ( 'supplier_id' )) {
$update_array [ 'supplier_id' ] = $request -> input ( 'supplier_id' );
2016-03-25 01:18:05 -07:00
}
2017-11-30 13:32:00 -08:00
if ( $request -> has ( 'model_id' )) {
$update_array [ 'model_id' ] = $request -> input ( 'model_id' );
2016-03-25 01:18:05 -07:00
}
2017-11-30 13:32:00 -08:00
if ( $request -> has ( 'company_id' )) {
if ( $request -> input ( 'company_id' ) == " clear " ) {
2016-03-25 01:18:05 -07:00
$update_array [ 'company_id' ] = null ;
} else {
2017-11-30 13:32:00 -08:00
$update_array [ 'company_id' ] = $request -> input ( 'company_id' );
2016-03-25 01:18:05 -07:00
}
}
2017-11-30 13:32:00 -08:00
if ( $request -> has ( 'order_number' )) {
$update_array [ 'order_number' ] = $request -> input ( 'order_number' );
2016-03-25 01:18:05 -07:00
}
2017-11-30 13:32:00 -08:00
if ( $request -> has ( 'warranty_months' )) {
$update_array [ 'warranty_months' ] = $request -> input ( 'warranty_months' );
2016-03-25 01:18:05 -07:00
}
2018-02-02 07:36:40 -08:00
2017-11-30 13:32:00 -08:00
if ( $request -> has ( 'rtd_location_id' )) {
$update_array [ 'rtd_location_id' ] = $request -> input ( 'rtd_location_id' );
2018-01-24 04:38:25 -08:00
if (( $request -> has ( 'update_real_loc' ))
&& (( $request -> input ( 'update_real_loc' )) == '1' ))
{
2018-02-02 07:36:40 -08:00
$update_array [ 'location_id' ] = $request -> input ( 'rtd_location_id' );
2018-01-24 04:38:25 -08:00
}
2016-03-25 01:18:05 -07:00
}
2018-01-24 04:38:25 -08:00
2017-11-30 13:32:00 -08:00
if ( $request -> has ( 'status_id' )) {
$update_array [ 'status_id' ] = $request -> input ( 'status_id' );
2016-03-25 01:18:05 -07:00
}
2017-11-30 13:32:00 -08:00
if ( $request -> has ( 'requestable' )) {
$update_array [ 'requestable' ] = $request -> input ( 'requestable' );
2016-03-25 01:18:05 -07:00
}
2017-06-15 20:54:14 -07:00
DB :: table ( 'assets' )
2017-10-01 12:57:04 -07:00
-> where ( 'id' , $key )
-> update ( $update_array );
2016-03-25 01:18:05 -07:00
} // endforeach
2017-11-30 13:32:00 -08:00
return redirect () -> route ( " hardware.index " ) -> with ( 'success' , trans ( 'admin/hardware/message.update.success' ));
2016-03-25 01:18:05 -07:00
// no values given, nothing to update
}
2017-11-30 13:32:00 -08:00
return redirect () -> route ( " hardware.index " ) -> with ( 'warning' , trans ( 'admin/hardware/message.update.nothing_updated' ));
2016-03-25 01:18:05 -07:00
} // endif
2017-11-30 13:32:00 -08:00
return redirect () -> route ( " hardware.index " ) -> with ( 'warning' , trans ( 'No assets selected, so nothing was updated.' ));
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 );
2017-10-01 12:57:04 -07:00
2017-06-20 14:41:30 -07:00
if ( Input :: has ( 'ids' )) {
$assets = Asset :: find ( Input :: get ( 'ids' ));
2016-03-25 01:18:05 -07:00
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 ;
2017-06-15 20:54:14 -07:00
DB :: table ( 'assets' )
2017-10-01 12:57:04 -07:00
-> where ( 'id' , $asset -> id )
-> update ( $update_array );
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-29 14:02:18 -08:00
public function getBulkCheckout ()
2016-03-25 01:18:05 -07:00
{
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.
2017-10-31 05:41:06 -07:00
2017-06-09 16:31:25 -07:00
return view ( 'hardware/bulk-checkout' )
2017-10-31 05:41:06 -07:00
-> with ( 'users_list' , Helper :: usersList ());
2016-12-29 14:02:18 -08:00
}
2016-03-25 01:18:05 -07:00
2016-12-29 14:02:18 -08:00
public function postBulkCheckout ( Request $request )
{
$this -> validate ( $request , [
" assigned_to " => 'required'
]);
2016-03-25 01:18:05 -07:00
2016-12-29 14:02:18 -08:00
$user = User :: find ( e ( Input :: get ( 'assigned_to' )));
$admin = Auth :: user ();
2016-03-25 01:18:05 -07:00
2018-01-17 12:39:49 -08:00
if ( ! $user ) {
return redirect () -> route ( 'hardware/bulkcheckout' ) -> withInput () -> with ( 'error' , trans ( 'admin/hardware/message.checkout.user_does_not_exist' ));
}
2017-10-18 07:15:16 -07:00
if ( ! is_array ( Input :: get ( 'selected_assets' ))) {
return redirect () -> route ( 'hardware/bulkcheckout' ) -> withInput () -> with ( 'error' , trans ( 'admin/hardware/message.checkout.no_assets_selected' ));
}
2018-01-17 12:39:49 -08:00
2016-12-29 14:02:18 -08:00
$asset_ids = array_filter ( Input :: get ( 'selected_assets' ));
2016-03-25 01:18:05 -07:00
2016-12-29 14:02:18 -08: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 {
2016-12-29 14:02:18 -08:00
$checkout_at = date ( " Y-m-d H:i:s " );
2016-03-25 01:18:05 -07:00
}
2016-12-29 14:02:18 -08:00
if ( Input :: has ( 'expected_checkin' )) {
$expected_checkin = e ( Input :: get ( 'expected_checkin' ));
2016-03-25 01:18:05 -07:00
} else {
2016-12-29 14:02:18 -08:00
$expected_checkin = '' ;
2016-03-25 01:18:05 -07:00
}
2018-01-17 12:39:49 -08:00
2016-12-29 14:02:18 -08:00
$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 );
2017-09-28 22:22:21 -07:00
$error = $asset -> checkOut ( $user , $admin , $checkout_at , $expected_checkin , e ( Input :: get ( 'note' )), null );
2016-03-25 01:18:05 -07:00
2018-01-17 12:39:49 -08:00
if ( $user -> location_id != '' ) {
$asset -> location_id = $user -> location_id ;
$asset -> unsetEventDispatcher ();
$asset -> save ();
}
2016-12-29 14:02:18 -08:00
if ( $error ) {
array_merge_recursive ( $errors , $asset -> getErrors () -> toArray ());
2016-08-02 00:54:38 -07:00
}
2016-03-25 01:18:05 -07:00
}
2016-12-29 14:02:18 -08:00
});
2016-08-18 12:44:55 -07:00
2016-12-29 14:02:18 -08:00
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-08-16 13:02:42 -07:00
2016-03-25 01:18:05 -07:00
2017-10-28 11:17:52 -07:00
public function quickScan ()
2017-08-29 16:00:22 -07:00
{
$this -> authorize ( 'audit' , Asset :: class );
$dt = Carbon :: now () -> addMonths ( 12 ) -> toDateString ();
2017-10-28 11:17:52 -07:00
return view ( 'hardware/quickscan' ) -> with ( 'next_audit_date' , $dt );
2017-08-29 16:00:22 -07:00
}
2016-08-25 21:03:24 -07:00
2017-10-27 17:38:11 -07:00
public function audit ( $id )
2017-08-25 10:04:19 -07:00
{
2017-11-21 20:13:51 -08:00
$settings = Setting :: getSettings ();
2017-08-25 10:04:19 -07:00
$this -> authorize ( 'audit' , Asset :: class );
2017-11-21 20:13:51 -08:00
$dt = Carbon :: now () -> addMonths ( $settings -> audit_interval ) -> toDateString ();
2017-08-25 10:04:19 -07:00
$asset = Asset :: findOrFail ( $id );
2017-10-28 11:17:52 -07:00
return view ( 'hardware/audit' ) -> with ( 'asset' , $asset ) -> with ( 'next_audit_date' , $dt ) -> with ( 'locations_list' );
2017-08-25 10:04:19 -07:00
}
2016-08-18 12:44:55 -07:00
2017-12-12 02:32:45 -08:00
2017-08-25 10:04:19 -07:00
public function auditStore ( Request $request , $id )
{
$this -> authorize ( 'audit' , Asset :: class );
2016-09-27 14:56:05 -07:00
2017-08-25 18:40:20 -07:00
$rules = array (
'location_id' => 'exists:locations,id|nullable|numeric' ,
'next_audit_date' => 'date|nullable'
);
2016-09-27 14:56:05 -07:00
2017-08-25 18:40:20 -07:00
$validator = \Validator :: make ( $request -> all (), $rules );
if ( $validator -> fails ()) {
return response () -> json ( Helper :: formatStandardApiResponse ( 'error' , null , $validator -> errors () -> all ()));
2016-03-25 01:18:05 -07:00
}
2017-08-25 10:04:19 -07:00
$asset = Asset :: findOrFail ( $id );
2017-12-12 02:32:45 -08:00
// We don't want to log this as a normal update, so let's bypass that
$asset -> unsetEventDispatcher ();
2017-08-25 10:04:19 -07:00
$asset -> next_audit_date = $request -> input ( 'next_audit_date' );
2017-12-12 02:32:45 -08:00
$asset -> last_audit_date = date ( 'Y-m-d h:i:s' );
2016-03-25 01:18:05 -07:00
2017-08-25 10:04:19 -07:00
if ( $asset -> save ()) {
2017-10-01 12:57:04 -07:00
$asset -> logAudit ( request ( 'note' ), request ( 'location_id' ));
2017-08-25 10:04:19 -07:00
return redirect () -> to ( " hardware " ) -> with ( 'success' , trans ( 'admin/hardware/message.audit.success' ));
}
2016-03-25 01:18:05 -07:00
}
2017-11-02 04:21:57 -07:00
public function getRequestedIndex ( $id = null )
{
if ( $id ) {
$requestedItems = CheckoutRequest :: where ( 'user_id' , $id ) -> with ( 'user' , 'requestedItem' ) -> get ();
}
$requestedItems = CheckoutRequest :: with ( 'user' , 'requestedItem' ) -> get ();
return view ( 'hardware/requested' , compact ( 'requestedItems' ));
}
2016-03-25 01:18:05 -07:00
}