From 09f7b3debecc6f5d4f69054bf38a3e8ea89c2304 Mon Sep 17 00:00:00 2001 From: Johnson Yi Date: Fri, 1 Sep 2023 13:12:07 +1000 Subject: [PATCH] Track changes on asset checkin/out --- app/Events/CheckoutableCheckedIn.php | 4 +- app/Events/CheckoutableCheckedOut.php | 4 +- app/Http/Controllers/Api/AssetsController.php | 6 ++- .../Assets/AssetCheckinController.php | 5 ++- app/Listeners/LogListener.php | 4 +- app/Models/Asset.php | 9 +++- app/Models/Loggable.php | 42 ++++++++++++++++--- 7 files changed, 61 insertions(+), 13 deletions(-) diff --git a/app/Events/CheckoutableCheckedIn.php b/app/Events/CheckoutableCheckedIn.php index 9609f7d415..48aed2a64d 100644 --- a/app/Events/CheckoutableCheckedIn.php +++ b/app/Events/CheckoutableCheckedIn.php @@ -15,18 +15,20 @@ class CheckoutableCheckedIn public $checkedInBy; public $note; public $action_date; // Date setted in the hardware.checkin view at the checkin_at input, for the action log + public $originalValues; /** * Create a new event instance. * * @return void */ - public function __construct($checkoutable, $checkedOutTo, User $checkedInBy, $note, $action_date = null) + public function __construct($checkoutable, $checkedOutTo, User $checkedInBy, $note, $action_date = null, $originalValues = []) { $this->checkoutable = $checkoutable; $this->checkedOutTo = $checkedOutTo; $this->checkedInBy = $checkedInBy; $this->note = $note; $this->action_date = $action_date ?? date('Y-m-d'); + $this->originalValues = $originalValues; } } diff --git a/app/Events/CheckoutableCheckedOut.php b/app/Events/CheckoutableCheckedOut.php index 30f70ca0d8..3f215bd3bc 100644 --- a/app/Events/CheckoutableCheckedOut.php +++ b/app/Events/CheckoutableCheckedOut.php @@ -14,17 +14,19 @@ class CheckoutableCheckedOut public $checkedOutTo; public $checkedOutBy; public $note; + public $originalValues; /** * Create a new event instance. * * @return void */ - public function __construct($checkoutable, $checkedOutTo, User $checkedOutBy, $note) + public function __construct($checkoutable, $checkedOutTo, User $checkedOutBy, $note, $originalValues = []) { $this->checkoutable = $checkoutable; $this->checkedOutTo = $checkedOutTo; $this->checkedOutBy = $checkedOutBy; $this->note = $note; + $this->originalValues = $originalValues; } } diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index e8f37d8574..1a47b545cd 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -924,10 +924,14 @@ class AssetsController extends Controller } $checkin_at = $request->filled('checkin_at') ? $request->input('checkin_at').' '. date('H:i:s') : date('Y-m-d H:i:s'); + $originalValues = $asset->getRawOriginal(); + if (($request->filled('checkin_at')) && ($request->get('checkin_at') != date('Y-m-d'))) { + $originalValues['action_date'] = $checkin_at; + } if ($asset->save()) { - event(new CheckoutableCheckedIn($asset, $target, Auth::user(), $request->input('note'), $checkin_at)); + event(new CheckoutableCheckedIn($asset, $target, Auth::user(), $request->input('note'), $checkin_at, $originalValues)); return response()->json(Helper::formatStandardApiResponse('success', ['asset'=> e($asset->asset_tag)], trans('admin/hardware/message.checkin.success'))); } diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php index fd5cee30ea..47f9c79dfd 100644 --- a/app/Http/Controllers/Assets/AssetCheckinController.php +++ b/app/Http/Controllers/Assets/AssetCheckinController.php @@ -108,8 +108,11 @@ class AssetCheckinController extends Controller } } + $originalValues = $asset->getRawOriginal(); + $checkin_at = date('Y-m-d H:i:s'); if (($request->filled('checkin_at')) && ($request->get('checkin_at') != date('Y-m-d'))) { + $originalValues['action_date'] = $checkin_at; $checkin_at = $request->get('checkin_at'); } @@ -132,7 +135,7 @@ class AssetCheckinController extends Controller // Was the asset updated? if ($asset->save()) { - event(new CheckoutableCheckedIn($asset, $target, Auth::user(), $request->input('note'), $checkin_at)); + event(new CheckoutableCheckedIn($asset, $target, Auth::user(), $request->input('note'), $checkin_at, $originalValues)); if ((isset($user)) && ($backto == 'user')) { return redirect()->route('users.show', $user->id)->with('success', trans('admin/hardware/message.checkin.success')); diff --git a/app/Listeners/LogListener.php b/app/Listeners/LogListener.php index d14b674364..2ffee3e3c3 100644 --- a/app/Listeners/LogListener.php +++ b/app/Listeners/LogListener.php @@ -33,7 +33,7 @@ class LogListener */ public function onCheckoutableCheckedIn(CheckoutableCheckedIn $event) { - $event->checkoutable->logCheckin($event->checkedOutTo, $event->note, $event->action_date); + $event->checkoutable->logCheckin($event->checkedOutTo, $event->note, $event->action_date, $event->originalValues); } /** @@ -46,7 +46,7 @@ class LogListener */ public function onCheckoutableCheckedOut(CheckoutableCheckedOut $event) { - $event->checkoutable->logCheckout($event->note, $event->checkedOutTo, $event->checkoutable->last_checkout); + $event->checkoutable->logCheckout($event->note, $event->checkedOutTo, $event->checkoutable->last_checkout, $event->originalValues); } /** diff --git a/app/Models/Asset.php b/app/Models/Asset.php index d95aec51ce..b161f7fc3c 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -332,6 +332,13 @@ class Asset extends Depreciable } } + $originalValues = $this->getRawOriginal(); + + // attempt to detect change in value if different from today's date + if ($checkout_at && strpos($checkout_at, date('Y-m-d')) === false) { + $originalValues['action_date'] = date('Y-m-d H:i:s'); + } + if ($this->save()) { if (is_int($admin)) { $checkedOutBy = User::findOrFail($admin); @@ -340,7 +347,7 @@ class Asset extends Depreciable } else { $checkedOutBy = Auth::user(); } - event(new CheckoutableCheckedOut($this, $target, $checkedOutBy, $note)); + event(new CheckoutableCheckedOut($this, $target, $checkedOutBy, $note, $originalValues)); $this->increment('checkout_counter', 1); diff --git a/app/Models/Loggable.php b/app/Models/Loggable.php index d0bbd10733..e4f8554a39 100644 --- a/app/Models/Loggable.php +++ b/app/Models/Loggable.php @@ -23,7 +23,7 @@ trait Loggable * @since [v3.4] * @return \App\Models\Actionlog */ - public function logCheckout($note, $target, $action_date = null) + public function logCheckout($note, $target, $action_date = null, $originalValues = []) { $log = new Actionlog; $log = $this->determineLogItemType($log); @@ -62,6 +62,23 @@ trait Loggable $log->action_date = date('Y-m-d H:i:s'); } + $changed = []; + $originalValues = array_intersect_key($originalValues, array_flip(['action_date','name','status_id','location_id','expected_checkin'])); + + foreach ($originalValues as $key => $value) { + if ($key == 'action_date' && $value != $action_date) { + $changed[$key]['old'] = $value; + $changed[$key]['new'] = is_string($action_date) ? $action_date : $action_date->format('Y-m-d H:i:s'); + } elseif ($value != $this->getAttributes()[$key]) { + $changed[$key]['old'] = $value; + $changed[$key]['new'] = $this->getAttributes()[$key]; + } + } + + if (!empty($changed)){ + $log->log_meta = json_encode($changed); + } + $log->logaction('checkout'); return $log; @@ -89,7 +106,7 @@ trait Loggable * @since [v3.4] * @return \App\Models\Actionlog */ - public function logCheckin($target, $note, $action_date = null) + public function logCheckin($target, $note, $action_date = null, $originalValues = []) { $settings = Setting::getSettings(); $log = new Actionlog; @@ -114,13 +131,9 @@ trait Loggable } } - $log->location_id = null; $log->note = $note; $log->action_date = $action_date; - if (! $log->action_date) { - $log->action_date = date('Y-m-d H:i:s'); - } if (! $log->action_date) { $log->action_date = date('Y-m-d H:i:s'); @@ -130,6 +143,23 @@ trait Loggable $log->user_id = Auth::user()->id; } + $changed = []; + $originalValues = array_intersect_key($originalValues, array_flip(['action_date','name','status_id','location_id','expected_checkin'])); + + foreach ($originalValues as $key => $value) { + if ($key == 'action_date' && $value != $action_date) { + $changed[$key]['old'] = $value; + $changed[$key]['new'] = is_string($action_date) ? $action_date : $action_date->format('Y-m-d H:i:s'); + } elseif ($value != $this->getAttributes()[$key]) { + $changed[$key]['old'] = $value; + $changed[$key]['new'] = $this->getAttributes()[$key]; + } + } + + if (!empty($changed)){ + $log->log_meta = json_encode($changed); + } + $log->logaction('checkin from'); // $params = [