A free open source IT asset/license management system
Updated info panel order info for assets
snipe6bd05a42dbdf
diff --git a/resources/views/blade/info-panel/index.blade.php b/resources/views/blade/info-panel/index.blade.phpindex ba5c966ea3..834d536b19 100644--- a/resources/views/blade/info-panel/index.blade.php+++ b/resources/views/blade/info-panel/index.blade.php@@ -190,15 +190,17 @@@endif@php- // Prefer the most recent OrderItem's price + currency so the- // Unit Cost row reflects the last acquisition, not the- // initial-create parent value. Falls back to parent- // purchase_cost + location.currency for legacy rows that- // never got an OrderItem written.+ // Assets own purchase_cost as a canonical column on the parent+ // row and Accessories / Consumables / Components moved that+ // concept off the parent onto per-transaction OrderItem lines,+ // so read the parent's own value first and let the+ // Order-derived fallback fill in for the post-Orders inventory+ // types where the parent no longer carries a purchase_cost+ // column. See GH #19572.$lastOrderInfoPanel = method_exists($infoPanelObj, 'lastOrderDefaults')? $infoPanelObj->lastOrderDefaults(): null;- $unitCost = $lastOrderInfoPanel['unit_cost'] ?? $infoPanelObj->purchase_cost ?? null;+ $unitCost = $infoPanelObj->purchase_cost ?? $lastOrderInfoPanel['unit_cost'] ?? null;$unitCostCurrency = $lastOrderInfoPanel['currency']?? ($infoPanelObj->location->currency ?? null)?: $snipeSettings->default_currency;@@ -561,12 +563,19 @@@php- // Prefer the most recent OrderItem's parent Order.purchase_date- // so the info-panel reflects the LAST acquisition, not the- // initial-create parent value. Falls back to parent- // purchase_date for legacy rows without an OrderItem.- $displayPurchaseDate = ($lastOrderInfoPanel['purchase_date'] ?? null)- ?: $infoPanelObj->purchase_date;+ // Same canonical-parent-first rule as unit cost above. Assets+ // keep purchase_date on the parent as the source of truth and+ // the AssetObserver doesn't sync it back to Order because+ // multiple assets can share one Order row (dedupe by+ // order_number + supplier + company), so on that Order row+ // purchase_date is a write-once snapshot from the first+ // sibling asset's create. Reading the parent's canonical+ // column keeps the info-panel in step with the edit form.+ // Post-Orders inventory types (Accessory / Consumable /+ // Component) have no parent purchase_date column, so their+ // Order-derived fallback still wins. See GH #19572.+ $displayPurchaseDate = $infoPanelObj->purchase_date+ ?: ($lastOrderInfoPanel['purchase_date'] ?? null);@endphp@if ($displayPurchaseDate)<x-info-element>diff --git a/tests/Feature/Assets/Ui/ShowAssetTest.php b/tests/Feature/Assets/Ui/ShowAssetTest.phpindex 2c79c37f39..9b3ee22347 100644--- a/tests/Feature/Assets/Ui/ShowAssetTest.php+++ b/tests/Feature/Assets/Ui/ShowAssetTest.php@@ -4,6 +4,7 @@ namespace Tests\Feature\Assets\Ui;use App\Models\Actionlog;use App\Models\Asset;+use App\Models\OrderItem;use App\Models\User;use Tests\TestCase;@@ -62,6 +63,65 @@ class ShowAssetTest extends TestCase$response->assertSee('complete-maintenance', false);}+ public function test_info_panel_purchase_date_reflects_the_edited_asset_value_not_the_snapshot_from_create()+ {+ // GH #19572: the observer writes an Order + OrderItem at create+ // and does not sync purchase_date back on subsequent asset+ // edits (multiple assets can share one Order row, so+ // per-asset writes there would drag siblings along). Before+ // the fix, the info-panel preferred Order.purchase_date via+ // lastOrderDefaults() using ?:, so once the initial snapshot+ // carried any date the asset's own purchase_date was never+ // rendered again. Now the parent's canonical value wins.+ $asset = Asset::factory()->create([+ 'purchase_date' => '2020-01-07',+ ]);++ $asset->purchase_date = '2028-07-01';+ $asset->save();++ $this->actingAs(User::factory()->superuser()->create())+ ->get(route('hardware.show', $asset))+ ->assertOk()+ ->assertSee('2028-07-01')+ ->assertDontSee('2020-01-07');+ }++ public function test_info_panel_unit_cost_reflects_the_edited_asset_value_when_the_order_item_is_out_of_sync()+ {+ // GH #19572, unit cost row. The AssetObserver::updated hook syncs+ // purchase_cost onto the OrderItem for edits made post-#19564,+ // but assets edited before that hook landed still carry+ // divergent OrderItem.price values. This test simulates that+ // legacy drift by writing straight to the OrderItem row and+ // confirms the info-panel reads the asset's own purchase_cost+ // in preference to the stale line-item price.+ $asset = Asset::factory()->create([+ 'purchase_cost' => 199.99,+ ]);++ // Simulate historic drift: OrderItem.price stuck at the+ // original create-time snapshot, asset.purchase_cost has since+ // been corrected in-place. We reach past the observer here to+ // reproduce the pre-#19564 state on purpose.+ OrderItem::where('item_type', Asset::class)+ ->where('item_id', $asset->id)+ ->update(['price' => 199.99]);++ $asset->purchase_cost = 42.00;+ $asset->saveQuietly();++ OrderItem::where('item_type', Asset::class)+ ->where('item_id', $asset->id)+ ->update(['price' => 199.99]);++ $this->actingAs(User::factory()->superuser()->create())+ ->get(route('hardware.show', $asset))+ ->assertOk()+ ->assertSee('42.00')+ ->assertDontSee('199.99');+ }+public function test_page_for_asset_with_missing_model_still_renders(){$asset = Asset::factory()->create();