Last active
September 24, 2018 06:41
-
-
Save lomotech/3f1e538a0aa5fdf8bab02f4b4ba4fb42 to your computer and use it in GitHub Desktop.
Running number management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Schema::create('runners', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->string('rule_1'); | |
$table->string('rule_2')->nullable(); | |
$table->string('rule_3')->nullable(); | |
$table->string('rule_4')->nullable(); | |
$table->integer('index')->nullable(); | |
$table->timestamps(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* To generate running number form various rule (up to 3) and type of number. | |
* | |
* Lets learn some magic! | |
* pertanyaan SOMEPREFIX1800001 rule_1 = SOMEPREFIX; rule_2 = 18; | |
* | |
* @param $rule_1 | |
* @param null $rule_2 | |
* @param null $rule_3 | |
* @param null $rule_4 | |
* @return string | |
*/ | |
public static function generateAppNumber($rule_1, $rule_2, $rule_3 = null, $rule_4 = null) | |
{ | |
$runner = Runner::where('rule_1', '=', $rule_1) | |
->where('rule_2', '=', $rule_2) | |
->first(); | |
if (!$runner) { | |
$runnerCreate = new Runner; | |
$runnerCreate->create([ | |
'rule_1' => $rule_1, | |
'rule_2' => $rule_2, | |
'index' => 1 | |
]); | |
$runningNumberRaw = 1; | |
} else { | |
Runner::where('rule_1', '=', $rule_1) | |
->where('rule_2', '=', $rule_2) | |
->increment('index'); | |
$runner = Runner::where('rule_1', '=', $rule_1) | |
->where('rule_2', '=', $rule_2) | |
->first(); | |
$runningNumberRaw = $runner->index; | |
} | |
return $rule_1.$rule_2.str_pad($runningNumberRaw, 5, '0', STR_PAD_LEFT); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment