Skip to content

A free open source IT asset/license management system

Fixed #5879 - added user department filter for custom report

snipe65aef11ae361
diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php
index 79d41f7b9c..2c2e466c5f 100644
--- a/app/Http/Controllers/ReportsController.php
+++ b/app/Http/Controllers/ReportsController.php
@@ -500,6 +500,11 @@ class ReportsController extends Controller
$assets->InCategory($request->input('by_category_id'));
}
+ if ($request->filled('by_dept_id')) {
+ \Log::debug('Only users in dept '.$request->input('by_dept_id'));
+ $assets->CheckedOutToTargetInDepartment($request->input('by_dept_id'));
+ }
+
if ($request->filled('by_manufacturer_id')) {
$assets->ByManufacturer($request->input('by_manufacturer_id'));
}
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index 3284d21c08..0a5434cb91 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -1174,6 +1174,26 @@ class Asset extends Depreciable
})->withTrashed()->whereNull("assets.deleted_at"); //workaround for laravel bug
}
+ /**
+ * Query builder scope to search the department ID of users assigned to assets
+ *
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v5.0]
+ * @return string | false
+ *
+ * @return \Illuminate\Database\Query\Builder Modified query builder
+ */
+ public function scopeCheckedOutToTargetInDepartment($query, $search)
+ {
+ return $query->leftJoin('users as assets_dept_users',function ($leftJoin) {
+ $leftJoin->on("assets_dept_users.id", "=", "assets.assigned_to")
+ ->where("assets.assigned_type", "=", User::class);
+ })->where(function ($query) use ($search) {
+ $query->where('assets_dept_users.department_id', '=', $search);
+
+ })->withTrashed()->whereNull("assets.deleted_at"); //workaround for laravel bug
+ }
+
/**
diff --git a/resources/views/reports/custom.blade.php b/resources/views/reports/custom.blade.php
index a53cbce60c..93e9388686 100644
--- a/resources/views/reports/custom.blade.php
+++ b/resources/views/reports/custom.blade.php
@@ -263,6 +263,7 @@
@include ('partials.forms.edit.company-select', ['translated_name' => trans('general.company'), 'fieldname' => 'by_company_id', 'hide_new' => 'true'])
@include ('partials.forms.edit.location-select', ['translated_name' => trans('general.location'), 'fieldname' => 'by_location_id', 'hide_new' => 'true'])
@include ('partials.forms.edit.location-select', ['translated_name' => trans('admin/hardware/form.default_location'), 'fieldname' => 'by_rtd_location_id', 'hide_new' => 'true'])
+ @include ('partials.forms.edit.department-select', ['translated_name' => trans('general.department'), 'fieldname' => 'by_dept_id', 'hide_new' => 'true'])
@include ('partials.forms.edit.supplier-select', ['translated_name' => trans('general.supplier'), 'fieldname' => 'by_supplier_id', 'hide_new' => 'true'])
@include ('partials.forms.edit.model-select', ['translated_name' => trans('general.asset_model'), 'fieldname' => 'by_model_id', 'hide_new' => 'true'])
@include ('partials.forms.edit.manufacturer-select', ['translated_name' => trans('general.manufacturer'), 'fieldname' => 'by_manufacturer_id', 'hide_new' => 'true'])