You are here

private function wf_crm_webform_preprocess::displayLineItems in Webform CiviCRM Integration 7.4

Same name and namespace in other branches
  1. 7.5 includes/wf_crm_webform_preprocess.inc \wf_crm_webform_preprocess::displayLineItems()

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

Return value

string

1 call to wf_crm_webform_preprocess::displayLineItems()
wf_crm_webform_preprocess::fillForm in includes/wf_crm_webform_preprocess.inc
Recursively walk through form array and set properties of CiviCRM fields

File

includes/wf_crm_webform_preprocess.inc, line 555

Class

wf_crm_webform_preprocess

Code

private function displayLineItems() {
  $rows = array();
  $total = 0;

  // Support hidden contribution field
  $fid = 'civicrm_1_contribution_1_contribution_total_amount';
  if (!$this->line_items && isset($this->enabled[$fid])) {
    $field = $this->node->webform['components'][$this->enabled[$fid]];
    if ($field['type'] == 'hidden') {
      $this->line_items[] = array(
        '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 (!is_null($itemTaxRate)) {

      // Change the line item label to display the tax rate it contains
      $taxSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
      if ($taxSettings['tax_display_settings'] != 'Do_not_show' && $itemTaxRate !== 0) {
        $item['label'] .= ' (' . t('includes !rate @tax', array(
          '!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[] = array(
        'data' => array(
          $label,
          CRM_Utils_Money::format($item['line_total'] + $item['tax_amount']),
        ),
        'class' => array(
          $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[] = array(
        'data' => array(
          $label,
          CRM_Utils_Money::format($item['line_total']),
        ),
        'class' => array(
          $item['element'],
          'line-item',
        ),
        'data-amount' => $item['line_total'],
      );
    }
  }
  $rows[] = array(
    'data' => array(
      t('Total'),
      CRM_Utils_Money::format($total),
    ),
    'id' => 'wf-crm-billing-total',
    'data-amount' => $total,
  );
  return theme('table', array(
    'sticky' => FALSE,
    'caption' => $this->contribution_page['title'],
    'header' => array(),
    'rows' => $rows,
    'attributes' => array(
      'id' => "wf-crm-billing-items",
    ),
  ));
}