From 67dc202e42f58aef9e4b13ca443829795518df2f Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 13 Aug 2024 12:33:48 -0700 Subject: [PATCH] makes model purchase a suggestion on create --- .../Controllers/AssetModelsController.php | 7 +----- .../Controllers/Assets/AssetsController.php | 21 +++-------------- app/Models/Asset.php | 13 ----------- ...432_adds_purchase_cost_to_models_table.php | 23 ------------------- resources/lang/en-US/general.php | 3 ++- resources/views/models/edit.blade.php | 2 +- .../forms/edit/purchase_cost.blade.php | 13 +++++++---- 7 files changed, 15 insertions(+), 67 deletions(-) diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index 4d13878f87..324ee239fa 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -174,12 +174,7 @@ class AssetModelsController extends Controller ->update(['asset_eol_date' => DB::raw('null')]); } } - if($model->wasChanged('purchase_cost')){ - if ($model->purchase_cost !== '') { - $model->assets()->whereNotNull('purchase_cost')->where('purchase_cost_explicit', false) - ->update(['purchase_cost' => $model->purchase_cost] ); - } - } + return redirect()->route('models.index')->with('success', trans('admin/models/message.update.success')); } diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php index 0c15d731f5..77876ad29c 100755 --- a/app/Http/Controllers/Assets/AssetsController.php +++ b/app/Http/Controllers/Assets/AssetsController.php @@ -135,13 +135,7 @@ class AssetsController extends Controller $asset->user_id = Auth::id(); $asset->status_id = request('status_id'); $asset->warranty_months = request('warranty_months', null); - if($request->filled('purchase_cost')) { - $asset->purchase_cost = $request->input('purchase_cost', null); - } - else if($asset->model->purchase_cost !== null) { - $asset->purchase_cost = $asset->model->purchase_cost; - } - $asset->purchase_cost_explicit = Asset::purchaseCostExplicit($asset, $request); + $asset->purchase_cost = request('purchase_cost') ? request('purchase_cost') : $asset->model?->purchase_cost; $asset->purchase_date = request('purchase_date', null); $asset->asset_eol_date = request('asset_eol_date', null); $asset->assigned_to = request('assigned_to', null); @@ -309,20 +303,11 @@ class AssetsController extends Controller $asset->status_id = $request->input('status_id', null); $asset->warranty_months = $request->input('warranty_months', null); - if($request->filled('purchase_cost')) { - $asset->purchase_cost = $request->input('purchase_cost', null); - } - else if($asset->model->purchase_cost !== null) { - $asset->purchase_cost = $asset->model->purchase_cost; - } - else{ - $asset->purchase_cost = null; - } - $asset->purchase_cost_explicit = Asset::purchaseCostExplicit($asset, $request); + $asset->purchase_cost = $request->input('purchase_cost', null); $asset->purchase_date = $request->input('purchase_date', null); $asset->next_audit_date = $request->input('next_audit_date', null); if ($request->filled('purchase_date') && !$request->filled('asset_eol_date') && ($asset->model->eol > 0)) { - $asset->purchase_date = $request->input('purchase_date', null); + $asset->purchase_date = $request->input('purchase_date', null); $asset->asset_eol_date = Carbon::parse($request->input('purchase_date'))->addMonths($asset->model->eol)->format('Y-m-d'); $asset->eol_explicit = false; } elseif ($request->filled('asset_eol_date')) { diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 4b1e73b4ea..c64fc50266 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -968,19 +968,6 @@ class Asset extends Depreciable } return $cost; } - public static function purchaseCostExplicit($asset, $request) - { - if ($request->input('purchase_cost')) { - if ($asset->model->purchase_cost) { - if ($request->input('purchase_cost') !== $asset->model->purchase_cost) { - return true; - } - return false; - } - } - return false; - } - /** * ----------------------------------------------- * BEGIN MUTATORS diff --git a/database/migrations/2024_08_12_173432_adds_purchase_cost_to_models_table.php b/database/migrations/2024_08_12_173432_adds_purchase_cost_to_models_table.php index a0cb0e5df7..2e9f2af258 100644 --- a/database/migrations/2024_08_12_173432_adds_purchase_cost_to_models_table.php +++ b/database/migrations/2024_08_12_173432_adds_purchase_cost_to_models_table.php @@ -12,28 +12,9 @@ return new class extends Migration */ public function up(): void { - Schema::table('assets', function (Blueprint $table) { - if (!Schema::hasColumn('assets', 'purchase_cost_explicit')) { - $table->boolean('purchase_cost_explicit')->default(false)->after('purchase_cost'); - } - }); - Schema::table('models', function (Blueprint $table) { $table->decimal('purchase_cost', 8, 2)->default(null)->nullable()->after('min_amt'); }); - - Asset::whereNotNull('purchase_cost')->with('model')->chunkById(500, function ($assetsWithPurchaseCosts) { - foreach ($assetsWithPurchaseCosts as $asset) { - if($asset->purchase_cost) { - if ($asset->purchase_cost !== $asset->model->purchase_cost) { - DB::table('assets')->where('id', $asset->id)->update(['purchase_cost_explicit' => true]); - } - } - else { - DB::table('assets')->where('id', $asset->id)->update(['purchase_cost_explicit' => false]); - } - } - }); } /** @@ -41,10 +22,6 @@ return new class extends Migration */ public function down(): void { - Schema::table('assets', function (Blueprint $table) { - $table->dropColumn('purchase_cost_explicit'); - }); - Schema::table('models', function (Blueprint $table) { $table->dropColumn('purchase_cost'); }); diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.php index 255a1564de..25ec1edded 100644 --- a/resources/lang/en-US/general.php +++ b/resources/lang/en-US/general.php @@ -225,7 +225,8 @@ return [ 'previous' => 'Previous', 'processing' => 'Processing', 'profile' => 'Your profile', - 'purchase_cost' => 'Purchase Cost', + 'purchase_cost' => 'Purchase Cost', + 'purchase_cost_help' => 'Leave purchase cost blank if the model has a purchase cost you would like to use.', 'purchase_date' => 'Purchase Date', 'qty' => 'QTY', 'quantity' => 'Quantity', diff --git a/resources/views/models/edit.blade.php b/resources/views/models/edit.blade.php index 8080cab91b..62ba3fcda5 100755 --- a/resources/views/models/edit.blade.php +++ b/resources/views/models/edit.blade.php @@ -43,4 +43,4 @@ @include ('partials.forms.edit.image-upload', ['image_path' => app('models_upload_path')]) -@stop \ No newline at end of file +@stop diff --git a/resources/views/partials/forms/edit/purchase_cost.blade.php b/resources/views/partials/forms/edit/purchase_cost.blade.php index 449fecc3a5..53f90a73b2 100644 --- a/resources/views/partials/forms/edit/purchase_cost.blade.php +++ b/resources/views/partials/forms/edit/purchase_cost.blade.php @@ -5,14 +5,17 @@
- @if (isset($currency_type)) - {{ $currency_type }} - @else - {{ $snipeSettings->default_currency }} - @endif + @if (isset($currency_type)) + {{ $currency_type }} + @else + {{ $snipeSettings->default_currency }} + @endif
+ @if(Route::is('hardware.create')) +

{{trans('general.purchase_cost_help')}}

+ @endif {!! $errors->first('purchase_cost', '') !!}