2017-06-15 20:54:14 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Observers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\Actionlog;
|
2017-06-15 20:54:14 -07:00
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Setting;
|
|
|
|
use Auth;
|
2023-07-24 14:41:25 -07:00
|
|
|
use Carbon\Carbon;
|
2017-06-15 20:54:14 -07:00
|
|
|
|
|
|
|
class AssetObserver
|
|
|
|
{
|
|
|
|
/**
|
2023-10-26 06:34:18 -07:00
|
|
|
* Listen to the Asset updating event. This fires automatically every time an existing asset is saved.
|
2017-06-15 20:54:14 -07:00
|
|
|
*
|
|
|
|
* @param Asset $asset
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-08 03:38:39 -07:00
|
|
|
public function updating(Asset $asset)
|
2017-06-15 20:54:14 -07:00
|
|
|
{
|
2022-03-23 09:23:53 -07:00
|
|
|
$attributes = $asset->getAttributes();
|
2022-06-06 17:14:32 -07:00
|
|
|
$attributesOriginal = $asset->getRawOriginal();
|
2022-06-09 16:08:04 -07:00
|
|
|
$same_checkout_counter = false;
|
|
|
|
$same_checkin_counter = false;
|
2023-11-22 12:09:10 -08:00
|
|
|
$restoring_or_deleting = false;
|
|
|
|
|
|
|
|
|
|
|
|
// This is a gross hack to prevent the double logging when restoring an asset
|
|
|
|
if (array_key_exists('deleted_at', $attributes) && array_key_exists('deleted_at', $attributesOriginal)){
|
|
|
|
$restoring_or_deleting = (($attributes['deleted_at'] != $attributesOriginal['deleted_at']));
|
|
|
|
}
|
2022-06-09 16:08:04 -07:00
|
|
|
|
|
|
|
if (array_key_exists('checkout_counter', $attributes) && array_key_exists('checkout_counter', $attributesOriginal)){
|
|
|
|
$same_checkout_counter = (($attributes['checkout_counter'] == $attributesOriginal['checkout_counter']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('checkin_counter', $attributes) && array_key_exists('checkin_counter', $attributesOriginal)){
|
|
|
|
$same_checkin_counter = (($attributes['checkin_counter'] == $attributesOriginal['checkin_counter']));
|
|
|
|
}
|
2022-06-06 17:14:32 -07:00
|
|
|
|
2017-12-12 03:40:04 -08:00
|
|
|
// If the asset isn't being checked out or audited, log the update.
|
|
|
|
// (Those other actions already create log entries.)
|
2023-11-22 12:09:10 -08:00
|
|
|
if (($attributes['assigned_to'] == $attributesOriginal['assigned_to'])
|
2022-06-09 16:08:04 -07:00
|
|
|
&& ($same_checkout_counter) && ($same_checkin_counter)
|
2022-03-23 09:23:53 -07:00
|
|
|
&& ((isset( $attributes['next_audit_date']) ? $attributes['next_audit_date'] : null) == (isset($attributesOriginal['next_audit_date']) ? $attributesOriginal['next_audit_date']: null))
|
2023-11-22 13:34:39 -08:00
|
|
|
&& ($attributes['last_checkout'] == $attributesOriginal['last_checkout']) && (!$restoring_or_deleting))
|
2017-07-08 03:38:39 -07:00
|
|
|
{
|
2017-12-12 04:59:28 -08:00
|
|
|
$changed = [];
|
|
|
|
|
2022-06-06 17:14:32 -07:00
|
|
|
foreach ($asset->getRawOriginal() as $key => $value) {
|
|
|
|
if ($asset->getRawOriginal()[$key] != $asset->getAttributes()[$key]) {
|
|
|
|
$changed[$key]['old'] = $asset->getRawOriginal()[$key];
|
2017-12-12 04:59:28 -08:00
|
|
|
$changed[$key]['new'] = $asset->getAttributes()[$key];
|
|
|
|
}
|
2022-06-06 20:00:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($changed)){
|
|
|
|
return;
|
|
|
|
}
|
2017-12-12 04:59:28 -08:00
|
|
|
|
2017-07-08 03:38:39 -07:00
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = Asset::class;
|
|
|
|
$logAction->item_id = $asset->id;
|
2021-06-10 13:15:52 -07:00
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
2017-07-08 03:38:39 -07:00
|
|
|
$logAction->user_id = Auth::id();
|
2017-12-12 04:59:28 -08:00
|
|
|
$logAction->log_meta = json_encode($changed);
|
2017-07-08 03:38:39 -07:00
|
|
|
$logAction->logaction('update');
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|
2017-06-15 20:54:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* Listen to the Asset created event, and increment
|
2017-06-15 20:54:14 -07:00
|
|
|
* the next_auto_tag_base value in the settings table when i
|
|
|
|
* a new asset is created.
|
|
|
|
*
|
|
|
|
* @param Asset $asset
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function created(Asset $asset)
|
|
|
|
{
|
2018-11-01 19:59:50 -07:00
|
|
|
if ($settings = Setting::getSettings()) {
|
2023-07-12 08:39:45 -07:00
|
|
|
$tag = $asset->asset_tag;
|
|
|
|
$prefix = $settings->auto_increment_prefix;
|
|
|
|
$number = substr($tag, strlen($prefix));
|
2023-08-14 16:40:34 -07:00
|
|
|
// IF - auto_increment_assets is on, AND (there is no prefix OR the prefix matches the start of the tag)
|
2023-07-12 08:39:45 -07:00
|
|
|
// AND the rest of the string after the prefix is all digits, THEN...
|
2023-08-14 16:40:34 -07:00
|
|
|
if ($settings->auto_increment_assets && ($prefix=='' || strpos($tag, $prefix) === 0) && preg_match('/\d+/',$number) === 1) {
|
2023-07-12 08:39:45 -07:00
|
|
|
// new way of auto-trueing-up auto_increment ID's
|
|
|
|
$next_asset_tag = intval($number, 10) + 1;
|
|
|
|
// we had to use 'intval' because the $number could be '01234' and
|
|
|
|
// might get interpreted in Octal instead of decimal
|
|
|
|
|
|
|
|
// only modify the 'next' one if it's *bigger* than the stored base
|
|
|
|
//
|
|
|
|
if($next_asset_tag > $settings->next_auto_tag_base) {
|
|
|
|
$settings->next_auto_tag_base = $next_asset_tag;
|
|
|
|
$settings->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// legacy method
|
|
|
|
$settings->increment('next_auto_tag_base');
|
|
|
|
$settings->save();
|
|
|
|
}
|
2017-08-23 14:07:01 -07:00
|
|
|
}
|
2017-06-15 20:54:14 -07:00
|
|
|
|
|
|
|
$logAction = new Actionlog();
|
2023-07-12 08:39:45 -07:00
|
|
|
$logAction->item_type = Asset::class; // can we instead say $logAction->item = $asset ?
|
2017-06-15 20:54:14 -07:00
|
|
|
$logAction->item_id = $asset->id;
|
2021-06-10 13:15:52 -07:00
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
2017-06-20 20:38:13 -07:00
|
|
|
$logAction->user_id = Auth::id();
|
2024-01-25 16:18:24 -08:00
|
|
|
$logAction->note = 'poop';
|
|
|
|
if($asset->getImported()) {
|
|
|
|
$logAction->note = "this asset was imported";
|
|
|
|
}
|
2017-06-15 20:54:14 -07:00
|
|
|
$logAction->logaction('create');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen to the Asset deleting event.
|
|
|
|
*
|
|
|
|
* @param Asset $asset
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function deleting(Asset $asset)
|
|
|
|
{
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = Asset::class;
|
|
|
|
$logAction->item_id = $asset->id;
|
2021-06-10 13:15:52 -07:00
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
2017-06-20 20:38:13 -07:00
|
|
|
$logAction->user_id = Auth::id();
|
2017-06-15 20:54:14 -07:00
|
|
|
$logAction->logaction('delete');
|
|
|
|
}
|
2023-10-13 04:18:19 -07:00
|
|
|
|
2023-11-22 12:09:10 -08:00
|
|
|
/**
|
|
|
|
* Listen to the Asset deleting event.
|
|
|
|
*
|
|
|
|
* @param Asset $asset
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function restoring(Asset $asset)
|
|
|
|
{
|
|
|
|
$logAction = new Actionlog();
|
|
|
|
$logAction->item_type = Asset::class;
|
|
|
|
$logAction->item_id = $asset->id;
|
|
|
|
$logAction->created_at = date('Y-m-d H:i:s');
|
|
|
|
$logAction->user_id = Auth::id();
|
|
|
|
$logAction->logaction('restore');
|
|
|
|
}
|
|
|
|
|
2023-10-13 04:18:19 -07:00
|
|
|
/**
|
|
|
|
* Executes every time an asset is saved.
|
|
|
|
*
|
|
|
|
* This matters specifically because any database fields affected here MUST already exist on
|
|
|
|
* the assets table (and/or any related models), or related migrations WILL fail.
|
|
|
|
*
|
|
|
|
* For example, if there is a database migration that's a bit older and modifies an asset, if the save
|
|
|
|
* fires before a field gets created in a later migration and that field in the later migration
|
2023-10-13 04:34:46 -07:00
|
|
|
* is used in this observer, it doesn't actually exist yet and the migration will break unless we
|
|
|
|
* use saveQuietly() in the migration which skips this observer.
|
2023-10-13 04:18:19 -07:00
|
|
|
*
|
|
|
|
* @see https://github.com/snipe/snipe-it/issues/13723#issuecomment-1761315938
|
|
|
|
*/
|
2023-07-12 22:43:48 -07:00
|
|
|
public function saving(Asset $asset)
|
|
|
|
{
|
2023-10-13 04:18:19 -07:00
|
|
|
// determine if calculated eol and then calculate it - this should only happen on a new asset
|
2023-10-22 09:23:30 -07:00
|
|
|
if (is_null($asset->asset_eol_date) && !is_null($asset->purchase_date) && ($asset->model->eol > 0)){
|
2023-09-12 16:01:33 -07:00
|
|
|
$asset->asset_eol_date = $asset->purchase_date->addMonths($asset->model->eol)->format('Y-m-d');
|
2023-07-29 11:45:39 -07:00
|
|
|
$asset->eol_explicit = false;
|
2023-07-24 14:41:25 -07:00
|
|
|
}
|
|
|
|
|
2023-10-13 04:18:19 -07:00
|
|
|
// determine if explicit and set eol_explicit to true
|
|
|
|
if (!is_null($asset->asset_eol_date) && !is_null($asset->purchase_date)) {
|
2023-10-22 09:23:30 -07:00
|
|
|
if($asset->model->eol > 0) {
|
2023-07-24 14:41:25 -07:00
|
|
|
$months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date);
|
|
|
|
if($months != $asset->model->eol) {
|
|
|
|
$asset->eol_explicit = true;
|
2023-09-06 15:01:00 -07:00
|
|
|
}
|
2023-07-24 14:41:25 -07:00
|
|
|
}
|
2023-09-06 15:01:00 -07:00
|
|
|
} elseif (!is_null($asset->asset_eol_date) && is_null($asset->purchase_date)) {
|
|
|
|
$asset->eol_explicit = true;
|
|
|
|
}
|
2023-10-22 09:23:30 -07:00
|
|
|
if ((!is_null($asset->asset_eol_date)) && (!is_null($asset->purchase_date)) && (is_null($asset->model->eol) || ($asset->model->eol == 0))) {
|
2023-09-13 15:16:17 -07:00
|
|
|
$asset->eol_explicit = true;
|
|
|
|
}
|
2023-07-12 22:43:48 -07:00
|
|
|
|
|
|
|
}
|
2017-06-15 20:54:14 -07:00
|
|
|
}
|