You are here

public function InvoiceTotalSummary::buildTotals in Commerce Invoice 8.2

Builds the totals for the given invoice.

Parameters

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

Return value

array An array of totals with the following elements:

  • subtotal: The order subtotal price.
  • adjustments: The adjustments:
    • type: The adjustment type.
    • label: The adjustment label.
    • amount: The adjustment amount.
    • percentage: The decimal adjustment percentage, when available.
  • total: The invoice total price.

Overrides InvoiceTotalSummaryInterface::buildTotals

File

src/InvoiceTotalSummary.php, line 31

Class

InvoiceTotalSummary

Namespace

Drupal\commerce_invoice

Code

public function buildTotals(InvoiceInterface $invoice) {
  $adjustments = $invoice
    ->collectAdjustments();
  $adjustments = $this->adjustmentTransformer
    ->processAdjustments($adjustments);

  // Included adjustments are not displayed to the customer, they
  // exist to allow the developer to know what the price is made of.
  // The one exception is taxes, which need to be shown for legal reasons.
  $adjustments = array_filter($adjustments, function (Adjustment $adjustment) {
    return $adjustment
      ->getType() === 'tax' || !$adjustment
      ->isIncluded();
  });

  // Convert the adjustments to arrays.
  $adjustments = array_map(function (Adjustment $adjustment) {
    return $adjustment
      ->toArray();
  }, $adjustments);

  // Provide the "total" key for backwards compatibility reasons.
  foreach ($adjustments as $index => $adjustment) {
    $adjustments[$index]['total'] = $adjustments[$index]['amount'];
  }
  return [
    'subtotal' => $invoice
      ->getSubtotalPrice(),
    'adjustments' => $adjustments,
    'total' => $invoice
      ->getTotalPrice(),
  ];
}