Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/Http/Controllers/Api/MaintenancesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function index(Request $request) : JsonResponse | array
$this->authorize('view', Asset::class);

$maintenances = Maintenance::select('maintenances.*')
->with('asset', 'asset.model', 'asset.location', 'asset.defaultLoc', 'supplier', 'asset.company', 'asset.assetstatus', 'adminuser');
->with('asset', 'asset.model', 'asset.location', 'asset.defaultLoc', 'supplier', 'asset.company', 'asset.assetstatus', 'adminuser', 'userResponsible');

if ($request->filled('search')) {
$maintenances = $maintenances->TextSearch($request->input('search'));
Expand All @@ -56,6 +56,10 @@ public function index(Request $request) : JsonResponse | array
$maintenances->where('asset_maintenance_type', '=', $request->input('asset_maintenance_type'));
}

if ($request->filled('user_responsible_id')) {
$maintenances->where('user_responsible_id', '=', $request->input('user_responsible_id'));
}


// Make sure the offset and limit are actually integers and do not exceed system limits
$offset = ($request->input('offset') > $maintenances->count()) ? $maintenances->count() : abs($request->input('offset'));
Expand All @@ -78,6 +82,7 @@ public function index(Request $request) : JsonResponse | array
'location',
'is_warranty',
'status_label',
'user_responsible_id'
];

$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/MaintenancesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function store(ImageUploadRequest $request) : RedirectResponse
$maintenance->is_warranty = $request->input('is_warranty');
$maintenance->cost = $request->input('cost');
$maintenance->notes = $request->input('notes');
$maintenance->user_responsible_id = $request->input('user_responsible_id');

// Save the asset maintenance data
$maintenance->asset_id = $asset->id;
Expand Down Expand Up @@ -152,6 +153,7 @@ public function update(ImageUploadRequest $request, Maintenance $maintenance) :
$maintenance->name = $request->input('name');
$maintenance->start_date = $request->input('start_date');
$maintenance->completion_date = $request->input('completion_date');
$maintenance->user_responsible_id = $request->input('user_responsible_id');


// Todo - put this in a getter/setter?
Expand Down
13 changes: 12 additions & 1 deletion app/Http/Transformers/MaintenancesTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function transformMaintenances(Collection $maintenances, $total)
public function transformMaintenance(Maintenance $assetmaintenance)
{
$array = [
'id' => (int) $assetmaintenance->id,
'id' => (int) $assetmaintenance->id,
'asset' => ($assetmaintenance->asset) ? [
'id' => (int) $assetmaintenance->asset->id,
'name'=> ($assetmaintenance->asset->name) ? e($assetmaintenance->asset->name) : null,
Expand Down Expand Up @@ -82,6 +82,17 @@ public function transformMaintenance(Maintenance $assetmaintenance)
'created_at' => Helper::getFormattedDateObject($assetmaintenance->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($assetmaintenance->updated_at, 'datetime'),
'is_warranty'=> $assetmaintenance->is_warranty,
'user_responsible' => ($assetmaintenance->userResponsible) ? [
'id' => (int) $assetmaintenance->userResponsible->id,
'username' => e($assetmaintenance->userResponsible->username),
'name' => e($assetmaintenance->userResponsible->getFullNameAttribute()),
'first_name'=> e($assetmaintenance->userResponsible->first_name),
'last_name'=> ($assetmaintenance->userResponsible->last_name) ? e($assetmaintenance->userResponsible->last_name) : null,
'email'=> ($assetmaintenance->userResponsible->email) ? e($assetmaintenance->userResponsible->email) : null,
'employee_number' => ($assetmaintenance->userResponsible->employee_num) ? e($assetmaintenance->userResponsible->employee_num) : null,
'jobtitle' => $assetmaintenance->userResponsible->jobtitle ? e($assetmaintenance->userResponsible->jobtitle) : null,
'type' => 'user',
] : null,

];

Expand Down
13 changes: 12 additions & 1 deletion app/Models/Maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Maintenance extends SnipeModel implements ICompanyableChild
'completion_date' => 'date_format:Y-m-d|nullable|after_or_equal:start_date',
'notes' => 'string|nullable',
'cost' => 'numeric|nullable|gte:0|max:99999999999999999.99',
'user_responsible_id' => 'nullable|integer'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want a check here to make sure the user's ID exists in the user table.

];


Expand All @@ -57,6 +58,7 @@ class Maintenance extends SnipeModel implements ICompanyableChild
'asset_maintenance_time',
'notes',
'cost',
'user_responsible_id',
];

use Searchable;
Expand Down Expand Up @@ -87,6 +89,7 @@ class Maintenance extends SnipeModel implements ICompanyableChild
'asset.supplier' => ['name'],
'asset.assetstatus' => ['name'],
'supplier' => ['name'],
'user_responsible' => ['first_name', 'last_name'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to refer to the relationship here, no? userResponsible down below.

];

public function getCompanyableParents()
Expand Down Expand Up @@ -185,7 +188,15 @@ public function assetlog()
->orderBy('created_at', 'desc')
->withTrashed();
}


