Skip to content

A free open source IT asset/license management system

Moves logging checkin/checkout to separate listener

Till Deekee0423418d2ca
diff --git a/app/Http/Controllers/Accessories/AccessoryCheckinController.php b/app/Http/Controllers/Accessories/AccessoryCheckinController.php
index 7acfe855cd..e64d24f169 100644
--- a/app/Http/Controllers/Accessories/AccessoryCheckinController.php
+++ b/app/Http/Controllers/Accessories/AccessoryCheckinController.php
@@ -7,6 +7,7 @@ use App\Http\Controllers\Controller;
use App\Models\Accessory;
use App\Models\User;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
@@ -62,7 +63,6 @@ class AccessoryCheckinController extends Controller
// Was the accessory updated?
if (DB::table('accessories_users')->where('id', '=', $accessory_user->id)->delete()) {
$return_to = e($accessory_user->assigned_to);
- $accessory->logCheckin(User::find($return_to), e(Input::get('note')));
event(new AccessoryCheckedIn($accessory, User::find($return_to), Auth::user(), $request->input('note')));
diff --git a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
index 30e5fd25d8..02107bcd2f 100644
--- a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
+++ b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php
@@ -78,8 +78,6 @@ class AccessoryCheckoutController extends Controller
'assigned_to' => $request->get('assigned_to')
]);
- $logaction = $accessory->logCheckout(e(Input::get('note')), $user);
-
DB::table('accessories_users')->where('assigned_to', '=', $accessory->assigned_to)->where('accessory_id', '=', $accessory->id)->first();
event(new AccessoryCheckedOut($accessory, $user, Auth::user(), $request->input('note'));
diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php
index 26f77e7fcd..da141f8b3d 100644
--- a/app/Http/Controllers/Assets/AssetCheckinController.php
+++ b/app/Http/Controllers/Assets/AssetCheckinController.php
@@ -7,6 +7,7 @@ use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Requests\AssetCheckinRequest;
use App\Models\Asset;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
@@ -83,9 +84,7 @@ class AssetCheckinController extends Controller
// Was the asset updated?
if ($asset->save()) {
- $asset->logCheckin($target, e(request('note')));
-
-
+
event(new AssetCheckedIn($asset, $target, Auth::user(), $request->input('note')));
if ($backto=='user') {
diff --git a/app/Http/Controllers/Components/ComponentCheckinController.php b/app/Http/Controllers/Components/ComponentCheckinController.php
index cd070a5067..24c684c4b6 100644
--- a/app/Http/Controllers/Components/ComponentCheckinController.php
+++ b/app/Http/Controllers/Components/ComponentCheckinController.php
@@ -8,6 +8,7 @@ use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Component;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
@@ -87,16 +88,6 @@ class ComponentCheckinController extends Controller
DB::table('components_assets')->where('id',
$component_asset_id)->update(['assigned_qty' => $qty_remaining_in_checkout]);
- $log = new Actionlog();
- $log->user_id = auth()->id();
- $log->action_type = 'checkin from';
- $log->target_type = Asset::class;
- $log->target_id = $component_assets->asset_id;
- $log->item_id = $component_assets->component_id;
- $log->item_type = Component::class;
- $log->note = $request->input('note');
- $log->save();
-
// If the checked-in qty is exactly the same as the assigned_qty,
// we can simply delete the associated components_assets record
if ($qty_remaining_in_checkout == 0) {
diff --git a/app/Http/Controllers/Components/ComponentCheckoutController.php b/app/Http/Controllers/Components/ComponentCheckoutController.php
index c979f86186..c7e7fe3db5 100644
--- a/app/Http/Controllers/Components/ComponentCheckoutController.php
+++ b/app/Http/Controllers/Components/ComponentCheckoutController.php
@@ -87,8 +87,6 @@ class ComponentCheckoutController extends Controller
'asset_id' => $asset_id
]);
- $logaction = $component->logCheckout(e(Input::get('note')), $asset);
-
event(new ComponentCheckedOut($component, $asset, $request->input('assigned_qty'), Auth::user()));
return redirect()->route('components.index')->with('success', trans('admin/components/message.checkout.success'));
diff --git a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
index ef2d30beb3..0b3d8c6c91 100644
--- a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
+++ b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
@@ -68,8 +68,6 @@ class ConsumableCheckoutController extends Controller
'assigned_to' => e(Input::get('assigned_to'))
]);
- $logaction = $consumable->logCheckout(e(Input::get('note')), $user);
-
event(new ConsumableCheckedOut($consumable, $user, Auth::user(), $request->input('note')));
// Redirect to the new consumable page
diff --git a/app/Http/Controllers/Licenses/LicenseCheckinController.php b/app/Http/Controllers/Licenses/LicenseCheckinController.php
index 6144c35062..9612028465 100644
--- a/app/Http/Controllers/Licenses/LicenseCheckinController.php
+++ b/app/Http/Controllers/Licenses/LicenseCheckinController.php
@@ -9,6 +9,7 @@ use App\Models\LicenseSeat;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
@@ -89,7 +90,6 @@ class LicenseCheckinController extends Controller
// Was the asset updated?
if ($licenseSeat->save()) {
- $licenseSeat->logCheckin($return_to, e(request('note')));
event(new LicenseCheckedIn($license, $return_to, Auth::user(), $request->input('note')));
diff --git a/app/Http/Controllers/Licenses/LicenseCheckoutController.php b/app/Http/Controllers/Licenses/LicenseCheckoutController.php
index acde2d6205..1dd8935f68 100644
--- a/app/Http/Controllers/Licenses/LicenseCheckoutController.php
+++ b/app/Http/Controllers/Licenses/LicenseCheckoutController.php
@@ -104,7 +104,6 @@ class LicenseCheckoutController extends Controller
$licenseSeat->assigned_to = $target->assigned_to;
}
if ($licenseSeat->save()) {
- $licenseSeat->logCheckout(request('note'), $target);
event(new LicenseCheckedOut($licenseSeat->license, $target, Auth::user(), request('note')));
@@ -122,7 +121,6 @@ class LicenseCheckoutController extends Controller
$licenseSeat->assigned_to = request('assigned_to');
if ($licenseSeat->save()) {
- $licenseSeat->logCheckout(request('note'), $target);
event(new LicenseCheckedOut($licenseSeat->license, $target, Auth::user(), request('note')));
diff --git a/app/Listeners/LogListener.php b/app/Listeners/LogListener.php
new file mode 100644
index 0000000000..c3dfe76220
--- /dev/null
+++ b/app/Listeners/LogListener.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace App\Listeners;
+
+use App\Events\AccessoryCheckedIn;
+use App\Events\AccessoryCheckedOut;
+use App\Events\AssetCheckedIn;
+use App\Events\AssetCheckedOut;
+use App\Events\ComponentCheckedIn;
+use App\Events\ComponentCheckedOut;
+use App\Events\ConsumableCheckedOut;
+use App\Events\LicenseCheckedIn;
+use App\Events\LicenseCheckedOut;
+use App\Models\Actionlog;
+use App\Models\Asset;
+use App\Models\Component;
+
+
+class LogListener
+{
+ public function onAccessoryCheckedIn(AccessoryCheckedIn $event) {
+ $event->accessory->logCheckin($event->checkedOutTo, $event->note);
+ }
+
+ public function onAccessoryCheckedOut(AccessoryCheckedOut $event) {
+ $event->accessory->logCheckout($event->note, $event->checkedOutTo);
+ }
+
+ public function onAssetCheckedIn(AssetCheckedIn $event) {
+ $event->asset->logCheckin($event->checkedOutTo, $event->note);
+ }
+
+ public function onAssetCheckedOut(AssetCheckedOut $event) {
+ $event->asset->logCheckout($event->note, $event->checkedOutTo);
+ }
+
+ public function onComponentCheckedIn(ComponentCheckedIn $event) {
+ $log = new Actionlog();
+ $log->user_id = $event->checkedInBy->id;
+ $log->action_type = 'checkin from';
+ $log->target_type = Asset::class;
+ $log->target_id = $event->checkedOutTo->asset_id;
+ $log->item_id = $event->checkedOutTo->component_id;
+ $log->item_type = Component::class;
+ $log->note = $event->note;
+ $log->save();
+ }
+
+ public function onComponentCheckedOut(ComponentCheckedOut $event) {
+ $event->component->logCheckout($event->note, $event->checkedOutTo);
+ }
+
+ public function onConsumableCheckedOut(ConsumableCheckedOut $event) {
+ $event->consumable->logCheckout($event->note, $event->checkedOutTo);
+ }
+
+ public function onLicenseCheckedIn(LicenseCheckedIn $event) {
+ $event->license->logCheckin($event->checkedOutTo, $event->note);
+ }
+
+ public function onLicenseCheckedOut(LicenseCheckedOut $event) {
+ $event->license->logCheckout($event->note, $event->checkedOutTo);
+ }
+
+ /**
+ * Register the listeners for the subscriber.
+ *
+ * @param Illuminate\Events\Dispatcher $events
+ */
+ public function subscribe($events)
+ {
+ $list = [
+ 'AccessoryCheckedIn',
+ 'AccessoryCheckedout',
+ 'AssetCheckedIn',
+ 'AssetCheckedOut',
+ 'ComponentCheckedIn',
+ 'ComponentCheckedOut',
+ 'ConsumableCheckedOut',
+ 'LicenseCheckedIn',
+ 'LicenseCheckedOut',
+ ];
+
+ foreach($list as $event) {
+ $events->listen(
+ 'App\Events\\' . $event,
+ 'App\Listeners\LogListener@on' . $event
+ );
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index 051596ad2e..68d2cf8453 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -262,9 +262,8 @@ class Asset extends Depreciable
}
if ($this->save()) {
- $loggedAction = $this->logCheckout($note, $target);
- event(new AssetCheckedOut($this, $target, $loggedAction));
+ event(new AssetCheckedOut($this, $target, Auth::user(), $note));
$this->increment('checkout_counter', 1);
return true;
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 822f82c8cf..df3c643b24 100644
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -3,6 +3,7 @@
namespace App\Providers;
use Illuminate\Support\Facades\Event;
+use App\Listeners\LogListener;
use App\Listeners\SendingCheckInNotificationsListener;
use App\Listeners\SendingCheckOutNotificationsListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@@ -33,6 +34,7 @@ class EventServiceProvider extends ServiceProvider
protected $subscribe = [
SendingCheckOutNotificationsListener::class,
SendingCheckInNotificationsListener::class,
+ LogListener::class
];
/**