You are here

class InvoiceSubscriber in Commerce Invoice 8.2

Hierarchy

  • class \Drupal\commerce_invoice\EventSubscriber\InvoiceSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of InvoiceSubscriber

1 string reference to 'InvoiceSubscriber'
commerce_invoice.services.yml in ./commerce_invoice.services.yml
commerce_invoice.services.yml
1 service uses InvoiceSubscriber
commerce_invoice.invoice_subscriber in ./commerce_invoice.services.yml
Drupal\commerce_invoice\EventSubscriber\InvoiceSubscriber

File

src/EventSubscriber/InvoiceSubscriber.php, line 8

Namespace

Drupal\commerce_invoice\EventSubscriber
View source
class InvoiceSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      'commerce_invoice.pay.pre_transition' => [
        'onPayPreTransition',
      ],
      'commerce_invoice.pay.post_transition' => [
        'onPayPostTransition',
      ],
    ];
    return $events;
  }

  /**
   * Sets the total_paid field when an invoice is paid.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onPayPreTransition(WorkflowTransitionEvent $event) {

    /** @var \Drupal\commerce_invoice\Entity\InvoiceInterface $invoice */
    $invoice = $event
      ->getEntity();
    if (!$invoice
      ->isPaid()) {
      $invoice
        ->setTotalPaid($invoice
        ->getTotalPrice());
    }
  }

  /**
   * Mark the orders as paid when an invoice is paid.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onPayPostTransition(WorkflowTransitionEvent $event) {

    /** @var \Drupal\commerce_invoice\Entity\InvoiceInterface $invoice */
    $invoice = $event
      ->getEntity();

    // When an invoice is paid, we need to mark the referenced orders as paid.
    foreach ($invoice
      ->getOrders() as $order) {
      if ($order
        ->isPaid()) {
        continue;
      }
      $order
        ->setTotalPaid($order
        ->getTotalPrice());
      $order
        ->save();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InvoiceSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
InvoiceSubscriber::onPayPostTransition public function Mark the orders as paid when an invoice is paid.
InvoiceSubscriber::onPayPreTransition public function Sets the total_paid field when an invoice is paid.