Overcoming Adobe Commerce Cloud's sales increment ID step limitation


Author Photo
Vasyl Malanka
Senior Developer at comwrap

Overcome Adobe Commerce Cloud's sales increment ID

Being an Adobe Platinum Solution Partner, comwrap focuses on Adobe Commerce Cloud integration for our clients. And of course, like any other piece of software, "Magento Cloud" has its nuances, limitations and bottlenecks.

One of the nuances is the implementation of Galera on cloud pro architecture. As it is stated in the devdocs, Galera — database cluster with one MariaDB MySQL database per node with an auto-increment setting of three for unique IDs across every database. The interesting part here is: an auto-increment setting of three for unique IDs.

This brings us to 2 potential issues:


1. ERP synchronization

Some ERP systems expect consecutive order IDs to be pushed from the Adobe Commerce (Magento) side.


2. German VAT Act § 14 Section 4

Tax and input VAT, an invoice must contain a consecutive number with one or several unique sequences of numbers allocated by the issuer of the invoice for the purposes of identifying the invoice (invoice number). Source: § 14 UStG - Einzelnorm


Overcoming Adobe Commerce Cloud's sales increment ID step limitation

While the second issue could be solved by passing the technical details of the software to the respective financial institution, the first issue (in some cases) could be sometimes resolved only by migrating to another ERP system. And for some clients, this can be tricky or even impossible.

Luckily, our comwrap team has a solution — a tiny module whose only responsibility is to substitute the sales entity's ID generation mechanism.

The substituted Sequence class changes getNextValue method to call a new method getNextSequence which calculates the next sequence value:

private function getNextSequence(): string
{
    $select = $this->connection->select();
    $select->from($this->meta->getSequenceTable(), 'MAX(sequence_value)');
    $lastValue = ($this->connection->fetchOne($select)) ?: 0;

    return $this->insertToSequence($lastValue + $this->meta->getActiveProfile()->getStep());
}

In the insertToSequence method, the code tries to insert the new sequence ID and, if it fails because some other transaction occupied the new ID during the execution, the code tries to insert again after incrementing the ID by 1:

private function insertToSequence(int $nextValue): string
{
    try {
        $this->connection->insert(
            $this->meta->getSequenceTable(),
            [
                'sequence_value' => $nextValue
            ]
        );
        return $this->connection->lastInsertId($this->meta->getSequenceTable());
    } catch (DuplicateException $e) {
        // The value already exists in the sequence table, so let's assume it's the last value
        $lastValue = $nextValue;

        return $this->insertToSequence($lastValue + $this->meta->getActiveProfile()->getStep());
    }
}

To preserve the solution's stability, the substituted functionality is covered with an integration test.


Conclusion

The Adobe Commerce Cloud's hard limitation on the sequence ID step could be an issue for a merchant. Our module will help overcome this limitation to get consecutive order IDs.

Using Magento's strong customization mechanism, the module rewrites only one core method, which minimizes the chances of getting unpredictable side effects.


Recent Articles

  • 2022-05-10
    Advanced routing using Fastly CDN
    Author Photo
    Alexandr Tihanov
    Senior Developer at comwrap
    Fastly is a Varnish-based service used in Adobe Commerce Cloud (Magento) for content delivery, security, and image optimization. In this article, I describe some of the advanced routing features for developers.
  • 2022-04-22
    Disaster Recovery in an Adobe Commerce Cloud environment
    Author Photo
    Alex Lyzun
    Lead Developer Adobe Commerce
    Adobe Commerce Cloud has an automated backup process by design. So, if you act quickly after a notice, you have a good chance to restore your system almost completely.
  • 2021-12-29
    Recurring payments and Credit Card Management in Adobe Commerce
    Author Photo
    Sunjay Patel
    Senior Developer at comwrap
    Step-by-step guide on how to add recurring payments for subscription business in Adobe Commerce using Payone as PSP
  • 2021-12-15
    VAT Management in Adobe Commerce
    Author Photo
    Oleg Blinnikov
    Senior Developer at comwrap
    Hands-on how to manage VAT in Adobe Commerce (Magento)