Files
stemcell/app/code/Stemcell/Sales/Model/Sequence.php
T
2026-09-15 10:26:34 +08:00

91 lines
2.2 KiB
PHP

<?php
namespace Stemcell\Sales\Model;
use Magento\Customer\Model\Session;
use Magento\Framework\App\ResourceConnection as AppResource;
use Magento\SalesSequence\Model\Meta;
class Sequence extends \Magento\SalesSequence\Model\Sequence
{
const DEFAULT_PATTERN = "%s%s%'.05d";
/**
* @var string
*/
protected string $lastIncrementId;
/**
* @var Meta
*/
protected Meta $meta;
protected false|\Magento\Framework\DB\Adapter\AdapterInterface $connection;
/**
* @var string
*/
protected string $pattern;
protected Session $customerSession;
public function __construct(
Meta $meta,
AppResource $resource,
Session $customerSession,
string $pattern = self::DEFAULT_PATTERN
) {
$this->customerSession = $customerSession;
$this->meta = $meta;
$this->connection = $resource->getConnection('sales');
$this->pattern = $pattern;
parent::__construct($meta, $resource, $pattern);
}
/**
* Calculate current value depends on start value
*
* @return string
*/
protected function calculateCurrentValue()
{
return ($this->lastIncrementId - $this->meta->getActiveProfile()->getStartValue())
* $this->meta->getActiveProfile()->getStep() + $this->meta->getActiveProfile()->getStartValue();
}
protected function getCustomerAbbr(){
if ($this->customerSession->isLoggedIn()) {
return $this->customerSession->getCustomer()->getData('customer_abbr');
}
return '';
}
/**
* Retrieve next value
*
* @return string
*/
public function getNextValue(): ?string
{
$this->connection->insert($this->meta->getSequenceTable(), []);
$this->lastIncrementId = $this->connection->lastInsertId($this->meta->getSequenceTable());
return $this->getCurrentValue();
}
/**
* Retrieve current value
*
* @return string
*/
public function getCurrentValue(): ?string
{
if (!isset($this->lastIncrementId)) {
return null;
}
return 'STI'.
date('Ymd').
$this->getCustomerAbbr().
sprintf("%'.05d", $this->calculateCurrentValue());
}
}