A free open source IT asset/license management system
Hopefully fixes #3203 and #3439
snipe99f65cbf6989
diff --git a/app/Models/Asset.php b/app/Models/Asset.phpindex ad291d5cd7..25aee2e5ee 100644--- a/app/Models/Asset.php+++ b/app/Models/Asset.php@@ -426,6 +426,7 @@ class Asset extends Depreciable{$settings = \App\Models\Setting::getSettings();+if ($settings->auto_increment_assets == '1') {$temp_asset_tag = \DB::table('assets')->where('physical', '=', '1')@@ -435,14 +436,43 @@ class Asset extends Depreciable$asset_tag = preg_replace('/^0*/', '', $asset_tag_digits);if ($settings->zerofill_count > 0) {- return $settings->auto_increment_prefix.Asset::zerofill(($asset_tag + 1), $settings->zerofill_count);+ return $settings->auto_increment_prefix.Asset::zerofill($settings->next_auto_tag_base, $settings->zerofill_count);}- return $settings->auto_increment_prefix.($asset_tag + 1);+ return $settings->auto_increment_prefix.$settings->next_auto_tag_bas;} else {return false;}}+ /*+ * Get the next base number for the auto-incrementer. We'll add the zerofill and+ * prefixes on the fly as we generate the number+ *+ */+ public static function nextAutoIncrement($assets)+ {++ $max = 1;++ foreach ($assets as $asset) {+ $results = preg_match ( "/\d+$/" , $asset['asset_tag'], $matches);++ if ($results)+ {+ $number = $matches[0];++ if ($number > $max)+ {+ $max = $number;+ }+ }+ }+ return $max + 1;++ }+++public static function zerofill($num, $zerofill = 3){@@ -688,9 +718,9 @@ class Asset extends Depreciable/*** Query builder scope to get accepted assets*- * @param Illuminate\Database\Query\Builder $query Query builder instance+ * @param \Illuminate\Database\Query\Builder $query Query builder instance*- * @return Illuminate\Database\Query\Builder Modified query builder+ * @return \Illuminate\Database\Query\Builder Modified query builder*/public function scopeAccepted($query){diff --git a/database/migrations/2017_06_14_122059_add_next_autoincrement_to_settings.php b/database/migrations/2017_06_14_122059_add_next_autoincrement_to_settings.phpnew file mode 100644index 0000000000..c574a2d6cb--- /dev/null+++ b/database/migrations/2017_06_14_122059_add_next_autoincrement_to_settings.php@@ -0,0 +1,47 @@+<?php++use Illuminate\Support\Facades\Schema;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Database\Migrations\Migration;+use App\Models\Asset;++class AddNextAutoincrementToSettings extends Migration+{+++ /**+ * Run the migrations.+ *+ * @return void+ */+ public function up()+ {+ $assets = Asset::select('asset_tag')->whereNull('deleted_at')->get();+ if (!$next = Asset::nextAutoIncrement($assets)) {+ $next = 1;+ }++ Schema::table('settings', function (Blueprint $table) use ($next) {+ $table->bigInteger('next_auto_tag_base')->nullable()->default(null);+ });++ \Log::debug('Setting '.$next.' as default auto-increment');+ $settings = App\Models\Setting::first();+ $settings->next_auto_tag_base = $next;+ $settings->save();+++ }++ /**+ * Reverse the migrations.+ *+ * @return void+ */+ public function down()+ {+ Schema::table('settings', function ($table) {+ $table->dropColumn('next_auto_tag_base');+ });+ }+}diff --git a/tests/unit/AssetTest.php b/tests/unit/AssetTest.phpindex 57e9659778..293cda32f8 100644--- a/tests/unit/AssetTest.php+++ b/tests/unit/AssetTest.php@@ -51,6 +51,42 @@ class AssetTest extends \Codeception\TestCase\Test}+ public function testAutoIncrementMixed()+ {+ $expected = '123411';+ $next = Asset::nextAutoIncrement(+ collect([+ ['asset_tag' => '0012345'],+ ['asset_tag' => 'WTF00134'],+ ['asset_tag' => 'WTF-745'],+ ['asset_tag' => '0012346'],+ ['asset_tag' => '00123410'],+ ['asset_tag' => 'U8T7597h77']+ ])+ );++ \Log::debug(print_r($next));+ $this->assertEquals($expected, $next);+ }++// public function testAutoIncrementMixedFullTagNumber()+// {+// $expected = '123411';+// $next = Asset::nextAutoIncrement(+// [+// ['asset_tag' => '0012345'],+// ['asset_tag' => 'WTF00134'],+// ['asset_tag' => 'WTF-745'],+// ['asset_tag' => '0012346'],+// ['asset_tag' => '00123410'],+// ['asset_tag' => 'U8T7597h77']+// ]+// );+// $this->assertEquals($expected, $next);+// }+//++/*** @test*/