|
| 1 | +# Laravel 5.4+ integer sequence helpers |
| 2 | + |
| 3 | +This package provides helpers to work with no-gap and ordered integer sequences in Laravel models. |
| 4 | + |
| 5 | +It has been built to help the generation of "administrative sequences" where there should be no missing value between records (invoices, etc). |
| 6 | + |
| 7 | +**WARNING** This is *beta* version (POC). This is not yet production ready, so you should use at your own risk. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +Add the service provider to your configuration : |
| 12 | + |
| 13 | +```php |
| 14 | +'providers' => [ |
| 15 | + // ... |
| 16 | + |
| 17 | + Bnb\Laravel\Sequence\SequenceServiceProvider::class, |
| 18 | + |
| 19 | + // ... |
| 20 | +], |
| 21 | +``` |
| 22 | + |
| 23 | + |
| 24 | +## Configuration |
| 25 | + |
| 26 | +You can customize this package behavior by publishing the configuration file : |
| 27 | + |
| 28 | + php artisan vendor:publish --provider='Bnb\Laravel\Sequence\SequenceServiceProvider' |
| 29 | + |
| 30 | +You can customize values without publishing by specifying those keys in your `.env` file : |
| 31 | + |
| 32 | +``` |
| 33 | +SEQUENCE_START=123 # defaults to 1 |
| 34 | +SEQUENCE_QUEUE_CONNECTION=database # defaults to default |
| 35 | +SEQUENCE_QUEUE_NAME=sequence # defaults to default |
| 36 | +SEQUENCE_AUTO_DISPATCH=false # defaults to true |
| 37 | +``` |
| 38 | + |
| 39 | +> To avoid concurrency issues when generating sequence number, the queue worker number should be set to one. |
| 40 | + It is recommended to use a dedicated queue (and worker). |
| 41 | + |
| 42 | + |
| 43 | +## Adding a sequence to a model |
| 44 | + |
| 45 | +Sequence columns should be generated with the following configuration in your migration : |
| 46 | + |
| 47 | + $table->unsignedInteger('sequence_name')->nullable()->unique(); |
| 48 | + |
| 49 | +To work with sequence you must enhance your model class with the `Bnb\Laravel\Sequence\HasSequence` trait. |
| 50 | + |
| 51 | +The `sequences` array property of your model must contain the list of the sequences names : |
| 52 | + |
| 53 | +``` |
| 54 | + protected $sequences = ['my_sequence']; |
| 55 | +``` |
| 56 | + |
| 57 | +Some sequence properties can be overridden by specifying a method in the model class (where `MySequence` is the name your sequence in PascalCase) : |
| 58 | +- sequence start value (per-sequence) with the `getMySequenceStartValue() : int` |
| 59 | +- sequence format (per-sequence) with the `formatMySequenceSequence($next, $last) : int` |
| 60 | +- sequence generation authorization (per-sequence) with the `canGenerateMySequenceSequence() : bool` |
| 61 | +- sequence generation queue connection (for the model class) `getSequenceConnection() : string` |
| 62 | +- sequence generation queue name (for the model class) `getSequenceQueue() : string` |
| 63 | +- sequence auto-dispatch activation (for the model class) `isSequenceAutoDispatch() : bool` |
| 64 | + |
| 65 | +Example : |
| 66 | + |
| 67 | +``` |
| 68 | +use Bnb\Laravel\Sequence\HasSequence; |
| 69 | +use Illuminate\Database\Eloquent\Model; |
| 70 | +
|
| 71 | +/** |
| 72 | + * MyModel model uses a sequence named |
| 73 | + */ |
| 74 | +class MyModel extends Model |
| 75 | +{ |
| 76 | + use HasSequence; |
| 77 | +
|
| 78 | + const SEQ_INVOICE_NUMBER_START = 0; |
| 79 | +
|
| 80 | + public $timestamps = false; |
| 81 | +
|
| 82 | + protected $fillable = ['active']; |
| 83 | +
|
| 84 | + protected $sequences = ['invoice_number']; |
| 85 | +
|
| 86 | + /** |
| 87 | + * Assume the sequence can only be generated if active is true |
| 88 | + */ |
| 89 | + protected function canGenerateReadOnlySequence() |
| 90 | + { |
| 91 | + return $this->active; |
| 92 | + } |
| 93 | +
|
| 94 | + /** |
| 95 | + * The sequence default value must match the format |
| 96 | + */ |
| 97 | + protected function getInvoiceNumberStartValue() |
| 98 | + { |
| 99 | + return sprintf('%1$04d%2$06d', date('Y'), static::SEQ_INVOICE_NUMBER_START); |
| 100 | + } |
| 101 | +
|
| 102 | +
|
| 103 | + /** |
| 104 | + * Format the sequence number with current Year that resets its counter to 0 on year change |
| 105 | + */ |
| 106 | + protected function formatInvoiceNumberSequence($next, $last) |
| 107 | + { |
| 108 | + $newYear = date('Y'); |
| 109 | + $newCounter = substr($next, 4); |
| 110 | +
|
| 111 | + if ($last) { |
| 112 | + $lastYear = substr($last, 0, 4); |
| 113 | +
|
| 114 | + if ($lastYear < $newYear) { |
| 115 | + $newCounter = static::SEQ_INVOICE_NUMBER_START; |
| 116 | + } |
| 117 | + } |
| 118 | +
|
| 119 | + return sprintf('%1$04d%2$06d', $newYear, $newCounter); |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### Sequence generation event |
| 125 | + |
| 126 | +When a sequence number is generated a model event is thrown for running custom tasks. |
| 127 | +You can listen to the sequence event using the `sequenceGenerated($sequenceName, $callback)` method (in a service provider boot method for example) : |
| 128 | + |
| 129 | +``` |
| 130 | + MyModel::sequenceGenerated('invoice_number', function ($model) { |
| 131 | + Mail::to($model->recipient_email)->send(new InvoiceGenerated($model)); |
| 132 | + }); |
| 133 | +``` |
| 134 | + |
| 135 | + |
| 136 | +## Schedule generation |
| 137 | + |
| 138 | +You can schedule inside your console kernel the `Bnb\Laravel\Sequence\Console\Commands\UpdateSequence` at the required frequency to generate the missing sequence numbers asynchronously. |
| 139 | + |
| 140 | +``` |
| 141 | + protected function schedule(Schedule $schedule) |
| 142 | + { |
| 143 | + $schedule->command('sequence:update') |
| 144 | + ->hourly(); |
| 145 | + } |
| 146 | +``` |
| 147 | + |
| 148 | +This may not be required when using auto dispatch mode but can be used as a security fallback. |
0 commit comments