Skip to content

A free open source IT asset/license management system

Fixed #19689 - add order number and order date as native mappable fields

snipe49c2dca99d39
diff --git a/app/SyncAdapters/MappingTargets.php b/app/SyncAdapters/MappingTargets.php
index 3764f4ee18..d7bafc3c21 100644
--- a/app/SyncAdapters/MappingTargets.php
+++ b/app/SyncAdapters/MappingTargets.php
@@ -100,7 +100,10 @@ class MappingTargets
[$type, $id] = array_pad(explode(':', $target, 2), 2, '');
return match ($type) {
- 'native' => trans('admin/settings/sync_adapters.target_native_'.$id),
+ 'native' => match ($id) {
+ 'purchase_date', 'order_number' => trans('general.' . $id),
+ default => trans('admin/settings/sync_adapters.target_native_' . $id),
+ },
'external' => trans('admin/settings/sync_adapters.target_external_'.$id),
'custom' => trans('admin/settings/sync_adapters.target_custom_prefix').': '
.(CustomField::find((int) $id)?->name ?? '?'),
@@ -149,8 +152,8 @@ class MappingTargets
// (arbitrary tenant-labeled data doesn't belong in native
// slots).
if ($type !== 'boolean' && ! $adminDefined) {
- foreach (['asset_tag', 'model', 'notes'] as $nativeColumn) {
- $options['native:'.$nativeColumn] = trans('admin/settings/sync_adapters.target_native_'.$nativeColumn);
+ foreach (['asset_tag', 'model', 'notes', 'purchase_date', 'order_number'] as $nativeColumn) {
+ $options['native:' . $nativeColumn] = self::labelFor('native:' . $nativeColumn);
}
}
diff --git a/app/SyncAdapters/SyncsHostFromRecord.php b/app/SyncAdapters/SyncsHostFromRecord.php
index bab062cd74..eb1902093f 100644
--- a/app/SyncAdapters/SyncsHostFromRecord.php
+++ b/app/SyncAdapters/SyncsHostFromRecord.php
@@ -731,6 +731,23 @@ trait SyncsHostFromRecord
$asset->byod = ($value === '1');
}
break;
+ case 'purchase_date':
+ // Watson validating rule wants date_format:Y-m-d.
+ // Log + skip if the string isn't parseable so a bad record
+ // doesn't donk the sync run.
+ if ($value !== null && $value !== '') {
+ try {
+ $asset->purchase_date = \Carbon\Carbon::parse((string) $value)->format('Y-m-d');
+ } catch (\Exception $e) {
+ Log::channel('sync-adapters')->warning("Skipping unparseable purchase_date \"{$value}\" for asset {$asset->id}: " . $e->getMessage());
+ }
+ }
+ break;
+ case 'order_number':
+ if ($value !== null && $value !== '') {
+ $asset->order_number = (string) $value;
+ }
+ break;
}
}
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index deb8957662..256ac5dab0 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -9768,6 +9768,12 @@ parameters:
count: 3
path: app/SyncAdapters/SyncAdapter.php
+ -
+ message: '#^Property App\\Models\\Asset\:\:\$purchase_date \(Carbon\\Carbon\|null\) does not accept string\.$#'
+ identifier: assign.propertyType
+ count: 1
+ path: app/SyncAdapters/SyncAdapter.php
+
-
message: '#^Using nullsafe property access "\?\-\>email" on left side of \?\? is unnecessary\. Use \-\> instead\.$#'
identifier: nullsafe.neverNull
diff --git a/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php b/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php
index 76794a0a5b..5840de77bc 100644
--- a/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php
+++ b/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php
@@ -134,8 +134,11 @@ class ExtraFieldMappingTest extends TestCase
$this->assertArrayHasKey('native:asset_tag', $options);
$this->assertArrayHasKey('native:model', $options);
$this->assertArrayHasKey('native:notes', $options);
+ $this->assertArrayHasKey('native:purchase_date', $options);
+ $this->assertArrayHasKey('native:order_number', $options);
+ $nativeTargets = ['skip', 'native:asset_tag', 'native:model', 'native:notes', 'native:purchase_date', 'native:order_number'];
foreach (array_keys($options) as $target) {
- if (in_array($target, ['skip', 'native:asset_tag', 'native:model', 'native:notes'], true)) {
+ if (in_array($target, $nativeTargets, true)) {
continue;
}
$this->assertStringStartsWith('custom:', $target);
@@ -156,6 +159,98 @@ class ExtraFieldMappingTest extends TestCase
$this->assertArrayNotHasKey('native:notes', $options);
}
+ public function test_extra_can_route_to_native_purchase_date()
+ {
+ $fleet = $this->configuredFleetInstance();
+ SyncAdapterConfig::put($fleet->id, 'mapping.fleet_team', 'native:purchase_date');
+
+ $record = new \App\SyncAdapters\HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'purchase-1',
+ hostname: 'host-1',
+ hardwareSerial: 'SN-1',
+ hardwareModel: 'MacBook Pro',
+ extra: ['fleet_team' => '2024-03-15'],
+ );
+
+ SyncAdapter::syncFromRecord($record);
+
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'host-1',
+ 'purchase_date' => '2024-03-15',
+ ]);
+ }
+
+ public function test_purchase_date_accepts_iso_8601_datetime()
+ {
+ // ABM emits orderDateTime as ISO 8601 with a time component.
+ // The write path normalizes to YYYY-MM-DD so it fits Snipe-IT's
+ // date column.
+ $fleet = $this->configuredFleetInstance();
+ SyncAdapterConfig::put($fleet->id, 'mapping.fleet_team', 'native:purchase_date');
+
+ $record = new \App\SyncAdapters\HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'purchase-2',
+ hostname: 'host-2',
+ hardwareSerial: 'SN-2',
+ hardwareModel: 'MacBook Pro',
+ extra: ['fleet_team' => '2024-03-15T00:00:00Z'],
+ );
+
+ SyncAdapter::syncFromRecord($record);
+
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'host-2',
+ 'purchase_date' => '2024-03-15',
+ ]);
+ }
+
+ public function test_unparseable_purchase_date_is_skipped_not_crashed()
+ {
+ // Fail-safe: bad vendor data on one field should not abort the
+ // whole sync. Asset still gets created, purchase_date stays
+ // null, the sync run continues.
+ $fleet = $this->configuredFleetInstance();
+ SyncAdapterConfig::put($fleet->id, 'mapping.fleet_team', 'native:purchase_date');
+
+ $record = new \App\SyncAdapters\HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'bad-date',
+ hostname: 'host-3',
+ hardwareSerial: 'SN-3',
+ hardwareModel: 'MacBook Pro',
+ extra: ['fleet_team' => 'not-a-date'],
+ );
+
+ SyncAdapter::syncFromRecord($record);
+
+ $this->assertDatabaseHas('assets', ['name' => 'host-3']);
+ $this->assertDatabaseMissing('assets', ['name' => 'host-3', 'purchase_date' => 'not-a-date']);
+ }
+
+ public function test_extra_can_route_to_native_order_number()
+ {
+ $fleet = $this->configuredFleetInstance();
+ SyncAdapterConfig::put($fleet->id, 'mapping.fleet_team', 'native:order_number');
+
+ $record = new \App\SyncAdapters\HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'order-1',
+ hostname: 'host-4',
+ hardwareSerial: 'SN-4',
+ hardwareModel: 'MacBook Pro',
+ extra: ['fleet_team' => 'PO-12345'],
+ );
+
+ SyncAdapter::syncFromRecord($record);
+
+ $this->assertDatabaseHas('assets', [
+ 'name' => 'host-4',
+ 'order_number' => 'PO-12345',
+ ]);
+ }
+
public function test_asset_tag_native_target_overwrites_the_asset_tag_column()
{
$fleet = $this->configuredFleetInstance();