Merge branch 'features/flatten-locations' into develop

This commit is contained in:
snipe 2017-10-28 03:50:32 -07:00
commit 52f10232a1
22 changed files with 273 additions and 112 deletions

View file

@ -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);
}

View file

@ -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;
}

View file

@ -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);
});

View file

@ -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';
}

View file

@ -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'));

View file

@ -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;
}

View file

@ -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

View file

@ -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;
}

View file

@ -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,

View file

@ -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,

View file

@ -0,0 +1,35 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class DenormAssetLocations extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Add a new effective location ID to flatten out the assets queries
Schema::table('assets', function (Blueprint $table) {
$table->integer('location_id')->nullable()->default(null);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('assets', function (Blueprint $table) {
$table->dropColumn('location_id');
});
}
}

View file

@ -0,0 +1,92 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Models\Asset;
use App\Models\User;
use App\Models\Location;
class MigrateDenormedAssetLocations extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Unassigned
$rtd_assets = Asset::whereNull('assigned_to')->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()
{
//
}
}

View file

@ -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();
}
}

View file

@ -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();

View file

@ -1,13 +1,27 @@
<?php
use Illuminate\Database\Seeder;
use App\Models\CustomField;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
class CustomFieldSeeder extends Seeder
{
public function run()
{
$columns = DB::getSchemaBuilder()->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();
}
}

View file

@ -8,6 +8,6 @@ class LocationSeeder extends Seeder
public function run()
{
Location::truncate();
factory(Location::class, 5)->create();
factory(Location::class, 30)->create();
}
}

View file

@ -64,6 +64,8 @@
</div>
</div>
@include ('partials.forms.edit.location-select', ['translated_name' => trans('general.location'), 'fieldname' => 'location_id'])
<!-- Checkout/Checkin Date -->
<div class="form-group{{ $errors->has('checkin_at') ? ' has-error' : '' }}">
{{ Form::label('checkin_at', trans('admin/hardware/form.checkin_date'), array('class' => 'col-md-3 control-label')) }}

View file

@ -46,9 +46,17 @@
</div>
@include ('partials.forms.edit.user-select', ['translated_name' => trans('admin/hardware/form.checkout_to'), 'fieldname' => 'assigned_user'])
@if ($asset->requireAcceptance())
<div class="form-group">
@if (!$asset->requireAcceptance())
<div class="col-md-8 col-md-offset-3">
<p class="help-block">
Because this asset category requires acceptance,
it cannot be checked out to another asset or to a location.
</p>
</div>
</div>
@else
@include ('partials.forms.edit.asset-select', ['translated_name' => trans('admin/hardware/form.checkout_to'), 'fieldname' => 'assigned_asset'])
@ -56,6 +64,7 @@
@endif
<!-- Checkout/Checkin Date -->
<div class="form-group {{ $errors->has('checkout_at') ? 'error' : '' }}">
{{ Form::label('name', trans('admin/hardware/form.checkout_date'), array('class' => 'col-md-3 control-label')) }}
@ -89,24 +98,26 @@
</div>
</div>
@if ($asset->requireAcceptance())
<div class="form-group">
<div class="col-md-8 col-md-offset-3">
<p class="text-yellow">
<i class="fa fa-warning"></i>
{{ trans('admin/categories/general.required_acceptance') }}
</p>
</div>
</div>
@endif
@if ($asset->requireAcceptance() || $asset->getEula())
<div class="form-group">
<div class="col-md-8 col-md-offset-3">
<div class="callout callout-info">
@if ($asset->requireAcceptance())
<i class="fa fa-envelope"></i>
{{ trans('admin/categories/general.required_acceptance') }}
<br>
@endif
@if ($asset->getEula())
<i class="fa fa-envelope"></i>
{{ trans('admin/categories/general.required_eula') }}
@endif
</div>
</div>
</div>
@endif
@if ($asset->getEula())
<div class="form-group">
<div class="col-md-8 col-md-offset-3">
<p class="text-yellow"><i class="fa fa-warning"></i> {{ trans('admin/categories/general.required_eula') }}</p>
</div>
</div>
@endif
</div> <!--/.box-body-->
<div class="box-footer">
<a class="btn btn-link" href="{{ URL::previous() }}"> {{ trans('button.cancel') }}</a>

