diff --git a/app/Http/Controllers/Api/AssetMaintenancesController.php b/app/Http/Controllers/Api/AssetMaintenancesController.php index dfd5f976d9..df0a3e30a8 100644 --- a/app/Http/Controllers/Api/AssetMaintenancesController.php +++ b/app/Http/Controllers/Api/AssetMaintenancesController.php @@ -97,6 +97,9 @@ class AssetMaintenancesController extends Controller case 'status_label': $maintenances = $maintenances->OrderStatusName($order); break; + case 'created_by': + $maintenances = $maintenances->OrderByCreatedBy($order); + break; default: $maintenances = $maintenances->orderBy($sort, $order); break; diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php index d6f6ffd81c..d58d3a583f 100644 --- a/app/Http/Controllers/Api/ConsumablesController.php +++ b/app/Http/Controllers/Api/ConsumablesController.php @@ -92,6 +92,9 @@ class ConsumablesController extends Controller case 'supplier': $consumables = $consumables->OrderSupplier($order); break; + case 'created_by': + $consumables = $consumables->OrderByCreatedBy($order); + break; default: // This array is what determines which fields should be allowed to be sorted on ON the table itself. // These must match a column on the consumables table directly. diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php index 41b911e035..db39f987aa 100644 --- a/app/Http/Controllers/Api/LicensesController.php +++ b/app/Http/Controllers/Api/LicensesController.php @@ -117,7 +117,7 @@ class LicensesController extends Controller $licenses = $licenses->leftJoin('companies', 'licenses.company_id', '=', 'companies.id')->orderBy('companies.name', $order); break; case 'created_by': - $licenses = $licenses->OrderCreatedBy($order); + $licenses = $licenses->OrderByCreatedBy($order); break; default: $allowed_columns = diff --git a/app/Http/Transformers/AssetMaintenancesTransformer.php b/app/Http/Transformers/AssetMaintenancesTransformer.php index 88ac447c25..b186558df6 100644 --- a/app/Http/Transformers/AssetMaintenancesTransformer.php +++ b/app/Http/Transformers/AssetMaintenancesTransformer.php @@ -65,6 +65,10 @@ class AssetMaintenancesTransformer 'asset_maintenance_time' => $assetmaintenance->asset_maintenance_time, 'completion_date' => Helper::getFormattedDateObject($assetmaintenance->completion_date, 'date'), 'user_id' => ($assetmaintenance->admin) ? ['id' => $assetmaintenance->admin->id, 'name'=> e($assetmaintenance->admin->getFullNameAttribute())] : null, + 'created_by' => ($assetmaintenance->adminuser) ? [ + 'id' => (int) $assetmaintenance->adminuser->id, + 'name'=> e($assetmaintenance->adminuser->present()->fullName()), + ] : null, 'created_at' => Helper::getFormattedDateObject($assetmaintenance->created_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($assetmaintenance->updated_at, 'datetime'), 'is_warranty'=> $assetmaintenance->is_warranty, diff --git a/app/Models/AssetMaintenance.php b/app/Models/AssetMaintenance.php index 5f66783cbb..ecd05176f3 100644 --- a/app/Models/AssetMaintenance.php +++ b/app/Models/AssetMaintenance.php @@ -174,7 +174,7 @@ class AssetMaintenance extends Model implements ICompanyableChild * @author A. Gianotto * @version v3.0 */ - public function admin() + public function adminuser() { return $this->belongsTo(\App\Models\User::class, 'user_id') ->withTrashed(); @@ -278,4 +278,12 @@ class AssetMaintenance extends Model implements ICompanyableChild ->leftjoin('status_labels as maintained_asset_status', 'maintained_asset_status.id', '=', 'maintained_asset.status_id') ->orderBy('maintained_asset_status.name', $order); } + + /** + * Query builder scope to order on the user that created it + */ + public function scopeOrderByCreatedBy($query, $order) + { + return $query->leftJoin('users as admin_sort', 'asset_maintenances.created_by', '=', 'admin_sort.id')->select('asset_maintenances.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order); + } } diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php index bbd14db8ac..eb0acc8016 100644 --- a/app/Models/Consumable.php +++ b/app/Models/Consumable.php @@ -451,4 +451,9 @@ class Consumable extends SnipeModel { return $query->leftJoin('suppliers', 'consumables.supplier_id', '=', 'suppliers.id')->orderBy('suppliers.name', $order); } + + public function scopeOrderByCreatedBy($query, $order) + { + return $query->leftJoin('users as users_sort', 'consumables.created_by', '=', 'users_sort.id')->select('consumables.*')->orderBy('users_sort.first_name', $order)->orderBy('users_sort.last_name', $order); + } }