Merge remote-tracking branch 'origin/develop'

This commit is contained in:
snipe 2023-09-06 09:37:35 +01:00
commit 2a94fd17ee
8 changed files with 68 additions and 14 deletions

View file

@ -15,18 +15,20 @@ class CheckoutableCheckedIn
public $checkedInBy; public $checkedInBy;
public $note; public $note;
public $action_date; // Date setted in the hardware.checkin view at the checkin_at input, for the action log 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. * Create a new event instance.
* *
* @return void * @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->checkoutable = $checkoutable;
$this->checkedOutTo = $checkedOutTo; $this->checkedOutTo = $checkedOutTo;
$this->checkedInBy = $checkedInBy; $this->checkedInBy = $checkedInBy;
$this->note = $note; $this->note = $note;
$this->action_date = $action_date ?? date('Y-m-d'); $this->action_date = $action_date ?? date('Y-m-d');
$this->originalValues = $originalValues;
} }
} }

View file

@ -14,17 +14,19 @@ class CheckoutableCheckedOut
public $checkedOutTo; public $checkedOutTo;
public $checkedOutBy; public $checkedOutBy;
public $note; public $note;
public $originalValues;
/** /**
* Create a new event instance. * Create a new event instance.
* *
* @return void * @return void
*/ */
public function __construct($checkoutable, $checkedOutTo, User $checkedOutBy, $note) public function __construct($checkoutable, $checkedOutTo, User $checkedOutBy, $note, $originalValues = [])
{ {
$this->checkoutable = $checkoutable; $this->checkoutable = $checkoutable;
$this->checkedOutTo = $checkedOutTo; $this->checkedOutTo = $checkedOutTo;
$this->checkedOutBy = $checkedOutBy; $this->checkedOutBy = $checkedOutBy;
$this->note = $note; $this->note = $note;
$this->originalValues = $originalValues;
} }
} }

View file

@ -925,10 +925,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'); $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()) { 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'))); return response()->json(Helper::formatStandardApiResponse('success', ['asset'=> e($asset->asset_tag)], trans('admin/hardware/message.checkin.success')));
} }

View file

@ -109,8 +109,11 @@ class AssetCheckinController extends Controller
} }
} }
$originalValues = $asset->getRawOriginal();
$checkin_at = date('Y-m-d H:i:s'); $checkin_at = date('Y-m-d H:i:s');
if (($request->filled('checkin_at')) && ($request->get('checkin_at') != date('Y-m-d'))) { if (($request->filled('checkin_at')) && ($request->get('checkin_at') != date('Y-m-d'))) {
$originalValues['action_date'] = $checkin_at;
$checkin_at = $request->get('checkin_at'); $checkin_at = $request->get('checkin_at');
} }
@ -133,7 +136,7 @@ class AssetCheckinController extends Controller
// Was the asset updated? // Was the asset updated?
if ($asset->save()) { 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')) { if ((isset($user)) && ($backto == 'user')) {
return redirect()->route('users.show', $user->id)->with('success', trans('admin/hardware/message.checkin.success')); return redirect()->route('users.show', $user->id)->with('success', trans('admin/hardware/message.checkin.success'));

View file

@ -58,8 +58,14 @@ class BulkAssetsController extends Controller
switch ($request->input('bulk_actions')) { switch ($request->input('bulk_actions')) {
case 'labels': case 'labels':
$this->authorize('view', Asset::class); $this->authorize('view', Asset::class);
$assets_found = Asset::find($asset_ids);
if ($assets_found->isEmpty()){
return redirect()->back();
}
return (new Label) return (new Label)
->with('assets', Asset::find($asset_ids)) ->with('assets', $assets_found)
->with('settings', Setting::getSettings()) ->with('settings', Setting::getSettings())
->with('bulkedit', true) ->with('bulkedit', true)
->with('count', 0); ->with('count', 0);

View file

@ -33,7 +33,7 @@ class LogListener
*/ */
public function onCheckoutableCheckedIn(CheckoutableCheckedIn $event) 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) 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);
} }
/** /**

View file

@ -333,6 +333,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 ($this->save()) {
if (is_int($admin)) { if (is_int($admin)) {
$checkedOutBy = User::findOrFail($admin); $checkedOutBy = User::findOrFail($admin);
@ -341,7 +348,7 @@ class Asset extends Depreciable
} else { } else {
$checkedOutBy = Auth::user(); $checkedOutBy = Auth::user();
} }
event(new CheckoutableCheckedOut($this, $target, $checkedOutBy, $note)); event(new CheckoutableCheckedOut($this, $target, $checkedOutBy, $note, $originalValues));
$this->increment('checkout_counter', 1); $this->increment('checkout_counter', 1);

View file

@ -23,7 +23,7 @@ trait Loggable
* @since [v3.4] * @since [v3.4]
* @return \App\Models\Actionlog * @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 = new Actionlog;
$log = $this->determineLogItemType($log); $log = $this->determineLogItemType($log);
@ -62,6 +62,23 @@ trait Loggable
$log->action_date = date('Y-m-d H:i:s'); $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'); $log->logaction('checkout');
return $log; return $log;
@ -89,7 +106,7 @@ trait Loggable
* @since [v3.4] * @since [v3.4]
* @return \App\Models\Actionlog * @return \App\Models\Actionlog
*/ */
public function logCheckin($target, $note, $action_date = null) public function logCheckin($target, $note, $action_date = null, $originalValues = [])
{ {
$settings = Setting::getSettings(); $settings = Setting::getSettings();
$log = new Actionlog; $log = new Actionlog;
@ -114,13 +131,9 @@ trait Loggable
} }
} }
$log->location_id = null; $log->location_id = null;
$log->note = $note; $log->note = $note;
$log->action_date = $action_date; $log->action_date = $action_date;
if (! $log->action_date) {
$log->action_date = date('Y-m-d H:i:s');
}
if (! $log->action_date) { if (! $log->action_date) {
$log->action_date = date('Y-m-d H:i:s'); $log->action_date = date('Y-m-d H:i:s');
@ -130,6 +143,23 @@ trait Loggable
$log->user_id = Auth::user()->id; $log->user_id = Auth::user()->id;
} }
$changed = [];
$originalValues = array_intersect_key($originalValues, array_flip(['action_date','name','status_id','location_id','rtd_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'); $log->logaction('checkin from');
// $params = [ // $params = [