updates assets when model is updated, renamed default_purchase_cost

This commit is contained in:
Godfrey M 2024-08-12 12:54:09 -07:00
parent a440f6f87c
commit 0c75d41751
5 changed files with 16 additions and 16 deletions

View file

@ -75,7 +75,7 @@ class AssetModelsController extends Controller
$model->name = $request->input('name');
$model->model_number = $request->input('model_number');
$model->min_amt = $request->input('min_amt');
$model->default_purchase_cost = $request->input('purchase_cost');
$model->purchase_cost = $request->input('purchase_cost');
$model->manufacturer_id = $request->input('manufacturer_id');
$model->category_id = $request->input('category_id');
$model->notes = $request->input('notes');
@ -147,7 +147,7 @@ class AssetModelsController extends Controller
$model->name = $request->input('name');
$model->model_number = $request->input('model_number');
$model->min_amt = $request->input('min_amt');
$model->default_purchase_cost = $request->input('purchase_cost');
$model->purchase_cost = $request->input('purchase_cost');
$model->manufacturer_id = $request->input('manufacturer_id');
$model->category_id = $request->input('category_id');
$model->notes = $request->input('notes');
@ -174,10 +174,10 @@ class AssetModelsController extends Controller
->update(['asset_eol_date' => DB::raw('null')]);
}
}
if($model->wasChanged('default_purchase_cost')){
if ($model->purchase_cost != '') {
$model->assets()->whereNotNull('purchase_cost')->where('cost_explicit', false)
->update(['purchase_cost']);
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'));

View file

@ -138,8 +138,8 @@ class AssetsController extends Controller
if($request->filled('purchase_cost')) {
$asset->purchase_cost = $request->input('purchase_cost', null);
}
else if($asset->model->default_purchase_cost !== null) {
$asset->purchase_cost = $asset->model->default_purchase_cost;
else if($asset->model->purchase_cost !== null) {
$asset->purchase_cost = $asset->model->purchase_cost;
}
$asset->purchase_cost_explicit = Asset::purchaseCostExplicit($asset, $request);
$asset->purchase_date = request('purchase_date', null);
@ -312,8 +312,8 @@ class AssetsController extends Controller
if($request->filled('purchase_cost')) {
$asset->purchase_cost = $request->input('purchase_cost', null);
}
else if($asset->model->default_purchase_cost !== null) {
$asset->purchase_cost = $asset->model->default_purchase_cost;
else if($asset->model->purchase_cost !== null) {
$asset->purchase_cost = $asset->model->purchase_cost;
}
else{
$asset->purchase_cost = null;

View file

@ -971,8 +971,8 @@ class Asset extends Depreciable
public static function purchaseCostExplicit($asset, $request)
{
if ($request->input('purchase_cost')) {
if ($asset->model->default_purchase_cost) {
if ($request->input('purchase_cost') !== $asset->model->default_purchase_cost) {
if ($asset->model->purchase_cost) {
if ($request->input('purchase_cost') !== $asset->model->purchase_cost) {
return true;
}
return false;

View file

@ -19,13 +19,13 @@ return new class extends Migration
});
Schema::table('models', function (Blueprint $table) {
$table->decimal('default_purchase_cost', 8, 2)->default(null)->nullable()->after('min_amt');
$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->default_purchase_cost) {
if ($asset->purchase_cost !== $asset->model->purchase_cost) {
DB::table('assets')->where('id', $asset->id)->update(['purchase_cost_explicit' => true]);
}
}
@ -46,7 +46,7 @@ return new class extends Migration
});
Schema::table('models', function (Blueprint $table) {
$table->dropColumn('default_purchase_cost');
$table->dropColumn('purchase_cost');
});
}
};

View file

@ -3,7 +3,7 @@
<label for="purchase_cost" class="col-md-3 control-label">{{ trans('general.purchase_cost') }}</label>
<div class="col-md-9">
<div class="input-group col-md-4" style="padding-left: 0px;">
<input class="form-control" type="text" name="purchase_cost" aria-label="purchase_cost" id="purchase_cost" value="{{ old('purchase_cost', Helper::formatCurrencyOutput($item->purchase_cost)) }}" />
<input class="form-control" type="text" name="purchase_cost" aria-label="purchase_cost" id="purchase_cost" value="{{ (old('purchase_cost', Helper::formatCurrencyOutput($item->purchase_cost))) }}" />
<span class="input-group-addon">
@if (isset($currency_type))
{{ $currency_type }}