Skip to content

A free open source IT asset/license management system

Fixed #19564 - added order info back into bulk asset edit

snipeab8324cab729
diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php
index 40ccc09ae3..c5ee21fa92 100644
--- a/app/Http/Controllers/Assets/BulkAssetsController.php
+++ b/app/Http/Controllers/Assets/BulkAssetsController.php
@@ -292,14 +292,12 @@ class BulkAssetsController extends Controller
* make sense (for example, changing the status ID to something incompatible with
* its checkout status.
*/
- // purchase_cost and order_number are intentionally not bulk-editable.
- // Assets carry no currency column, so bulk-writing purchase_cost on
- // rows that trace back to an Order can silently diverge from
- // order_items.price and misrepresent the currency of the original
- // purchase. Removing them from both the "should we build an update
- // array" trigger AND from the conditionallyAddItem chain below
- // prevents both UI-form and hand-crafted-POST callers from setting
- // them via bulk edit. Single-asset edit still allows both.
+ // purchase_cost and order_number bulk edits are allowed but
+ // carry a caveat: assets have no per-row currency column, so
+ // if the selection contains assets whose original orders were
+ // in different currencies, the value written here reads as
+ // the system default for every row. Callers are expected to
+ // narrow the selection to a single currency first. See #19564.
if (($request->filled('name'))
|| ($request->filled('purchase_date'))
|| ($request->filled('expected_checkin'))
@@ -313,6 +311,8 @@ class BulkAssetsController extends Controller
|| ($request->filled('notes'))
|| ($request->filled('next_audit_date'))
|| ($request->filled('asset_eol_date'))
+ || ($request->filled('order_number'))
+ || ($request->filled('purchase_cost'))
|| ($request->filled('null_name'))
|| ($request->filled('null_purchase_date'))
|| ($request->filled('null_expected_checkin_date'))
@@ -408,6 +408,10 @@ class BulkAssetsController extends Controller
$this->update_array['purchase_cost'] = $request->input('purchase_cost');
}
+ if ($request->filled('order_number')) {
+ $this->update_array['order_number'] = $request->input('order_number');
+ }
+
if ($request->filled('company_id')) {
$this->update_array['company_id'] = Company::getIdForCurrentUser($request->input('company_id'));
if ($request->input('company_id') == 'clear') {
diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php
index c1ee9faba7..7119dcdef9 100644
--- a/app/Observers/AssetObserver.php
+++ b/app/Observers/AssetObserver.php
@@ -177,6 +177,89 @@ class AssetObserver
}
}
+ /**
+ * Keep the asset's linked OrderItem (and its parent Order) in
+ * step with edits to `purchase_cost` / `order_number` so downstream
+ * order-side reads (lastOrderDefaults, order-based reports) don't
+ * diverge from the current asset row. Only fires when those
+ * specific fields actually change, so unrelated saves stay cheap.
+ *
+ * Scope note: this handles the two fields the bulk edit form
+ * writes on the order-details side. Broader drift on
+ * `supplier_id`, `purchase_date`, `company_id`, and the missing
+ * per-asset `currency` column are captured in the orders-sync
+ * follow-up. See GH #19564.
+ */
+ public function updated(Asset $asset): void
+ {
+ if (! $asset->wasChanged(['purchase_cost', 'order_number'])) {
+ return;
+ }
+
+ $orderItem = OrderItem::where('item_type', Asset::class)
+ ->where('item_id', $asset->id)
+ ->first();
+
+ if (! $orderItem) {
+ // Legacy assets that predate the orders refactor may not
+ // have an OrderItem row. Skip silently — the create-path
+ // is the only place that mints one.
+ return;
+ }
+
+ if ($asset->wasChanged('purchase_cost')) {
+ $orderItem->price = $asset->purchase_cost;
+ $orderItem->save();
+ }
+
+ if ($asset->wasChanged('order_number')) {
+ $newNumber = trim((string) ($asset->order_number ?? '')) ?: null;
+ $oldOrderId = $orderItem->order_id;
+
+ // Rebuild the (order_number, supplier_id, company_id)
+ // dedupe key from the create-path. A blank order_number
+ // becomes a distinct Order each time, matching create.
+ if ($newNumber !== null) {
+ $targetOrder = Order::firstOrNew(
+ [
+ 'order_number' => $newNumber,
+ 'supplier_id' => $asset->supplier_id,
+ 'company_id' => $asset->company_id,
+ ],
+ [
+ 'purchase_date' => $asset->purchase_date,
+ ],
+ );
+ if (! $targetOrder->exists) {
+ $targetOrder->created_by = auth()->id() ?? $asset->created_by;
+ $targetOrder->save();
+ }
+ } else {
+ $targetOrder = new Order([
+ 'order_number' => null,
+ 'supplier_id' => $asset->supplier_id,
+ 'company_id' => $asset->company_id,
+ 'purchase_date' => $asset->purchase_date,
+ ]);
+ $targetOrder->created_by = auth()->id() ?? $asset->created_by;
+ $targetOrder->save();
+ }
+
+ $orderItem->order_id = $targetOrder->id;
+ $orderItem->save();
+
+ // Clean up the previous Order if nothing else points at
+ // it. Avoids leaving orphan rows behind when a rename
+ // happens to be the only OrderItem on the old Order.
+ if ($oldOrderId && $oldOrderId !== $targetOrder->id) {
+ $oldOrder = Order::find($oldOrderId);
+ if ($oldOrder && $oldOrder->orderItems()->count() === 0) {
+ $oldOrder->delete();
+ }
+ }
+ }
+ }
+
/**
* Listen to the Asset deleting event.
*
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index afbcc66e2a..2b86c454f6 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -3534,12 +3534,6 @@ parameters:
count: 1
path: app/Http/Controllers/ReportsController.php
- -
- message: '#^Using nullsafe property access on non\-nullable type mixed\. Use \-\> instead\.$#'
- identifier: nullsafe.neverNull
- count: 2
- path: app/Http/Controllers/ReportsController.php
-
-
message: '#^Access to an undefined property App\\Models\\Actionlog\:\:\$created_by\.$#'
identifier: property.notFound
@@ -4668,12 +4662,6 @@ parameters:
count: 1
path: app/Importer/ItemImporter.php
- -
- message: '#^Method App\\Importer\\ItemImporter\:\:createOrFetchCompany\(\) should return int but returns null\.$#'
- identifier: return.type
- count: 1
- path: app/Importer/ItemImporter.php
-
-
message: '#^Method App\\Importer\\ItemImporter\:\:createOrFetchLocation\(\) should return App\\Models\\Location\|null but returns int\.$#'
identifier: return.type
@@ -8970,18 +8958,6 @@ parameters:
count: 1
path: app/Notifications/AuditNotification.php
- -
- message: '#^Access to an undefined property App\\Notifications\\CheckinAccessoryNotification\:\:\$admin\.$#'
- identifier: property.notFound
- count: 2
- path: app/Notifications/CheckinAccessoryNotification.php
-
- -
- message: '#^Constructor of class App\\Notifications\\CheckinAccessoryNotification has an unused parameter \$admin\.$#'
- identifier: constructor.unusedParameter
- count: 1
- path: app/Notifications/CheckinAccessoryNotification.php
-
-
message: '#^Ternary operator condition is always true\.$#'
identifier: ternary.alwaysTrue
@@ -9243,7 +9219,7 @@ parameters:
-
message: '#^Access to an undefined property App\\Models\\Asset\:\:\$created_by\.$#'
identifier: property.notFound
- count: 1
+ count: 3
path: app/Observers/AssetObserver.php
-
diff --git a/resources/views/hardware/bulk.blade.php b/resources/views/hardware/bulk.blade.php
index 95191571e0..16905d91ad 100755
--- a/resources/views/hardware/bulk.blade.php
+++ b/resources/views/hardware/bulk.blade.php
@@ -167,13 +167,29 @@
input_div_class="col-md-9 col-md-offset-3"
/>
- {{-- purchase_cost and order_number are intentionally not
- bulk-editable here. Assets have no currency column, so
- bulk-writing purchase_cost on rows that trace back to an
- Order can silently diverge from order_items.price and
- misrepresent the currency of the original purchase. The
- single-asset edit form still exposes both, and the
- controller enforces this omission on the POST side too. --}}
+ {{-- Purchase cost + Order number bulk edits. Assets
+ have no per-row currency column, so if the
+ selection contains assets whose original orders
+ were in different currencies, one numeric value
+ written here reads as the system default currency
+ for every row regardless. Behavior is preserved
+ from the pre-orders-refactor era and callers are
+ expected to narrow the selection to one currency
+ before using these fields. --}}
+ <x-form.row
+ :label="trans('general.order_number')"
+ name="order_number"
+ type="text"
+ :maxlength="191"
+ input_div_class="col-md-7 col-sm-12"
+ />
+
+ <x-form.row
+ :label="trans('general.purchase_cost')"
+ name="purchase_cost"
+ type="text"
+ input_div_class="col-md-4 col-sm-12"
+ />
<x-input.supplier-select
:label="trans('general.supplier')"
diff --git a/tests/Feature/Assets/AssetObserverOrderTest.php b/tests/Feature/Assets/AssetObserverOrderTest.php
index 1238b569f9..90eeacc053 100644
--- a/tests/Feature/Assets/AssetObserverOrderTest.php
+++ b/tests/Feature/Assets/AssetObserverOrderTest.php
@@ -99,4 +99,124 @@ class AssetObserverOrderTest extends TestCase
// their own Order rows even though supplier + company match.
$this->assertSame($ordersBefore + 2, Order::count());
}
+
+ public function test_asset_purchase_cost_change_syncs_to_linked_order_item(): void
+ {
+ // #19564 sync: editing purchase_cost on a saved asset (single
+ // edit, bulk edit, importer update, or API) must write through
+ // to the linked OrderItem's price so order-side reads don't
+ // diverge from the current asset row.
+ $supplier = Supplier::factory()->create();
+
+ $asset = Asset::factory()->create([
+ 'supplier_id' => $supplier->id,
+ 'order_number' => 'PO-EDIT-COST',
+ 'purchase_cost' => 100.00,
+ ]);
+
+ $asset->purchase_cost = 175.50;
+ $asset->save();
+
+ $line = OrderItem::where('item_type', Asset::class)
+ ->where('item_id', $asset->id)
+ ->firstOrFail();
+
+ $this->assertEquals(175.50, (float) $line->price);
+ }
+
+ public function test_asset_order_number_change_repoints_line_and_cleans_up_orphan(): void
+ {
+ // Rename an asset's order_number. The asset's OrderItem must
+ // detach from the old Order, attach to a firstOrNew'd Order
+ // matching the new (order_number, supplier_id, company_id)
+ // tuple, and the old Order must be deleted when it's left
+ // with no other OrderItems.
+ $supplier = Supplier::factory()->create();
+
+ $asset = Asset::factory()->create([
+ 'supplier_id' => $supplier->id,
+ 'order_number' => 'PO-OLD',
+ ]);
+
+ $oldOrderId = OrderItem::where('item_type', Asset::class)
+ ->where('item_id', $asset->id)
+ ->value('order_id');
+
+ $this->assertNotNull(Order::find($oldOrderId));
+
+ $asset->order_number = 'PO-NEW';
+ $asset->save();
+
+ $line = OrderItem::where('item_type', Asset::class)
+ ->where('item_id', $asset->id)
+ ->firstOrFail();
+
+ $newOrder = Order::find($line->order_id);
+
+ $this->assertSame('PO-NEW', $newOrder->order_number);
+ $this->assertNotSame($oldOrderId, $line->order_id);
+ $this->assertNull(Order::find($oldOrderId), 'The old Order should be deleted after the only OrderItem moves away.');
+ }
+
+ public function test_asset_order_number_change_preserves_old_order_when_it_has_siblings(): void
+ {
+ // Two assets share PO-SHARED. Rename asset A to PO-NEW. Old
+ // Order (PO-SHARED) must survive because asset B still points
+ // at it.
+ $supplier = Supplier::factory()->create();
+
+ $assetA = Asset::factory()->create([
+ 'supplier_id' => $supplier->id,
+ 'order_number' => 'PO-SHARED',
+ ]);
+ $assetB = Asset::factory()->create([
+ 'supplier_id' => $supplier->id,
+ 'company_id' => $assetA->company_id,
+ 'order_number' => 'PO-SHARED',
+ ]);
+
+ $sharedOrderId = OrderItem::where('item_type', Asset::class)
+ ->where('item_id', $assetA->id)
+ ->value('order_id');
+
+ $this->assertSame(
+ $sharedOrderId,
+ OrderItem::where('item_type', Asset::class)->where('item_id', $assetB->id)->value('order_id'),
+ 'Precondition: both assets share the same Order.',
+ );
+
+ $assetA->order_number = 'PO-NEW';
+ $assetA->save();
+
+ $this->assertNotNull(Order::find($sharedOrderId), 'The old Order must survive because asset B still references it.');
+ }
+
+ public function test_unrelated_save_does_not_touch_the_linked_order_item(): void
+ {
+ // Sanity: touching a field other than purchase_cost /
+ // order_number leaves the OrderItem's price and order_id
+ // completely untouched.
+ $supplier = Supplier::factory()->create();
+
+ $asset = Asset::factory()->create([
+ 'supplier_id' => $supplier->id,
+ 'order_number' => 'PO-NOTES',
+ 'purchase_cost' => 42.00,
+ ]);
+
+ $originalLine = OrderItem::where('item_type', Asset::class)
+ ->where('item_id', $asset->id)
+ ->firstOrFail();
+
+ $asset->notes = 'edited notes';
+ $asset->save();
+
+ $freshLine = OrderItem::where('item_type', Asset::class)
+ ->where('item_id', $asset->id)
+ ->firstOrFail();
+
+ $this->assertSame($originalLine->id, $freshLine->id);
+ $this->assertSame($originalLine->order_id, $freshLine->order_id);
+ $this->assertEquals((float) $originalLine->price, (float) $freshLine->price);
+ }
}