cosmicvibes/laraseries

Number series package for Laravel

dev-master 2019-05-10 11:58 UTC

This package is auto-updated.

Last update: 2024-05-10 23:06:59 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License StyleCI Build Status

Laraseries: Laravel Number Series Package

I had a business requirement to create an incrementing part number for products, that cannot be reused. E.g: first item: IN00001, next item IN00002 etc.

This package simplifies the creation of multiple number series and allows you to easily retrieve the next available number, saving the incremented value to the number series. It uses database transactions to ensure the next number is always atomic.

Requirements

  • PHP 7.1.3+
  • Laravel 5.8+

Will likely work on other versions, but is untested.

Installation

  1. Install the package by running this command in your terminal/cmd:
composer require cosmicvibes/laraseries dev-master

Usage

Implements Laravel Eloquent models. A demo application with tests can be found at: https://github.com/cosmicvibes/laraseries-demo

First create a number series:

$numberSeries = new Cosmicvibes\Laraseries\NumberSeries();

$numberSeries->fill([
            'code'              => 'TEST',
            'name'              => 'Test Number Series',
            'prefix'            => 'TEST',
            'suffix'            => 'Y',
            'length'            => 10,
            'increment_by'      => 1,
            'padding_character' => 0, // Can be anything
            'start_date'        => Carbon::createFromFormat('d/m/Y', '01/01/2001'), // Can be left blank, implement checks in your own code
            'end_date'          => null, // Can be left blank, implement checks in your own code
            'active'            => true, // For your reference, implement checks in your own code
            'starting_number'   => 1, // Defaults to 0
            'ending_number'     => null, // Defaults to max possible number based on length        
]);

$numberSeries->save();

Or find an existing model (any eloquent commands will work, see the Laravel documentation for more details):

$numberSeries = NumberSeries::whereCode('TEST')->get()->first();

Retrieve the current number:

echo $numberSeries->current;
TEST0000000001Y

Advance to the next number (the function will return the new current number):

$currentNumber = $numberSeries->advance():
TEST0000000002Y

Advance by a set amount:

$currentNumber = $numberSeries->advance(15):
TEST00000000017Y

Note that if you advance past the ending number then advancing further will return null. It is recommended that you implement checking for this situation.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Special Thanks to