You are here

private function WebformCivicrmPostProcess::tallyLineItems in Webform CiviCRM Integration 8.5

Calculate line-items for this webform submission

1 call to WebformCivicrmPostProcess::tallyLineItems()
WebformCivicrmPostProcess::validate in src/WebformCivicrmPostProcess.php
Called after a webform is submitted Or, for a multipage form, called after each page

File

src/WebformCivicrmPostProcess.php, line 1685
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function tallyLineItems() {

  // Contribution
  $utils = \Drupal::service('webform_civicrm.utils');
  $fid = 'civicrm_1_contribution_1_contribution_total_amount';
  if (isset($this->enabled[$fid]) || $this
    ->getData($fid) > 0) {
    $this->line_items[] = [
      'qty' => 1,
      'unit_price' => $this
        ->getData($fid),
      'financial_type_id' => wf_crm_aval($this->data, 'contribution:1:contribution:1:financial_type_id'),
      'label' => wf_crm_aval($this->node
        ->getElementsDecodedAndFlattened(), $this->enabled[$fid] . ':#title', t('Contribution')),
      'element' => 'civicrm_1_contribution_1',
      'entity_table' => 'civicrm_contribution',
    ];
  }

  // LineItems
  $fid = 'civicrm_1_lineitem_1_contribution_line_total';
  if (isset($this->enabled[$fid])) {
    foreach ($this->data['lineitem'][1]['contribution'] as $n => $lineitem) {
      $fid = "civicrm_1_lineitem_{$n}_contribution_line_total";
      if ($this
        ->getData($fid) > 0) {
        $this->line_items[] = [
          'qty' => 1,
          'unit_price' => $lineitem['line_total'],
          'financial_type_id' => $lineitem['financial_type_id'],
          'label' => wf_crm_aval($this->node
            ->getElementsDecodedAndFlattened(), $this->enabled[$fid] . ':#title', t('Line Item')),
          'element' => "civicrm_1_lineitem_{$n}",
          'entity_table' => 'civicrm_contribution',
        ];
      }
    }
  }

  // Membership
  foreach (wf_crm_aval($this->data, 'membership', []) as $c => $memberships) {
    if (isset($this->existing_contacts[$c]) && !empty($memberships['number_of_membership'])) {
      foreach ($memberships['membership'] as $n => $membership_item) {
        if (!empty($membership_item['membership_type_id'])) {
          $type = $membership_item['membership_type_id'];
          $price = $this
            ->getMembershipTypeField($type, 'minimum_fee');

          // if number of terms is set, regard membership fee field as price per term
          // if you choose to set dates manually while membership fee field is enabled, take the membership fee as total cost of this membership
          if (isset($membership_item['fee_amount'])) {
            $price = $membership_item['fee_amount'];
            if (empty($membership_item['num_terms'])) {
              $membership_item['num_terms'] = 1;
            }
          }
          $membership_financialtype = $this
            ->getMembershipTypeField($type, 'financial_type_id');
          if (isset($membership_item['financial_type_id']) && $membership_item['financial_type_id'] !== 0) {
            $membership_financialtype = $membership_item['financial_type_id'];
          }
          if ($price) {
            $this->line_items[] = [
              'qty' => $membership_item['num_terms'],
              'unit_price' => $price,
              'financial_type_id' => $membership_financialtype,
              'label' => $this
                ->getMembershipTypeField($type, 'name') . ": " . $utils
                ->wf_crm_display_name($this->existing_contacts[$c]),
              'element' => "civicrm_{$c}_membership_{$n}",
              'entity_table' => 'civicrm_membership',
            ];
          }
        }
      }
    }
  }

  // Calculate totals
  $this->totalContribution = 0;
  $taxRates = \CRM_Core_PseudoConstant::getTaxRates();
  foreach ($this->line_items as &$line_item) {

    // Sales Tax integration
    if (!empty($line_item['financial_type_id'])) {
      $itemTaxRate = isset($taxRates[$line_item['financial_type_id']]) ? $taxRates[$line_item['financial_type_id']] : NULL;
    }
    else {
      $itemTaxRate = $this->tax_rate;
    }
    if ($itemTaxRate !== NULL) {
      $line_item['line_total'] = $line_item['unit_price'] * (int) $line_item['qty'];
      $line_item['tax_amount'] = $itemTaxRate / 100 * $line_item['line_total'];
      $this->totalContribution += $line_item['unit_price'] * (int) $line_item['qty'] + $line_item['tax_amount'];
    }
    else {
      $line_item['line_total'] = $line_item['unit_price'] * (int) $line_item['qty'];
      $this->totalContribution += $line_item['line_total'];
    }
  }
  return round($this->totalContribution, 2);
}