From c8168700830edc94c1162a0520f96b3798b2b86d Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 12 Dec 2017 03:03:43 -0800 Subject: [PATCH] De-norm last audit date so we can display it in the asset listing --- app/Http/Controllers/Api/AssetsController.php | 6 +++ app/Http/Controllers/ReportsController.php | 16 ++++++ app/Http/Transformers/AssetsTransformer.php | 2 + app/Models/Asset.php | 5 +- app/Presenters/AssetPresenter.php | 14 ++++++ ...010457_normalize_asset_last_audit_date.php | 49 +++++++++++++++++++ resources/views/reports/custom.blade.php | 12 +++++ 7 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2017_12_12_010457_normalize_asset_last_audit_date.php diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 3976d473dd..d41d2c0930 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -72,6 +72,8 @@ class AssetsController extends Controller 'updated_at', 'purchase_date', 'purchase_cost', + 'last_audit_date', + 'next_audit_date', 'warranty_months', ]; @@ -675,7 +677,11 @@ class AssetsController extends Controller if ($asset) { + // We don't want to log this as a normal update, so let's bypass that + $asset->unsetEventDispatcher(); $asset->next_audit_date = $request->input('next_audit_date'); + $asset->last_audit_date = date('Y-m-d h:i:s'); + if ($asset->save()) { $log = $asset->logAudit(request('note'),request('location_id')); return response()->json(Helper::formatStandardApiResponse('success', [ diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index ba93c0da4c..834aa91db7 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -535,6 +535,14 @@ class ReportsController extends Controller $header[] = trans('general.updated_at'); } + if ($request->has('last_audit_date')) { + $header[] = trans('general.last_audit'); + } + + if ($request->has('next_audit_date')) { + $header[] = trans('general.next_audit_date'); + } + if ($request->has('notes')) { $header[] = trans('general.notes'); } @@ -709,6 +717,14 @@ class ReportsController extends Controller $row[] = ($asset->updated_at) ? $asset->updated_at : ''; } + if ($request->has('last_audit_date')) { + $row[] = ($asset->last_audit_date) ? $asset->last_audit_date : ''; + } + + if ($request->has('next_audit_date')) { + $row[] = ($asset->next_audit_date) ? $asset->next_audit_date : ''; + } + if ($request->has('notes')) { $row[] = ($asset->notes) ? $asset->notes : ''; } diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index 0f8109dc1c..c37a534783 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -69,6 +69,8 @@ class AssetsTransformer 'warranty_expires' => ($asset->warranty_months > 0) ? Helper::getFormattedDateObject($asset->warranty_expires, 'date') : null, 'created_at' => Helper::getFormattedDateObject($asset->created_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($asset->updated_at, 'datetime'), + 'last_audit_date' => Helper::getFormattedDateObject($asset->last_audit_date, 'datetime'), + 'next_audit_date' => Helper::getFormattedDateObject($asset->next_audit_date, 'date'), 'deleted_at' => Helper::getFormattedDateObject($asset->deleted_at, 'datetime'), 'purchase_date' => Helper::getFormattedDateObject($asset->purchase_date, 'date'), 'last_checkout' => Helper::getFormattedDateObject($asset->last_checkout, 'datetime'), diff --git a/app/Models/Asset.php b/app/Models/Asset.php index fe35898177..6ecc883a38 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -49,7 +49,9 @@ class Asset extends Depreciable 'deleted_at', 'purchase_date', 'last_checkout', - 'expected_checkin' + 'expected_checkin', + 'last_audit_date', + 'next_audit_date' ]; @@ -68,6 +70,7 @@ class Asset extends Depreciable 'status' => 'integer', 'purchase_cost' => 'numeric|nullable', 'next_audit_date' => 'date|nullable', + 'last_audit_date' => 'date|nullable', ]; /** diff --git a/app/Presenters/AssetPresenter.php b/app/Presenters/AssetPresenter.php index d5b87b8e0d..872e37ffea 100644 --- a/app/Presenters/AssetPresenter.php +++ b/app/Presenters/AssetPresenter.php @@ -201,6 +201,20 @@ class AssetPresenter extends Presenter "visible" => false, "title" => trans('admin/hardware/form.expected_checkin'), "formatter" => "dateDisplayFormatter" + ], [ + "field" => "last_audit_date", + "searchable" => false, + "sortable" => true, + "visible" => false, + "title" => trans('general.last_audit'), + "formatter" => "dateDisplayFormatter" + ], [ + "field" => "next_audit_date", + "searchable" => false, + "sortable" => true, + "visible" => false, + "title" => trans('general.next_audit_date'), + "formatter" => "dateDisplayFormatter" ], ]; diff --git a/database/migrations/2017_12_12_010457_normalize_asset_last_audit_date.php b/database/migrations/2017_12_12_010457_normalize_asset_last_audit_date.php new file mode 100644 index 0000000000..cf80c2faa2 --- /dev/null +++ b/database/migrations/2017_12_12_010457_normalize_asset_last_audit_date.php @@ -0,0 +1,49 @@ +datetime('last_audit_date')->after('assigned_type')->nullable()->default(null); + }); + + // Grab the latest info from the Actionlog table where the action is 'audit' + $audits = Actionlog::selectRaw('MAX(created_at) AS created_at, item_id')->where('action_type', 'audit')->where('item_type', Asset::class)->groupBy('item_id')->orderBy('created_at', 'desc')->get(); + + if ($audits) { + foreach ($audits as $audit) { + $assets = Asset::where('id', $audit->item_id)->first(); + $assets->last_audit_date = $audit->created_at; + $assets->unsetEventDispatcher(); + $assets->save(); + } + } + + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('assets', function (Blueprint $table) { + $table->dropColumn('last_audit_date'); + }); + } +} diff --git a/resources/views/reports/custom.blade.php b/resources/views/reports/custom.blade.php index e2f08be004..2e311b8bfa 100644 --- a/resources/views/reports/custom.blade.php +++ b/resources/views/reports/custom.blade.php @@ -176,6 +176,18 @@ {{ trans('general.updated_at') }} +
+ +
+
+ +