Serial numbers
Introduction
Serial numbers are numbers that may contain fully customisable formats.
For example, invoices may need to have its own numbering because database ID's are inherently not build for this.
An example serial number may be: INV.0023
.
Getting started
To use serial numbers, you will have to create one first.
Creating a serial number
Qore provides a SerialNumberCreator
which you may use in a seeder:
class SerialNumbersSeeder extends Seeder
{
public function run()
{
/** @var SerialNumberCreator $creator */
$creator = app(SerialNumberCreator::class);
// Name // Description // Format
$creator->create('male_employees', 'Male employees', 'E[MM].[YY].[XXXX]');
}
}
Adding a column
You are free to choose where to store your generated numbers.
For this example we will add a serial_number
to our employees
table:
$table->string('serial_number')
// Optionally nullable
->nullable();
Definitions
Tags
Serial numbers may contain pre-defined tags:
[YY] (Year short) : 21 [YYYY] (Year long) : 2021 [M] (Month short) : 6 [MM] (Month long) : 06 [X] (The number) : 1 [XXXX] (The number with leading zero's) : 0001
Example: TAS[YYYY].[M]-[XXXX] TAS2022.4-0494
Rules
A serial number should at least contain [X]
, this will determine where the number will be placed.
Generating serial numbers
Qore provides a Field to generate serial numbers to resources automatically:
// Column // Name
SerialNumber::make(__('Serial number'), 'serial_number', 'male_employees')
This field will fill the generated serial number when creating the resource.
Generating manually
In some cases you might want to generate numbers manually based on conditions:
/** @var SerialNumberGenerator $generator */
$generator= app(SerialNumberGenerator::class);
$model->serial_number = $generator->generate('male_employees');
$model->save()
You can also generate a number without updating the serial_numbers
table by default (which it does by default)
by supplying false
as a third argument:
/** @var SerialNumberGenerator $generator */
$generator = app(SerialNumberGenerator::class);
$number = $generator->generate('male_employees', null, false);