Skip to content

Commit

Permalink
Track changes on asset checkin/out
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson-yi committed Sep 1, 2023
1 parent f08cef8 commit ada851c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 13 deletions.
4 changes: 3 additions & 1 deletion app/Events/CheckoutableCheckedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion app/Events/CheckoutableCheckedOut.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 5 additions & 1 deletion app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -924,10 +924,14 @@ public function checkin(Request $request, $asset_id)
}

$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')));
}
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/Assets/AssetCheckinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ public function store(AssetCheckinRequest $request, $assetId = null, $backto = n
}
}

$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');
}

Expand All @@ -132,7 +135,7 @@ function (Builder $query) use ($asset) {

// 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'));
Expand Down
4 changes: 2 additions & 2 deletions app/Listeners/LogListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -46,7 +46,7 @@ public function onCheckoutableCheckedIn(CheckoutableCheckedIn $event)
*/
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);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion app/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ public function checkOut($target, $admin = null, $checkout_at = null, $expected_
}
}

$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);
Expand All @@ -340,7 +347,7 @@ public function checkOut($target, $admin = null, $checkout_at = null, $expected_
} else {
$checkedOutBy = Auth::user();
}
event(new CheckoutableCheckedOut($this, $target, $checkedOutBy, $note));
event(new CheckoutableCheckedOut($this, $target, $checkedOutBy, $note, $originalValues));

$this->increment('checkout_counter', 1);

Expand Down
42 changes: 36 additions & 6 deletions app/Models/Loggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function log()
* @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);
Expand Down Expand Up @@ -62,6 +62,23 @@ public function logCheckout($note, $target, $action_date = null)
$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;
Expand Down Expand Up @@ -89,7 +106,7 @@ private function determineLogItemType($log)
* @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;
Expand All @@ -114,13 +131,9 @@ public function logCheckin($target, $note, $action_date = null)
}
}


$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');
Expand All @@ -130,6 +143,23 @@ public function logCheckin($target, $note, $action_date = null)
$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 = [
Expand Down

0 comments on commit ada851c

Please sign in to comment.