Skip to content

A free open source IT asset/license management system

Added accessor for component qty

snipebac8299ea696
diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php
index 6bf1aa50da..3cd79485c4 100755
--- a/app/Http/Controllers/Assets/AssetsController.php
+++ b/app/Http/Controllers/Assets/AssetsController.php
@@ -364,9 +364,9 @@ class AssetsController extends Controller
$total_asset_cost = ($asset->assignedAssets()?->AssetsForShow()) ? $asset->assignedAssets()?->AssetsForShow()?->sum('purchase_cost') : 0;
$total_license_cost = ($asset->licenses) ? $asset->licenses->sum('purchase_cost') : 0;
$total_accessory_cost = ($asset->accessories) ? $asset->accessories()->sum('purchase_cost') : 0;
- $total_component_cost = ($asset->components) ? $asset->components->sum('purchase_cost') : 0;
+ $total_component_cost = ($asset->components) ? $asset->components->sum('calculated_purchase_cost') : 0;
- $total_cost_for_asset = $total_license_cost + $total_asset_cost + $total_maintenance_cost;
+ $total_cost_for_asset = $asset->purchase_cost + $total_maintenance_cost + $total_asset_cost + $total_license_cost + $total_accessory_cost + $total_component_cost;
return view('hardware/view', compact('asset', 'qr_code', 'settings'))
->with('total_maintenance_cost', $total_maintenance_cost)
diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php
index ee81d2c04c..ae816c34fe 100644
--- a/app/Http/Transformers/AssetsTransformer.php
+++ b/app/Http/Transformers/AssetsTransformer.php
@@ -192,8 +192,8 @@ class AssetsTransformer
'pivot_id' => $component->pivot->id,
'name' => e($component->name),
'qty' => $component->pivot->assigned_qty,
- 'price_cost' => $component->purchase_cost,
- 'purchase_total' => $component->purchase_cost * $component->pivot->assigned_qty,
+ 'purchase_cost' => $component->purchase_cost,
+ 'purchase_total' => $component->calculated_purchase_cost,
'checkout_date' => Helper::getFormattedDateObject($component->pivot->created_at, 'datetime'),
];
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index d0fe5648e5..ffb8b453a9 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -1264,12 +1264,7 @@ class Asset extends Depreciable
public function getComponentCost()
{
- $cost = 0;
- foreach ($this->components as $component) {
- $cost += $component->pivot->assigned_qty * $component->purchase_cost;
- }
-
- return $cost;
+ return (float) $this->components->sum('calculated_purchase_cost');
}
public function getAccessoryCost()
diff --git a/app/Models/Component.php b/app/Models/Component.php
index e2a581fb5f..2adc8ffefb 100644
--- a/app/Models/Component.php
+++ b/app/Models/Component.php
@@ -8,6 +8,7 @@ use App\Models\Traits\Loggable;
use App\Models\Traits\Searchable;
use App\Presenters\ComponentPresenter;
use App\Presenters\Presentable;
+use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\Relation;
@@ -165,6 +166,26 @@ class Component extends SnipeModel
return $this->belongsToMany(Asset::class, 'components_assets')->withPivot('id', 'assigned_qty', 'created_at', 'created_by', 'note');
}
+ protected function calculatedPurchaseCost(): Attribute
+ {
+ return Attribute::make(
+ get: function ($value) {
+ $unitPurchaseCost = $this->getRawOriginal('purchase_cost');
+ $assignedQty = $this->pivot?->assigned_qty;
+
+ if ($unitPurchaseCost === null) {
+ return $assignedQty !== null ? 0.0 : null;
+ }
+
+ if ($assignedQty !== null) {
+ return (float) $unitPurchaseCost * (int) $assignedQty;
+ }
+
+ return (float) $unitPurchaseCost;
+ }
+ );
+ }
+
/**
* Establishes the component -> company relationship
*
diff --git a/tests/Unit/AssetTest.php b/tests/Unit/AssetTest.php
index f969ede119..ff792c337d 100644
--- a/tests/Unit/AssetTest.php
+++ b/tests/Unit/AssetTest.php
@@ -8,6 +8,7 @@ use App\Models\AccessoryCheckout;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Category;
+use App\Models\Component;
use App\Models\Setting;
use App\Models\Statuslabel;
use App\Models\User;
@@ -324,4 +325,49 @@ class AssetTest extends TestCase
$this->assertCount(3, $asset->accessories()->get());
$this->assertSame(35.0, $asset->getAccessoryCost());
}
+
+ public function test_asset_components_calculated_total_uses_assigned_quantity(): void
+ {
+ $asset = Asset::factory()->create();
+
+ $firstComponent = Component::factory()->create(['purchase_cost' => 10]);
+ $secondComponent = Component::factory()->create(['purchase_cost' => 25]);
+
+ $asset->components()->attach($firstComponent->id, [
+ 'assigned_qty' => 3,
+ 'created_by' => User::factory()->create()->id,
+ 'created_at' => now(),
+ ]);
+
+ $asset->components()->attach($secondComponent->id, [
+ 'assigned_qty' => 2,
+ 'created_by' => User::factory()->create()->id,
+ 'created_at' => now(),
+ ]);
+
+ $freshAsset = $asset->fresh();
+
+ $this->assertEquals(35, $freshAsset->components->sum('purchase_cost'));
+ $this->assertEquals(80, $freshAsset->components->sum('calculated_purchase_cost'));
+ $this->assertSame(80.0, $freshAsset->getComponentCost());
+ }
+
+ public function test_asset_components_calculated_total_treats_null_purchase_cost_as_zero(): void
+ {
+ $asset = Asset::factory()->create();
+
+ $componentWithoutCost = Component::factory()->create(['purchase_cost' => null]);
+
+ $asset->components()->attach($componentWithoutCost->id, [
+ 'assigned_qty' => 4,
+ 'created_by' => User::factory()->create()->id,
+ 'created_at' => now(),
+ ]);
+
+ $freshAsset = $asset->fresh();
+
+ $this->assertSame(0.0, $freshAsset->components->first()->calculated_purchase_cost);
+ $this->assertEquals(0, $freshAsset->components->sum('calculated_purchase_cost'));
+ $this->assertSame(0.0, $freshAsset->getComponentCost());
+ }
}