Hopefully fixes #3203 and #3439

This commit is contained in:
snipe 2017-06-15 19:42:43 -07:00
parent 5d4920c741
commit 99f65cbf69
3 changed files with 117 additions and 4 deletions

View file

@ -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)
{

View file

@ -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');
});
}
}

View file

@ -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
*/