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,38 @@
<?php
declare(strict_types=1);
namespace Esun\LegacyOrder\Block\Account;
use Esun\LegacyOrder\Model\ResourceModel\Order\Collection;
use Esun\LegacyOrder\Model\ResourceModel\Order\CollectionFactory;
use Magento\Customer\Model\Session;
use Magento\Framework\View\Element\Template;
class History extends Template
{
private ?Collection $orders = null;
public function __construct(
Template\Context $context,
private readonly CollectionFactory $collectionFactory,
private readonly Session $customerSession,
array $data = []
) {
parent::__construct($context, $data);
}
public function getOrders(): Collection
{
if ($this->orders === null) {
$this->orders = $this->collectionFactory->create()
->addFieldToFilter('customer_id', (int)$this->customerSession->getCustomerId())
->setOrder('ordered_at', 'DESC');
}
return $this->orders;
}
public function getViewUrl(int $legacyOrderId): string
{
return $this->getUrl('legacy_order/account/view', ['id' => $legacyOrderId]);
}
}
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Esun\LegacyOrder\Block\Account;
use Magento\Customer\Block\Account\SortLinkInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\DefaultPathInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\View\Element\Html\Link\Current;
use Magento\Framework\View\Element\Template\Context;
class NavigationLink extends Current implements SortLinkInterface
{
private ?bool $hasLegacyOrders = null;
public function __construct(
Context $context,
DefaultPathInterface $defaultPath,
private readonly Session $customerSession,
private readonly ResourceConnection $resource,
array $data = []
) {
parent::__construct($context, $defaultPath, $data);
}
public function getSortOrder(): int
{
return (int)$this->getData(self::SORT_ORDER);
}
public function isCurrent(): bool
{
if (
$this->getRequest()->getModuleName() === 'legacy_order'
&& $this->getRequest()->getControllerName() === 'account'
) {
return true;
}
return parent::isCurrent();
}
protected function _toHtml(): string
{
return $this->hasLegacyOrders() ? parent::_toHtml() : '';
}
private function hasLegacyOrders(): bool
{
if ($this->hasLegacyOrders !== null) {
return $this->hasLegacyOrders;
}
$customerId = (int)$this->customerSession->getCustomerId();
if ($customerId <= 0) {
return $this->hasLegacyOrders = false;
}
$connection = $this->resource->getConnection();
return $this->hasLegacyOrders = (bool)$connection->fetchOne(
$connection->select()
->from($this->resource->getTableName('esun_legacy_order'), ['legacy_order_id'])
->where('customer_id = ?', $customerId)
->limit(1)
);
}
}
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Esun\LegacyOrder\Block\Account;
use Magento\Customer\Model\Session;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\View\Element\Template;
class OrderView extends Template
{
/** @var array<string, mixed>|null */
private ?array $order = null;
public function __construct(
Template\Context $context,
private readonly Session $customerSession,
private readonly ResourceConnection $resource,
private readonly Json $json,
array $data = []
) {
parent::__construct($context, $data);
}
/** @return array<string, mixed> */
public function getOrder(): array
{
if ($this->order !== null) {
return $this->order;
}
$connection = $this->resource->getConnection();
return $this->order = $connection->fetchRow(
$connection->select()
->from($this->resource->getTableName('esun_legacy_order'))
->where('legacy_order_id = ?', (int)$this->getRequest()->getParam('id'))
->where('customer_id = ?', (int)$this->customerSession->getCustomerId())
) ?: [];
}
/** @return array<int, array<string, mixed>> */
public function getItems(): array
{
$order = $this->getOrder();
if (!$order) {
return [];
}
$connection = $this->resource->getConnection();
return $connection->fetchAll(
$connection->select()
->from($this->resource->getTableName('esun_legacy_order_item'))
->where('legacy_order_id = ?', (int)$order['legacy_order_id'])
->order('line_number ASC')
);
}
/** @return array<string, mixed> */
public function decodeAddress(?string $value): array
{
try {
return $value ? (array)$this->json->unserialize($value) : [];
} catch (\Throwable) {
return [];
}
}
}
@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace Esun\LegacyOrder\Block\Adminhtml\Customer\Edit\Tab;
use Magento\Backend\Block\Template;
use Magento\Customer\Controller\RegistryConstants;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Registry;
use Magento\Ui\Component\Layout\Tabs\TabInterface;
class LegacyOrders extends Template implements TabInterface
{
protected $_template = 'Esun_LegacyOrder::customer/tab/legacy_orders.phtml';
/** @var array<int, array<string, mixed>>|null */
private ?array $legacyOrders = null;
public function __construct(
Template\Context $context,
private readonly Registry $registry,
private readonly ResourceConnection $resource,
array $data = []
) {
parent::__construct($context, $data);
}
public function getTabLabel()
{
return __('Legacy Orders');
}
public function getTabTitle()
{
return __('Legacy Orders');
}
public function getTabClass(): string
{
return '';
}
public function getTabUrl(): string
{
return '';
}
public function isAjaxLoaded(): bool
{
return false;
}
public function canShowTab(): bool
{
return $this->getCustomerId() > 0;
}
public function isHidden(): bool
{
return false;
}
public function getAfter(): string
{
return 'orders';
}
public function getCustomerId(): int
{
return (int)$this->registry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
}
/** @return array<int, array<string, mixed>> */
public function getLegacyOrders(): array
{
if ($this->legacyOrders !== null) {
return $this->legacyOrders;
}
$customerId = $this->getCustomerId();
if ($customerId <= 0) {
return $this->legacyOrders = [];
}
$connection = $this->resource->getConnection();
$select = $connection->select()
->from($this->resource->getTableName('esun_legacy_order'), [
'legacy_order_id',
'source_order_number',
'ordered_at',
'order_status',
'payment_status',
'shipping_status',
'grand_total',
'currency',
])
->where('customer_id = ?', $customerId)
->order('ordered_at DESC');
return $this->legacyOrders = $connection->fetchAll($select);
}
public function getLegacyOrderUrl(int $legacyOrderId): string
{
return $this->getUrl('esun_legacy_order/order/view', ['id' => $legacyOrderId]);
}
}
@@ -0,0 +1,5 @@
<?php
declare(strict_types=1);
namespace Esun\LegacyOrder\Block\Adminhtml\Import;
use Magento\Backend\Block\Template;use Magento\Store\Model\System\Store;
class Form extends Template {protected $_template='Esun_LegacyOrder::import/form.phtml';public function __construct(Template\Context$c,private readonly Store$stores,array$d=[]){parent::__construct($c,$d);}public function getWebsites():array{return$this->stores->getWebsiteValuesForForm(false,true);}public function hasValidatedFile():bool{return(bool)$this->_backendSession->getLegacyOrderImport();}}
@@ -0,0 +1,5 @@
<?php
declare(strict_types=1);
namespace Esun\LegacyOrder\Block\Adminhtml\Order;
use Magento\Backend\Block\Template;use Magento\Framework\App\ResourceConnection;use Magento\Framework\Serialize\Serializer\Json;
class View extends Template {protected $_template='Esun_LegacyOrder::order/view.phtml';private?array$order=null;public function __construct(Template\Context$c,private readonly ResourceConnection$r,private readonly Json$j,array$d=[]){parent::__construct($c,$d);}public function getOrder():array{if($this->order!==null)return$this->order;$db=$this->r->getConnection();return$this->order=$db->fetchRow($db->select()->from($this->r->getTableName('esun_legacy_order'))->where('legacy_order_id=?',(int)$this->getRequest()->getParam('id')))?:[];}public function getItems():array{$o=$this->getOrder();if(!$o)return[];$db=$this->r->getConnection();return$db->fetchAll($db->select()->from($this->r->getTableName('esun_legacy_order_item'))->where('legacy_order_id=?',(int)$o['legacy_order_id'])->order('line_number'));}public function decode(?string$v):array{try{return$v?(array)$this->j->unserialize($v):[];}catch(\Throwable){return[];}}}