You are here

public function Invoice::recalculateTotalPrice in Commerce Invoice 8.2

Recalculates the invoice total price.

Return value

$this

Overrides InvoiceInterface::recalculateTotalPrice

6 calls to Invoice::recalculateTotalPrice()
Invoice::addAdjustment in src/Entity/Invoice.php
Adds an adjustment.
Invoice::addItem in src/Entity/Invoice.php
Adds an invoice item.
Invoice::removeAdjustment in src/Entity/Invoice.php
Removes an adjustment.
Invoice::removeItem in src/Entity/Invoice.php
Removes an invoice item.
Invoice::setAdjustments in src/Entity/Invoice.php
Sets the adjustments.

... See full list

File

src/Entity/Invoice.php, line 391

Class

Invoice
Defines the invoice entity class.

Namespace

Drupal\commerce_invoice\Entity

Code

public function recalculateTotalPrice() {

  /** @var \Drupal\commerce_price\Price $total_price */
  $total_price = NULL;
  foreach ($this
    ->getItems() as $invoice_item) {
    if ($invoice_item_total = $invoice_item
      ->getTotalPrice()) {
      $total_price = $total_price ? $total_price
        ->add($invoice_item_total) : $invoice_item_total;
    }
  }
  if ($total_price) {
    $adjustments = $this
      ->collectAdjustments();
    if ($adjustments) {

      /** @var \Drupal\commerce_order\AdjustmentTransformerInterface $adjustment_transformer */
      $adjustment_transformer = \Drupal::service('commerce_order.adjustment_transformer');
      $adjustments = $adjustment_transformer
        ->combineAdjustments($adjustments);
      $adjustments = $adjustment_transformer
        ->roundAdjustments($adjustments);
      foreach ($adjustments as $adjustment) {
        if (!$adjustment
          ->isIncluded()) {
          $total_price = $total_price
            ->add($adjustment
            ->getAmount());
        }
      }
    }
  }
  $this->total_price = $total_price;
  return $this;
}