2016-09-06 19:39:42 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Models\Actionlog;
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\CheckoutRequest;
|
|
|
|
use App\Models\User;
|
2016-12-26 15:19:04 -08:00
|
|
|
use App\Notifications\CheckinNotification;
|
2017-08-25 10:04:19 -07:00
|
|
|
use App\Notifications\AuditNotification;
|
2016-12-26 15:19:04 -08:00
|
|
|
use App\Notifications\CheckoutNotification;
|
2016-09-06 19:39:42 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
trait Loggable
|
|
|
|
{
|
|
|
|
|
2016-09-19 23:51:45 -07:00
|
|
|
/**
|
|
|
|
* @author Daniel Meltzer <parallelgrapefruit@gmail.com
|
|
|
|
* @since [v3.4]
|
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
public function log()
|
|
|
|
{
|
|
|
|
return $this->morphMany(Actionlog::class, 'item');
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:51:45 -07:00
|
|
|
/**
|
|
|
|
* @author Daniel Meltzer <parallelgrapefruit@gmail.com
|
|
|
|
* @since [v3.4]
|
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
2016-09-06 19:39:42 -07:00
|
|
|
public function logCheckout($note, $target = null /*target is overridable for components*/)
|
|
|
|
{
|
|
|
|
$log = new Actionlog;
|
2016-09-19 23:51:45 -07:00
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
// We need to special case licenses because of license_seat vs license. So much for clean polymorphism :)
|
|
|
|
if (static::class == LicenseSeat::class) {
|
|
|
|
$log->item_type = License::class;
|
|
|
|
$log->item_id = $this->license_id;
|
|
|
|
} else {
|
|
|
|
$log->item_type = static::class;
|
|
|
|
$log->item_id = $this->id;
|
|
|
|
}
|
2016-09-19 23:51:45 -07:00
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
$log->user_id = Auth::user()->id;
|
2016-09-19 23:51:45 -07:00
|
|
|
|
2016-12-27 19:04:11 -08:00
|
|
|
// @FIXME This needs to be generalized with new asset checkout.
|
2017-06-12 17:39:03 -07:00
|
|
|
if(isset($target)) {
|
|
|
|
$log->target_type = get_class($target);
|
|
|
|
$log->target_id = $target->id;
|
|
|
|
} else {
|
|
|
|
if (!is_null($this->asset_id)) {
|
|
|
|
$log->target_type = Asset::class;
|
|
|
|
$log->target_id = $this->asset_id;
|
|
|
|
} elseif (!is_null($this->assigned_to)) {
|
|
|
|
$log->target_type = User::class;
|
|
|
|
$log->target_id = $this->assigned_to;
|
|
|
|
}
|
2016-09-06 19:39:42 -07:00
|
|
|
}
|
2016-09-19 23:51:45 -07:00
|
|
|
|
|
|
|
$item = call_user_func(array($log->target_type, 'find'), $log->target_id);
|
2016-12-27 19:04:11 -08:00
|
|
|
if($this->assignedTo) {
|
|
|
|
$item = $this->assignedTo;
|
|
|
|
}
|
2016-12-27 16:24:41 -08:00
|
|
|
$class = get_class($item);
|
|
|
|
if($class == Location::class) {
|
|
|
|
// We can checkout to a location
|
|
|
|
$log->location_id = $item->id;
|
|
|
|
} else if ($class== Asset::class) {
|
|
|
|
$log->location_id = $item->rtd_location_id;
|
|
|
|
} else {
|
|
|
|
$log->location_id = $item->location_id;
|
|
|
|
}
|
2016-09-06 19:39:42 -07:00
|
|
|
$log->note = $note;
|
|
|
|
$log->logaction('checkout');
|
|
|
|
|
2016-12-26 15:19:04 -08:00
|
|
|
$params = [
|
|
|
|
'item' => $log->item,
|
|
|
|
'target' => $log->target,
|
|
|
|
'admin' => $log->user,
|
|
|
|
'note' => $note
|
|
|
|
];
|
|
|
|
|
2017-08-24 23:20:51 -07:00
|
|
|
if ($settings = Setting::getSettings()) {
|
|
|
|
$settings->notify(new CheckoutNotification($params));
|
|
|
|
}
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:51:45 -07:00
|
|
|
/**
|
|
|
|
* @author Daniel Meltzer <parallelgrapefruit@gmail.com
|
|
|
|
* @since [v3.4]
|
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
2016-11-17 15:54:29 -08:00
|
|
|
public function logCheckin($target, $note)
|
2016-09-06 19:39:42 -07:00
|
|
|
{
|
|
|
|
$log = new Actionlog;
|
2016-11-17 15:54:29 -08:00
|
|
|
$log->target_type = get_class($target);
|
|
|
|
$log->target_id = $target->id;
|
2016-09-06 19:39:42 -07:00
|
|
|
if (static::class == LicenseSeat::class) {
|
|
|
|
$log->item_type = License::class;
|
|
|
|
$log->item_id = $this->license_id;
|
|
|
|
} else {
|
|
|
|
$log->item_type = static::class;
|
|
|
|
$log->item_id = $this->id;
|
|
|
|
}
|
|
|
|
$log->location_id = null;
|
|
|
|
$log->note = $note;
|
|
|
|
$log->user_id = Auth::user()->id;
|
|
|
|
$log->logaction('checkin from');
|
|
|
|
|
2016-12-26 15:19:04 -08:00
|
|
|
$params = [
|
|
|
|
'item' => $log->item,
|
|
|
|
'admin' => $log->user,
|
|
|
|
'note' => $note
|
|
|
|
];
|
|
|
|
Setting::getSettings()->notify(new CheckinNotification($params));
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
|
2017-08-25 10:04:19 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author A. Gianotto <snipe@snipe.net>
|
|
|
|
* @since [v4.0]
|
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
|
|
|
public function logAudit($note)
|
|
|
|
{
|
|
|
|
$log = new Actionlog;
|
|
|
|
if (static::class == LicenseSeat::class) {
|
|
|
|
$log->item_type = License::class;
|
|
|
|
$log->item_id = $this->license_id;
|
|
|
|
} else {
|
|
|
|
$log->item_type = static::class;
|
|
|
|
$log->item_id = $this->id;
|
|
|
|
}
|
|
|
|
$log->location_id = null;
|
|
|
|
$log->note = $note;
|
|
|
|
$log->user_id = Auth::user()->id;
|
|
|
|
$log->logaction('audit');
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'item' => $log->item,
|
|
|
|
'admin' => $log->user,
|
|
|
|
'note' => $note
|
|
|
|
];
|
|
|
|
Setting::getSettings()->notify(new AuditNotification($params));
|
|
|
|
|
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-12 18:45:32 -07:00
|
|
|
/**
|
|
|
|
* @author Daniel Meltzer <parallelgrapefruit@gmail.com
|
2016-10-17 11:07:08 -07:00
|
|
|
* @since [v3.5]
|
2016-10-12 18:45:32 -07:00
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
|
|
|
public function logCreate($note = null)
|
|
|
|
{
|
2016-10-17 11:07:08 -07:00
|
|
|
$user_id = -1;
|
|
|
|
if (Auth::user()) {
|
|
|
|
$user_id = Auth::user()->id;
|
|
|
|
}
|
2016-10-12 18:45:32 -07:00
|
|
|
$log = new Actionlog;
|
|
|
|
if (static::class == LicenseSeat::class) {
|
|
|
|
$log->item_type = License::class;
|
|
|
|
$log->item_id = $this->license_id;
|
|
|
|
} else {
|
|
|
|
$log->item_type = static::class;
|
|
|
|
$log->item_id = $this->id;
|
|
|
|
}
|
|
|
|
$log->location_id = null;
|
|
|
|
$log->note = $note;
|
2016-10-17 11:07:08 -07:00
|
|
|
$log->user_id = $user_id;
|
2016-10-12 18:45:32 -07:00
|
|
|
$log->logaction('created');
|
2016-10-17 11:07:08 -07:00
|
|
|
$log->save();
|
2016-10-12 18:45:32 -07:00
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:51:45 -07:00
|
|
|
/**
|
|
|
|
* @author Daniel Meltzer <parallelgrapefruit@gmail.com
|
|
|
|
* @since [v3.4]
|
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
2016-09-06 19:39:42 -07:00
|
|
|
public function logUpload($filename, $note)
|
|
|
|
{
|
|
|
|
$log = new Actionlog;
|
|
|
|
if (static::class == LicenseSeat::class) {
|
|
|
|
$log->item_type = License::class;
|
|
|
|
$log->item_id = $this->license_id;
|
|
|
|
} else {
|
|
|
|
$log->item_type = static::class;
|
|
|
|
$log->item_id = $this->id;
|
|
|
|
}
|
|
|
|
$log->user_id = Auth::user()->id;
|
|
|
|
$log->note = $note;
|
|
|
|
$log->target_id = null;
|
2017-04-06 19:48:15 -07:00
|
|
|
$log->created_at = date("Y-m-d H:i:s");
|
2016-09-06 19:39:42 -07:00
|
|
|
$log->filename = $filename;
|
|
|
|
$log->logaction('uploaded');
|
|
|
|
|
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
}
|