Files
esun3d/app/code/Esun/CustomerVat/Controller/Adminhtml/Application/Review.php
T
2026-09-15 10:35:56 +08:00

53 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\CustomerVat\Controller\Adminhtml\Application;
use Esun\CustomerVat\Model\ResourceModel\VatApplication as VatResource;
use Esun\CustomerVat\Model\VatApplication;
use Esun\CustomerVat\Model\VatApplicationFactory;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\Auth\Session as AuthSession;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Stdlib\DateTime\DateTime;
class Review extends Action implements HttpPostActionInterface
{
public const ADMIN_RESOURCE = 'Esun_CustomerVat::review';
public function __construct(Context $context, private readonly VatApplicationFactory $factory,
private readonly VatResource $resource, private readonly AuthSession $authSession,
private readonly DateTime $dateTime)
{
parent::__construct($context);
}
public function execute()
{
$id = (int)$this->getRequest()->getParam('id');
$status = (string)$this->getRequest()->getParam('status');
$allowed = [VatApplication::STATUS_PENDING, VatApplication::STATUS_APPROVED, VatApplication::STATUS_REJECTED];
$application = $this->factory->create();
$this->resource->load($application, $id);
if (!$application->getId() || !in_array($status, $allowed, true)) {
$this->messageManager->addErrorMessage(__('Unable to review this VAT application.'));
return $this->resultRedirectFactory->create()->setPath('*/*/index');
}
try {
$admin = $this->authSession->getUser();
$application->addData([
'status' => $status,
'review_note' => trim((string)$this->getRequest()->getParam('review_note')) ?: null,
'reviewed_by' => $admin ? (int)$admin->getId() : null,
'reviewed_at' => $this->dateTime->gmtDate(),
]);
$this->resource->save($application);
$this->messageManager->addSuccessMessage(__('The VAT application review has been saved.'));
} catch (\Throwable $exception) {
$this->messageManager->addExceptionMessage($exception, __('Unable to save the VAT application review.'));
}
return $this->resultRedirectFactory->create()->setPath('*/*/view', ['id' => $id]);
}
}