38 lines
2.4 KiB
PHP
38 lines
2.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Reward\Controller\Adminhtml\Customer;
|
|
|
|
use Esun\Reward\Model\BalanceManager;
|
|
use Magento\Backend\App\Action;
|
|
use Magento\Backend\Model\Auth\Session as AuthSession;
|
|
use Magento\Customer\Api\CustomerRepositoryInterface;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Framework\Math\Random;
|
|
|
|
class Adjust extends Action
|
|
{
|
|
public const ADMIN_RESOURCE = 'Esun_Reward::adjust';
|
|
public function __construct(Action\Context $context, private readonly BalanceManager $balanceManager, private readonly CustomerRepositoryInterface $customers, private readonly AuthSession $authSession, private readonly Random $random) { parent::__construct($context); }
|
|
public function execute()
|
|
{
|
|
$customerId = (int)$this->getRequest()->getParam('customer_id');
|
|
$redirect = $this->resultRedirectFactory->create()->setPath('customer/index/edit', ['id' => $customerId, '_current' => true]);
|
|
try {
|
|
$points = abs((int)$this->getRequest()->getParam('points')); $operation = (string)$this->getRequest()->getParam('operation');
|
|
$reason = trim((string)$this->getRequest()->getParam('reason')); $evidence = trim((string)$this->getRequest()->getParam('evidence'));
|
|
if ($customerId <= 0 || $points <= 0) { throw new LocalizedException(__('Enter a positive number of points.')); }
|
|
if ($reason === '') { throw new LocalizedException(__('A reason is required.')); }
|
|
$adjustmentType = (string)$this->getRequest()->getParam('adjustment_type');
|
|
$type = $adjustmentType === 'social_share' ? 'social_share' : 'admin_adjustment';
|
|
$delta = $operation === 'subtract' ? -$points : $points; $customer = $this->customers->getById($customerId); $admin = $this->authSession->getUser();
|
|
$this->balanceManager->change($customerId, (int)$customer->getWebsiteId(), $delta, $type, $type . ':' . $this->random->getUniqueHash(), [
|
|
'reference_type' => 'admin', 'reference_id' => $admin ? (int)$admin->getId() : null, 'reason' => $reason,
|
|
'admin_user_id' => $admin ? (int)$admin->getId() : null, 'metadata' => $evidence === '' ? [] : ['evidence' => $evidence]
|
|
]);
|
|
$this->messageManager->addSuccessMessage(__('The reward points were adjusted.'));
|
|
} catch (\Throwable $e) { $this->messageManager->addErrorMessage($e->getMessage()); }
|
|
return $redirect;
|
|
}
|
|
}
|