/**
* Get the user responsible for this maintenance.
*
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function userResponsible() {
return $this->belongsTo(\App\Models\User::class, 'user_responsible_id')->withTrashed();
}

/**
* Get the admin who created the maintenance
Expand Down
13 changes: 13 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,19 @@ public function maintenances()
return $this->hasMany(\App\Models\Maintenance::class, 'user_id')->withTrashed();
}

/**
* Establishes the user -> maintenances relationship
*
* This would only be used to return maintenances that this user
* is responsible for.
*
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function maintenanceCausedBy()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this to maintenanceCausedByUser or causedByUser for clarity?

{
return $this->hasMany(\App\Models\Maintenance::class, 'user_responsible_id')->withTrashed();
}

/**
* Establishes the user -> accessories relationship
*
Expand Down
6 changes: 6 additions & 0 deletions app/Presenters/MaintenancesPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public static function dataTableLayout()
'sortable' => true,
'title' => trans('general.location'),
'formatter' => 'locationsLinkObjFormatter',
], [
'field' => 'user_responsible',
'searchable' => 'true',
'sortable' => 'true',
'title' => trans('admin/maintenances/table.user_responsible'),
'formatter' => 'usersLinkObjFormatter',
], [
'field' => 'asset_maintenance_type',
'searchable' => true,
Expand Down
2 changes: 2 additions & 0 deletions database/factories/MaintenanceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Asset;
use App\Models\Maintenance;
use App\Models\Supplier;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class MaintenanceFactory extends Factory
Expand All @@ -31,6 +32,7 @@ public function definition()
'start_date' => $this->faker->date(),
'is_warranty' => $this->faker->boolean(),
'notes' => $this->faker->paragraph(),
'user_responsible_id' => User::factory(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('maintenances', function (Blueprint $table) {
if (!Schema::hasColumn('maintenances', 'user_responsible_id')) {
$table->integer('user_responsible_id')->nullable()->default(null);
}
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('maintenances', function (Blueprint $table) {
$table->dropColumn('user_responsible_id');
});
}
};
3 changes: 2 additions & 1 deletion resources/lang/en-US/admin/maintenances/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
'asset_maintenance_time' => 'Asset Maintenance Time (in days)',
'notes' => 'Notes',
'update' => 'Update Asset Maintenance',
'create' => 'Create Asset Maintenance'
'create' => 'Create Asset Maintenance',
'user_responsible' => 'User responsible',
];
1 change: 1 addition & 0 deletions resources/lang/en-US/admin/maintenances/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
'asset_name' => 'Asset Name',
'is_warranty' => 'Warranty',
'dl_csv' => 'Download CSV',
'user_responsible' => 'User responsible',
];
1 change: 1 addition & 0 deletions resources/lang/en-US/admin/users/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
'all_assigned_list_generation' => 'Generated on:',
'email_user_creds_on_create' => 'Email this user their credentials?',
'department_manager' => 'Department Manager',
'maintenance_caused_by_user' => 'Maintenance caused by user',
];
2 changes: 1 addition & 1 deletion resources/views/maintenances/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@


@include ('partials.forms.edit.maintenance_type')

@include ('partials.forms.edit.user-select', ['fieldname' => 'user_responsible_id', 'translated_name' => trans('admin/maintenances/table.user_responsible')] )
<!-- Start Date -->
<div class="form-group {{ $errors->has('start_date') ? ' has-error' : '' }}">
<label for="start_date" class="col-md-3 control-label">
Expand Down
12 changes: 12 additions & 0 deletions resources/views/maintenances/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@
</div>
</div> <!-- /row -->
@endif
@if ($maintenance->userResponsible)
<div class="row">
<div class="col-md-3">
{{ trans('admin/maintenances/form.user_responsible') }}
</div>
<div class="col-md-9">
<a href="{{ route('users.show', $maintenance->userResponsible->id) }}">
{{ $maintenance->userResponsible->name }}
</a>
</div>
</div> <!-- /row -->
@endif

<div class="row">
<div class="col-md-3">
Expand Down
30 changes: 29 additions & 1 deletion resources/views/users/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,16 @@
</a>
</li>
@endif

<li>
<a href="#maintenancecausedby" data-toggle="tab">
<span class="hidden-lg hidden-md">
<x-icon type="maintenancecausedby" class="fa-2x" />
</span>
<span class="hidden-xs hidden-sm">{{ trans('admin/users/general.maintenance_caused_by_user') }}
{!! ($user->maintenanceCausedBy->count() > 0 ) ? '<span class="badge badge-secondary">'.number_format($user->maintenanceCausedBy->count()).'</span>' : '' !!}
</span>
</a>
</li>

@can('update', $user)
<li class="dropdown pull-right">
Expand Down Expand Up @@ -1113,6 +1122,25 @@ class="table table-striped snipe-table"
</table>

</div>
<div class="tab-pane" id="maintenancecausedby">

<table
data-columns="{{ \App\Presenters\MaintenancesPresenter::dataTableLayout() }}"
data-cookie-id-table="maintenancesTable"
data-side-pagination="server"
data-show-footer="true"
id="maintenancesTable"
data-buttons="maintenanceButtons"
class="table table-striped snipe-table"
data-url="{{route('api.maintenances.index', ['user_responsible_id' => $user->id]) }}"
data-export-options='{
"fileName": "export-maintenances-{{ date('Y-m-d') }}",
"ignoreColumn": ["actions","image","change","checkbox","checkincheckout","icon"]
}'>

</table>

</div>
</div><!-- /consumables-tab -->
</div><!-- /.tab-content -->
</div><!-- nav-tabs-custom -->
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/Maintenances/Api/CreateMaintenanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testCanCreateMaintenance()

Storage::fake('public');
$actor = User::factory()->superuser()->create();
$userResponsible = User::factory()->create();

$asset = Asset::factory()->create();
$supplier = Supplier::factory()->create();
Expand All @@ -43,6 +44,7 @@ public function testCanCreateMaintenance()
'cost' => '100.00',
'image' => UploadedFile::fake()->image('test_image.png'),
'notes' => 'A note',
'user_responsible_id' => $userResponsible->id,
])
->assertOk()
->assertStatus(200);
Expand All @@ -64,6 +66,7 @@ public function testCanCreateMaintenance()
'notes' => 'A note',
'image' => $maintenance->image,
'created_by' => $actor->id,
'user_responsible_id' => $userResponsible->id,
]);

$this->assertHasTheseActionLogs($maintenance, ['create']);
Expand Down
4 changes: 4 additions & 0 deletions tests/Feature/Maintenances/Api/EditMaintenanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function testCanEditMaintenance()
{
Storage::fake('public');
$actor = User::factory()->superuser()->create();
$userResponsible = User::factory()->create();

$supplier = Supplier::factory()->create();
$maintenance = Maintenance::factory()->create();

Expand All @@ -38,6 +40,7 @@ public function testCanEditMaintenance()
'is_warranty' => '1',
'image' => UploadedFile::fake()->image('test_image.png'),
'notes' => 'A note',
'user_responsible_id' => $userResponsible->id,
])
->assertOk();

Expand All @@ -58,6 +61,7 @@ public function testCanEditMaintenance()
'asset_maintenance_time' => '9',
'notes' => 'A note',
'image' => $maintenance->image,
'user_responsible_id' => $userResponsible->id,
]);

$this->assertHasTheseActionLogs($maintenance, ['create', 'update']);
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/Maintenances/Ui/CreateMaintenanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function testCanCreateMaintenance()
{
Storage::fake('public');
$actor = User::factory()->superuser()->create();
$userResponsible = User::factory()->create();
$asset = Asset::factory()->create();
$supplier = Supplier::factory()->create();

Expand All @@ -46,6 +47,7 @@ public function testCanCreateMaintenance()
'cost' => '100.00',
'image' => UploadedFile::fake()->image('test_image.png'),
'notes' => 'A note',
'user_responsible_id' => $userResponsible->id,
])
->assertSessionHasNoErrors()
->assertRedirect(route('maintenances.index'));
Expand All @@ -70,6 +72,7 @@ public function testCanCreateMaintenance()
'cost' => '100.00',
'image' => $maintenance->image,
'created_by' => $actor->id,
'user_responsible_id' => $userResponsible->id,
]);

$this->assertHasTheseActionLogs($maintenance, ['create']);
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/Maintenances/Ui/EditMaintenanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testPageRenders()
public function testCanUpdateMaintenance()
{
$actor = User::factory()->superuser()->create();
$userResponsible = User::factory()->create();
$asset = Asset::factory()->create();
$maintenance = Maintenance::factory()->create(['asset_id' => $asset]);
$supplier = Supplier::factory()->create();
Expand All @@ -38,6 +39,7 @@ public function testCanUpdateMaintenance()
'image' => UploadedFile::fake()->image('test_image.png'),
'cost' => '100.99',
'notes' => 'A note',
'user_responsible_id' => $userResponsible->id,
])
->assertSessionHasNoErrors()
->assertRedirect(route('maintenances.index'));
Expand All @@ -59,6 +61,7 @@ public function testCanUpdateMaintenance()
'asset_maintenance_time' => '9',
'notes' => 'A note',
'cost' => '100.99',
'user_responsible_id' => $userResponsible->id,
]);

$this->assertHasTheseActionLogs($maintenance, ['create', 'update']);
Expand Down