mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-26 06:04:08 -08:00
c7f48951a9
* To see checkin/checkout Date in History of Asset * Added some logic to get today's date if the checkin_at input is blank * Updating the action_date taking the value from the action_log table * Hide created_at field and rename 'Action Date' as only 'Date'
200 lines
5.3 KiB
PHP
200 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Notifications\AuditNotification;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
trait Loggable
|
|
{
|
|
|
|
/**
|
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
|
* @since [v3.4]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
|
|
public function log()
|
|
{
|
|
return $this->morphMany(Actionlog::class, 'item');
|
|
}
|
|
|
|
/**
|
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
|
* @since [v3.4]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
public function logCheckout($note, $target, $action_date)
|
|
{
|
|
$log = new Actionlog;
|
|
$log = $this->determineLogItemType($log);
|
|
if(Auth::user())
|
|
$log->user_id = Auth::user()->id;
|
|
|
|
if (!isset($target)) {
|
|
throw new Exception('All checkout logs require a target');
|
|
return;
|
|
}
|
|
$log->target_type = get_class($target);
|
|
$log->target_id = $target->id;
|
|
|
|
|
|
// Figure out what the target is
|
|
if ($log->target_type == Location::class) {
|
|
$log->location_id = $target->id;
|
|
} elseif ($log->target_type == Asset::class) {
|
|
$log->location_id = $target->location_id;
|
|
} else {
|
|
$log->location_id = $target->location_id;
|
|
}
|
|
|
|
$log->note = $note;
|
|
$log->action_date = $action_date;
|
|
$log->logaction('checkout');
|
|
|
|
return $log;
|
|
}
|
|
|
|
/**
|
|
* Helper method to determine the log item type
|
|
*/
|
|
private function determineLogItemType($log)
|
|
{
|
|
// 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;
|
|
}
|
|
|
|
return $log;
|
|
}
|
|
/**
|
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
|
* @since [v3.4]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
public function logCheckin($target, $note, $action_date)
|
|
{
|
|
$log = new Actionlog;
|
|
$log->target_type = get_class($target);
|
|
$log->target_id = $target->id;
|
|
|
|
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;
|
|
|
|
if (static::class == Asset::class) {
|
|
if ($asset = Asset::find($log->item_id)) {
|
|
$asset->increment('checkin_counter', 1);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
$log->location_id = null;
|
|
$log->note = $note;
|
|
$log->action_date = $action_date;
|
|
$log->user_id = Auth::user()->id;
|
|
$log->logaction('checkin from');
|
|
|
|
return $log;
|
|
}
|
|
|
|
|
|
/**
|
|
* @author A. Gianotto <snipe@snipe.net>
|
|
* @since [v4.0]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
public function logAudit($note, $location_id, $filename = null)
|
|
{
|
|
$log = new Actionlog;
|
|
$location = Location::find($location_id);
|
|
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 = ($location_id) ? $location_id : null;
|
|
$log->note = $note;
|
|
$log->user_id = Auth::user()->id;
|
|
$log->filename = $filename;
|
|
$log->logaction('audit');
|
|
|
|
$params = [
|
|
'item' => $log->item,
|
|
'filename' => $log->filename,
|
|
'admin' => $log->user,
|
|
'location' => ($location) ? $location->name : '',
|
|
'note' => $note
|
|
];
|
|
Setting::getSettings()->notify(new AuditNotification($params));
|
|
|
|
return $log;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
|
* @since [v3.5]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
public function logCreate($note = null)
|
|
{
|
|
$user_id = -1;
|
|
if (Auth::user()) {
|
|
$user_id = Auth::user()->id;
|
|
}
|
|
$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 = $user_id;
|
|
$log->logaction('create');
|
|
$log->save();
|
|
return $log;
|
|
}
|
|
|
|
/**
|
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
|
* @since [v3.4]
|
|
* @return \App\Models\Actionlog
|
|
*/
|
|
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;
|
|
$log->created_at = date("Y-m-d H:i:s");
|
|
$log->filename = $filename;
|
|
$log->logaction('uploaded');
|
|
|
|
return $log;
|
|
}
|
|
}
|