Skip to content

A free open source IT asset/license management system

Accessories/Consumables/Components - add order numbers to table display

snipe59a371d99e27
diff --git a/app/Http/Transformers/AccessoriesTransformer.php b/app/Http/Transformers/AccessoriesTransformer.php
index 99dece00a6..84154b5555 100644
--- a/app/Http/Transformers/AccessoriesTransformer.php
+++ b/app/Http/Transformers/AccessoriesTransformer.php
@@ -66,11 +66,14 @@ class AccessoriesTransformer
'purchase_date' => ($lastDefaults['purchase_date'] ?? null) ? Helper::getFormattedDateObject($lastDefaults['purchase_date'], 'date') : null,
'purchase_cost' => Helper::formatCurrencyOutput($lastDefaults['unit_cost'] ?? null),
'total_cost' => Helper::formatCurrencyOutput($accessory->totalCostSum()),
- // Parent-level order_number was renamed to legacy_order_number
- // and dropped from the transformer output. Historical order
- // numbers now live on QuantityAdjust action_log rows. API
- // consumers looking up a PO should query action_logs directly
- // or search via free-text (searchableRelations covers it).
+ // Distinct order numbers this accessory has been purchased on,
+ // pulled from the eager-loaded orderItems.order relation
+ // (Api\AccessoriesController::index preloads it). Replaces
+ // the removed parent-level order_number attribute. Feeds the
+ // datatable's ordersSummaryFormatter which renders empty /
+ // single / comma-list / first + "(+N more)" depending on
+ // count.
+ 'orders' => $accessory->orderItems->pluck('order.order_number')->filter()->unique()->values()->all(),
'min_qty' => ($accessory->min_amt) ? (int) $accessory->min_amt : null, // Legacy - should phase out - replaced by below, for the bootstrap table formatter
'min_amt' => ($accessory->min_amt) ? (int) $accessory->min_amt : null,
'remaining_qty' => (int) ($accessory->qty - $accessory->checkouts_count), // Legacy - should phase out - replaced by below, for the bootstrap table formatter
diff --git a/app/Http/Transformers/ComponentsTransformer.php b/app/Http/Transformers/ComponentsTransformer.php
index a37c9299a8..d77ea0117e 100644
--- a/app/Http/Transformers/ComponentsTransformer.php
+++ b/app/Http/Transformers/ComponentsTransformer.php
@@ -58,6 +58,12 @@ class ComponentsTransformer
'model_number' => ($component->model_number) ? e($component->model_number) : null,
// See AccessoriesTransformer for why order_number is no longer
// in the parent-level output.
+ //
+ // Distinct order numbers this component has been purchased on,
+ // pulled from the eager-loaded orderItems.order relation
+ // (Api\ComponentsController::index preloads it). Feeds the
+ // datatable's ordersSummaryFormatter.
+ 'orders' => $component->orderItems->pluck('order.order_number')->filter()->unique()->values()->all(),
'purchase_date' => ($lastDefaults['purchase_date'] ?? null) ? Helper::getFormattedDateObject($lastDefaults['purchase_date'], 'date') : null,
'purchase_cost' => Helper::formatCurrencyOutput($lastDefaults['unit_cost'] ?? null),
'total_cost' => Helper::formatCurrencyOutput($component->totalCostSum()),
diff --git a/app/Http/Transformers/ConsumablesTransformer.php b/app/Http/Transformers/ConsumablesTransformer.php
index 7d815a15ad..5e888e7b12 100644
--- a/app/Http/Transformers/ConsumablesTransformer.php
+++ b/app/Http/Transformers/ConsumablesTransformer.php
@@ -63,6 +63,12 @@ class ConsumablesTransformer
'percent_remaining' => round($consumable->percentRemaining()),
// See AccessoriesTransformer for why order_number is no longer
// in the parent-level output.
+ //
+ // Distinct order numbers this consumable has been purchased on,
+ // pulled from the eager-loaded orderItems.order relation
+ // (Api\ConsumablesController::index preloads it). Feeds the
+ // datatable's ordersSummaryFormatter.
+ 'orders' => $consumable->orderItems->pluck('order.order_number')->filter()->unique()->values()->all(),
'purchase_cost' => Helper::formatCurrencyOutput($lastDefaults['unit_cost'] ?? null),
'total_cost' => Helper::formatCurrencyOutput($consumable->totalCostSum()),
'purchase_date' => ($lastDefaults['purchase_date'] ?? null) ? Helper::getFormattedDateObject($lastDefaults['purchase_date'], 'date') : null,
diff --git a/app/Presenters/AccessoryPresenter.php b/app/Presenters/AccessoryPresenter.php
index 7e11f1b56c..442a9d44fd 100644
--- a/app/Presenters/AccessoryPresenter.php
+++ b/app/Presenters/AccessoryPresenter.php
@@ -165,6 +165,18 @@ class AccessoryPresenter extends Presenter
'sortable' => true,
'title' => trans('general.last_unit_cost'),
'class' => 'text-right text-padding-number-cell',
+ ], [
+ // Field name matches the transformer key
+ // ('orders') and the HasOrders relation name so advanced
+ // search resolves straight through $searchableRelations.
+ 'field' => 'orders',
+ 'scope' => 'col',
+ 'searchable' => true,
+ 'sortable' => false,
+ 'switchable' => true,
+ 'visible' => true,
+ 'title' => trans('general.order_number'),
+ 'formatter' => 'ordersSummaryFormatter',
], [
'field' => 'total_cost',
'scope' => 'col',
diff --git a/app/Presenters/ComponentPresenter.php b/app/Presenters/ComponentPresenter.php
index f60306f0ea..b36ec89f37 100644
--- a/app/Presenters/ComponentPresenter.php
+++ b/app/Presenters/ComponentPresenter.php
@@ -152,6 +152,16 @@ class ComponentPresenter extends Presenter
'visible' => true,
'class' => 'text-right',
'footerFormatter' => 'sumFormatter',
+ ], [
+ // Field name matches transformer key + HasOrders relation).
+ 'field' => 'orders',
+ 'scope' => 'col',
+ 'searchable' => true,
+ 'sortable' => false,
+ 'switchable' => true,
+ 'visible' => true,
+ 'title' => trans('general.order_number'),
+ 'formatter' => 'ordersSummaryFormatter',
], [
'field' => 'total_cost',
'scope' => 'col',
diff --git a/app/Presenters/ConsumablePresenter.php b/app/Presenters/ConsumablePresenter.php
index 343c799456..6459e7bd8f 100644
--- a/app/Presenters/ConsumablePresenter.php
+++ b/app/Presenters/ConsumablePresenter.php
@@ -152,6 +152,16 @@ class ConsumablePresenter extends Presenter
'visible' => true,
'class' => 'text-right text-padding-number-cell',
'footerFormatter' => 'sumFormatter',
+ ], [
+ // Field name matches transformer key + HasOrders relation).
+ 'field' => 'orders',
+ 'scope' => 'col',
+ 'searchable' => true,
+ 'sortable' => false,
+ 'switchable' => true,
+ 'visible' => true,
+ 'title' => trans('general.order_number'),
+ 'formatter' => 'ordersSummaryFormatter',
], [
'field' => 'total_cost',
'scope' => 'col',
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index 71e0dc00d2..5d4f28bd71 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -2022,12 +2022,6 @@ parameters:
count: 1
path: app/Http/Controllers/Api/UploadedFilesController.php
- -
- message: '#^Access to an undefined property App\\Models\\Actionlog\:\:\$created_by\.$#'
- identifier: property.notFound
- count: 1
- path: app/Http/Controllers/Api/UsersController.php
-
-
message: '#^Call to an undefined method Illuminate\\Database\\Eloquent\\Builder\:\:withoutTrashed\(\)\.$#'
identifier: method.notFound
@@ -2070,12 +2064,6 @@ parameters:
count: 1
path: app/Http/Controllers/Api/UsersController.php
- -
- message: '#^Access to an undefined property App\\Models\\Actionlog\:\:\$created_by\.$#'
- identifier: property.notFound
- count: 1
- path: app/Http/Controllers/AssetModelsController.php
-
-
message: '#^Access to an undefined property App\\Models\\AssetModel\:\:\$created_by\.$#'
identifier: property.notFound
@@ -3403,29 +3391,28 @@ parameters:
path: app/Http/Controllers/Reports/CustomComponentReportController.php
-
- message: '#^Call to an undefined method Illuminate\\Database\\Eloquent\\Builder\:\:onlyTrashed\(\)\.$#'
+ message: '#^Call to an undefined method Illuminate\\Database\\Eloquent\\Builder\:\:withTrashed\(\)\.$#'
identifier: method.notFound
count: 1
- path: app/Http/Controllers/Reports/CustomConsumableReportController.php
+ path: app/Http/Controllers/Reports/CustomComponentReportController.php
-
- message: '#^Call to an undefined method Illuminate\\Database\\Eloquent\\Builder\:\:withTrashed\(\)\.$#'
- identifier: method.notFound
+ message: '#^Called ''env'' outside of the config directory which returns null when the config is cached, use ''config''\.$#'
+ identifier: larastan.noEnvCallsOutsideOfConfig
count: 1
path: app/Http/Controllers/Reports/CustomComponentReportController.php
-
- message: '#^Call to an undefined method Illuminate\\Database\\Eloquent\\Builder\:\:withTrashed\(\)\.$#'
+ message: '#^Call to an undefined method Illuminate\\Database\\Eloquent\\Builder\:\:onlyTrashed\(\)\.$#'
identifier: method.notFound
count: 1
path: app/Http/Controllers/Reports/CustomConsumableReportController.php
-
- message: '#^Called ''env'' outside of the config directory which returns null when the config is cached, use ''config''\.$#'
- identifier: larastan.noEnvCallsOutsideOfConfig
+ message: '#^Call to an undefined method Illuminate\\Database\\Eloquent\\Builder\:\:withTrashed\(\)\.$#'
+ identifier: method.notFound
count: 1
- path: app/Http/Controllers/Reports/CustomComponentReportController.php
-
+ path: app/Http/Controllers/Reports/CustomConsumableReportController.php
-
message: '#^Access to an undefined property App\\Models\\Asset\:\:\$assigned\.$#'
@@ -3844,7 +3831,7 @@ parameters:
-
message: '#^Access to an undefined property App\\Models\\Actionlog\:\:\$created_by\.$#'
identifier: property.notFound
- count: 2
+ count: 1
path: app/Http/Controllers/Users/UsersController.php
-
@@ -4225,12 +4212,6 @@ parameters:
count: 1
path: app/Http/Transformers/MaintenancesTransformer.php
- -
- message: '#^Using nullsafe property access on non\-nullable type mixed\. Use \-\> instead\.$#'
- identifier: nullsafe.neverNull
- count: 3
- path: app/Http/Transformers/MaintenancesTransformer.php
-
-
message: '#^Access to an undefined property App\\Models\\Manufacturer\:\:\$accessories_count\.$#'
identifier: property.notFound
@@ -4561,12 +4542,6 @@ parameters:
count: 2
path: app/Importer/Importer.php
- -
- message: '#^Access to an undefined property App\\Importer\\Importer\:\:\$send_welcome\.$#'
- identifier: property.notFound
- count: 1
- path: app/Importer/Importer.php
-
-
message: '#^If condition is always true\.$#'
identifier: if.alwaysTrue
@@ -9268,13 +9243,7 @@ parameters:
-
message: '#^Using nullsafe property access "\?\-\>name" on left side of \?\? is unnecessary\. Use \-\> instead\.$#'
identifier: nullsafe.neverNull
- count: 1
- path: app/Notifications/CheckinAssetNotification.php
-
- -
- message: '#^Using nullsafe property access on non\-nullable type mixed\. Use \-\> instead\.$#'
- identifier: nullsafe.neverNull
- count: 1
+ count: 2
path: app/Notifications/CheckinAssetNotification.php
-
@@ -11029,6 +10998,12 @@ parameters:
count: 1
path: app/Services/PredefinedKitCheckoutService.php
+ -
+ message: '#^Using nullsafe property access "\?\-\>name" on left side of \?\? is unnecessary\. Use \-\> instead\.$#'
+ identifier: nullsafe.neverNull
+ count: 4
+ path: app/Services/PredefinedKitCheckoutService.php
+
-
message: '#^Call to method getAttributes\(\) on an unknown class App\\Services\\OneLogin\\Saml2\\Auth\.$#'
identifier: class.notFound
@@ -11760,10 +11735,3 @@ parameters:
identifier: match.unhandled
count: 2
path: routes/web.php
-
-
- -
- message: '#^Using nullsafe property access "\?\-\>name" on left side of \?\? is unnecessary\. Use \-\> instead\.$#'
- identifier: nullsafe.neverNull
- count: 4
- path: app/Services/PredefinedKitCheckoutService.php
diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php
index 461ee15657..f41cf535f7 100644
--- a/resources/views/partials/bootstrap-table.blade.php
+++ b/resources/views/partials/bootstrap-table.blade.php
@@ -2529,6 +2529,37 @@
}
}
+ // Compact display for the `orders` array on Accessory / Consumable /
+ // Component rows. These items can be tied to many orders as quantity
+ // is re-ordered over time, so a raw list would overwhelm the table
+ // on high-volume rows. Renders based on count:
+ // 0 empty cell
+ // 1 the single order number
+ // 2-3 comma-separated list
+ // 4+ first order number + "(+N more)" with the full list as tooltip
+ // Every rendered order number is passed through jQuery text() to
+ // defuse any HTML in the order_number string.
+ function ordersSummaryFormatter(value) {
+ if (!Array.isArray(value) || value.length === 0) {
+ return '';
+ }
+
+ var esc = function (v) {
+ return $('<div/>').text(v == null ? '' : v).html();
+ };
+
+ if (value.length === 1) {
+ return esc(value[0]);
+ }
+
+ if (value.length <= 3) {
+ return value.map(esc).join(', ');
+ }
+
+ var tooltip = esc(value.join(', '));
+ return esc(value[0]) + ' <span class="text-muted" data-tooltip="true" title="' + tooltip + '">(+' + (value.length - 1) + ' more)</span>';
+ }
+
// Check if checkbox should be selectable
// Selectability is determined by the API field "selectable" which is set at the Presenter/API Transformer
// However since different bulk actions have different requirements, we have to walk through the available_actions object
diff --git a/tests/Feature/Accessories/Api/IndexAccessoryTest.php b/tests/Feature/Accessories/Api/IndexAccessoryTest.php
index 59a1c0a952..a27d458583 100644
--- a/tests/Feature/Accessories/Api/IndexAccessoryTest.php
+++ b/tests/Feature/Accessories/Api/IndexAccessoryTest.php
@@ -8,6 +8,8 @@ use App\Models\Category;
use App\Models\Company;
use App\Models\Location;
use App\Models\Manufacturer;
+use App\Models\Order;
+use App\Models\OrderItem;
use App\Models\Supplier;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
@@ -192,4 +194,62 @@ class IndexAccessoryTest extends TestCase implements TestsFullMultipleCompaniesS
$ascRelevant = array_values(array_intersect($ascNames, ['Lots left', 'Some left', 'None left']));
$this->assertSame(['None left', 'Some left', 'Lots left'], $ascRelevant);
}
+
+ public function test_index_response_includes_distinct_order_numbers_per_accessory()
+ {
+ // The Accessory observer writes one Order + OrderItem per create()
+ // with a null order_number. Set explicit order numbers on those
+ // seed rows so the transformer's `orders` array has non-null
+ // values to surface. Multiple orders on one accessory must land
+ // in the output as a distinct list (feeds ordersSummaryFormatter).
+ $user = User::factory()->viewAccessories()->create();
+
+ $accessory = Accessory::factory()->create(['name' => 'Accessory With Orders']);
+ $accessory->orderItems()->first()->order->update(['order_number' => 'ORD-1001']);
+
+ $secondOrder = Order::create(['order_number' => 'ORD-1002']);
+ OrderItem::create([
+ 'order_id' => $secondOrder->id,
+ 'item_type' => Accessory::class,
+ 'item_id' => $accessory->id,
+ 'qty' => 5,
+ 'price' => 10,
+ ]);
+
+ $rows = $this->actingAsForApi($user)
+ ->getJson(route('api.accessories.index'))
+ ->assertOk()
+ ->json('rows');
+
+ $row = collect($rows)->firstWhere('id', $accessory->id);
+
+ $this->assertNotNull($row);
+ $this->assertIsArray($row['orders']);
+ $this->assertEqualsCanonicalizing(['ORD-1001', 'ORD-1002'], $row['orders']);
+ }
+
+ public function test_advanced_search_filter_on_order_number_returns_only_matching_accessories()
+ {
+ // Column is wired to searchableRelations['orders' => ['order_number']],
+ // so a `filter[orders]=X` query threads through the Searchable
+ // trait to a whereHas('orders') LIKE match on orders.order_number.
+ // Prefix operators (is:, not:, is:null, is:not_null) are honored
+ // by the same code path; this test pins the default LIKE path so
+ // the relation is provably wired.
+ $user = User::factory()->superuser()->create();
+
+ $targetAccessory = Accessory::factory()->create(['name' => 'Target']);
+ $otherAccessory = Accessory::factory()->create(['name' => 'Other']);
+
+ $targetAccessory->orderItems()->first()->order->update(['order_number' => 'ADV-SEARCH-777']);
+ $otherAccessory->orderItems()->first()->order->update(['order_number' => 'UNRELATED-999']);
+
+ $this->actingAsForApi($user)
+ ->getJson(route('api.accessories.index', [
+ 'filter' => json_encode(['orders' => 'ADV-SEARCH-777']),
+ ]))
+ ->assertOk()
+ ->assertResponseContainsInRows($targetAccessory)
+ ->assertResponseDoesNotContainInRows($otherAccessory);
+ }
}