From 65aef11ae36170a4a0565bf6cbe9abd28c8c02aa Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 31 Oct 2018 15:19:13 -0700 Subject: [PATCH] Fixed #5879 - added user department filter for custom report --- app/Http/Controllers/ReportsController.php | 5 +++++ app/Models/Asset.php | 20 ++++++++++++++++++++ resources/views/reports/custom.blade.php | 1 + 3 files changed, 26 insertions(+) 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] [] + * @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'])