You are here

class InvoiceTotalSummary in Commerce Invoice 8.2

Same name in this branch
  1. 8.2 src/InvoiceTotalSummary.php \Drupal\commerce_invoice\InvoiceTotalSummary
  2. 8.2 src/Plugin/Field/FieldFormatter/InvoiceTotalSummary.php \Drupal\commerce_invoice\Plugin\Field\FieldFormatter\InvoiceTotalSummary

Hierarchy

Expanded class hierarchy of InvoiceTotalSummary

1 string reference to 'InvoiceTotalSummary'
commerce_invoice.services.yml in ./commerce_invoice.services.yml
commerce_invoice.services.yml
1 service uses InvoiceTotalSummary
commerce_invoice.invoice_total_summary in ./commerce_invoice.services.yml
Drupal\commerce_invoice\InvoiceTotalSummary

File

src/InvoiceTotalSummary.php, line 9

Namespace

Drupal\commerce_invoice
View source
class InvoiceTotalSummary implements InvoiceTotalSummaryInterface {

  /**
   * The adjustment transformer.
   *
   * @var \Drupal\commerce_order\AdjustmentTransformerInterface
   */
  protected $adjustmentTransformer;

  /**
   * Constructs a new InvoiceTotalSummary object.
   *
   * @param \Drupal\commerce_order\AdjustmentTransformerInterface $adjustment_transformer
   *   The adjustment transformer.
   */
  public function __construct(AdjustmentTransformerInterface $adjustment_transformer) {
    $this->adjustmentTransformer = $adjustment_transformer;
  }

  /**
   * {@inheritdoc}
   */
  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(),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InvoiceTotalSummary::$adjustmentTransformer protected property The adjustment transformer.
InvoiceTotalSummary::buildTotals public function Builds the totals for the given invoice. Overrides InvoiceTotalSummaryInterface::buildTotals
InvoiceTotalSummary::__construct public function Constructs a new InvoiceTotalSummary object.