A free open source IT asset/license management system
Fixed totals
snipecbae494c5436
diff --git a/app/Models/Asset.php b/app/Models/Asset.phpindex 2470d1ab80..d0fe5648e5 100644--- a/app/Models/Asset.php+++ b/app/Models/Asset.php@@ -761,23 +761,19 @@ class Asset extends Depreciable*/public function assignedAccessories(){- return $this->morphMany(AccessoryCheckout::class, 'assigned', 'assigned_type', 'assigned_to')->with('accessories');+ return $this->morphMany(AccessoryCheckout::class, 'assigned', 'assigned_type', 'assigned_to')->with('accessory');}public function accessories(){-- // return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');- //- // return $this->morphMany(AccessoryCheckout::class, 'assigned', 'assigned_type', 'assigned_to');-- return $this->hasManyThrough(Accessory::class, AccessoryCheckout::class, 'assigned_to', 'id')- ->where('assigned_type', self::ASSET);-- // return $this->hasManyThrough(AccessoryCheckout::class, AssetModel::class, 'id', 'id', 'model_id', 'accessory_id');-- // return $this->belongsToMany(AccessoryCheckout::class, 'components_assets', 'asset_id', 'component_id')- // ->withPivot('id', 'assigned_qty', 'created_at', 'note', 'created_by');+ return $this->hasManyThrough(+ Accessory::class,+ AccessoryCheckout::class,+ 'assigned_to',+ 'id',+ 'id',+ 'accessory_id'+ )->where('assigned_type', self::class);}// {@@ -1278,12 +1274,7 @@ class Asset extends Depreciablepublic function getAccessoryCost(){- $cost = 0;- foreach ($this->assignedAccessories() as $accessory) {- $cost += $accessory->pivot->assigned_qty * $accessory->purchase_cost;- }-- return $cost;+ return (float) $this->accessories()->sum('purchase_cost');}/**diff --git a/tests/Unit/AssetTest.php b/tests/Unit/AssetTest.phpindex 11468c2f48..f969ede119 100644--- a/tests/Unit/AssetTest.php+++ b/tests/Unit/AssetTest.php@@ -3,6 +3,8 @@namespace Tests\Unit;use App\Http\Controllers\Assets\BulkAssetsController;+use App\Models\Accessory;+use App\Models\AccessoryCheckout;use App\Models\Asset;use App\Models\AssetModel;use App\Models\Category;@@ -289,4 +291,37 @@ class AssetTest extends TestCase$this->assertEquals([$deployable->id], array_values($filtered));}++ public function test_asset_accessories_relationship_uses_accessory_checkout_rows(): void+ {+ $asset = Asset::factory()->create();+ $otherAsset = Asset::factory()->create();++ $primaryAccessory = Accessory::factory()->create(['purchase_cost' => 10]);+ $secondaryAccessory = Accessory::factory()->create(['purchase_cost' => 15]);++ AccessoryCheckout::factory()->create([+ 'accessory_id' => $primaryAccessory->id,+ 'assigned_to' => $asset->id,+ 'assigned_type' => Asset::class,+ ]);+ AccessoryCheckout::factory()->create([+ 'accessory_id' => $primaryAccessory->id,+ 'assigned_to' => $asset->id,+ 'assigned_type' => Asset::class,+ ]);+ AccessoryCheckout::factory()->create([+ 'accessory_id' => $secondaryAccessory->id,+ 'assigned_to' => $asset->id,+ 'assigned_type' => Asset::class,+ ]);+ AccessoryCheckout::factory()->create([+ 'accessory_id' => $primaryAccessory->id,+ 'assigned_to' => $otherAsset->id,+ 'assigned_type' => Asset::class,+ ]);++ $this->assertCount(3, $asset->accessories()->get());+ $this->assertSame(35.0, $asset->getAccessoryCost());+ }}