diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index b2b7f93e7..822a0c58d 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -85,7 +85,7 @@ class AssetsController extends Controller } $assets = Company::scopeCompanyables(Asset::select('assets.*'))->with( - 'assetloc', 'assetstatus', 'defaultLoc', 'assetlog', 'company', + 'location', 'assetstatus', 'assetlog', 'company', 'defaultLoc','assignedTo', 'model.category', 'model.manufacturer', 'model.fieldset','supplier'); @@ -256,12 +256,11 @@ class AssetsController extends Controller */ public function show($id) { - if ($asset = Asset::withTrashed()->find($id)) { + if ($asset = Asset::withTrashed()->findOrFail($id)) { $this->authorize('view', $asset); return (new AssetsTransformer)->transformAsset($asset); } - return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); } diff --git a/app/Http/Controllers/AssetMaintenancesController.php b/app/Http/Controllers/AssetMaintenancesController.php index 8ac153955..f7041f38e 100644 --- a/app/Http/Controllers/AssetMaintenancesController.php +++ b/app/Http/Controllers/AssetMaintenancesController.php @@ -115,8 +115,8 @@ class AssetMaintenancesController extends Controller ); } - if (($maintenance->cost) && (isset($maintenance->asset)) && ($maintenance->asset->assetloc) && ($maintenance->asset->assetloc->currency!='')) { - $maintenance_cost = $maintenance->asset->assetloc->currency.$maintenance->cost; + if (($maintenance->cost) && (isset($maintenance->asset)) && ($maintenance->asset->location) && ($maintenance->asset->location->currency!='')) { + $maintenance_cost = $maintenance->asset->location->currency.$maintenance->cost; } else { $maintenance_cost = $settings->default_currency.$maintenance->cost; } diff --git a/app/Http/Controllers/AssetsController.php b/app/Http/Controllers/AssetsController.php index ed5650201..a58291981 100755 --- a/app/Http/Controllers/AssetsController.php +++ b/app/Http/Controllers/AssetsController.php @@ -594,8 +594,8 @@ class AssetsController extends Controller if (isset($asset)) { - if (!is_null($asset->assetloc)) { - $use_currency = $asset->assetloc->currency; + if ($asset->location) { + $use_currency = $asset->location->currency; } else { if ($settings->default_currency!='') { $use_currency = $settings->default_currency; @@ -1022,7 +1022,7 @@ class AssetsController extends Controller ->with('count', $count) ->with('settings', Setting::getSettings()); } elseif ($request->input('bulk_actions')=='delete') { - $assets = Asset::with('assignedTo', 'assetloc')->find($asset_ids); + $assets = Asset::with('assignedTo', 'location')->find($asset_ids); $assets->each(function ($asset) { $this->authorize('delete', $asset); }); diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index c3740c8c4..d9d2c1aa4 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -114,7 +114,7 @@ class ReportsController extends Controller // Open output stream $handle = fopen('php://output', 'w'); - $assets = Asset::with('assignedTo', 'assetLoc','defaultLoc','assignedTo','model','supplier','assetstatus','model.manufacturer'); + $assets = Asset::with('assignedTo', 'location','defaultLoc','assignedTo','model','supplier','assetstatus','model.manufacturer'); // This is used by the sidenav, mostly switch ($request->input('status')) { @@ -182,7 +182,7 @@ class ReportsController extends Controller ($asset->supplier) ? e($asset->supplier->name) : '', ($asset->assignedTo) ? e($asset->assignedTo->present()->name()) : '', ($asset->last_checkout!='') ? e($asset->last_checkout) : '', - ($asset->assetLoc) ? e($asset->assetLoc->present()->name()) : '', + ($asset->location) ? e($asset->location->present()->name()) : '', ($asset->notes) ? e($asset->notes) : '', ]; foreach ($customfields as $field) { @@ -215,7 +215,7 @@ class ReportsController extends Controller { // Grab all the assets - $assets = Asset::with( 'assignedTo', 'assetstatus', 'defaultLoc', 'assetloc', 'assetlog', 'company', 'model.category', 'model.depreciation') + $assets = Asset::with( 'assignedTo', 'assetstatus', 'defaultLoc', 'location', 'assetlog', 'company', 'model.category', 'model.depreciation') ->orderBy('created_at', 'DESC')->get(); return view('reports/depreciation', compact('assets')); @@ -270,7 +270,7 @@ class ReportsController extends Controller $row[] = ''; // Empty string if unassigned } - if (( $asset->assigned_to > 0 ) && ( $location = $asset->assetLoc )) { + if (( $asset->assigned_to > 0 ) && ( $location = $asset->location )) { if ($location->city) { $row[] = e($location->city) . ', ' . e($location->state); } elseif ($location->name) { @@ -282,8 +282,8 @@ class ReportsController extends Controller $row[] = ''; // Empty string if location is not set } - if ($asset->assetloc) { - $currency = e($asset->assetloc->currency); + if ($asset->location) { + $currency = e($asset->location->currency); } else { $currency = e(Setting::first()->default_currency); } @@ -562,8 +562,8 @@ class ReportsController extends Controller } if (e(Input::get('location')) == '1') { - if($asset->assetLoc) { - $show_loc = $asset->assetLoc->present()->name(); + if($asset->location) { + $show_loc = $asset->location->present()->name(); } else { $show_loc = 'Default location '.$asset->rtd_location_id.' is invalid'; } diff --git a/app/Http/Controllers/ViewAssetsController.php b/app/Http/Controllers/ViewAssetsController.php index 5a7ac1eb9..d913c216c 100755 --- a/app/Http/Controllers/ViewAssetsController.php +++ b/app/Http/Controllers/ViewAssetsController.php @@ -68,7 +68,7 @@ class ViewAssetsController extends Controller public function getRequestableIndex() { - $assets = Asset::with('model', 'defaultLoc', 'assetloc', 'assignedTo', 'requests')->Hardware()->RequestableAssets()->get(); + $assets = Asset::with('model', 'defaultLoc', 'location', 'assignedTo', 'requests')->Hardware()->RequestableAssets()->get(); $models = AssetModel::with('category', 'requests', 'assets')->RequestableModels()->get(); return view('account/requestable-assets', compact('user', 'assets', 'models')); diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index 92ba702a1..04e21df56 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -54,9 +54,9 @@ class AssetsTransformer 'id' => (int) $asset->company->id, 'name'=> e($asset->company->name) ] : null, - 'location' => ($asset->assetLoc) ? [ - 'id' => (int) $asset->assetLoc->id, - 'name'=> e($asset->assetLoc->name) + 'location' => ($asset->location) ? [ + 'id' => (int) $asset->location->id, + 'name'=> e($asset->location->name) ] : null, 'rtd_location' => ($asset->defaultLoc) ? [ 'id' => (int) $asset->defaultLoc->id, @@ -68,6 +68,7 @@ class AssetsTransformer 'warranty_expires' => ($asset->warranty_months > 0) ? Helper::getFormattedDateObject($asset->warranty_expires, 'date') : null, 'created_at' => Helper::getFormattedDateObject($asset->created_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($asset->updated_at, 'datetime'), + 'deleted_at' => Helper::getFormattedDateObject($asset->deleted_at, 'datetime'), 'purchase_date' => Helper::getFormattedDateObject($asset->purchase_date, 'date'), 'last_checkout' => Helper::getFormattedDateObject($asset->last_checkout, 'datetime'), 'expected_checkin' => Helper::getFormattedDateObject($asset->expected_checkin, 'date'), @@ -129,19 +130,19 @@ class AssetsTransformer public function transformAssignedTo($asset) { if ($asset->checkedOutToUser()) { - return $asset->assignedTo ? [ - 'id' => (int) $asset->assignedTo->id, - 'username' => e($asset->assignedTo->username), - 'name' => e($asset->assignedTo->getFullNameAttribute()), - 'first_name'=> e($asset->assignedTo->first_name), - 'last_name'=> e($asset->assignedTo->last_name), - 'employee_number' => e($asset->assignedTo->employee_num), + return $asset->assigned ? [ + 'id' => (int) $asset->assigned->id, + 'username' => e($asset->assigned->username), + 'name' => e($asset->assigned->getFullNameAttribute()), + 'first_name'=> e($asset->assigned->first_name), + 'last_name'=> e($asset->assigned->last_name), + 'employee_number' => e($asset->assigned->employee_num), 'type' => 'user' ] : null; } - return $asset->assignedTo ? [ - 'id' => $asset->assignedTo->id, - 'name' => $asset->assignedTo->display_name, + return $asset->assigned ? [ + 'id' => $asset->assigned->id, + 'name' => $asset->assigned->display_name, 'type' => $asset->assignedType() ] : null; } diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 9446df999..219f1ca0a 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -127,11 +127,10 @@ class Asset extends Depreciable public function availableForCheckout() { - return ( - empty($this->assigned_to) && - $this->assetstatus->deployable == 1 && - empty($this->deleted_at) - ); + if ((empty($this->assigned_to)) && (empty($this->deleted_at)) && ($this->assetstatus->deployable == 1)) { + return true; + } + return false; } /** @@ -255,23 +254,33 @@ class Asset extends Depreciable **/ public function assetLoc() { + static $iterations=0; + static $first_asset; if (!empty($this->assignedType())) { - // dd($this->assignedType()); if ($this->assignedType() == self::ASSET) { - return $this->assignedto->assetloc(); // Recurse until we have a final location + $iterations++; + if(!$first_asset) { + $first_asset=$this; + } + if($iterations>10) { + throw new \Exception("Asset assignment Loop for Asset ID: ".$first_asset->id); + } + $assigned_to=Asset::find($this->assigned_to); //have to do this this way because otherwise it errors + return $assigned_to->assetLoc(); // Recurse until we have a final location } if ($this->assignedType() == self::LOCATION) { - return $this->assignedTo(); + return $this->assignedTo; } if ($this->assignedType() == self::USER) { - if (!$this->assignedTo) { - return $this->defaultLoc(); + if (!$this->assignedTo->userLoc) { + //this makes no sense + return $this->defaultLoc; } - return $this->assignedTo->userLoc(); + return $this->assignedTo->userLoc; } } - return $this->defaultLoc(); + return $this->defaultLoc; } public function assignedType() @@ -407,6 +416,12 @@ class Asset extends Depreciable } + public function location() + { + return $this->belongsTo('\App\Models\Location', 'location_id'); + } + + /** * Get auto-increment diff --git a/app/Models/CheckoutRequest.php b/app/Models/CheckoutRequest.php index 39d046a9f..668f32640 100644 --- a/app/Models/CheckoutRequest.php +++ b/app/Models/CheckoutRequest.php @@ -37,12 +37,6 @@ class CheckoutRequest extends Model public function location() { - if ($this->itemType() == "asset") { - $asset = $this->itemRequested(); - if ($asset->assignedTo) { - return $asset->assetloc; - } - } return $this->itemRequested()->location; } diff --git a/database/factories/AssetFactory.php b/database/factories/AssetFactory.php index 9c4badd7f..112ecf889 100644 --- a/database/factories/AssetFactory.php +++ b/database/factories/AssetFactory.php @@ -16,7 +16,7 @@ use App\Models\Category; $factory->define(Asset::class, function (Faker\Generator $faker) { return [ 'name' => null, - 'rtd_location_id' => 1, + 'rtd_location_id' => rand(1,30), 'serial' => $faker->uuid, 'status_id' => 1, 'user_id' => 1, diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 37e37219f..eb79b53eb 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -2,19 +2,17 @@ use App\Models\Company; +$password = bcrypt('password'); - -$factory->define(App\Models\User::class, function (Faker\Generator $faker) { +$factory->define(App\Models\User::class, function (Faker\Generator $faker) use ($password) { return [ 'first_name' => $faker->firstName, 'last_name' => $faker->lastName, 'username' => $faker->username, - 'password' => bcrypt('password'), + 'password' => $password, 'permissions' => '{"user":"0"}', 'email' => $faker->safeEmail, - 'company_id' => function () { - return factory(App\Models\Company::class)->create()->id; - }, + 'company_id' => rand(1,4), 'locale' => $faker->locale, 'employee_num' => $faker->numberBetween(3500, 35050), 'jobtitle' => $faker->jobTitle, diff --git a/database/migrations/2017_10_27_180947_denorm_asset_locations.php b/database/migrations/2017_10_27_180947_denorm_asset_locations.php new file mode 100644 index 000000000..b6ec3df8e --- /dev/null +++ b/database/migrations/2017_10_27_180947_denorm_asset_locations.php @@ -0,0 +1,35 @@ +integer('location_id')->nullable()->default(null); + }); + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('assets', function (Blueprint $table) { + $table->dropColumn('location_id'); + }); + } +} diff --git a/database/migrations/2017_10_27_192423_migrate_denormed_asset_locations.php b/database/migrations/2017_10_27_192423_migrate_denormed_asset_locations.php new file mode 100644 index 000000000..ab047319f --- /dev/null +++ b/database/migrations/2017_10_27_192423_migrate_denormed_asset_locations.php @@ -0,0 +1,92 @@ +with('defaultLoc')->get(); + \Log::info('Unasigned assets: '); + foreach ($rtd_assets as $rtd_asset) { + \Log::info('Setting asset '.$rtd_asset->id.' to location: '.$rtd_asset->rtd_location_id." Because asset's default location is: ".$rtd_asset->rtd_location_id); + $rtd_asset->location_id=$rtd_asset->rtd_location_id; + $rtd_asset->save(); + } + + // Assigned to users - ::with('assignedTo') //can't eager-load polymorphic relations? + $assigned_user_assets = Asset::where('assigned_type',User::class)->get(); + \Log::debug('User-assigned assets:'); + foreach ($assigned_user_assets as $assigned_user_asset) { + if ($assigned_user_asset->assignedTo->userLoc) { + $new_location=$assigned_user_asset->assignedTo->userloc->id; + \Log::info(' They are in '.$assigned_user_asset->assignedTo->userloc->name.' which is id: '.$new_location); + } else { + \Log::info('They have no location! '); + $new_location=$assigned_user_asset->rtd_location_id; + } + $assigned_user_asset->location_id=$new_location; + $assigned_user_asset->save(); + + } + + // Assigned to locations // with('assetloc')-> //can't eager-load polymorphic relationships + $assigned_location_assets = Asset::where('assigned_type',Location::class)->get(); + \Log::info('Location-assigned assets: '); + foreach ($assigned_location_assets as $assigned_location_asset) { + $assigned_location_asset->location_id=$assigned_location_asset->assignedTo->id; + \Log::info('(calculated to be: '.$assigned_location_asset->assetLoc()); + $assigned_location_asset->save(); + } + + // Assigned to assets + $assigned_asset_assets = Asset::with('assetloc')->where('assigned_type',Asset::class)->get(); + \Log::info('Asset-assigned assets: '); + foreach ($assigned_asset_assets as $assigned_asset_asset) { + \Log::info('This asset is: '.$assigned_asset_asset->assignedTo->asset_tag); + if ($assigned_asset_asset->assignedTo->location) { + \Log::info('They are in '.$assigned_asset_asset->assignedTo->location->name); + } + \Log::info('User location is: '.$assigned_asset_asset->assetloc->name); + \Log::info('Setting asset '.$assigned_asset_asset->id.' location to '.$assigned_asset_asset->assetloc->id.' ('.$assigned_asset_asset->assetloc->name.')'); + $assigned_asset_asset->location_id=$assigned_asset_asset->assetloc->id; + + } + + $unassigned_assets=Asset::whereNull("location_id")->get(); + foreach($unassigned_assets as $unassigned_asset) { + \Log::info('Asset: ".$unassigned_asset->id." still has no location'); + } + + Asset::all()->each(function ($asset) { + if($asset->location_id != $asset->assetLoc()->id) { + \Log::info('MISMATCH MISMATCH '.$asset->id. "doesn't match its location"); + } + }); + + die(); + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/database/seeds/ActionlogSeeder.php b/database/seeds/ActionlogSeeder.php index 258cde3af..e186bf6ab 100644 --- a/database/seeds/ActionlogSeeder.php +++ b/database/seeds/ActionlogSeeder.php @@ -7,7 +7,7 @@ class ActionlogSeeder extends Seeder public function run() { Actionlog::truncate(); - factory(Actionlog::class, 'asset-checkout-user',5)->create(); - factory(Actionlog::class, 'asset-checkout-location',5)->create(); + factory(Actionlog::class, 'asset-checkout-user',300)->create(); + factory(Actionlog::class, 'asset-checkout-location',100)->create(); } } diff --git a/database/seeds/AssetSeeder.php b/database/seeds/AssetSeeder.php index baaa65d3b..63975b5ba 100644 --- a/database/seeds/AssetSeeder.php +++ b/database/seeds/AssetSeeder.php @@ -8,19 +8,19 @@ class AssetSeeder extends Seeder public function run() { Asset::truncate(); - factory(Asset::class, 10)->states('laptop-mbp')->create(); - factory(Asset::class, 5)->states('laptop-mbp-pending')->create(); - factory(Asset::class, 5)->states('laptop-mbp-archived')->create(); - factory(Asset::class, 10)->states('laptop-air')->create(); + factory(Asset::class, 1000)->states('laptop-mbp')->create(); + factory(Asset::class, 50)->states('laptop-mbp-pending')->create(); + factory(Asset::class, 50)->states('laptop-mbp-archived')->create(); + factory(Asset::class, 50)->states('laptop-air')->create(); factory(Asset::class, 5)->states('laptop-surface')->create(); factory(Asset::class, 5)->states('laptop-xps')->create(); factory(Asset::class, 5)->states('laptop-spectre')->create(); factory(Asset::class, 5)->states('laptop-zenbook')->create(); factory(Asset::class, 3)->states('laptop-yoga')->create(); - factory(Asset::class, 3)->states('desktop-macpro')->create(); - factory(Asset::class, 3)->states('desktop-lenovo-i5')->create(); - factory(Asset::class, 10)->states('desktop-optiplex')->create(); + factory(Asset::class, 30)->states('desktop-macpro')->create(); + factory(Asset::class, 30)->states('desktop-lenovo-i5')->create(); + factory(Asset::class, 30)->states('desktop-optiplex')->create(); factory(Asset::class, 5)->states('conf-polycom')->create(); factory(Asset::class, 2)->states('conf-polycomcx')->create(); diff --git a/database/seeds/CustomFieldSeeder.php b/database/seeds/CustomFieldSeeder.php index f497b9406..f6ec934a3 100644 --- a/database/seeds/CustomFieldSeeder.php +++ b/database/seeds/CustomFieldSeeder.php @@ -1,13 +1,27 @@ getColumnListing('assets'); + + + foreach ($columns as $column) { + if(strpos($column, '_snipeit_') !== FALSE) { + + Schema::table('assets', function (Blueprint $table) use ($column) { + $table->dropColumn($column); + }); + } + } CustomField::truncate(); + factory(CustomField::class, 4)->create(); } } diff --git a/database/seeds/LocationSeeder.php b/database/seeds/LocationSeeder.php index 3f0b81181..494f08c88 100644 --- a/database/seeds/LocationSeeder.php +++ b/database/seeds/LocationSeeder.php @@ -8,6 +8,6 @@ class LocationSeeder extends Seeder public function run() { Location::truncate(); - factory(Location::class, 5)->create(); + factory(Location::class, 30)->create(); } } diff --git a/resources/views/hardware/checkin.blade.php b/resources/views/hardware/checkin.blade.php index 3ed81aaab..228119b49 100755 --- a/resources/views/hardware/checkin.blade.php +++ b/resources/views/hardware/checkin.blade.php @@ -64,6 +64,8 @@ + @include ('partials.forms.edit.location-select', ['translated_name' => trans('general.location'), 'fieldname' => 'location_id']) +
{{ Form::label('checkin_at', trans('admin/hardware/form.checkin_date'), array('class' => 'col-md-3 control-label')) }} diff --git a/resources/views/hardware/checkout.blade.php b/resources/views/hardware/checkout.blade.php index c328f1622..49909003a 100755 --- a/resources/views/hardware/checkout.blade.php +++ b/resources/views/hardware/checkout.blade.php @@ -46,9 +46,17 @@
@include ('partials.forms.edit.user-select', ['translated_name' => trans('admin/hardware/form.checkout_to'), 'fieldname' => 'assigned_user']) + @if ($asset->requireAcceptance()) +
- - @if (!$asset->requireAcceptance()) +
+

+ Because this asset category requires acceptance, + it cannot be checked out to another asset or to a location. +

+
+
+ @else @include ('partials.forms.edit.asset-select', ['translated_name' => trans('admin/hardware/form.checkout_to'), 'fieldname' => 'assigned_asset']) @@ -56,6 +64,7 @@ @endif +
{{ Form::label('name', trans('admin/hardware/form.checkout_date'), array('class' => 'col-md-3 control-label')) }} @@ -89,24 +98,26 @@
- @if ($asset->requireAcceptance()) -
-
-

- - {{ trans('admin/categories/general.required_acceptance') }} -

-
-
- @endif + @if ($asset->requireAcceptance() || $asset->getEula()) +
+
+
+ + @if ($asset->requireAcceptance()) + + {{ trans('admin/categories/general.required_acceptance') }} +
+ @endif + + @if ($asset->getEula()) + + {{ trans('admin/categories/general.required_eula') }} + @endif +
+
+
+ @endif - @if ($asset->getEula()) -
-
-

{{ trans('admin/categories/general.required_eula') }}

-
-
- @endif