This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Esun\OrderNumber\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
class Config
{
public const XML_PATH_SITE_NAME = 'sales/order_number/site_name';
public const XML_PATH_START_NUMBER = 'sales/order_number/start_number';
public function __construct(
private readonly ScopeConfigInterface $scopeConfig,
private readonly StoreManagerInterface $storeManager
) {
}
public function getSiteName(int $websiteId): string
{
$configuredName = trim((string)$this->scopeConfig->getValue(
self::XML_PATH_SITE_NAME,
ScopeInterface::SCOPE_WEBSITE,
$websiteId
));
if ($configuredName !== '') {
return $configuredName;
}
return (string)$this->storeManager->getWebsite($websiteId)->getCode();
}
public function getStartNumber(int $websiteId): int
{
return max(1, (int)$this->scopeConfig->getValue(
self::XML_PATH_START_NUMBER,
ScopeInterface::SCOPE_WEBSITE,
$websiteId
));
}
}
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Esun\OrderNumber\Model;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Sequence\SequenceInterface;
class WebsiteSequence implements SequenceInterface
{
private const TABLE_NAME = 'esun_order_number_sequence';
private ?int $lastValue = null;
public function __construct(
private readonly ResourceConnection $resource,
private readonly Config $config,
private readonly int $websiteId
) {
}
public function getCurrentValue(): ?string
{
if ($this->lastValue === null) {
return null;
}
return $this->config->getSiteName($this->websiteId) . $this->lastValue;
}
public function getNextValue(): string
{
$connection = $this->resource->getConnection('sales');
$table = $this->resource->getTableName(self::TABLE_NAME, 'sales');
$initialLastValue = $this->config->getStartNumber($this->websiteId) - 1;
$connection->beginTransaction();
try {
// The no-op duplicate update safely creates one counter per Website under concurrency.
$connection->insertOnDuplicate(
$table,
['website_id' => $this->websiteId, 'last_value' => $initialLastValue],
['website_id']
);
$lastValue = (int)$connection->fetchOne(
$connection->select()
->from($table, ['last_value'])
->where('website_id = ?', $this->websiteId)
->forUpdate(true)
);
$this->lastValue = $lastValue + 1;
$connection->update(
$table,
['last_value' => $this->lastValue],
['website_id = ?' => $this->websiteId]
);
$connection->commit();
} catch (\Throwable $exception) {
$connection->rollBack();
throw $exception;
}
return (string)$this->getCurrentValue();
}
}