Nlabs.InvoiceNumberGenerator 9.0.0

dotnet add package Nlabs.InvoiceNumberGenerator --version 9.0.0                
NuGet\Install-Package Nlabs.InvoiceNumberGenerator -Version 9.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nlabs.InvoiceNumberGenerator" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nlabs.InvoiceNumberGenerator --version 9.0.0                
#r "nuget: Nlabs.InvoiceNumberGenerator, 9.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Nlabs.InvoiceNumberGenerator as a Cake Addin
#addin nuget:?package=Nlabs.InvoiceNumberGenerator&version=9.0.0

// Install Nlabs.InvoiceNumberGenerator as a Cake Tool
#tool nuget:?package=Nlabs.InvoiceNumberGenerator&version=9.0.0                

InvoiceNumberGenerator

InvoiceNumberGenerator is a robust C# library designed for generating invoice or purchase order numbers with customizable prefixes, a date part, and a numeric portion of user-defined width. It supports error handling for invalid input parameters to ensure reliable usage.

Installation

You can install the InvoiceNumberGenerator library via NuGet Package Manager Console by running the following command:

Install-Package Nlabs.InvoiceNumberGenerator

Or via .NET CLI:

dotnet add package Nlabs.InvoiceNumberGenerator

Features

Customizable Prefix: Specify a prefix for your invoice numbers. Date Integration: Automatically includes the current date in yyyyMMdd format. Numeric Width Customization: Control the width of the numeric portion (e.g., 5, 10, or more digits). Error Handling: Validates input parameters and throws appropriate exceptions for invalid input.

Usage

Example Code

using Nlabs.InvoiceNumberGenerator;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            string prefix = "INV_";
            string currentMaxNumber = "INV_20240607000005"; // Example of the current maximum number
            int numberWidth = 10; // Define the width of the numeric part

            string newNumber = InvoiceNumberGenerator.GenerateInvoiceNumber(prefix, currentMaxNumber, numberWidth);
            Console.WriteLine(newNumber); // Example output: "INV_202406070000000006"
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine($"Argument error: {ex.Message}");
        }
        catch (ArgumentOutOfRangeException ex)
        {
            Console.WriteLine($"Out of range error: {ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");
        }
    }
}

Example Outputs

Valid Input:
Generated Invoice Number: INV_202406070000000006
Invalid Prefix:
Argument error: Prefix cannot be null, empty, or whitespace.
Invalid Number Width:
Out of range error: Number width must be at least 1.

Method Documentation

GenerateInvoiceNumber

public static string GenerateInvoiceNumber(string prefix, string? currentMaxNumber, int numberWidth)

Parameters

  • prefix (string): The prefix to include in the invoice number. Must not be null, empty, or whitespace.
  • currentMaxNumber (string?): The current maximum number. If null or empty, the first number starts at 1.

Returns

  • A newly generated invoice number in the format:
[prefix][yyyyMMdd][number (zero-padded to numberWidth)]

Exceptions

  • ArgumentException:
    • Thrown when the prefix is null, empty, or whitespace.
  • ArgumentOutOfRangeException:
    • Thrown if numberWidth is less than 1.

Error Handling

This library provides comprehensive error handling to prevent invalid usage. Below are some common scenarios:

Invalid Prefix

If the prefix is null, empty, or whitespace, an ArgumentException is thrown:

try
{
    string invalidPrefix = " ";
    string newNumber = InvoiceNumberGenerator.GenerateInvoiceNumber(invalidPrefix, null, 10);
}
catch (ArgumentException ex)
{
    Console.WriteLine($"Argument error: {ex.Message}");
}

Output:

Argument error: Prefix cannot be null, empty, or whitespace.

Invalid Number Width

If the numberWidth is less than 1, an ArgumentOutOfRangeException is thrown:

try
{
    int invalidNumberWidth = 0;
    string newNumber = InvoiceNumberGenerator.GenerateInvoiceNumber("INV_", null, invalidNumberWidth);
}
catch (ArgumentOutOfRangeException ex)
{
    Console.WriteLine($"Out of range error: {ex.Message}");
}

Output:

Out of range error: Number width must be at least 1.

Examples

First Number Generation

If no previous maximum number exists:

string newNumber = InvoiceNumberGenerator.GenerateInvoiceNumber("INV_", null, 10);
Console.WriteLine(newNumber);

Output:

INV_202406070000000001

Incrementing Numbers

Given the last generated number:

string currentMaxNumber = "INV_202406070000000009";
string newNumber = InvoiceNumberGenerator.GenerateInvoiceNumber("INV_", currentMaxNumber, 10);
Console.WriteLine(newNumber);

Output:

INV_202406070000000010

Conclusion

The InvoiceNumberGenerator library is a flexible and powerful tool for generating invoice numbers in .NET applications. With customizable options and robust error handling, it ensures reliability and ease of use for developers.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.0 35 11/15/2024
1.0.0 101 6/7/2024