HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/brameda/brameda.nl/app/Komma/Shop/Orders/OrderNumberSequence.php
<?php
namespace App\Komma\Shop\Orders;

use App\Komma\Kms\Core\Sequence\Parts\NumberPart;
use App\Komma\Kms\Core\Sequence\Sequence;
use Carbon\Carbon;

/**
 *  Defines an order number
 */
class OrderNumberSequence extends Sequence
{
    public function __construct()
    {
        parent::__construct();

        //Define how an order number looks like
        $this->startsWith('O') //O to indicate that it is an order number
            ->followedBy(new NumberPart(4, 'year'))
            ->followedBy(new NumberPart(5, 'number'));

        //Define its starting number when the orders table is empty
        $this->startingAt('O'.Carbon::now()->year.'00001');

        //The table and column the in this case "order number" is meant for. Checks uniqueness and starts the sequence from the latest order number if present
        $this->uniqueForTable('orders', 'order_number');

        //Force the year part to be the current year, even if the latest order for the table is a different year
        $this->getPartByName('year')->startingAt(Carbon::now()->year);
    }
}