View file

@ -345,15 +345,15 @@
{{ $asset->assignedTo->present()->nameUrl() }}
</li>
@if (isset($asset->assetloc->address))
@if (isset($asset->location->address))
<li>
{{ $asset->assetloc->address }}
@if (isset($asset->assetloc->address2))
{{ $asset->assetloc->address2 }}
{{ $asset->location->address }}
@if (isset($asset->location->address2))
{{ $asset->location->address2 }}
@endif
</li>
@if (isset($asset->assetloc->city))
<li>{{ $asset->assetloc->city }}, {{ $asset->assetloc->state }} {{ $asset->assetloc->zip }}</li>
@if (isset($asset->location->city))
<li>{{ $asset->location->city }}, {{ $asset->location->state }} {{ $asset->location->zip }}</li>
@endif
@endif

View file

@ -224,10 +224,10 @@
<tr>
<td>{{ trans('admin/hardware/form.cost') }}</td>
<td>
@if (($asset->id) && ($asset->userloc))
{{ $asset->userloc->currency }}
@elseif (($asset->id) && ($asset->assetloc))
{{ $asset->assetloc->currency }}
@if (($asset->id) && ($asset->location))
{{ $asset->location->currency }}
@elseif (($asset->id) && ($asset->location))
{{ $asset->location->currency }}
@else
{{ $snipeSettings->default_currency }}
@endif
@ -335,16 +335,16 @@
<td> {!! nl2br(e($asset->notes)) !!}</td>
</tr>
@if ($asset->assetloc)
@if ($asset->location)
<tr>
<td>{{ trans('general.location') }}</td>
<td>
@can('superuser')
<a href="{{ route('locations.show', ['location' => $asset->assetloc->id]) }}">
{{ $asset->assetloc->name }}
<a href="{{ route('locations.show', ['location' => $asset->location->id]) }}">
{{ $asset->location->name }}
</a>
@else
{{ $asset->assetloc->name }}
{{ $asset->location->name }}
@endcan
</td>
</tr>

View file

@ -83,8 +83,8 @@
@endif
</td>
<td>
@if ($asset->assetloc)
{{ $asset->assetloc->name }}
@if ($asset->location)
{{ $asset->location->name }}
@elseif ($asset->defaultloc)
{{ $asset->defaultloc->name }}
@endif
@ -98,16 +98,16 @@
@if ($asset->purchase_cost > 0)
<td class="align-right">
@if ($asset->assetloc )
{{ $asset->assetloc->currency }}
@if ($asset->location )
{{ $asset->location->currency }}
@else
{{ $snipeSettings->default_currency }}
@endif
{{ \App\Helpers\Helper::formatCurrencyOutput($asset->purchase_cost) }}
</td>
<td class="align-right">
@if ($asset->assetloc )
{{ $asset->assetloc->currency }}
@if ($asset->location )
{{ $asset->location->currency }}
@else
{{ $snipeSettings->default_currency }}
@endif
@ -115,8 +115,8 @@
{{ \App\Helpers\Helper::formatCurrencyOutput($asset->getDepreciatedValue()) }}
</td>
<td class="align-right">
@if ($asset->assetloc)
{{ $asset->assetloc->currency }}
@if ($asset->location)
{{ $asset->location->currency }}
@else
{{ $snipeSettings->default_currency }}
@endif

View file

@ -66,9 +66,9 @@ class ApiAssetsCest
'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,
@ -223,9 +223,9 @@ class ApiAssetsCest
'id' => (int) $temp_asset->company->id,
'name'=> e($temp_asset->company->name)
] : null,
'location' => ($temp_asset->assetLoc) ? [
'id' => (int) $temp_asset->assetLoc->id,
'name'=> e($temp_asset->assetLoc->name)
'location' => ($temp_asset->location) ? [
'id' => (int) $temp_asset->location->id,
'name'=> e($temp_asset->location->name)
] : null,
'rtd_location' => ($temp_asset->defaultLoc) ? [
'id' => (int) $temp_asset->defaultLoc->id,