From 8456b3ec0c9c88ef9de24b615eb2a942d9731811 Mon Sep 17 00:00:00 2001 From: slong753 Date: Thu, 13 Jul 2023 00:43:48 -0500 Subject: [PATCH] wip stuff --- app/Observers/AssetObserver.php | 11 ++++ ...add_column_for_explicit_date_to_assets.php | 53 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 database/migrations/2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php index 636f159b65..cf963f78e1 100644 --- a/app/Observers/AssetObserver.php +++ b/app/Observers/AssetObserver.php @@ -119,4 +119,15 @@ class AssetObserver $logAction->user_id = Auth::id(); $logAction->logaction('delete'); } + + public function saving(Asset $asset) + { + //turning this off for right now so i can check out the note in the migration + + //calculate and set the EOL date if it is not already set + // if(is_null($asset->asset_eol_date) && !is_null($asset->asset_purchase_date) && !is_null($asset->model()->get()->eol)){ + // $asset->asset_eol_date = date('Y-m-d', strtotime($asset->asset_purchase_date . ' + ' . $asset->asset_warranty_months . ' months')); + // } + + } } diff --git a/database/migrations/2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php b/database/migrations/2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php new file mode 100644 index 0000000000..8975eeeba5 --- /dev/null +++ b/database/migrations/2023_07_13_052204_denormalized_eol_and_add_column_for_explicit_date_to_assets.php @@ -0,0 +1,53 @@ +date('eol_explicit')->after('asset_eol_date')->nullable(); + }); + + // this is really just a scratch pad for the next step... but it might actually work? + // need to check out some things before trying it out, specifically whether or not + // asset_eol_date is only actually set when it's custom + $customEolAssets = DB::table('assets')->whereNotNull('eol_explicit')->get(); + foreach ($customEolAssets as $asset) { + DB::table('assets')->where('id', $asset->id)->update(['asset_eol_date' => $asset->eol_explicit]); + } + + $assets = DB::table('assets')->whereNull('asset_eol_date')->get(); + foreach ($assets as $asset) { + $model = DB::table('models')->where('id', $asset->model_id)->first(); + if ($model) { + $eol = $model->eol; + if ($eol) { + $asset_eol_date = date('Y-m-d', strtotime($asset->asset_purchase_date . ' + ' . $eol . ' months')); + DB::table('assets')->where('id', $asset->id)->update(['asset_eol_date' => $asset_eol_date]); + } + } + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('assets', function (Blueprint $table) { + // + }); + } +}