2016-03-25 01:18:05 -07:00
< ? php
2018-07-24 19:35:26 -07:00
namespace App\Http\Controllers\Assets ;
2016-03-25 01:18:05 -07:00
use App\Helpers\Helper ;
2018-07-24 19:35:26 -07:00
use App\Http\Controllers\Controller ;
2018-09-29 21:33:52 -07:00
use App\Http\Requests\ImageUploadRequest ;
2016-03-25 01:18:05 -07:00
use App\Models\Actionlog ;
use App\Models\Asset ;
use App\Models\AssetModel ;
2019-03-13 20:12:03 -07:00
use App\Models\CheckoutRequest ;
2016-03-25 01:18:05 -07:00
use App\Models\Company ;
use App\Models\Location ;
use App\Models\Setting ;
use App\Models\User ;
use Auth ;
2016-12-19 11:04:28 -08:00
use Carbon\Carbon ;
2016-03-25 01:18:05 -07:00
use DB ;
2016-12-19 11:04:28 -08:00
use Gate ;
use Illuminate\Http\Request ;
2019-03-13 20:12:03 -07:00
use Illuminate\Support\Facades\Cache ;
use Illuminate\Support\Facades\Storage ;
2016-03-25 01:18:05 -07:00
use Input ;
2016-12-19 11:04:28 -08:00
use League\Csv\Reader ;
2019-02-08 16:05:56 -08:00
use League\Csv\Statement ;
2016-03-25 01:18:05 -07:00
use Paginator ;
use Redirect ;
use Response ;
use Slack ;
use Str ;
use TCPDF ;
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 ();
}
/**
2018-07-16 20:09:53 -07:00
* 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 ]
2018-07-24 19:35:26 -07:00
* @ param Request $request
2018-07-16 20:09:53 -07:00
* @ return View
2018-07-24 19:35:26 -07:00
* @ throws \Illuminate\Auth\Access\AuthorizationException
2018-07-16 20:09:53 -07:00
*/
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 );
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'company_id' )) {
2017-02-08 08:48:41 -08:00
$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-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 ( 'statuslabel_list' , Helper :: statusLabelList ())
-> with ( 'item' , new Asset )
2017-10-26 21:50:27 -07:00
-> with ( 'statuslabel_types' , Helper :: statusTypeList ());
2016-03-25 01:18:05 -07:00
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'model_id' )) {
2016-12-22 17:08:42 -08:00
$selected_model = AssetModel :: find ( $request -> input ( 'model_id' ));
2016-03-25 01:18:05 -07:00
$view -> with ( 'selected_model' , $selected_model );
}
return $view ;
}
/**
2018-07-16 20:09:53 -07:00
* Validate and process new asset form data .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ since [ v1 . 0 ]
* @ return Redirect
*/
2018-09-29 21:33:52 -07:00
public function store ( ImageUploadRequest $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
2018-10-05 04:34:47 -07:00
// Handle asset tags - there could be one, or potentially many.
// This is only necessary on create, not update, since bulk editing is handled
// differently
$asset_tags = $request -> input ( 'asset_tags' );
2017-10-26 21:50:27 -07:00
2018-10-05 04:34:47 -07:00
$success = false ;
2018-10-05 05:30:13 -07:00
$serials = $request -> input ( 'serials' );
2016-03-25 01:18:05 -07:00
2018-10-05 04:34:47 -07:00
for ( $a = 1 ; $a <= count ( $asset_tags ); $a ++ ) {
2017-11-03 19:41:26 -07:00
2018-10-05 04:34:47 -07:00
$asset = new Asset ();
$asset -> model () -> associate ( AssetModel :: find ( $request -> input ( 'model_id' )));
$asset -> name = $request -> input ( 'name' );
2019-08-14 23:13:23 -07:00
2018-10-05 05:30:13 -07:00
// Check for a corresponding serial
if (( $serials ) && ( array_key_exists ( $a , $serials ))) {
$asset -> serial = $serials [ $a ];
2016-07-10 18:43:00 -07:00
}
2019-08-14 23:13:23 -07:00
if (( $asset_tags ) && ( array_key_exists ( $a , $asset_tags ))) {
$asset -> asset_tag = $asset_tags [ $a ];
2016-07-10 18:43:00 -07:00
}
2016-03-25 01:18:05 -07:00
2018-10-05 04:34:47 -07:00
$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 -> user_id = Auth :: id ();
$asset -> archived = '0' ;
$asset -> physical = '1' ;
$asset -> depreciate = '0' ;
$asset -> status_id = request ( 'status_id' , 0 );
$asset -> warranty_months = request ( 'warranty_months' , null );
$asset -> purchase_cost = Helper :: ParseFloat ( $request -> 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 );
2019-09-03 10:58:51 -07:00
if ( ! empty ( $settings -> audit_interval )) {
$asset -> next_audit_date = Carbon :: now () -> addMonths ( $settings -> audit_interval ) -> toDateString ();
}
2018-10-05 04:34:47 -07:00
if ( $asset -> assigned_to == '' ) {
$asset -> location_id = $request -> input ( 'rtd_location_id' , null );
}
2017-11-03 19:41:26 -07:00
Integrations/develop into master (#7352)
* Fixes #6204 - added email alerts and web/API access to assets due for audits (#6992)
* Added upcoming audit report
TODO: Fid diff/threshold math
* Added route to list overdue / upcoming assets via API
* Controller/API methods for due/overdue audits
We could probably skip this and just handle it via view in the routes…
* Added query scopes for due and overdue audits
* Added audit due console command to kernel
* Added ability to pass audit specs to main API asset search method
* Added audit presenter
* Added bootstrap-tables presenter formatter to display an audit button
* Added gated sidenav items to left nav
* Added audit due/overdue blades
* Cleanup on audit due/overdue console command
* Added language strings for audit views
* Fixed :threshold placeholder
* Removed unused setting variable
* Fixed next audit date math
* Added scope for both overdue and upcoming
* Derp. Wrong version
* Bumped version
(I will release this version officially tomorrow)
* Leave the activated state for users alone in normal LDAP synchronisation. (#6988)
* Fixed #7003 - crash when warranty months or purchase date is null
* Fixed #6956 - viewKeys policy inconsistent (#7009)
* Fixed #6956 - Added additional gates show showing/hiding license keys
* Modified gate to allow user to see licenses if they can create or edit the license as well
* Added API middleware to API routes to enable throttling
TODO: Figure out how to make this costumizable without touching the code
* Import locations from CSV via command line (#7021)
* Added import locations command
* Small fixes to location importer
* Added country, LDAP OU
* Cleaned up comments, added more clarification to what the script does
* Added ability to update groups via API
Fixes [ch9139]
* Bumped version
* Fixed #6883 - remove escaping of fields on LDAP import
* Fixed #6880 - correctly encrypt encrypted fields via the API
* Fixes #5054: LDAP users deactivated for none-ad (#7032)
When using none-AD ldap, users are automatically deactivated every LDAP
sync. This commit changes the behaviour so that if the active flag isn't set,
the users are enabled.
Fixed #5054, at least for 4.X
* Updated packages
- Updating erusev/parsedown (v1.7.2 => 1.7.3): Downloading (100%)
- Updating squizlabs/php_codesniffer (3.4.1 => 3.4.2): Downloading (100%)
- Updating symfony/polyfill-mbstring (v1.10.0 => v1.11.0): Downloading (100%)
- Updating symfony/var-dumper (v3.4.23 => v3.4.27): Downloading (100%)
- Updating league/flysystem (1.0.50 => 1.0.51): Downloading (100%)
- Updating symfony/translation (v3.4.23 => v3.4.27): Downloading (100%)
- Updating nesbot/carbon (1.36.2 => 1.37.1): Downloading (100%)
- Updating symfony/debug (v3.4.23 => v3.4.27): Downloading (100%)
- Updating symfony/console (v3.4.23 => v3.4.27): Downloading (100%)
- Updating symfony/finder (v3.4.23 => v3.4.27): Downloading (100%)
- Updating symfony/polyfill-ctype (v1.10.0 => v1.11.0): Downloading (100%)
- Updating symfony/polyfill-php70 (v1.10.0 => v1.11.0): Downloading (100%)
- Updating symfony/http-foundation (v3.4.23 => v3.4.27): Downloading (100%)
- Updating symfony/event-dispatcher (v3.4.23 => v3.4.27): Downloading (100%)
- Updating symfony/http-kernel (v3.4.23 => v3.4.27): Downloading (100%)
- Updating symfony/process (v3.4.23 => v3.4.27): Downloading (100%)
- Updating symfony/routing (v3.4.23 => v3.4.27): Downloading (100%)
- Updating symfony/polyfill-util (v1.10.0 => v1.11.0): Downloading (100%)
- Updating symfony/polyfill-php56 (v1.10.0 => v1.11.0): Downloading (100%)
- Updating symfony/psr-http-message-bridge (v1.1.1 => v1.1.2): Downloading (failed)
Downloading (100%)
- Updating rollbar/rollbar (v1.7.5 => v1.8.1): Downloading (100%)
- Updating symfony/yaml (v3.4.23 => v3.4.27): Downloading (100%)
- Updating symfony/browser-kit (v3.4.23 => v3.4.27): Downloading (100%)
* Fixed #7044 - API update deleted custom fields if they are not re-presented
* Fixed XSS vulnerability when creating a new categories, etc via modal on create
Same fix as before, because of the weird select2 post-parsing ajax behavior
* Updated email strings
* Fixed #7046 - added user website url back into UI
* Updated language strings
* Bumped version
* Updated packages
* New backups config for spatie
* Removed debugbar service provider (autodiscovery)
* Use laravel v5.5 withCount manual aliases
* Added spatie language files
* Removed old laravel backups config
This config file was renamed in a newer version of spatie laravel-backup
* Set the serialization
* Added the command loader to console kernel
* Renamed fire() to handle()
* Updated withCount to use manual naming
* Updated backup path in backup admin
* Updated travis with new php versions
* Bumped laravel version in readme
* Fixed custom field edit screen
* Fixed baseUrl is undefined error
I literally cannot figure out how this ever worked before.
* Fix for included files in backup
* Bumped version
* Switch has() to filled()
* Change ->has() to ->filled()
* Removed cosole log
* Bumped packages
* Use getReader instead of fetchAssoc for CSV parser
https://csv.thephpleague.com/9.0/upgrading/
* Handle JSON validation errors like 5.4
* Handle JSON validation errors like 5.4
* Handle JSON validation errors like 5.4
* Trying to fix ajax asset validation
This I think gets us closer, but still not handling the validation on the asset properly.
When I do a print_r of the validation in the other items, its looking for an error bag that looks something like this:
```
Illuminate\Support\MessageBag Object
(
[messages:protected] => Array
(
[name] => Array
(
[0] => The name field is required.
)
[seats] => Array
(
[0] => The seats field is required.
)
[category_id] => Array
(
[0] => The category id field is required.
)
)
[format:protected] => :message
)
```
Currently the Assets ajax returns:
```
[2019-05-24 06:52:06] develop.ERROR: array (
'messages' =>
array (
'model_id' =>
array (
0 => 'The model id field is required.',
),
'status_id' =>
array (
0 => 'The status id field is required.',
),
'asset_tag' =>
array (
0 => 'The asset tag field is required.',
),
),
)
```
So not sure why it’s not working.
* Fixed missing asset validation
* Check that a model exists before trying to fiddle with fieldsets
* Tidied up license check
* Removed extra escaping on checkin
* Updated importer to work with newer CSV Reader::getRecords() method
* Fixed field mapping
* Small fix for reordering fields
Fixes Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'order' cannot be null (SQL: insert into `custom_field_custom_fieldset` (`custom_field_id`, `custom_fieldset_id`, `order`, `required`) values (12, 7, , 0)) [ch1151]
This needs revisiting for a more solid fix, especially for data that was already entered bad.
* Fixed bug where sorting by company name in Users API did not work
Fixes [ch9200]
* Removed custom fields from AssignedSearch to prevent confusing data in selectlist
Fixes [ch9193]
* Removed alert-danger from tests
* Fixed missed consumables_count withCount() statement
* Fixed Undefined variable user in $backto if checked out to a non-user
Fixes [ch9194]
* Check for valid model before attempting to access fieldsets
Fixes [ch1249]
* Only build the log upload destination path if there is a matching record
Fixes [ch1232]
* Fixed free_seats_count variable name
(I forgot that Laravel switched camel case to snake case for their old 5.4 withCount variables)
* Only gtry to delete the file if a record is found in the log
* Only try to get fieldset if model is valid
* Fixed more camel-casing -> snake-casing
* Only display the file if the log record can be found
* Fixed casing in sync command
* Updated README
* Derp - typo
* Added link to Atlassian plugin
* More Atlassian clarifications
* Show accessory image on view page
* Increased image size to 800px, added lightboxes
* Fixed #7083 - Removed user_exists constraint on department save
If the user has been deleted, this prevented the department from being successfully saved on edit
* Updated branch in version file
* Dockerfile update to bring us up to php v7.1 for Laravel 5.5 (#7084)
* bump up to php7.1
& change deprecated MAINTAINER to a LABEL so it is visible with `docker inspect`
* AND modapache ><
* 2 updates required to get software-properties+ppa
* Bumped version
* Bumped release again :(
* Missed one
* Fixed #7098 - updated backup config for deleteFile() method
* Fixed #7092 - handle weird port forwarding/port numbers for baseUrl
* Bumped version
* Fixed #7099 - set email to null by default for backup notifications
* Removed old comments
* Fixed #7100 - Check if $user isset on checkin
* Increased throttle to 120 requests per minute
* Added Filipino, corrected order for Spanish variations
* Update language strings
* Bumped hash
* Changed has to filled to fix bulk asset editing
* Bumped point version
* Small fixes for phpleague CSB reader v9
* Improved error checking in locations importer
* Fixed #7145 - rename groups table to permissions_group for mysql 8 reserved word compatibility
* Reduce minimum group name length to 2 (from 3)
eg: IT
* Back in time fix FOR #7145 for new installs on MySQL 8+
* Fixed permission insert
//TODO
Handle this via model
* Possible fix for reporting/admin migration back in time
* Fixed #7164 - change table name to permission_groups
* Fixed LDAP password blanking on save
* fixing previous commit's actual wiping of password (#7183)
replaced Input::fille('ldap_pword') with _filled_. Should be good to go.
https://github.com/snipe/snipe-it/issues/7179
https://github.com/snipe/snipe-it/issues/7169
* Bumped version
* Downgrading rollbar for Laravel 5.5
* Spelling Correction (#7206)
Fixed Spelling for the word reqrite, to be rewrite.
* Fix #6910: Add logic to manipulate the eloquent query. (#7006)
* Added company_id to consumables_users table
* Added logic to manage when a pivot table doesn't have the column company_id trough a join with users
* Remove a migration that tries to fix this problem, but is not longer necessary
* Addresses #7238 - add PWA code to layout
Needs additional UX testing
* Better log message for bad LDAP connection
* Fixed #7186 - has vs filled in User’s API blanking out groups if no group_ids are passed
* Comment clarification on #7186
* Check for valid seat on hardware view
* Added space between footer and custom message
* Cap warranty months to three characters
Filles rollbar 209
* Cap warranty months to 3 on the frontend blade
* Fixed countable() strings on user destroy
* Check that the user has assets and that the aset model is valid
* Bumped hash
* Caps asset warranty to 20 years
* Command to fix custom field unicode conversion differences between PHP versions (#7263)
* Fixes #7252 form request changes (#7272)
* Fixes for #7252 - custom fields not validating / no validaton messages in API w/form requests
* Removed debug info
* More fixes for #7252
This is mostly working as intended, if not yet the way Laravel wants us to do it.
Right now, the API returns correctly, and the form UI will return highlighted errors, with the input filled in ~sometimes~. I’m not sure why it’s only sometimes yet, but this is potentially progress.
* Removed experimental method
* Check for digits_between:0,240 for warranty
* Removed debug code
* Apply fix from PR #7273 to master
* Bumped hash
* Fixed #7250 - permission issue for API fieldsets and fields endpoints
This applies the change from #7294 to master
* Add @mskrip as a contributor
* Fixed #7270 - Checking-in Assets via API Removes the Item's Asset Name
* CORS for api (#7292)
* Added CORS support to API
* Changed order so CORS will still work if throttle hit
* Added APP_CORS_ALLOWED_ORIGINS env option
* Fixed typo
* Clarified header comments
* More clarification
* DIsable CORS allowed origins by default to replicate existing behavior
* Change variable name to be clearer
* Bumped version
* Added condition to deal with fieldname 'rtd_location' which can be tried to be queried in some places and doesn't exist in database (#7317)
* Added comments to the ByFilter query scope for clarity
* Added accessories checkout/checkin API endpoint
* Fixed CVE-2019-10742
https://nvd.nist.gov/vuln/detail/CVE-2019-10742
* Update README.md (#7334)
Add reference to CSV importer.
* Group related variables in .env
* History importer fixes
* Fixes to history importer
2019-08-14 21:48:14 -07:00
// Create the image (if one was chosen.)
if ( $request -> has ( 'image' )) {
2019-08-14 23:13:23 -07:00
$asset = $request -> handleImages ( $asset );
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
2019-05-24 04:48:04 -07:00
if (( $model ) && ( $model -> fieldset )) {
2016-06-22 12:27:41 -07:00
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
}
}
2018-10-11 17:15:09 -07:00
// Validate the asset before saving
if ( $asset -> isValid () && $asset -> save ()) {
2018-10-05 04:34:47 -07:00
if ( request ( 'assigned_user' )) {
$target = User :: find ( request ( 'assigned_user' ));
$location = $target -> location_id ;
} elseif ( request ( 'assigned_asset' )) {
$target = Asset :: find ( request ( 'assigned_asset' ));
$location = $target -> location_id ;
} elseif ( request ( 'assigned_location' )) {
$target = Location :: find ( request ( 'assigned_location' ));
$location = $target -> id ;
}
2017-12-12 03:39:55 -08:00
2018-10-05 04:34:47 -07:00
if ( isset ( $target )) {
$asset -> checkOut ( $target , Auth :: user (), date ( 'Y-m-d H:i:s' ), '' , 'Checked out on asset creation' , e ( $request -> get ( 'name' )), $location );
}
2017-12-12 03:39:55 -08:00
2018-10-05 04:34:47 -07:00
$success = true ;
2017-12-12 03:39:55 -08:00
2017-11-03 19:41:26 -07:00
2016-12-29 08:10:52 -08:00
}
2017-11-03 19:41:26 -07:00
2018-10-05 04:34:47 -07:00
}
if ( $success ) {
2018-07-16 17:44:03 -07:00
$asset -> checkOut ( $target , Auth :: user (), date ( 'Y-m-d H:i:s' ), '' , 'Checked out on asset creation' , e ( $request -> get ( 'name' )), $location );
2016-03-25 01:18:05 -07:00
}
// Redirect to the asset listing page
2018-09-29 21:33:52 -07:00
return redirect () -> route ( 'hardware.index' )
-> with ( 'success' , trans ( 'admin/hardware/message.create.success' ));
2016-03-25 01:18:05 -07:00
}
2018-10-05 04:34:47 -07:00
2018-09-29 21:33:52 -07:00
return redirect () -> back () -> withInput () -> withErrors ( $asset -> getErrors ());
2016-03-25 01:18:05 -07:00
}
/**
2018-07-16 20:09:53 -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 ( '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
}
2018-07-16 17:44:03 -07:00
/**
2018-07-16 20:09:53 -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
*/
2018-07-16 17:44:03 -07:00
public function show ( $assetId = null )
{
$asset = Asset :: withTrashed () -> find ( $assetId );
$this -> authorize ( 'view' , $asset );
$settings = Setting :: getSettings ();
if ( isset ( $asset )) {
$audit_log = Actionlog :: where ( 'action_type' , '=' , 'audit' )
-> where ( 'item_id' , '=' , $assetId )
-> where ( 'item_type' , '=' , Asset :: class )
-> orderBy ( 'created_at' , 'DESC' ) -> first ();
if ( $asset -> location ) {
$use_currency = $asset -> location -> currency ;
} else {
if ( $settings -> default_currency != '' ) {
$use_currency = $settings -> default_currency ;
} else {
$use_currency = trans ( 'general.currency' );
}
}
$qr_code = ( object ) array (
'display' => $settings -> qr_code == '1' ,
'url' => route ( 'qr_code/hardware' , $asset -> id )
);
return view ( 'hardware/view' , compact ( 'asset' , 'qr_code' , 'settings' ))
-> with ( 'use_currency' , $use_currency ) -> with ( 'audit_log' , $audit_log );
}
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
}
2016-03-25 01:18:05 -07:00
/**
2018-07-16 20:09:53 -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
2018-09-29 21:33:52 -07:00
public function update ( ImageUploadRequest $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.
2019-05-23 17:39:50 -07:00
$asset -> requestable = $request -> filled ( '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 );
}
2019-05-23 17:39:50 -07:00
if ( $request -> filled ( 'image_delete' )) {
2018-04-24 02:54:54 -07:00
try {
unlink ( public_path () . '/uploads/assets/' . $asset -> image );
$asset -> image = '' ;
} catch ( \Exception $e ) {
2020-02-04 18:15:01 -08:00
\Log :: debug ( $e );
2018-04-24 02:54:54 -07:00
}
2016-03-25 01:18:05 -07:00
}
// Update the asset data
2018-10-05 07:30:42 -07:00
$asset_tag = $request -> input ( 'asset_tags' );
$serial = $request -> input ( 'serials' );
2016-12-19 22:00:50 -08:00
$asset -> name = $request -> input ( 'name' );
2018-10-05 07:30:42 -07:00
$asset -> serial = $serial [ 1 ];
2016-12-19 22:00:50 -08:00
$asset -> company_id = Company :: getIdForCurrentUser ( $request -> input ( 'company_id' ));
$asset -> model_id = $request -> input ( 'model_id' );
$asset -> order_number = $request -> input ( 'order_number' );
2018-10-05 07:30:42 -07:00
$asset -> asset_tag = $asset_tag [ 1 ];
2016-12-19 22:00:50 -08:00
$asset -> notes = $request -> input ( 'notes' );
2016-03-25 01:18:05 -07:00
$asset -> physical = '1' ;
2018-09-29 21:33:52 -07:00
$asset = $request -> handleImages ( $asset );
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' ));
2019-05-24 15:03:15 -07:00
if (( $model ) && ( $model -> fieldset )) {
2016-06-22 12:27:41 -07:00
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 ()) {
2019-12-04 16:19:25 -08:00
2020-04-20 23:20:34 -07:00
// Update any assigned assets with the new location_id from the parent asset
2019-12-04 16:19:25 -08:00
Asset :: where ( 'assigned_type' , '\\App\\Models\\Asset' ) -> where ( 'assigned_to' , $asset -> id )
-> update ([ 'location_id' => $asset -> location_id ]);
2016-03-25 01:18:05 -07:00
// 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 ());
2018-10-05 07:30:42 -07:00
return redirect () -> route ( " hardware.show " , $assetId )
-> with ( 'success' , trans ( 'admin/hardware/message.update.success' ));
2016-03-25 01:18:05 -07:00
}
2018-10-05 07:30:42 -07:00
2019-12-06 11:07:13 -08:00
return redirect () -> back () -> withInput () -> withErrors ( $asset -> getErrors ());
2016-03-25 01:18:05 -07:00
}
/**
2018-07-16 20:09:53 -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
2018-09-29 21:33:52 -07:00
if ( $asset -> image ) {
try {
Storage :: disk ( 'public' ) -> delete ( 'assets' . '/' . $asset -> image );
} catch ( \Exception $e ) {
\Log :: debug ( $e );
}
}
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
}
2017-11-27 21:18:29 -08:00
2016-03-25 01:18:05 -07:00
/**
2020-03-06 14:55:20 -08:00
* Searches the assets table by tag , and redirects if it finds one .
*
* This is used by the top search box in Snipe - IT , but as of 4.9 . x
* can also be used as a url segment .
*
* https :// yoursnipe . com / hardware / bytag / ? assetTag = foo
*
* OR
*
* https :// yoursnipe . com / hardware / bytag / foo
*
* The latter is useful if you ' re doing home - grown barcodes , or
* some other automation where you don ' t always know the internal ID of
* an asset and don ' t want to query for it .
2016-12-26 15:19:04 -08:00
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
2020-03-06 14:55:20 -08:00
* @ param string $tag
2018-07-16 17:44:03 -07:00
* @ since [ v3 . 0 ]
2016-12-26 15:19:04 -08:00
* @ return Redirect
*/
2020-03-06 14:55:20 -08:00
public function getAssetByTag ( Request $request , $tag = null )
2016-03-25 01:18:05 -07:00
{
2020-03-06 14:55:20 -08:00
2018-07-16 17:44:03 -07:00
$topsearch = ( $request -> get ( 'topsearch' ) == " true " );
2016-03-25 01:18:05 -07:00
2020-03-06 14:55:20 -08:00
// We need this part to determine whether a url query parameter has been passed, OR
// whether it's the url fragment we need to look at
$tag = ( $request -> get ( 'assetTag' )) ? $request -> get ( 'assetTag' ) : $tag ;
if ( ! $asset = Asset :: where ( 'asset_tag' , '=' , $tag ) -> first ()) {
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 ( 'view' , $asset );
2018-07-16 17:44:03 -07:00
return redirect () -> route ( 'hardware.show' , $asset -> id ) -> with ( 'topsearch' , $topsearch );
2016-03-25 01:18:05 -07:00
}
2020-03-06 14:55:20 -08:00
/**
* Searches the assets table by serial , and redirects if it finds one
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param string $serial
* @ since [ v4 . 9.1 ]
* @ return Redirect
*/
public function getAssetBySerial ( Request $request , $serial = null )
{
$serial = ( $request -> get ( 'serial' )) ? $request -> get ( 'serial' ) : $serial ;
if ( ! $asset = Asset :: where ( 'serial' , '=' , $serial ) -> first ()) {
return redirect () -> route ( 'hardware.index' ) -> with ( 'error' , trans ( 'admin/hardware/message.does_not_exist' ));
}
$this -> authorize ( 'view' , $asset );
return redirect () -> route ( 'hardware.show' , $asset -> id );
}
2016-03-25 01:18:05 -07:00
/**
2018-07-16 20:09:53 -07:00
* Return a QR code for the asset
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return Response
*/
2016-03-25 01:18:05 -07:00
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 )) {
2020-02-04 18:15:01 -08:00
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-07 07:15:28 -07:00
2020-02-04 18:15:01 -08:00
try {
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
}
}
2020-02-04 18:15:01 -08:00
$barcode_obj = $barcode -> getBarcodeObj ( $settings -> alt_barcode , $asset -> asset_tag ,( $barcode_width < 300 ? $barcode_width : 300 ), 50 );
2019-09-03 11:02:55 -07:00
/**
* Return a label for an individual asset .
*
* @ author [ L . Swartzendruber ] [ < logan . swartzendruber @ gmail . com >
* @ param int $assetId
* @ return View
*/
2020-02-04 18:15:01 -08:00
file_put_contents ( $barcode_file , $barcode_obj -> getPngData ());
2019-09-03 11:02:55 -07:00
{
if ( isset ( $assetId )) {
$asset = Asset :: find ( $assetId );
2020-02-04 18:15:01 -08:00
return response ( $barcode_obj -> getPngData ()) -> header ( 'Content-type' , 'image/png' );
2020-04-20 23:20:34 -07:00
2019-09-03 11:02:55 -07:00
-> with ( 'settings' , Setting :: getSettings ())
2020-02-04 18:15:01 -08:00
\Log :: debug ( 'This usually happens because the asset tags are of a format that is not compatible with the selected barcode type.' );
$img = file_get_contents ( public_path () . '/uploads/barcodes/invalid_barcode.gif' );
return response ( $img ) -> header ( 'Content-type' , 'image/gif' );
}
2016-08-01 22:56:28 -07:00
}
2016-05-19 19:12:42 -07:00
}
}
2016-03-25 01:18:05 -07:00
/**
2018-07-16 20:09:53 -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
*/
2016-03-25 01:18:05 -07:00
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' )
2018-07-16 20:09:53 -07:00
-> with ( 'statuslabel_list' , Helper :: statusLabelList ())
-> with ( 'statuslabel_types' , Helper :: statusTypeList ())
-> 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 ()
{
2019-02-08 16:05:56 -08:00
$this -> authorize ( 'admin' );
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 )
{
2019-08-13 18:00:21 -07:00
if ( ! $request -> hasFile ( 'user_import_csv' )) {
return back () -> with ( 'error' , 'No file provided. Please select a file for import and try again. ' );
}
2016-08-12 16:01:59 -07:00
if ( ! ini_get ( " auto_detect_line_endings " )) {
ini_set ( " auto_detect_line_endings " , '1' );
}
$csv = Reader :: createFromPath ( Input :: file ( 'user_import_csv' ));
2019-08-13 18:00:21 -07:00
$csv -> setHeaderOffset ( 0 );
2019-05-23 19:09:58 -07:00
$results = $csv -> getRecords ();
2016-08-12 16:01:59 -07:00
$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-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 ]);
2019-08-13 18:15:42 -07:00
$item [ $asset_tag ][ $batch_counter ][ 'checkout_date' ] = Carbon :: parse ( Helper :: array_smart_fetch ( $row , " checkout date " )) -> format ( 'Y-m-d H:i:s' );
$item [ $asset_tag ][ $batch_counter ][ 'checkin_date' ] = Carbon :: parse ( Helper :: array_smart_fetch ( $row , " checkin date " )) -> format ( 'Y-m-d H:i:s' );
\Log :: debug ( $item [ $asset_tag ][ $batch_counter ][ 'checkin_date' ]);
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-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' ];
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 ;
}
}
// 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 (
2018-07-16 20:09:53 -07:00
'item_id' => $asset -> id ,
'item_type' => Asset :: class ,
'user_id' => Auth :: user () -> id ,
'note' => 'Checkout imported by ' . Auth :: user () -> present () -> fullName () . ' from history importer' ,
'target_id' => $item [ $asset_tag ][ $batch_counter ][ 'user_id' ],
'target_type' => User :: class ,
'created_at' => $item [ $asset_tag ][ $batch_counter ][ 'checkout_date' ],
'action_type' => 'checkout' ,
));
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 ))) {
2019-08-13 18:15:42 -07:00
$checkin_date = Carbon :: parse ( $asset_batch [ $next ][ 'checkin_date' ]) -> format ( 'Y-m-d H:i:s' );
2016-08-12 19:03:32 -07:00
$asset_batch [ $x ][ 'real_checkin' ] = $checkin_date ;
2019-08-13 18:15:42 -07:00
\Log :: debug ( $asset_batch [ $next ][ 'checkin_date' ]);
\Log :: debug ( $checkin_date );
2016-08-12 17:10:03 -07:00
Actionlog :: firstOrCreate ( array (
2018-07-16 20:09:53 -07:00
'item_id' => $asset_batch [ $x ][ 'asset_id' ],
'item_type' => Asset :: class ,
'user_id' => Auth :: user () -> id ,
'note' => 'Checkin imported by ' . Auth :: user () -> present () -> fullName () . ' from history importer' ,
'target_id' => null ,
'created_at' => $checkin_date ,
'action_type' => 'checkin'
));
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
}
2019-08-14 22:27:17 -07:00
public function sortByName ( array $recordA , array $recordB ) : int
2019-02-08 16:05:56 -08:00
{
return strcmp ( $recordB [ 'Full Name' ], $recordA [ 'Full Name' ]);
}
2016-03-25 01:18:05 -07:00
/**
2018-07-16 20:09:53 -07:00
* Retore a deleted asset .
*
* @ author [ A . Gianotto ] [ < snipe @ snipe . net > ]
* @ param int $assetId
* @ since [ v1 . 0 ]
* @ return View
*/
2016-03-25 01:18:05 -07:00
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
}
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
2019-05-05 19:32:52 -07:00
public function dueForAudit ()
{
$this -> authorize ( 'audit' , Asset :: class );
return view ( 'hardware/audit-due' );
}
public function overdueForAudit ()
{
$this -> authorize ( 'audit' , Asset :: class );
return view ( 'hardware/audit-overdue' );
}
2017-12-12 02:32:45 -08:00
2018-09-29 21:33:52 -07:00
public function auditStore ( Request $request , $id )
2017-08-25 10:04:19 -07:00
{
$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 );
2018-04-24 02:54:54 -07:00
2017-08-25 18:40:20 -07:00
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 );
2018-04-24 02:54:54 -07:00
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
2018-09-07 05:39:41 -07:00
// Check to see if they checked the box to update the physical location,
// not just note it in the audit notes
if ( $request -> input ( 'update_location' ) == '1' ) {
\Log :: debug ( 'update location in audit' );
$asset -> location_id = $request -> input ( 'location_id' );
}
2018-04-24 02:54:54 -07:00
2017-08-25 10:04:19 -07:00
if ( $asset -> save ()) {
2019-01-24 14:04:06 -08:00
$file_name = '' ;
// Upload an image, if attached
2018-04-24 02:54:54 -07:00
if ( $request -> hasFile ( 'image' )) {
2019-01-24 14:04:06 -08:00
$path = 'private_uploads/audits' ;
if ( ! Storage :: exists ( $path )) Storage :: makeDirectory ( $path , 775 );
$upload = $image = $request -> file ( 'image' );
$ext = $image -> getClientOriginalExtension ();
$file_name = 'audit-' . str_random ( 18 ) . '.' . $ext ;
Storage :: putFileAs ( $path , $upload , $file_name );
2018-04-24 02:54:54 -07:00
}
2018-09-29 21:33:52 -07:00
$asset -> logAudit ( $request -> input ( 'note' ), $request -> input ( 'location_id' ), $file_name );
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
2018-04-04 17:33:02 -07:00
public function getRequestedIndex ( $user_id = null )
2017-11-02 04:21:57 -07:00
{
2018-04-04 17:33:02 -07:00
$requestedItems = CheckoutRequest :: with ( 'user' , 'requestedItem' ) -> whereNull ( 'canceled_at' ) -> with ( 'user' , 'requestedItem' );
if ( $user_id ) {
$requestedItems -> where ( 'user_id' , $user_id ) -> get ();
2017-11-02 04:21:57 -07:00
}
2018-04-04 17:33:02 -07:00
$requestedItems = $requestedItems -> orderBy ( 'created_at' , 'desc' ) -> get ();
2017-11-02 04:21:57 -07:00
return view ( 'hardware/requested' , compact ( 'requestedItems' ));
}
2016-03-25 01:18:05 -07:00
}