A free open source IT asset/license management system
FMCS+location+floater+importer: handle the importer as well
snipe75f86cd669c1
diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.phpindex e56ebde78a..bd8cce65a7 100644--- a/app/Importer/AssetImporter.php+++ b/app/Importer/AssetImporter.php@@ -214,16 +214,25 @@ class AssetImporter extends ItemImporter// -- the class that needs to use it (command importer or GUI importer inside the project).if (isset($target) && ($target !== false)) {$asset = $asset->fresh();- $targetType = get_class($target);- $alreadyCheckedOutToTarget = ($asset->assigned_to == $target->id) && ($asset->assigned_type === $targetType);- // Skip duplicate checkout noise when update mode keeps the same assignment target.- if (! $alreadyCheckedOutToTarget) {- if (! is_null($asset->assigned_to)) {- event(new CheckoutableCheckedIn($asset, $asset->assigned, auth()->user(), 'Checkin from CSV Importer', $checkin_date));- }+ if (! $asset->canCheckoutTo($target)) {+ $this->log(trans('general.error_checkout_company_mismatch', [+ 'item' => trans('general.asset').' "'.$asset->display_name.'"',+ 'item_company' => $asset->company?->name ?? trans('general.unassigned'),+ 'target' => ($target->name ?? $target->username ?? $target->id),+ ]));+ } else {+ $targetType = get_class($target);+ $alreadyCheckedOutToTarget = ($asset->assigned_to == $target->id) && ($asset->assigned_type === $targetType);- $asset->checkOut($target, $this->created_by, $checkout_date, null, 'Checkout from CSV Importer', $asset->name);+ // Skip duplicate checkout noise when update mode keeps the same assignment target.+ if (! $alreadyCheckedOutToTarget) {+ if (! is_null($asset->assigned_to)) {+ event(new CheckoutableCheckedIn($asset, $asset->assigned, auth()->user(), 'Checkin from CSV Importer', $checkin_date));+ }++ $asset->checkOut($target, $this->created_by, $checkout_date, null, 'Checkout from CSV Importer', $asset->name);+ }}}diff --git a/app/Importer/ComponentImporter.php b/app/Importer/ComponentImporter.phpindex 5066c8eb2b..0145997745 100644--- a/app/Importer/ComponentImporter.php+++ b/app/Importer/ComponentImporter.php@@ -59,13 +59,21 @@ class ComponentImporter extends ItemImporter// If we have an asset tag, checkout to that asset.if (isset($this->item['asset_tag']) && ($asset = Asset::where('asset_tag', $this->item['asset_tag'])->first())) {- $component->assets()->attach($component->id, [- 'component_id' => $component->id,- 'created_by' => auth()->id(),- 'created_at' => date('Y-m-d H:i:s'),- 'assigned_qty' => 1, // Only assign the first one to the asset- 'asset_id' => $asset->id,- ]);+ if (! $component->canCheckoutTo($asset)) {+ $this->log(trans('general.error_checkout_company_mismatch', [+ 'item' => trans('general.component').' "'.$component->name.'"',+ 'item_company' => $component->company?->name ?? trans('general.unassigned'),+ 'target' => trans('general.asset').' "'.$asset->display_name.'"',+ ]));+ } else {+ $component->assets()->attach($component->id, [+ 'component_id' => $component->id,+ 'created_by' => auth()->id(),+ 'created_at' => date('Y-m-d H:i:s'),+ 'assigned_qty' => 1, // Only assign the first one to the asset+ 'asset_id' => $asset->id,+ ]);+ }}return;diff --git a/app/Importer/LicenseImporter.php b/app/Importer/LicenseImporter.phpindex 101adc3b41..d637d9084e 100644--- a/app/Importer/LicenseImporter.php+++ b/app/Importer/LicenseImporter.php@@ -106,16 +106,32 @@ class LicenseImporter extends ItemImporter}if ($checkout_target) {- $targetLicense->assigned_to = $checkout_target->id;- $targetLicense->created_by = auth()->id();- if ($asset) {- $targetLicense->asset_id = $asset->id;+ if (! $license->canCheckoutTo($checkout_target)) {+ $this->log(trans('general.error_checkout_company_mismatch', [+ 'item' => trans('general.license').' "'.$license->name.'"',+ 'item_company' => $license->company?->name ?? trans('general.unassigned'),+ 'target' => ($checkout_target->name ?? $checkout_target->username ?? $checkout_target->id),+ ]));+ } else {+ $targetLicense->assigned_to = $checkout_target->id;+ $targetLicense->created_by = auth()->id();+ if ($asset) {+ $targetLicense->asset_id = $asset->id;+ }+ $targetLicense->save();}- $targetLicense->save();} elseif ($asset) {- $targetLicense->created_by = auth()->id();- $targetLicense->asset_id = $asset->id;- $targetLicense->save();+ if (! $license->canCheckoutTo($asset)) {+ $this->log(trans('general.error_checkout_company_mismatch', [+ 'item' => trans('general.license').' "'.$license->name.'"',+ 'item_company' => $license->company?->name ?? trans('general.unassigned'),+ 'target' => trans('general.asset').' "'.$asset->display_name.'"',+ ]));+ } else {+ $targetLicense->created_by = auth()->id();+ $targetLicense->asset_id = $asset->id;+ $targetLicense->save();+ }}}