A free open source IT asset/license management system
Fixed #19568 - fixed user companies in custom asset report
snipea0aea63d6dae
diff --git a/app/Http/Controllers/Reports/CustomConsumableReportController.php b/app/Http/Controllers/Reports/CustomConsumableReportController.phpindex ac0b3108f9..a922a8022f 100644--- a/app/Http/Controllers/Reports/CustomConsumableReportController.php+++ b/app/Http/Controllers/Reports/CustomConsumableReportController.php@@ -181,7 +181,10 @@ class CustomConsumableReportController extends Controllerreturn [$user->display_name ?? '',- $user->company->name ?? '',+ $user->companies+ ->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)+ ->pluck('name')+ ->join(' | '),$user->pivot->created_at ?? '',$creator ? ($creator->display_name ?? '') : '',];@@ -290,7 +293,7 @@ class CustomConsumableReportController extends Controller'orderItems.order.supplier',]);- $request->whenFilled('include_assignments', fn () => $query->with('users.company'));+ $request->whenFilled('include_assignments', fn() => $query->with('users.companies'));$query = $this->appendLocalConstraints($query, $request, ['by_model_number' => 'consumables.model_number',diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.phpindex 034720fb78..08ba459b73 100644--- a/app/Http/Controllers/ReportsController.php+++ b/app/Http/Controllers/ReportsController.php@@ -745,9 +745,16 @@ class ReportsController extends Controller// do we scope here or??}- $assets = Asset::select('assets.*')->with(- 'location', 'status', 'company', 'defaultLoc', 'assignedTo',- 'model.category', 'model.manufacturer', 'model.fieldset.fields', 'supplier');+ $assets = Asset::select('assets.*')->with([+ 'location', 'status', 'company', 'defaultLoc',+ 'model.category', 'model.manufacturer', 'model.fieldset.fields', 'supplier',+ // assignedTo is a morphTo. The user_company column below+ // reads $assignee->companies when the assignee is a User,+ // and only User has a companies pivot. morphWith constrains+ // the .companies load to User targets so Assets / Locations+ // resolving through assignedTo don't blow up. See #19568.+ 'assignedTo' => fn ($morph) => $morph->morphWith([\App\Models\User::class => ['companies']]),+ ]);if ($request->filled('by_location_id')) {$assets->whereIn('assets.location_id', $request->input('by_location_id'));@@ -1026,7 +1033,15 @@ class ReportsController extends Controllerif ($request->filled('user_company')) {if ($asset->checkedOutToUser()) {- $row[] = ($asset->assignedto?->company) ? $asset->assignedto?->company?->display_name : '';+ // Under FMCS a user can belong to multiple companies via+ // the company_user pivot. Legacy $user->company reads the+ // scalar users.company_id mirror, which only holds ONE.+ // Report the full pivot set, alphabetized, pipe-joined.+ // See GH #19568.+ $row[] = $asset->assignedto?->companies+ ->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)+ ->pluck('name')+ ->join(' | ') ?? '';} else {$row[] = ''; // Empty string if unassigned}diff --git a/tests/Feature/Reporting/Custom/CustomAssetReportTest.php b/tests/Feature/Reporting/Custom/CustomAssetReportTest.phpindex f12829ab0d..65c86ba464 100644--- a/tests/Feature/Reporting/Custom/CustomAssetReportTest.php+++ b/tests/Feature/Reporting/Custom/CustomAssetReportTest.php@@ -105,6 +105,32 @@ class CustomAssetReportTest extends TestCase->assertSeeTextInStreamedResponse('Asset B');}+ public function test_user_company_column_lists_every_assignee_company_alphabetized_and_pipe_joined()+ {+ // GH #19568: the user_company column read the legacy scalar+ // company mirror on the assignee and only rendered ONE company+ // under FMCS. Report the full company_user pivot set,+ // alphabetized, pipe-joined.+ [$companyC, $companyA, $companyB] = Company::factory()->count(3)->sequence(+ ['name' => 'Zeta Co'],+ ['name' => 'alpha co'],+ ['name' => 'Beta Co'],+ )->create()->all();++ $assignee = User::factory()->create(['first_name' => 'Casey', 'last_name' => 'Multi']);+ $assignee->companies()->sync([$companyA->id, $companyB->id, $companyC->id]);++ Asset::factory()->assignedToUser($assignee)->create(['name' => 'Assigned Asset']);++ $this->actingAs(User::factory()->superuser()->create())+ ->post('reports/custom', [+ 'asset_name' => '1',+ 'user_company' => '1',+ ])+ ->assertOk()+ ->assertSeeTextInStreamedResponse('alpha co | Beta Co | Zeta Co');+ }+public function test_can_limit_assets_by_last_check_in(){Asset::factory()->create(['name' => 'Asset A', 'last_checkin' => '2023-08-01']);diff --git a/tests/Feature/Reporting/Custom/CustomConsumableReportTest.php b/tests/Feature/Reporting/Custom/CustomConsumableReportTest.phpindex 9a62abe453..9f871606b6 100644--- a/tests/Feature/Reporting/Custom/CustomConsumableReportTest.php+++ b/tests/Feature/Reporting/Custom/CustomConsumableReportTest.php@@ -687,6 +687,31 @@ class CustomConsumableReportTest extends TestCase]);}+ public function test_include_assignments_lists_every_user_company_alphabetized_and_pipe_joined()+ {+ // GH #19568: the user column read the legacy $user->company scalar+ // mirror and only rendered ONE company under FMCS. It should render+ // the full company_user pivot set, alphabetized, pipe-joined.+ [$companyC, $companyA, $companyB] = Company::factory()->count(3)->sequence(+ ['name' => 'Zeta Co'],+ ['name' => 'alpha co'],+ ['name' => 'Beta Co'],+ )->create()->all();++ $user = User::factory()->create(['first_name' => 'Casey', 'last_name' => 'Multi']);+ $user->companies()->sync([$companyA->id, $companyB->id, $companyC->id]);++ Consumable::factory()->checkedOutToUser($user)->create(['name' => 'Consumable A']);++ $this->sendRequest([+ 'consumable_name' => '1',+ 'include_assignments' => '1',+ ])+ ->assertOk()+ ->assertCsvHeader()+ ->assertSeeTextInStreamedResponse('alpha co | Beta Co | Zeta Co');+ }+public function test_custom_consumable_report_adheres_to_company_scoping_for_non_super_users(){[$companyA, $companyB] = Company::factory()->count(2)->create()->all();