mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-09 23:24:06 -08:00
Moves logging checkin/checkout to separate listener
This commit is contained in:
parent
e24f292a1a
commit
e0423418d2
|
@ -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')));
|
||||
|
||||
|
|
|
@ -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'));
|
||||
|
|
|
@ -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,8 +84,6 @@ 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')));
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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'));
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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')));
|
||||
|
||||
|
|
|
@ -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')));
|
||||
|
||||
|
|
92
app/Listeners/LogListener.php
Normal file
92
app/Listeners/LogListener.php
Normal file
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue