You are here

protected function InvoiceStorage::doInvoicePresave in Commerce Invoice 8.2

Performs invoice-specific pre-save tasks.

This includes:

  • Recalculating the total price.
  • Applying the "paid" transition.

Parameters

\Drupal\commerce_invoice\Entity\InvoiceInterface $invoice: The invoice.

1 call to InvoiceStorage::doInvoicePresave()
InvoiceStorage::invokeHook in src/InvoiceStorage.php
Invokes a hook on behalf of the entity.

File

src/InvoiceStorage.php, line 38

Class

InvoiceStorage

Namespace

Drupal\commerce_invoice

Code

protected function doInvoicePresave(InvoiceInterface $invoice) {
  $invoice
    ->recalculateTotalPrice();

  // Apply the "paid" transition when an invoice is paid.
  $original_paid = isset($invoice->original) ? $invoice->original
    ->isPaid() : FALSE;
  if ($invoice
    ->isPaid() && !$original_paid) {

    // Check if a "pay" transition exists for that workflow.
    $transitions = $invoice
      ->getState()
      ->getTransitions();
    if (isset($transitions['pay'])) {
      $invoice
        ->getState()
        ->applyTransitionById('pay');
    }
  }
}