Skip to content

A free open source IT asset/license management system

Added additional search and display fields for suppliers and locations

snipe4c7b6d130fb2
diff --git a/app/Http/Controllers/Api/SuppliersController.php b/app/Http/Controllers/Api/SuppliersController.php
index 10efd923b6..ab0db67909 100644
--- a/app/Http/Controllers/Api/SuppliersController.php
+++ b/app/Http/Controllers/Api/SuppliersController.php
@@ -24,10 +24,15 @@ class SuppliersController extends Controller
public function index(Request $request): array
{
$this->authorize('view', Supplier::class);
- $allowed_columns = ['
- id',
+ $allowed_columns = [
+ 'id',
'name',
'address',
+ 'address2',
+ 'city',
+ 'state',
+ 'country',
+ 'zip',
'phone',
'contact',
'fax',
@@ -43,7 +48,7 @@ class SuppliersController extends Controller
];
$suppliers = Supplier::select(
- ['id', 'name', 'address', 'address2', 'city', 'state', 'country', 'fax', 'phone', 'email', 'contact', 'created_at', 'created_by', 'updated_at', 'deleted_at', 'image', 'notes', 'url'])
+ ['id', 'name', 'address', 'address2', 'city', 'state', 'country', 'fax', 'phone', 'email', 'contact', 'created_at', 'created_by', 'updated_at', 'deleted_at', 'image', 'notes', 'url', 'zip'])
->withCount('assets as assets_count')
->withCount('licenses as licenses_count')
->withCount('accessories as accessories_count')
@@ -53,9 +58,10 @@ class SuppliersController extends Controller
if ($request->filled('search')) {
- $suppliers = $suppliers->TextSearch($request->input('search'));
+ $suppliers->TextSearch($request->input('search'));
}
+
if ($request->filled('name')) {
$suppliers->where('name', '=', $request->input('name'));
}
@@ -102,7 +108,15 @@ class SuppliersController extends Controller
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
- $suppliers->orderBy($sort, $order);
+
+ switch ($request->input('sort')) {
+ case 'created_by':
+ $suppliers->OrderByCreatedByName($order);
+ break;
+ default:
+ $suppliers->orderBy($sort, $order);
+ break;
+ }
$total = $suppliers->count();
$suppliers = $suppliers->skip($offset)->take($limit)->get();
diff --git a/app/Models/Location.php b/app/Models/Location.php
index a3b047579d..52236092d3 100755
--- a/app/Models/Location.php
+++ b/app/Models/Location.php
@@ -385,4 +385,13 @@ class Location extends SnipeModel
{
return $query->leftJoin('companies as company_sort', 'locations.company_id', '=', 'company_sort.id')->orderBy('company_sort.name', $order);
}
+
+ /**
+ * Query builder scope to order on the user that created it
+ */
+ public function scopeOrderByCreatedByName($query, $order)
+ {
+ return $query->leftJoin('users as admin_sort', 'locations.created_by', '=', 'admin_sort.id')->select('locations.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
+ }
+
}
diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php
index 1d667f700e..0166229f89 100755
--- a/app/Models/Supplier.php
+++ b/app/Models/Supplier.php
@@ -48,7 +48,7 @@ class Supplier extends SnipeModel
*
* @var array
*/
- protected $searchableAttributes = ['name'];
+ protected $searchableAttributes = ['name', 'notes', 'phone', 'fax', 'url', 'email', 'contact', 'address', 'address2', 'city', 'state', 'country', 'zip'];
/**
* The relations and their attributes that should be included when searching the model.
@@ -209,4 +209,13 @@ class Supplier extends SnipeModel
return $url;
}
+
+ /**
+ * Query builder scope to order on the user that created it
+ */
+ public function scopeOrderByCreatedByName($query, $order)
+ {
+ return $query->leftJoin('users as admin_sort', 'suppliers.created_by', '=', 'admin_sort.id')->select('suppliers.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
+ }
+
}