Skip to content

soap/laravel-running-numbers

Repository files navigation

Maintain document running number for Laravel application

Latest Version on Packagist PHPStan GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

The package provides a class to generate running number, keep track of them in database table. One running number type can have many prefix to generate running number. Generated running numbers was not stored in database, just keep last number for each prefix. You can reset it to specified value for each prefix. If specified type and prefix does not exists in the database, it will be create and assign number to 1.

Support us

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require soap/laravel-running-numbers

You can publish and run the migrations with:

php artisan vendor:publish --tag="running-numbers-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="running-numbers-config"

This is the contents of the published config file:

return [
    'table_prefix' => '',
];

Usage

Using RunningNumber class to generate running number is deprecated. Now I introduced RunningNumberGenerator class instead.

This is for version less than 0.0.6.

RunningNumber::generate('STUDENT_CODE', '672', 3);
// 673001
RunningNumber::generate('STUDENT_CODE', '672', 3);
// 673002

// Reset specified type and prefix to some value
RunningNumber::reset('STUDENT_CODE', '672', 0);

RunningNumber::generate('STUDENT_CODE', '672', 3);
// 673001

RunningNumber::delete('STUDENT_CODE', '672');

This is from my implementation.

namespace App\Observers;

use App\Models\Student;
use App\Models\EducationLevel;
use Soap\Laravel\RunningNumbers\RunningNumber;

class StudentObserver
{
    /**
     * Handle the Student "creating" event.
     *
     * @return void
     */
    public function creating(Student $student)
    {
        if (empty($student->student_code)) {
            $level = EducationLevel::find($student->education_level_id)->level;
            $prefix = ($student->registered_at->year + 543) % 100 . $level;
            $student->student_code = RunningNumber::generate('STUDENT_CODE', $prefix, 3);
        }
    }
}

For verison 0.0.6 and above;

Using RunningNumberGenerator to generate and reset value.

  RunningNumberGenerator::make()
     ->type('STUDENT_CODE')->prefix("672")->generate();

If type was not provided, 'Default' will be used. If prefix was not provided, date("Y") will be used.

Set Running number format

Availabe token are {TYPE}, {PREFIX}, {NUMBER}. These information will be keep tracked in database record.

    RunningNumberGenerator::make()
     ->type('STUDENT_CODE')->prefix("672")->format("{PREFIX}-{NUMBER}")->generate();   

Reset running number to specified value.

Zero value if no value passed. Call generate(), running number will be increased by one and saved to database.

   RunningNumberGenerator::make()
     ->type('STUDENT_CODE')->prefix("672")->->format("{PREFIX}-{NUMBER}")->reset()->generate();
   // return 672-001
   RunningNumberGenerator::make()
     ->type('STUDENT_CODE')->prefix("672")->->format("{PREFIX}-{NUMBER}")->reset(1)->generate();
   // return 672-002

Set length of padding number.

If not set length of 3 will be used.

   RunningNumberGenerator::make()
     ->type('STUDENT_CODE')->prefix("672")->->format("{PREFIX}-{NUMBER}")->length(4)->reset()->generate();
   // return 672-0001

Artisan Command

List

Usage:
  runningnumber:list [<type> [<prefix>]]

Arguments:
  type                  Type of running number
  prefix                Prefix before running number

Generate

Usage:
  runningnumber:generate [options] [--] <type> <prefix>

Arguments:
  type                             Type of running number
  prefix                           Prefix before running number

Reset

Usage:
  runningnumber:reset [options] [--] <type> <prefix>

Arguments:
  type                  Type of running number
  prefix                Prefix before running number

Options:
      --value[=VALUE]   Value to reset running number to [default: "1"]

Delele

Usage:
  runningnumber:delete <type> <prefix>

Arguments:
  type                  Type of running number
  prefix                Prefix before running number

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Manage running numbers of all types for Laravel application

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages