You are here

private function WebformCivicrmPreProcess::displayLineItems in Webform CiviCRM Integration 8.5

Format line-items to appear on front-end of webform

Return value

array The render array for line items table.

1 call to WebformCivicrmPreProcess::displayLineItems()
WebformCivicrmPreProcess::alterForm in src/WebformCivicrmPreProcess.php
Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display messages. Block users who should not have access. Set webform default values.

File

src/WebformCivicrmPreProcess.php, line 582
Front-end form pre-processor.

Class

WebformCivicrmPreProcess

Namespace

Drupal\webform_civicrm

Code

private function displayLineItems() {
  $utils = \Drupal::service('webform_civicrm.utils');
  $rows = [];
  $total = 0;

  // Support hidden contribution field
  $fid = 'civicrm_1_contribution_1_contribution_total_amount';
  if (!$this->line_items && isset($this->enabled[$fid])) {
    $elements = $this->node
      ->getElementsDecodedAndFlattened();
    $field = $elements[$this->enabled[$fid]];
    if ($field['#type'] === 'hidden') {
      $this->line_items[] = [
        'line_total' => $field['value'],
        'qty' => 1,
        'element' => 'civicrm_1_contribution_1',
        'label' => !empty($field['name']) ? $field['name'] : t('Contribution Amount'),
      ];
    }
  }
  $taxRates = \CRM_Core_PseudoConstant::getTaxRates();
  foreach ($this->line_items as $item) {
    $total += $item['line_total'];

    // Sales Tax integration
    if (!empty($item['financial_type_id'])) {
      $itemTaxRate = isset($taxRates[$item['financial_type_id']]) ? $taxRates[$item['financial_type_id']] : NULL;
    }
    else {
      $itemTaxRate = $this->tax_rate;
    }
    if ($itemTaxRate !== NULL) {

      // Change the line item label to display the tax rate it contains
      $taxSettings = $utils
        ->wf_crm_get_civi_setting('contribution_invoice_settings');
      if ($itemTaxRate !== 0 && $taxSettings['tax_display_settings'] !== 'Do_not_show') {
        $item['label'] .= ' (' . t('includes @rate @tax', [
          '@rate' => (double) $itemTaxRate . '%',
          '@tax' => $taxSettings['tax_term'],
        ]) . ')';
      }

      // Add calculation for financial type that contains tax
      $item['tax_amount'] = $itemTaxRate / 100 * $item['line_total'];
      $total += $item['tax_amount'];
      $label = $item['label'] . ($item['qty'] > 1 ? " ({$item['qty']})" : '');
      $rows[] = [
        'data' => [
          $label,
          \CRM_Utils_Money::format($item['line_total'] + $item['tax_amount']),
        ],
        'class' => [
          $item['element'],
          'line-item',
        ],
        'data-amount' => $item['line_total'] + $item['tax_amount'],
        'data-tax' => (double) $itemTaxRate,
      ];
    }
    else {
      $label = $item['label'] . ($item['qty'] > 1 ? " ({$item['qty']})" : '');
      $rows[] = [
        'data' => [
          $label,
          \CRM_Utils_Money::format($item['line_total']),
        ],
        'class' => [
          $item['element'],
          'line-item',
        ],
        'data-amount' => $item['line_total'],
      ];
    }
  }
  $rows[] = [
    'data' => [
      t('Total'),
      \CRM_Utils_Money::format($total),
    ],
    'id' => 'wf-crm-billing-total',
    'data-amount' => $total,
  ];
  return [
    '#type' => 'table',
    '#sticky' => FALSE,
    '#caption' => t('Payment Information'),
    '#header' => [],
    '#rows' => $rows,
    '#attributes' => [
      'id' => 'wf-crm-billing-items',
    ],
  ];
}