OrderRefresh.php in Commerce Core 8.2
File
modules/order/src/OrderRefresh.php
View source
<?php
namespace Drupal\commerce_order;
use Drupal\commerce\Context;
use Drupal\commerce_price\Calculator;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Entity\OrderType;
use Drupal\commerce_price\Resolver\ChainPriceResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
class OrderRefresh implements OrderRefreshInterface {
protected $orderTypeStorage;
protected $chainPriceResolver;
protected $currentUser;
protected $time;
protected $preprocessors = [];
protected $processors = [];
public function __construct(EntityTypeManagerInterface $entity_type_manager, ChainPriceResolverInterface $chain_price_resolver, AccountInterface $current_user, TimeInterface $time) {
$this->orderTypeStorage = $entity_type_manager
->getStorage('commerce_order_type');
$this->chainPriceResolver = $chain_price_resolver;
$this->currentUser = $current_user;
$this->time = $time;
}
public function addPreprocessor(OrderPreprocessorInterface $processor) {
$this->preprocessors[] = $processor;
}
public function addProcessor(OrderProcessorInterface $processor) {
$this->processors[] = $processor;
}
public function shouldRefresh(OrderInterface $order) {
if (!$this
->needsRefresh($order)) {
return FALSE;
}
$order_type = $this->orderTypeStorage
->load($order
->bundle());
if ($order_type
->getRefreshMode() == OrderType::REFRESH_CUSTOMER) {
if ($order
->getCustomerId() != $this->currentUser
->id()) {
return FALSE;
}
}
return TRUE;
}
public function needsRefresh(OrderInterface $order) {
if ($order
->getState()
->getId() != 'draft') {
return FALSE;
}
if ($order
->isLocked()) {
return FALSE;
}
$current_time = $this->time
->getCurrentTime();
$order_time = $order
->getChangedTime();
if (date('Y-m-d', $current_time) != date('Y-m-d', $order_time)) {
return TRUE;
}
$order_type = $this->orderTypeStorage
->load($order
->bundle());
$refreshed_ago = $current_time - $order_time;
if ($refreshed_ago >= $order_type
->getRefreshFrequency()) {
return TRUE;
}
return FALSE;
}
public function refresh(OrderInterface $order) {
foreach ($this->preprocessors as $processor) {
$processor
->preprocess($order);
}
$current_time = $this->time
->getCurrentTime();
$order
->setChangedTime($current_time);
$order
->clearAdjustments();
if (!$order
->hasItems()) {
return;
}
$time = $order
->getCalculationDate()
->format('U');
$context = new Context($order
->getCustomer(), $order
->getStore(), $time);
foreach ($order
->getItems() as $order_item) {
$purchased_entity = $order_item
->getPurchasedEntity();
if ($purchased_entity) {
$order_item
->setTitle($purchased_entity
->getOrderItemTitle());
if (!$order_item
->isUnitPriceOverridden()) {
$unit_price = $this->chainPriceResolver
->resolve($purchased_entity, $order_item
->getQuantity(), $context);
$order_item
->setUnitPrice($unit_price);
}
}
$order_item->order_id->entity = $order;
}
foreach ($this->processors as $processor) {
$processor
->process($order);
if (!$order
->hasItems()) {
return;
}
}
foreach ($order
->getItems() as $order_item) {
if ($order_item
->hasTranslationChanges()) {
if (Calculator::compare($order_item
->getQuantity(), '0') === 0) {
$order
->removeItem($order_item);
$order_item
->delete();
}
else {
$order_item->order_id->entity = NULL;
$order_item
->setChangedTime($current_time);
$order_item
->save();
}
}
}
}
}