You are here

class OrderSubscriber in Drupal Commerce Connector for AvaTax 8

Hierarchy

  • class \Drupal\commerce_avatax\EventSubscriber\OrderSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of OrderSubscriber

1 string reference to 'OrderSubscriber'
commerce_avatax.services.yml in ./commerce_avatax.services.yml
commerce_avatax.services.yml
1 service uses OrderSubscriber
commerce_avatax.order_subscriber in ./commerce_avatax.services.yml
Drupal\commerce_avatax\EventSubscriber\OrderSubscriber

File

src/EventSubscriber/OrderSubscriber.php, line 14

Namespace

Drupal\commerce_avatax\EventSubscriber
View source
class OrderSubscriber implements EventSubscriberInterface {

  /**
   * The AvaTax library.
   *
   * @var \Drupal\commerce_avatax\AvataxLibInterface
   */
  protected $avataxLib;

  /**
   * The AvaTax configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Constructs a new CommitTransactionSubscriber object.
   *
   * @param \Drupal\commerce_avatax\AvataxLibInterface $avatax_lib
   *   The AvaTax library.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(AvataxLibInterface $avatax_lib, ConfigFactoryInterface $config_factory) {
    $this->avataxLib = $avatax_lib;
    $this->config = $config_factory
      ->get('commerce_avatax.settings');
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      'commerce_order.place.post_transition' => [
        'commitTransaction',
      ],
      'commerce_order.cancel.pre_transition' => [
        'onOrderCancel',
      ],
      OrderEvents::ORDER_DELETE => [
        'onOrderDelete',
      ],
    ];
    return $events;
  }

  /**
   * Commits a transaction or the order in AvaTax.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The workflow transition event.
   */
  public function commitTransaction(WorkflowTransitionEvent $event) {

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $event
      ->getEntity();
    if ($this->config
      ->get('disable_commit') || !Avatax::hasAvataxAdjustments($order)) {
      return;
    }
    $this->avataxLib
      ->transactionsCreate($order, 'SalesInvoice');
  }

  /**
   * Voids the AvaTax transaction on order cancellation.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The workflow transition event.
   */
  public function onOrderCancel(WorkflowTransitionEvent $event) {
    $this
      ->voidTransaction($event
      ->getEntity());
  }

  /**
   * Voids the AvaTax transaction on order deletion.
   *
   * @param \Drupal\commerce_order\Event\OrderEvent $event
   *   The order event.
   */
  public function onOrderDelete(OrderEvent $event) {
    $this
      ->voidTransaction($event
      ->getOrder());
  }

  /**
   * Voids a transaction in AvaTax for the given order.
   *
   * @param \Drupal\commerce_order\Entity\OrderInterface $order
   *   The order.
   */
  protected function voidTransaction(OrderInterface $order) {
    if (!Avatax::hasAvataxAdjustments($order)) {
      return;
    }
    $this->avataxLib
      ->transactionsVoid($order);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OrderSubscriber::$avataxLib protected property The AvaTax library.
OrderSubscriber::$config protected property The AvaTax configuration.
OrderSubscriber::commitTransaction public function Commits a transaction or the order in AvaTax.
OrderSubscriber::getSubscribedEvents public static function
OrderSubscriber::onOrderCancel public function Voids the AvaTax transaction on order cancellation.
OrderSubscriber::onOrderDelete public function Voids the AvaTax transaction on order deletion.
OrderSubscriber::voidTransaction protected function Voids a transaction in AvaTax for the given order.
OrderSubscriber::__construct public function Constructs a new CommitTransactionSubscriber object.