mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-13 09:04:09 -08:00
Merge remote-tracking branch 'origin/develop'
This commit is contained in:
commit
dcdc294135
|
@ -120,17 +120,30 @@ class AssetObserver
|
||||||
$logAction->user_id = Auth::id();
|
$logAction->user_id = Auth::id();
|
||||||
$logAction->logaction('delete');
|
$logAction->logaction('delete');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @see https://github.com/snipe/snipe-it/issues/13723#issuecomment-1761315938
|
||||||
|
*/
|
||||||
public function saving(Asset $asset)
|
public function saving(Asset $asset)
|
||||||
{
|
{
|
||||||
//determine if calculated eol and then calculate it - this should only happen on a new asset
|
// determine if calculated eol and then calculate it - this should only happen on a new asset
|
||||||
if(is_null($asset->asset_eol_date) && !is_null($asset->purchase_date) && !is_null($asset->model->eol)){
|
if (is_null($asset->asset_eol_date) && !is_null($asset->purchase_date) && !is_null($asset->model->eol)){
|
||||||
$asset->asset_eol_date = $asset->purchase_date->addMonths($asset->model->eol)->format('Y-m-d');
|
$asset->asset_eol_date = $asset->purchase_date->addMonths($asset->model->eol)->format('Y-m-d');
|
||||||
$asset->eol_explicit = false;
|
$asset->eol_explicit = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//determine if explicit and set eol_explit to true
|
// determine if explicit and set eol_explicit to true
|
||||||
if(!is_null($asset->asset_eol_date) && !is_null($asset->purchase_date)) {
|
if (!is_null($asset->asset_eol_date) && !is_null($asset->purchase_date)) {
|
||||||
if($asset->model->eol) {
|
if($asset->model->eol) {
|
||||||
$months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date);
|
$months = Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date);
|
||||||
if($months != $asset->model->eol) {
|
if($months != $asset->model->eol) {
|
||||||
|
|
|
@ -17,19 +17,30 @@ class AddEolDateOnAssetsTable extends Migration
|
||||||
{
|
{
|
||||||
|
|
||||||
Schema::table('assets', function (Blueprint $table) {
|
Schema::table('assets', function (Blueprint $table) {
|
||||||
|
|
||||||
if (!Schema::hasColumn('assets', 'asset_eol_date')) {
|
if (!Schema::hasColumn('assets', 'asset_eol_date')) {
|
||||||
$table->date('asset_eol_date')->after('purchase_date')->nullable()->default(null);
|
$table->date('asset_eol_date')->after('purchase_date')->nullable()->default(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is a temporary shim so we don't have to modify the asset observer for migrations where
|
||||||
|
// there is a large version difference. (See the AssetObserver notes). This column gets created
|
||||||
|
// later in 2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php
|
||||||
|
// but we have to temporarily create it now so the save method below doesn't break
|
||||||
|
if (!Schema::hasColumn('assets', 'eol_explicit')) {
|
||||||
|
$table->boolean('eol_explicit')->default(false)->after('asset_eol_date');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Chunk the model query to get the models that do have an EOL date
|
// Chunk the model query to get the models that do have an EOL date
|
||||||
|
// We use saveQuietly() here to skip the AssetObserver, since it modifies fields
|
||||||
|
// that do not yet exist on the assets table.
|
||||||
AssetModel::whereNotNull('eol')->chunk(10, function ($models) {
|
AssetModel::whereNotNull('eol')->chunk(10, function ($models) {
|
||||||
foreach ($models as $model) {
|
foreach ($models as $model) {
|
||||||
foreach ($model->assets as $asset) {
|
foreach ($model->assets as $asset) {
|
||||||
|
|
||||||
if ($asset->purchase_date!='') {
|
if ($asset->purchase_date!='') {
|
||||||
$asset->asset_eol_date = $asset->present()->eol_date();
|
$asset->asset_eol_date = $asset->present()->eol_date();
|
||||||
$asset->save();
|
$asset->saveQuietly();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,9 @@ class DenormalizedEolAndAddColumnForExplicitDateToAssets extends Migration
|
||||||
public function up()
|
public function up()
|
||||||
{
|
{
|
||||||
Schema::table('assets', function (Blueprint $table) {
|
Schema::table('assets', function (Blueprint $table) {
|
||||||
$table->boolean('eol_explicit')->default(false)->after('asset_eol_date');
|
if (!Schema::hasColumn('assets', 'eol_explicit')) {
|
||||||
|
$table->boolean('eol_explicit')->default(false)->after('asset_eol_date');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue