init
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Observer;
|
||||
|
||||
use Esun\Reward\Model\BalanceManager;
|
||||
use Esun\Reward\Model\Config;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
use Magento\Review\Model\Review;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
|
||||
class AwardApprovedReviewPoints implements ObserverInterface
|
||||
{
|
||||
public function __construct(private readonly BalanceManager $balanceManager, private readonly Config $config, private readonly ResourceConnection $resource, private readonly CustomerRepositoryInterface $customers) {}
|
||||
public function execute(Observer $observer): void
|
||||
{
|
||||
/** @var Review|null $review */ $review = $observer->getEvent()->getDataObject();
|
||||
$customerId = (int)($review?->getCustomerId() ?? 0);
|
||||
if (!$review || (int)$review->getStatusId() !== Review::STATUS_APPROVED || $customerId <= 0 || !$this->config->isEnabled((int)$review->getStoreId())) { return; }
|
||||
$connection = $this->resource->getConnection();
|
||||
$purchase = $connection->fetchRow($connection->select()->from($this->resource->getTableName('esun_review_purchase'))->where('review_id = ?', (int)$review->getId())->limit(1));
|
||||
if (!$purchase) { return; }
|
||||
$hasMedia = (bool)$connection->fetchOne($connection->select()->from($this->resource->getTableName('esun_review_image'), ['image_id'])->where('review_id = ?', (int)$review->getId())->where('status = ?', 1)->limit(1));
|
||||
$type = $hasMedia ? 'review_media' : 'review_text';
|
||||
$rule = $this->config->earningRule($type, (int)$review->getStoreId());
|
||||
$points = (int)$rule['points'];
|
||||
$websiteId = (int)$this->customers->getById($customerId)->getWebsiteId();
|
||||
$this->balanceManager->change($customerId, $websiteId, $points, $type, 'review_reward:' . (int)$review->getId(), [
|
||||
'reference_type' => 'review', 'reference_id' => (int)$review->getId(), 'reason' => (string)__($rule['reason']),
|
||||
'metadata' => ['product_id' => (int)$review->getEntityPkValue(), 'order_id' => (int)$purchase['order_id'], 'order_item_id' => (int)$purchase['order_item_id'], 'has_media' => $hasMedia]
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Observer;
|
||||
|
||||
use Esun\Reward\Model\BalanceManager;
|
||||
use Esun\Reward\Model\Config;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
|
||||
class AwardFirstAddressPoints implements ObserverInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BalanceManager $balanceManager,
|
||||
private readonly Config $config,
|
||||
private readonly ResourceConnection $resource,
|
||||
private readonly CustomerRepositoryInterface $customers
|
||||
) {}
|
||||
|
||||
public function execute(Observer $observer): void
|
||||
{
|
||||
$address = $observer->getEvent()->getCustomerAddress();
|
||||
$customerId = (int)($address?->getCustomerId() ?? 0);
|
||||
if (!$this->config->isEnabled() || $customerId <= 0 || !$this->isComplete($address)) { return; }
|
||||
$connection = $this->resource->getConnection();
|
||||
$count = (int)$connection->fetchOne($connection->select()->from($this->resource->getTableName('customer_address_entity'), ['COUNT(*)'])->where('parent_id = ?', $customerId));
|
||||
if ($count > 1) { return; }
|
||||
$websiteId = (int)$this->customers->getById($customerId)->getWebsiteId();
|
||||
$rule = $this->config->earningRule('first_shipping_address');
|
||||
$this->balanceManager->change($customerId, $websiteId, (int)$rule['points'], 'first_shipping_address', 'first_shipping_address:' . $customerId, [
|
||||
'reference_type' => 'customer_address', 'reference_id' => (int)$address->getId(), 'reason' => (string)__($rule['reason'])
|
||||
]);
|
||||
}
|
||||
|
||||
private function isComplete(mixed $address): bool
|
||||
{
|
||||
return $address && trim((string)$address->getCountryId()) !== '' && trim((string)$address->getCity()) !== ''
|
||||
&& trim((string)$address->getPostcode()) !== '' && trim((string)$address->getTelephone()) !== ''
|
||||
&& count(array_filter((array)$address->getStreet(), static fn ($value) => trim((string)$value) !== '')) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Observer;
|
||||
|
||||
use Esun\Reward\Model\BalanceManager;
|
||||
use Esun\Reward\Model\Config;
|
||||
use Esun\Reward\Model\TierResolver;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
use Magento\Sales\Model\Order\Invoice;
|
||||
|
||||
class AwardInvoicePoints implements ObserverInterface
|
||||
{
|
||||
public function __construct(private readonly BalanceManager $balanceManager, private readonly Config $config, private readonly TierResolver $tierResolver) {}
|
||||
|
||||
public function execute(Observer $observer): void
|
||||
{
|
||||
/** @var Invoice|null $invoice */ $invoice = $observer->getEvent()->getInvoice();
|
||||
$order = $invoice?->getOrder(); $customerId = (int)($order?->getCustomerId() ?? 0);
|
||||
if (!$invoice || !$order || !$this->config->isEnabled((int)$order->getStoreId()) || $customerId <= 0) { return; }
|
||||
$websiteId = (int)$order->getStore()->getWebsiteId();
|
||||
$account = $this->balanceManager->getAccount($customerId, $websiteId);
|
||||
$tier = $this->tierResolver->resolveEffective((string)$account['tier_code'], (float)$account['lifetime_spend_base'], (int)$order->getStoreId());
|
||||
$rewardShare = (float)$order->getBaseSubtotal() > 0
|
||||
? (float)$order->getData('base_reward_discount_amount') * (float)$invoice->getBaseSubtotal() / (float)$order->getBaseSubtotal()
|
||||
: 0.0;
|
||||
$eligible = max(0.0, (float)$invoice->getBaseSubtotal() + (float)$invoice->getBaseDiscountAmount() - $rewardShare);
|
||||
$rule = $this->config->earningRule('order_earned', (int)$order->getStoreId());
|
||||
$points = (int)floor($eligible * (float)$rule['points'] * (float)$tier['multiplier']);
|
||||
if ($points > 0 && $this->balanceManager->change($customerId, $websiteId, $points, 'order_earned', 'invoice_earned:' . (int)$invoice->getId(), [
|
||||
'reference_type' => 'order', 'reference_id' => (int)$order->getId(), 'reason' => (string)__($rule['reason'], $order->getIncrementId()),
|
||||
'metadata' => ['invoice_id' => (int)$invoice->getId(), 'eligible_amount' => $eligible, 'tier' => $tier['code'], 'multiplier' => $tier['multiplier']]
|
||||
])) {
|
||||
$order->setData('reward_points_earned', (int)$order->getData('reward_points_earned') + $points);
|
||||
$order->setData('reward_tier_code', $tier['code']); $order->setData('reward_multiplier', $tier['multiplier']); $order->save();
|
||||
$this->balanceManager->addLifetimeSpend($customerId, $websiteId, $eligible, (int)$order->getStoreId());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Observer;
|
||||
|
||||
use Esun\Reward\Model\BalanceManager;
|
||||
use Esun\Reward\Model\Config;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
|
||||
class AwardRegistrationPoints implements ObserverInterface
|
||||
{
|
||||
public function __construct(private readonly BalanceManager $balanceManager, private readonly Config $config) {}
|
||||
|
||||
public function execute(Observer $observer): void
|
||||
{
|
||||
$customer = $observer->getEvent()->getCustomer();
|
||||
$customerId = (int)($customer?->getId() ?? 0);
|
||||
$websiteId = (int)($customer?->getWebsiteId() ?? 0);
|
||||
if (!$this->config->isEnabled() || $customerId <= 0) { return; }
|
||||
$rule = $this->config->earningRule('registration');
|
||||
$this->balanceManager->change($customerId, $websiteId, (int)$rule['points'], 'registration', 'registration:' . $customerId, [
|
||||
'reference_type' => 'customer', 'reference_id' => $customerId, 'reason' => (string)__($rule['reason'])
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Observer;
|
||||
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
|
||||
class CopyRewardToOrder implements ObserverInterface
|
||||
{
|
||||
public function execute(Observer $observer): void
|
||||
{
|
||||
$quote = $observer->getEvent()->getQuote(); $order = $observer->getEvent()->getOrder();
|
||||
foreach (['reward_points_used', 'reward_product_points', 'reward_discount_amount', 'base_reward_discount_amount'] as $field) { $order->setData($field, $quote->getData($field)); }
|
||||
foreach ($quote->getAllItems() as $quoteItem) { if ($quoteItem->getOrderItem()) { $quoteItem->getOrderItem()->setData('reward_redemption', $quoteItem->getData('reward_redemption'))->setData('reward_points_cost', $quoteItem->getData('reward_points_cost')); } }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Observer;
|
||||
|
||||
use Esun\Reward\Model\ReservationManager;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
|
||||
class RecalculateRedemptionPoints implements ObserverInterface
|
||||
{
|
||||
public function __construct(private readonly ReservationManager $reservations) {}
|
||||
public function execute(Observer $observer): void
|
||||
{
|
||||
$cart=$observer->getEvent()->getCart();$quote=$cart?->getQuote();if(!$quote||!(int)$quote->getCustomerId()){return;}$points=0;
|
||||
foreach($quote->getAllVisibleItems() as $item){if((int)$item->getData('reward_redemption')){$points+=(int)$item->getData('reward_points_cost')*(int)$item->getQty();}}
|
||||
$quote->setData('reward_product_points',$points);$this->reservations->reserve((int)$quote->getCustomerId(),(int)$quote->getStore()->getWebsiteId(),(int)$quote->getId(),$points+(int)$quote->getData('reward_points_used'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Observer;
|
||||
|
||||
use Esun\Reward\Model\BalanceManager;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
|
||||
class RefundCancelledOrderPoints implements ObserverInterface
|
||||
{
|
||||
public function __construct(private readonly BalanceManager $balances) {}
|
||||
public function execute(Observer $observer): void
|
||||
{
|
||||
$order = $observer->getEvent()->getOrder(); $points = (int)$order->getData('reward_points_used') + (int)$order->getData('reward_product_points');
|
||||
if ($points <= 0 || !(int)$order->getCustomerId()) { return; }
|
||||
$this->balances->change((int)$order->getCustomerId(), (int)$order->getStore()->getWebsiteId(), $points, 'cancel_refund', 'order_cancel_refund:' . (int)$order->getId(), [
|
||||
'reference_type' => 'order', 'reference_id' => (int)$order->getId(), 'reason' => (string)__('Points returned for cancelled order %1', $order->getIncrementId())
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Observer;
|
||||
|
||||
use Esun\Reward\Model\BalanceManager;
|
||||
use Esun\Reward\Model\Config;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
use Magento\Sales\Model\Order\Creditmemo;
|
||||
|
||||
class ReverseCreditmemoPoints implements ObserverInterface
|
||||
{
|
||||
public function __construct(private readonly BalanceManager $balanceManager, private readonly Config $config) {}
|
||||
public function execute(Observer $observer): void
|
||||
{
|
||||
/** @var Creditmemo|null $creditmemo */ $creditmemo = $observer->getEvent()->getCreditmemo();
|
||||
$order = $creditmemo?->getOrder(); $customerId = (int)($order?->getCustomerId() ?? 0);
|
||||
if (!$creditmemo || !$order || $customerId <= 0 || !$this->config->isEnabled((int)$order->getStoreId())) { return; }
|
||||
$rewardShare = (float)$order->getBaseSubtotal() > 0
|
||||
? (float)$order->getData('base_reward_discount_amount') * (float)$creditmemo->getBaseSubtotal() / (float)$order->getBaseSubtotal()
|
||||
: 0.0;
|
||||
$eligible = max(0.0, (float)$creditmemo->getBaseSubtotal() + (float)$creditmemo->getBaseDiscountAmount() - $rewardShare);
|
||||
$multiplier = max(1.0, (float)$order->getData('reward_multiplier'));
|
||||
$remainingEarned = max(0, (int)$order->getData('reward_points_earned') - (int)$order->getData('reward_points_reversed'));
|
||||
$points = min($remainingEarned, (int)floor($eligible * $this->config->orderRate((int)$order->getStoreId()) * $multiplier));
|
||||
$websiteId = (int)$order->getStore()->getWebsiteId();
|
||||
if ($points > 0) {
|
||||
$this->balanceManager->change($customerId, $websiteId, -$points, 'order_refund_reversal', 'creditmemo_reversal:' . (int)$creditmemo->getId(), [
|
||||
'reference_type' => 'order', 'reference_id' => (int)$order->getId(), 'reason' => (string)__('Points reversed for refund on order %1', $order->getIncrementId()),
|
||||
'metadata' => ['creditmemo_id' => (int)$creditmemo->getId()], 'allow_negative' => true
|
||||
]);
|
||||
$order->setData('reward_points_reversed', (int)$order->getData('reward_points_reversed') + $points);
|
||||
}
|
||||
$returnedProductPoints = 0;
|
||||
foreach ($creditmemo->getAllItems() as $creditmemoItem) {
|
||||
$orderItem = $creditmemoItem->getOrderItem();
|
||||
if ($orderItem && (int)$orderItem->getData('reward_redemption')) {
|
||||
$returnedProductPoints += (int)round((float)$creditmemoItem->getQty() * (int)$orderItem->getData('reward_points_cost'));
|
||||
}
|
||||
}
|
||||
if ($returnedProductPoints > 0) {
|
||||
$this->balanceManager->change($customerId, $websiteId, $returnedProductPoints, 'redemption_refund', 'creditmemo_redemption_refund:' . (int)$creditmemo->getId(), [
|
||||
'reference_type' => 'order', 'reference_id' => (int)$order->getId(), 'reason' => (string)__('Product redemption points returned for order %1', $order->getIncrementId()),
|
||||
'metadata' => ['creditmemo_id' => (int)$creditmemo->getId()]
|
||||
]);
|
||||
}
|
||||
$remainingCashPoints = max(0, (int)$order->getData('reward_points_used') - (int)$order->getData('reward_points_refunded'));
|
||||
$cashPointsToReturn = (float)$order->getBaseSubtotal() > 0
|
||||
? min($remainingCashPoints, (int)round((int)$order->getData('reward_points_used') * (float)$creditmemo->getBaseSubtotal() / (float)$order->getBaseSubtotal()))
|
||||
: 0;
|
||||
if ($cashPointsToReturn > 0) {
|
||||
$this->balanceManager->change($customerId, $websiteId, $cashPointsToReturn, 'checkout_refund', 'creditmemo_checkout_refund:' . (int)$creditmemo->getId(), [
|
||||
'reference_type' => 'order', 'reference_id' => (int)$order->getId(), 'reason' => (string)__('Checkout points returned for order %1', $order->getIncrementId()),
|
||||
'metadata' => ['creditmemo_id' => (int)$creditmemo->getId()]
|
||||
]);
|
||||
$order->setData('reward_points_refunded', (int)$order->getData('reward_points_refunded') + $cashPointsToReturn);
|
||||
}
|
||||
$order->save();
|
||||
$this->balanceManager->addLifetimeSpend($customerId, $websiteId, -$eligible, (int)$order->getStoreId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Observer;
|
||||
|
||||
use Esun\Reward\Model\BalanceManager;
|
||||
use Esun\Reward\Model\ReservationManager;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
|
||||
class SpendOrderPoints implements ObserverInterface
|
||||
{
|
||||
public function __construct(private readonly BalanceManager $balances, private readonly ReservationManager $reservations) {}
|
||||
public function execute(Observer $observer): void
|
||||
{
|
||||
$order = $observer->getEvent()->getOrder(); $quote = $observer->getEvent()->getQuote(); $points = (int)$order->getData('reward_points_used') + (int)$order->getData('reward_product_points');
|
||||
if ($points <= 0 || !(int)$order->getCustomerId()) { return; }
|
||||
$this->balances->change((int)$order->getCustomerId(), (int)$order->getStore()->getWebsiteId(), -$points, 'checkout_spend', 'order_spend:' . (int)$order->getId(), [
|
||||
'reference_type' => 'order', 'reference_id' => (int)$order->getId(), 'reason' => (string)__('Points used on order %1', $order->getIncrementId())
|
||||
]);
|
||||
$this->reservations->markUsed((int)$quote->getId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user