Skip to content

A free open source IT asset/license management system

Fixed #4190 - Added artisan command to regenerate asset tags

snipe51661b0a2147
diff --git a/app/Console/Commands/RegenerateAssetTags.php b/app/Console/Commands/RegenerateAssetTags.php
new file mode 100644
index 0000000000..9e436df6dd
--- /dev/null
+++ b/app/Console/Commands/RegenerateAssetTags.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Models\Asset;
+use App\Models\Setting;
+use DB;
+use Artisan;
+
+class RegenerateAssetTags extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'snipeit:regenerate-tags {--start=} {--output= : info|warn|error|all} ';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'This utility will regenerate all asset tags. THIS IS DATA-DESTRUCTIVE AND SHOULD BE USED WITH CAUTION. ';
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+
+ if ($this->confirm('This will regenerate all of the asset tags within your system. This action is data-destructive and should be used with caution. Do you wish to continue?'))
+ {
+
+ $output['info'] = [];
+ $output['warn'] = [];
+ $output['error'] = [];
+ $settings = Setting::getSettings();
+
+ $start_tag = ($this->option('start')) ? $this->option('start') : (($settings->next_auto_tag_base) ? Setting::getSettings()->next_auto_tag_base : 1) ;
+
+ $this->info('Starting at '.$start_tag);
+
+ $total_assets = Asset::orderBy('id','asc')->get();
+ $bar = $this->output->createProgressBar(count($total_assets));
+
+ try {
+ Artisan::call('backup:run');
+ } catch (\Exception $e) {
+ $output['error'][] = $e;
+ }
+
+ foreach ($total_assets as $asset) {
+
+ $start_tag++;
+ $output['info'][] = 'Asset tag:'.$asset->asset_tag;
+ $asset->asset_tag = $settings->auto_increment_prefix.$settings->auto_increment_prefix.$start_tag;
+
+ if ($settings->zerofill_count > 0) {
+ $asset->asset_tag = $settings->auto_increment_prefix.Asset::zerofill($start_tag, $settings->zerofill_count);
+ }
+
+ $output['info'][] = 'New Asset tag:'.$asset->asset_tag;
+
+ // Use forceSave here to override model level validation
+ $asset->forceSave();
+ }
+
+ $bar->finish();
+ $this->info("\n");
+
+
+ if (($this->option('output')=='all') || ($this->option('output')=='info')) {
+ foreach ($output['info'] as $key => $output_text) {
+ $this->info($output_text);
+ }
+ }
+ if (($this->option('output')=='all') || ($this->option('output')=='warn')) {
+ foreach ($output['warn'] as $key => $output_text) {
+ $this->warn($output_text);
+ }
+ }
+ if (($this->option('output')=='all') || ($this->option('output')=='error')) {
+ foreach ($output['error'] as $key => $output_text) {
+ $this->error($output_text);
+ }
+ }
+ }
+
+
+ }
+}
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index 9a49718df3..9a0d3090e5 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -28,6 +28,7 @@ class Kernel extends ConsoleKernel
Commands\RecryptFromMcrypt::class,
Commands\ResetDemoSettings::class,
Commands\SyncAssetLocations::class,
+ Commands\RegenerateAssetTags::class,
];
/**