init
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\OrderNumber\Plugin;
|
||||
|
||||
use Esun\OrderNumber\Model\WebsiteSequenceFactory;
|
||||
use Magento\Framework\DB\Sequence\SequenceInterface;
|
||||
use Magento\Sales\Model\Order;
|
||||
use Magento\SalesSequence\Model\Manager;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class SalesSequenceManagerPlugin
|
||||
{
|
||||
public function __construct(
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly WebsiteSequenceFactory $websiteSequenceFactory
|
||||
) {
|
||||
}
|
||||
|
||||
public function afterGetSequence(
|
||||
Manager $subject,
|
||||
SequenceInterface $result,
|
||||
string $entityType,
|
||||
int $storeId
|
||||
): SequenceInterface {
|
||||
if ($entityType !== Order::ENTITY) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$websiteId = (int)$this->storeManager->getStore($storeId)->getWebsiteId();
|
||||
|
||||
return $this->websiteSequenceFactory->create(['websiteId' => $websiteId]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
# Esun Order Number
|
||||
|
||||
Generates order increment IDs from one atomic sequence per Magento Website.
|
||||
|
||||
Configure **Stores > Configuration > Esun > Order Number** after
|
||||
switching the configuration scope to the target Website. The generated format
|
||||
is `Site Name` + `Number`, for example `US100001`.
|
||||
|
||||
`First Order Number` initializes a Website only when its first order number is
|
||||
reserved. It deliberately does not reset a sequence that has already started.
|
||||
Invoice, shipment and credit memo sequences are unchanged.
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0"?>
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
|
||||
<acl>
|
||||
<resources>
|
||||
<resource id="Magento_Backend::admin">
|
||||
<resource id="Magento_Backend::stores">
|
||||
<resource id="Magento_Backend::stores_settings">
|
||||
<resource id="Magento_Config::config">
|
||||
<resource id="Esun_OrderNumber::configuration"
|
||||
title="Order Number Configuration"
|
||||
sortOrder="98"/>
|
||||
</resource>
|
||||
</resource>
|
||||
</resource>
|
||||
</resource>
|
||||
</resources>
|
||||
</acl>
|
||||
</config>
|
||||
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0"?>
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
|
||||
<system>
|
||||
<tab id="esun" translate="label" sortOrder="300">
|
||||
<label>Esun</label>
|
||||
</tab>
|
||||
<section id="esun_order_number" translate="label" type="text" sortOrder="380"
|
||||
showInDefault="1" showInWebsite="1" showInStore="0">
|
||||
<label>Order Number</label>
|
||||
<tab>esun</tab>
|
||||
<resource>Esun_OrderNumber::configuration</resource>
|
||||
<group id="general" translate="label comment" type="text" sortOrder="10"
|
||||
showInDefault="1" showInWebsite="1" showInStore="0">
|
||||
<label>Website Order Number</label>
|
||||
<comment>Configure this separately for each Website.</comment>
|
||||
<field id="site_name" translate="label comment" type="text" sortOrder="10"
|
||||
showInDefault="1" showInWebsite="1" showInStore="0">
|
||||
<label>Site Name</label>
|
||||
<comment>Order numbers use Site Name + Number, for example US100001. If empty, the Website code is used.</comment>
|
||||
<validate>validate-length maximum-length-24</validate>
|
||||
<config_path>sales/order_number/site_name</config_path>
|
||||
</field>
|
||||
<field id="start_number" translate="label comment" type="text" sortOrder="20"
|
||||
showInDefault="1" showInWebsite="1" showInStore="0">
|
||||
<label>First Order Number</label>
|
||||
<comment>Used only when this Website generates its first custom order number. Later changes do not reset an active sequence.</comment>
|
||||
<validate>required-entry validate-digits validate-greater-than-zero</validate>
|
||||
<config_path>sales/order_number/start_number</config_path>
|
||||
</field>
|
||||
</group>
|
||||
</section>
|
||||
</system>
|
||||
</config>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0"?>
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
|
||||
<default>
|
||||
<sales>
|
||||
<order_number>
|
||||
<start_number>1</start_number>
|
||||
</order_number>
|
||||
</sales>
|
||||
</default>
|
||||
</config>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0"?>
|
||||
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
|
||||
<table name="esun_order_number_sequence" resource="sales" engine="innodb"
|
||||
comment="Esun Website Order Number Sequence">
|
||||
<column xsi:type="smallint" name="website_id" unsigned="true" nullable="false"
|
||||
comment="Website ID"/>
|
||||
<column xsi:type="bigint" name="last_value" unsigned="true" nullable="false"
|
||||
default="0" comment="Last Reserved Order Number"/>
|
||||
<constraint xsi:type="primary" referenceId="PRIMARY">
|
||||
<column name="website_id"/>
|
||||
</constraint>
|
||||
</table>
|
||||
</schema>
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"esun_order_number_sequence": {
|
||||
"column": {
|
||||
"website_id": true,
|
||||
"last_value": true
|
||||
},
|
||||
"constraint": {
|
||||
"PRIMARY": true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0"?>
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
|
||||
<type name="Magento\SalesSequence\Model\Manager">
|
||||
<plugin name="esun_website_order_number_sequence"
|
||||
type="Esun\OrderNumber\Plugin\SalesSequenceManagerPlugin"
|
||||
sortOrder="10"/>
|
||||
</type>
|
||||
</config>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0"?>
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
|
||||
<module name="Esun_OrderNumber">
|
||||
<sequence>
|
||||
<module name="Magento_Config"/>
|
||||
<module name="Magento_Quote"/>
|
||||
<module name="Magento_Sales"/>
|
||||
<module name="Magento_SalesSequence"/>
|
||||
<module name="Magento_Store"/>
|
||||
</sequence>
|
||||
</module>
|
||||
</config>
|
||||
@@ -0,0 +1,7 @@
|
||||
"Order Number","订单编号"
|
||||
"Website Order Number","Website 订单编号"
|
||||
"Configure this separately for each Website.","请切换配置范围,为每个 Website 分别设置。"
|
||||
"Site Name","站点名称"
|
||||
"Order numbers use Site Name + Number, for example US100001. If empty, the Website code is used.","订单编号格式为站点名称 + 数字,例如 US100001。留空时使用 Website 代码。"
|
||||
"First Order Number","第一个订单数字"
|
||||
"Used only when this Website generates its first custom order number. Later changes do not reset an active sequence.","仅在该 Website 首次生成自定义订单编号时使用;序列启用后再修改此项不会重置已有计数。"
|
||||
|
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Magento\Framework\Component\ComponentRegistrar;
|
||||
|
||||
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Esun_OrderNumber', __DIR__);
|
||||
Reference in New Issue
Block a user